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
):
39 def __init__(self
, log
, dts
, loop
, project
, callback
):
40 super().__init
__(log
, dts
, loop
, project
, callback
)
43 return self
._project
.add_project(
44 "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
47 class VnfdStatusSubscriber(DownloadStatusSubscriber
):
48 DOWNLOAD_DIR
= store
.VnfdPackageFilesystemStore
.DEFAULT_ROOT_DIR
49 MODULE_DESC
= 'vnfd rw-vnfd'.split()
52 def __init__(self
, log
, dts
, loop
, project
):
53 super().__init
__(log
, dts
, loop
, project
, self
.on_change
)
54 self
.subscriber
= mano_dts
.VnfdCatalogSubscriber(log
, dts
, loop
, project
)
56 def on_change(self
, msg
, action
):
57 log_msg
= "1. Vnfd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg
), msg
.id, msg
.name
, repr(action
))
58 self
.log
.debug(log_msg
)
59 if action
== RwDts
.QueryAction
.UPDATE
:
60 actionCreate(self
, msg
)
62 self
.log
.debug("VnfdStatusSubscriber: No action for {}".format(repr(action
)))
66 return self
.subscriber
.get_xpath()
69 class NsdStatusSubscriber(DownloadStatusSubscriber
):
70 DOWNLOAD_DIR
= store
.NsdPackageFilesystemStore
.DEFAULT_ROOT_DIR
71 MODULE_DESC
= 'nsd rw-nsd'.split()
74 def __init__(self
, log
, dts
, loop
, project
):
75 super().__init
__(log
, dts
, loop
, project
, self
.on_change
)
76 self
.subscriber
= mano_dts
.NsdCatalogSubscriber(log
, dts
, loop
, project
)
78 def on_change(self
, msg
, action
):
79 log_msg
= "1. Nsd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg
), msg
.id, msg
.name
, repr(action
))
80 self
.log
.debug(log_msg
)
81 if action
== RwDts
.QueryAction
.UPDATE
:
82 actionCreate(self
, msg
)
84 self
.log
.debug("NsdStatusSubscriber: No action for {}".format(repr(action
)))
88 return self
.subscriber
.get_xpath()
91 def actionCreate(descriptor
, msg
):
92 ''' Create folder structure if it doesn't exist: id/vnf name OR id/nsd name
93 Serialize the Vnfd/Nsd object to yaml and store yaml file in the created folder.
96 desc_name
= msg
.name
if msg
.name
else ""
97 download_dir
= os
.path
.join(descriptor
.DOWNLOAD_DIR
, msg
.id)
99 # If a download dir is present with contents, then we know it has been created in the
101 if os
.path
.exists(download_dir
) and os
.listdir(download_dir
):
102 descriptor
.log
.debug("Skpping folder creation, {} already present".format(download_dir
))
105 download_dir
= os
.path
.join(download_dir
, desc_name
)
106 if not os
.path
.exists(download_dir
):
107 os
.makedirs(download_dir
)
108 descriptor
.log
.debug("Created directory {}".format(download_dir
))
110 model
= RwYang
.Model
.create_libncx()
111 for module
in descriptor
.MODULE_DESC
: model
.load_module(module
)
113 yaml_path
= "{base}/{name}_{type}.yaml".format(base
=download_dir
, name
=msg
.name
, type=descriptor
.DESC_TYPE
)
114 with
open(yaml_path
,"w") as fh
:
115 fh
.write(msg
.to_yaml(model
))