Revert "Full Juju Charm support"
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / subscriber / download_status.py
1 #
2 # Copyright 2016-2017 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 gi
21 import os
22
23 import rift.mano.dts as mano_dts
24 import rift.package.store as store
25 from rift.package.convert import (
26 RwVnfdSerializer,
27 RwNsdSerializer,
28 )
29
30 from gi.repository import (
31 RwYang,
32 RwDts
33 )
34
35 class DownloadStatusSubscriber(mano_dts.AbstractOpdataSubscriber):
36 def __init__(self, log, dts, loop, project, callback):
37 super().__init__(log, dts, loop, project, callback)
38
39 def get_xpath(self):
40 return self._project.add_project(
41 "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
42
43
44 class VnfdStatusSubscriber(mano_dts.VnfdCatalogSubscriber):
45 DOWNLOAD_DIR = store.VnfdPackageFilesystemStore.DEFAULT_ROOT_DIR
46 DESC_TYPE = 'vnfd'
47 SERIALIZER = RwVnfdSerializer()
48
49 def __init__(self, log, dts, loop, project):
50 super().__init__(log, dts, loop, project, callback=self.on_change)
51
52 def on_change(self, msg, action):
53 log_msg = "1. Vnfd called w/ msg attributes: {} id {} name {} action: {}". \
54 format(repr(msg), msg.id, msg.name, repr(action))
55 self.log.debug(log_msg)
56 if action == RwDts.QueryAction.UPDATE or action == RwDts.QueryAction.CREATE:
57 actionCreate(self, msg, self.project.name)
58 else:
59 self.log.debug("VnfdStatusSubscriber: No action for {}".format(repr(action)))
60 pass
61
62
63 class NsdStatusSubscriber(mano_dts.NsdCatalogSubscriber):
64 DOWNLOAD_DIR = store.NsdPackageFilesystemStore.DEFAULT_ROOT_DIR
65 DESC_TYPE = 'nsd'
66 SERIALIZER = RwNsdSerializer()
67
68 def __init__(self, log, dts, loop, project):
69 super().__init__(log, dts, loop, project, callback=self.on_change)
70
71 def on_change(self, msg, action):
72 log_msg = "1. Nsd called w/ msg attributes: {} id {} name {} action: {}". \
73 format(repr(msg), msg.id, msg.name, repr(action))
74 self.log.debug(log_msg)
75 if action == RwDts.QueryAction.UPDATE or action == RwDts.QueryAction.CREATE:
76 actionCreate(self, msg, self.project.name)
77 else:
78 self.log.debug("NsdStatusSubscriber: No action for {}".format(repr(action)))
79 pass
80
81
82 def actionCreate(descriptor, msg, project_name=None):
83 ''' Create folder structure if it doesn't exist: id/vnf name OR id/nsd name
84 Serialize the Vnfd/Nsd object to yaml and store yaml file in the created folder.
85 '''
86
87 download_dir = os.path.join(
88 descriptor.DOWNLOAD_DIR,
89 project_name if project_name else "",
90 msg.id)
91
92 # If a download dir is present with contents, then we know it has been created in the
93 # upload path.
94 if os.path.exists(download_dir) and os.listdir(download_dir):
95 descriptor.log.debug("Skpping folder creation, {} already present".format(download_dir))
96 return
97 else:
98 # Folder structure is based on top-level package-id directory
99 if not os.path.exists(download_dir):
100 os.makedirs(download_dir)
101 descriptor.log.debug("Created directory {}".format(download_dir))
102 yaml_path = "{base}/{name}_{type}.yaml". \
103 format(base=download_dir, name=msg.name[0:50], type=descriptor.DESC_TYPE)
104 with open(yaml_path,"w") as fh:
105 fh.write(descriptor.SERIALIZER.to_yaml_string(msg))