Commit to master [RIFT 15737] Remove logic for deletion of folder on deletion of...
[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
40 def __init__(self, log, dts, loop, callback):
41 super().__init__(log, dts, loop, callback)
42
43 def get_xpath(self):
44 return ("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
45
46 class VnfdStatusSubscriber(DownloadStatusSubscriber):
47 DOWNLOAD_DIR = store.VnfdPackageFilesystemStore.DEFAULT_ROOT_DIR
48 MODULE_DESC = 'vnfd rw-vnfd'.split()
49 DESC_TYPE = 'vnfd'
50
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)
54
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)
60 else:
61 self.log.debug("VnfdStatusSubscriber: No action for ", action)
62 pass
63
64 def get_xpath(self):
65 return self.subscriber.get_xpath()
66
67
68 class NsdStatusSubscriber(DownloadStatusSubscriber):
69 DOWNLOAD_DIR = store.NsdPackageFilesystemStore.DEFAULT_ROOT_DIR
70 MODULE_DESC = 'nsd rw-nsd'.split()
71 DESC_TYPE = 'nsd'
72
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)
76
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)
82 else:
83 self.log.debug("NsdStatusSubscriber: No action for ", action)
84 pass
85
86 def get_xpath(self):
87 return self.subscriber.get_xpath()
88
89
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.
93 '''
94
95 desc_name = msg.name if msg.name else ""
96 download_dir = os.path.join(descriptor.DOWNLOAD_DIR, msg.id, desc_name)
97
98 if not os.path.exists(download_dir):
99 os.makedirs(download_dir)
100 descriptor.log.debug("Created directory {}".format(download_dir))
101
102 model = RwYang.Model.create_libncx()
103 for module in descriptor.MODULE_DESC: model.load_module(module)
104
105 yaml_path = "{base}/{name}_{type}.yaml".format(base=download_dir, name=msg.name, type=descriptor.DESC_TYPE)
106 with open(yaml_path,"w") as fh:
107 fh.write(msg.to_yaml(model))
108