2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
24 import rift
.mano
.dts
as mano_dts
25 import rift
.package
.package
as package
26 import rift
.package
.store
as store
27 import rift
.package
.convert
as convert
29 from gi
.repository
import (
38 class DownloadStatusSubscriber(mano_dts
.AbstractOpdataSubscriber
):
40 def __init__(self
, log
, dts
, loop
, callback
):
41 super().__init
__(log
, dts
, loop
, callback
)
44 return ("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
46 class VnfdStatusSubscriber(DownloadStatusSubscriber
):
47 DOWNLOAD_DIR
= store
.VnfdPackageFilesystemStore
.DEFAULT_ROOT_DIR
48 MODULE_DESC
= 'vnfd rw-vnfd'.split()
51 def __init__(self
, log
, dts
, loop
):
52 super().__init
__(log
, dts
, loop
, self
.on_change
)
53 self
.subscriber
= mano_dts
.VnfdCatalogSubscriber(log
, dts
, loop
)
55 def on_change(self
, msg
, action
):
56 log_msg
= "1. Vnfd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg
), msg
.id, msg
.name
, repr(action
))
57 self
.log
.debug(log_msg
)
58 if action
== RwDts
.QueryAction
.UPDATE
:
59 actionCreate(self
, msg
)
61 self
.log
.debug("VnfdStatusSubscriber: No action for {}".format(repr(action
)))
65 return self
.subscriber
.get_xpath()
68 class NsdStatusSubscriber(DownloadStatusSubscriber
):
69 DOWNLOAD_DIR
= store
.NsdPackageFilesystemStore
.DEFAULT_ROOT_DIR
70 MODULE_DESC
= 'nsd rw-nsd'.split()
73 def __init__(self
, log
, dts
, loop
):
74 super().__init
__(log
, dts
, loop
, self
.on_change
)
75 self
.subscriber
= mano_dts
.NsdCatalogSubscriber(log
, dts
, loop
)
77 def on_change(self
, msg
, action
):
78 log_msg
= "1. Nsd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg
), msg
.id, msg
.name
, repr(action
))
79 self
.log
.debug(log_msg
)
80 if action
== RwDts
.QueryAction
.UPDATE
:
81 actionCreate(self
, msg
)
83 self
.log
.debug("NsdStatusSubscriber: No action for {}".format(repr(action
)))
87 return self
.subscriber
.get_xpath()
90 def actionCreate(descriptor
, msg
):
91 ''' Create folder structure if it doesn't exist: id/vnf name OR id/nsd name
92 Serialize the Vnfd/Nsd object to yaml and store yaml file in the created folder.
95 desc_name
= msg
.name
if msg
.name
else ""
96 download_dir
= os
.path
.join(descriptor
.DOWNLOAD_DIR
, msg
.id)
98 # If a download dir is present with contents, then we know it has been created in the
100 if os
.path
.exists(download_dir
) and os
.listdir(download_dir
):
101 descriptor
.log
.debug("Skpping folder creation, {} already present".format(download_dir
))
104 # Folder structure is based on top-level package-id directory
105 if not os
.path
.exists(download_dir
):
106 os
.makedirs(download_dir
)
107 descriptor
.log
.debug("Created directory {}".format(download_dir
))
109 model
= RwYang
.Model
.create_libncx()
110 for module
in descriptor
.MODULE_DESC
: model
.load_module(module
)
112 yaml_path
= "{base}/{name}_{type}.yaml".format(base
=download_dir
, name
=msg
.name
, type=descriptor
.DESC_TYPE
)
113 with
open(yaml_path
,"w") as fh
:
114 fh
.write(msg
.to_yaml(model
))