update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / proxy / filesystem.py
index cc89889..907815e 100644 (file)
@@ -23,9 +23,10 @@ import os
 import rift.package.store as store
 import rift.package.package
 import rift.package.icon as icon
+import rift.package.checksums as checksums
 
 from .base import AbstractPackageManagerProxy
-
+from rift.tasklets.rwlaunchpad import image
 
 class UnknownPackageType(Exception):
     pass
@@ -40,35 +41,38 @@ class FileSystemProxy(AbstractPackageManagerProxy):
     # Refer: https://confluence.riftio.com/display/ATG/Launchpad+package+formats
     SCHEMA = {
         "nsd": ["icons", "ns_config", "scripts", "vnf_config"],
-        "vnfd": ["charms", "cloud_init", "icons", "images", "scripts", "readme"]
+        "vnfd": ["charms", "cloud_init", "icons", "images", "scripts", "readme", "test", "doc"]
     }
 
     SCHEMA_TO_PERMS = {'scripts': 0o777}
 
-    def __init__(self, loop, log):
+    def __init__(self, loop, log, dts):
         self.loop = loop
         self.log = log
+        self.dts = dts
         self.store_cache = {}
+        self.uploader = image.ImageUploader(self.log, self.loop, self.dts)
 
-    def _get_store(self, package_type):
+    def _get_store(self, package_type, project_name = None):
         store_cls = self.PACKAGE_TYPE_MAP[package_type]
-        store = self.store_cache.setdefault(package_type, store_cls(self.log))
+        self.store_cache[package_type] = store_cls(self.log, project=project_name)
+        store = self.store_cache[package_type]
 
         return store
 
     @asyncio.coroutine
-    def endpoint(self, package_type, package_id):
+    def endpoint(self, package_type, package_id, project_name=None):
         package_type = package_type.lower()
         if package_type not in self.PACKAGE_TYPE_MAP:
             raise UnknownPackageType()
-
-        store = self._get_store(package_type)
+        
+        store = self._get_store(package_type, project_name)
 
         package = store._get_package_dir(package_id)
-        rel_path = os.path.relpath(package, start=store.root_dir)
-
-        url = "https://127.0.0.1:4567/api/package/{}/{}".format(package_type, rel_path)
+        rel_path = os.path.relpath(package, start=os.path.dirname(store.root_dir))
 
+        url = "https://127.0.0.1:8008/mano/api/package/{}/{}".format(package_type, rel_path)
+        
         return url
 
     @asyncio.coroutine
@@ -79,15 +83,17 @@ class FileSystemProxy(AbstractPackageManagerProxy):
 
         return self.SCHEMA[package_type]
 
-    def package_file_add(self, new_file, package_type, package_id, package_path, package_file_type):
+    def package_file_add(self, new_file, package_type, package_id, package_path, package_file_type, project_name):
         # Get the schema from thr package path
         # the first part will always be the vnfd/nsd name
         mode = 0o664
 
         # for files other than README, create the package path from the asset type, e.g. icons/icon1.png
         # for README files, strip off any leading '/' 
+        file_name = package_path
         package_path = package_file_type + "/" + package_path \
             if package_file_type != "readme" else package_path.strip('/')
+        
         components = package_path.split("/")
         if len(components) > 2:
             schema = components[1]
@@ -95,7 +101,7 @@ class FileSystemProxy(AbstractPackageManagerProxy):
 
         # Fetch the package object
         package_type = package_type.lower()
-        store = self._get_store(package_type)
+        store = self._get_store(package_type, project_name)
         package = store.get_package(package_id)
 
         # Construct abs path of the destination obj
@@ -105,12 +111,22 @@ class FileSystemProxy(AbstractPackageManagerProxy):
         # Insert (by copy) the file in the package location. For icons, 
         # insert also in UI location for UI to pickup
         try:
+            self.log.debug("Inserting file {} in the destination {} - {} ".format(dest_file, package_path, dest_file))
             package.insert_file(new_file, dest_file, package_path, mode=mode)
 
             if package_file_type == 'icons': 
                 icon_extract = icon.PackageIconExtractor(self.log) 
                 icon_extract.extract_icons(package)
 
+            if package_file_type == 'images':                                
+                image_hdl = package.open(package_path)
+                image_checksum = checksums.checksum(image_hdl)
+                
+                try:
+                    self.uploader.upload_image(file_name, image_checksum, image_hdl, {})
+                    self.uploader.upload_image_to_cloud_accounts(file_name, image_checksum, project_name)
+                finally:
+                    _ = image_hdl.close()
         except rift.package.package.PackageAppendError as e:
             self.log.exception(e)
             return False
@@ -118,9 +134,9 @@ class FileSystemProxy(AbstractPackageManagerProxy):
         self.log.debug("File insertion complete at {}".format(dest_file))
         return True
 
-    def package_file_delete(self, package_type, package_id, package_path, package_file_type):
+    def package_file_delete(self, package_type, package_id, package_path, package_file_type, project_name):
         package_type = package_type.lower()
-        store = self._get_store(package_type)
+        store = self._get_store(package_type, project_name)
         package = store.get_package(package_id)
 
         # for files other than README, create the relative package path from the asset type