X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=rwlaunchpad%2Fplugins%2Frwpkgmgr%2Frift%2Ftasklets%2Frwpkgmgr%2Fsubscriber%2Fdownload_status.py;h=8fd041ecac9b3730226ab852e1fd531b3ec26895;hb=a3fef71edb69a08e1d0ac8b498296809aab7188c;hp=c4a5a5315c6ad5b4f78b4e2d05fa7ca38ac8e05b;hpb=df4e972f5e6581a85dd5a072ac4da8585b4c83e6;p=osm%2FSO.git diff --git a/rwlaunchpad/plugins/rwpkgmgr/rift/tasklets/rwpkgmgr/subscriber/download_status.py b/rwlaunchpad/plugins/rwpkgmgr/rift/tasklets/rwpkgmgr/subscriber/download_status.py index c4a5a531..8fd041ec 100644 --- a/rwlaunchpad/plugins/rwpkgmgr/rift/tasklets/rwpkgmgr/subscriber/download_status.py +++ b/rwlaunchpad/plugins/rwpkgmgr/rift/tasklets/rwpkgmgr/subscriber/download_status.py @@ -17,12 +17,100 @@ # Creation Date: 09/25/2016 # +import os +import io +import shutil + import rift.mano.dts as mano_dts +import rift.package.package as package +import rift.package.store as store +import rift.package.convert as convert +from gi.repository import ( + RwYang, + NsdYang, + RwNsdYang, + VnfdYang, + RwVnfdYang, + RwDts +) class DownloadStatusSubscriber(mano_dts.AbstractOpdataSubscriber): - def __init__(self, log, dts, loop, callback=None): - super().__init__(log, dts, loop, callback) - def get_xpath(self): + def __init__(self, log, dts, loop, callback): + super().__init__(log, dts, loop, callback) + + def get_xpath(self): return ("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job") + +class VnfdStatusSubscriber(DownloadStatusSubscriber): + DOWNLOAD_DIR = store.VnfdPackageFilesystemStore.DEFAULT_ROOT_DIR + MODULE_DESC = 'vnfd rw-vnfd'.split() + DESC_TYPE = 'vnfd' + + def __init__(self, log, dts, loop): + super().__init__(log, dts, loop, self.on_change) + self.subscriber = mano_dts.VnfdCatalogSubscriber(log, dts, loop) + + def on_change(self, msg, action): + log_msg = "1. Vnfd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg), msg.id, msg.name, repr(action)) + self.log.debug(log_msg) + if action == RwDts.QueryAction.UPDATE: + actionCreate(self, msg) + + elif action == RwDts.QueryAction.DELETE: + actionDelete(self, msg) + + def get_xpath(self): + return self.subscriber.get_xpath() + + +class NsdStatusSubscriber(DownloadStatusSubscriber): + DOWNLOAD_DIR = store.NsdPackageFilesystemStore.DEFAULT_ROOT_DIR + MODULE_DESC = 'nsd rw-nsd'.split() + DESC_TYPE = 'nsd' + + def __init__(self, log, dts, loop): + super().__init__(log, dts, loop, self.on_change) + self.subscriber = mano_dts.NsdCatalogSubscriber(log, dts, loop) + + def on_change(self, msg, action): + log_msg = "1. Nsd called w/ msg attributes: {} id {} name {} action: {}".format(repr(msg), msg.id, msg.name, repr(action)) + self.log.debug(log_msg) + if action == RwDts.QueryAction.UPDATE: + actionCreate(self, msg) + + elif action == RwDts.QueryAction.DELETE: + actionDelete(self, msg) + + def get_xpath(self): + return self.subscriber.get_xpath() + + +def actionCreate(descriptor, msg): + ''' Create folder structure if it doesn't exist: id/vnf name OR id/nsd name + Serialize the Vnfd/Nsd object to yaml and store yaml file in the created folder. + ''' + + download_dir = os.path.join(descriptor.DOWNLOAD_DIR, msg.id, msg.name) + + if not os.path.exists(download_dir): + os.makedirs(download_dir) + descriptor.log.debug("Created directory {}".format(download_dir)) + + model = RwYang.Model.create_libncx() + for module in descriptor.MODULE_DESC: model.load_module(module) + + yaml_path = "{base}/{name}_{type}.yaml".format(base=download_dir, name=msg.name, type=descriptor.DESC_TYPE) + with open(yaml_path,"w") as fh: + fh.write(msg.to_yaml(model)) + +def actionDelete(descriptor, msg): + ''' Delete folder structure created above. + ''' + + download_dir = os.path.join(descriptor.DOWNLOAD_DIR, msg.id) + if os.path.exists(download_dir): + descriptor.log.debug("Removing directory {}".format(download_dir)) + shutil.rmtree(download_dir) +