Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / subscriber / download_status.py
1 #
2 # Copyright 2016 RIFT.IO Inc
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
18 #
19
20 import os
21 import io
22 import shutil
23
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
28
29 from gi.repository import (
30 RwYang,
31 NsdYang,
32 RwNsdYang,
33 VnfdYang,
34 RwVnfdYang,
35 RwDts
36 )
37
38 class DownloadStatusSubscriber(mano_dts.AbstractOpdataSubscriber):
39 def __init__(self, log, dts, loop, project, callback):
40 super().__init__(log, dts, loop, project, callback)
41
42 def get_xpath(self):
43 return self._project.add_project(
44 "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
45
46
47 class VnfdStatusSubscriber(DownloadStatusSubscriber):
48 DOWNLOAD_DIR = store.VnfdPackageFilesystemStore.DEFAULT_ROOT_DIR
49 MODULE_DESC = 'vnfd rw-vnfd'.split()
50 DESC_TYPE = 'vnfd'
51
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)
55
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)
61 else:
62 self.log.debug("VnfdStatusSubscriber: No action for {}".format(repr(action)))
63 pass
64
65 def get_xpath(self):
66 return self.subscriber.get_xpath()
67
68
69 class NsdStatusSubscriber(DownloadStatusSubscriber):
70 DOWNLOAD_DIR = store.NsdPackageFilesystemStore.DEFAULT_ROOT_DIR
71 MODULE_DESC = 'nsd rw-nsd'.split()
72 DESC_TYPE = 'nsd'
73
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)
77
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)
83 else:
84 self.log.debug("NsdStatusSubscriber: No action for {}".format(repr(action)))
85 pass
86
87 def get_xpath(self):
88 return self.subscriber.get_xpath()
89
90
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.
94 '''
95
96 desc_name = msg.name if msg.name else ""
97 download_dir = os.path.join(descriptor.DOWNLOAD_DIR, msg.id)
98
99 # If a download dir is present with contents, then we know it has been created in the
100 # upload path.
101 if os.path.exists(download_dir) and os.listdir(download_dir):
102 descriptor.log.debug("Skpping folder creation, {} already present".format(download_dir))
103 return
104 else:
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))
109
110 model = RwYang.Model.create_libncx()
111 for module in descriptor.MODULE_DESC: model.load_module(module)
112
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))
116