String conversion bug fix
[osm/SO.git] / rwlaunchpad / plugins / rwvnfm / rift / tasklets / rwvnfmtasklet / rwvnfmtasklet.py
index daf8360..14153c9 100755 (executable)
@@ -425,24 +425,22 @@ class VirtualDeploymentUnitRecord(object):
                            metadata_list = list()
                            for metadata_item in opvolume.custom_meta_data:
                                metadata_list.append(metadata_item.as_dict())
-                           if 'guest_params' not in vdurvol_data[0]:
-                               vdurvol_data[0]['guest_params'] = dict()
-                           vdurvol_data[0]['guest_params']['custom_meta_data'] = metadata_list
-
-            if self._vm_resp.has_field('custom_boot_data'):
-                vdur_dict['custom_boot_data'] = dict()
-                if self._vm_resp.custom_boot_data.has_field('custom_drive'):
-                    vdur_dict['custom_boot_data']['custom_drive'] = self._vm_resp.custom_boot_data.custom_drive
-                if self._vm_resp.custom_boot_data.has_field('custom_meta_data'):
+                           vdurvol_data[0]['custom_meta_data'] = metadata_list
+
+            if self._vm_resp.has_field('supplemental_boot_data'):
+                vdur_dict['supplemental_boot_data'] = dict()
+                if self._vm_resp.supplemental_boot_data.has_field('boot_data_drive'):
+                    vdur_dict['supplemental_boot_data']['boot_data_drive'] = self._vm_resp.supplemental_boot_data.boot_data_drive
+                if self._vm_resp.supplemental_boot_data.has_field('custom_meta_data'):
                     metadata_list = list()
-                    for metadata_item in self._vm_resp.custom_boot_data.custom_meta_data:
+                    for metadata_item in self._vm_resp.supplemental_boot_data.custom_meta_data:
                        metadata_list.append(metadata_item.as_dict())
-                    vdur_dict['custom_boot_data']['custom_meta_data'] = metadata_list
-                if self._vm_resp.custom_boot_data.has_field('custom_config_files'):
+                    vdur_dict['supplemental_boot_data']['custom_meta_data'] = metadata_list
+                if self._vm_resp.supplemental_boot_data.has_field('config_file'):
                     file_list = list()
-                    for file_item in self._vm_resp.custom_boot_data.custom_config_files:
+                    for file_item in self._vm_resp.supplemental_boot_data.config_file:
                        file_list.append(file_item.as_dict())
-                    vdur_dict['custom_boot_data']['custom_config_files'] = file_list
+                    vdur_dict['supplemental_boot_data']['config_file'] = file_list
 
         icp_list = []
         ii_list = []
@@ -523,6 +521,7 @@ class VirtualDeploymentUnitRecord(object):
             try:
                 return cloud_init_extractor.read_script(stored_package, filename)
             except rift.package.cloud_init.CloudInitExtractionError as e:
+                self.instantiation_failed(str(e))
                 raise VirtualDeploymentUnitRecordError(e)
         else:
             self._log.debug("VDU Instantiation: cloud-init script not provided")
@@ -577,24 +576,24 @@ class VirtualDeploymentUnitRecord(object):
 
     def process_custom_bootdata(self, vm_create_msg_dict):
         """Process the custom boot data"""
-        if 'custom_config_files' not in vm_create_msg_dict['custom_boot_data']:
+        if 'config_file' not in vm_create_msg_dict['supplemental_boot_data']:
             return
 
+        self._vnfd_package_store.refresh()
         stored_package = self._vnfd_package_store.get_package(self._vnfr.vnfd_id)
-        script_extractor = rift.package.script.PackageScriptExtractor(self._log)
-        for custom_file_item in vm_create_msg_dict['custom_boot_data']['custom_config_files']:
-            if 'source' not in custom_file_item or 'dest' not in custom_file_item:
+        cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(self._log)
+        for file_item in vm_create_msg_dict['supplemental_boot_data']['config_file']:
+            if 'source' not in file_item or 'dest' not in file_item:
                 continue
-            source = custom_file_item['source']
+            source = file_item['source']
             # Find source file in scripts dir of VNFD
-            self._vnfd_package_store.refresh()
             self._log.debug("Checking for source config file at %s", source)
             try:
-               source_file_str = script_extractor.read_script(stored_package, source)
-            except rift.package.script.ScriptExtractionError as e:
+               source_file_str = cloud_init_extractor.read_script(stored_package, source)
+            except rift.package.cloud_init.CloudInitExtractionError as e:
                raise  VirtualDeploymentUnitRecordError(e)
             # Update source file location with file contents
-            custom_file_item['source'] = source_file_str
+            file_item['source'] = source_file_str
 
         return
 
@@ -605,7 +604,7 @@ class VirtualDeploymentUnitRecord(object):
                       "hypervisor_epa",
                       "host_epa",
                       "volumes",
-                      "custom_boot_data"]
+                      "supplemental_boot_data"]
 
         self._log.debug("Creating params based on VDUD: %s", self._vdud)
         vdu_copy_dict = {k: v for k, v in self._vdud.as_dict().items() if k in vdu_fields}
@@ -633,10 +632,12 @@ class VirtualDeploymentUnitRecord(object):
 
         cp_list = []
         for intf, cp, vlr in self._ext_intf:
-            cp_info = {"name": cp.name,
-                       "virtual_link_id": vlr.network_id,
-                       "type_yang": intf.virtual_interface.type_yang,
-                       "port_security_enabled": cp.port_security_enabled}
+            cp_info = { "name": cp.name,
+                        "virtual_link_id": vlr.network_id,
+                        "type_yang": intf.virtual_interface.type_yang }
+
+            if cp.has_field('port_security_enabled'):
+                cp_info["port_security_enabled"] = cp.port_security_enabled
 
             if (intf.virtual_interface.has_field('vpci') and
                     intf.virtual_interface.vpci is not None):
@@ -655,16 +656,22 @@ class VirtualDeploymentUnitRecord(object):
                                 "type_yang": intf.virtual_interface.type_yang,
                                 "vpci": intf.virtual_interface.vpci})
             else:
-                cp_list.append({"name": cp,
-                                "virtual_link_id": vlr.network_id,
-                                "type_yang": intf.virtual_interface.type_yang,
-                                "port_security_enabled": cp.port_security_enabled})
+                if cp.has_field('port_security_enabled'):
+                    cp_list.append({"name": cp,
+                                    "virtual_link_id": vlr.network_id,
+                                    "type_yang": intf.virtual_interface.type_yang,
+                                    "port_security_enabled": cp.port_security_enabled})
+                else:
+                    cp_list.append({"name": cp,
+                                    "virtual_link_id": vlr.network_id,
+                                    "type_yang": intf.virtual_interface.type_yang})
+
 
         vm_create_msg_dict["connection_points"] = cp_list
         vm_create_msg_dict.update(vdu_copy_dict)
 
         self.process_placement_groups(vm_create_msg_dict)
-        if 'custom_boot_data' in vm_create_msg_dict:
+        if 'supplemental_boot_data' in vm_create_msg_dict:
              self.process_custom_bootdata(vm_create_msg_dict) 
 
         msg = RwResourceMgrYang.VDUEventData()
@@ -944,8 +951,8 @@ class VirtualDeploymentUnitRecord(object):
 
             vm_resp = yield from self.create_resource(xact, vnfr, config)
             self._vm_resp = vm_resp
-
             self._state = VDURecordState.RESOURCE_ALLOC_PENDING
+
             self._log.debug("Requested VM from resource manager response %s",
                             vm_resp)
             if vm_resp.resource_state == "active":
@@ -988,13 +995,14 @@ class VlRecordState(enum.Enum):
 
 class InternalVirtualLinkRecord(object):
     """ Internal Virtual Link record """
-    def __init__(self, dts, log, loop, ivld_msg, vnfr_name, cloud_account_name):
+    def __init__(self, dts, log, loop, ivld_msg, vnfr_name, cloud_account_name, ip_profile=None):
         self._dts = dts
         self._log = log
         self._loop = loop
         self._ivld_msg = ivld_msg
         self._vnfr_name = vnfr_name
         self._cloud_account_name = cloud_account_name
+        self._ip_profile = ip_profile
 
         self._vlr_req = self.create_vlr()
         self._vlr = None
@@ -1008,7 +1016,10 @@ class InternalVirtualLinkRecord(object):
     @property
     def name(self):
         """ Name of this VL """
-        return self._vnfr_name + "." + self._ivld_msg.name
+        if self._ivld_msg.vim_network_name:
+            return self._ivld_msg.vim_network_name
+        else:
+            return self._vnfr_name + "." + self._ivld_msg.name
 
     @property
     def network_id(self):
@@ -1027,6 +1038,7 @@ class InternalVirtualLinkRecord(object):
                       "description",
                       "version",
                       "type_yang",
+                      "vim_network_name",
                       "provider_network"]
 
         vld_copy_dict = {k: v for k, v in self._ivld_msg.as_dict().items() if k in vld_fields}
@@ -1035,6 +1047,10 @@ class InternalVirtualLinkRecord(object):
                     "name": self.name,
                     "cloud_account": self._cloud_account_name,
                     }
+
+        if self._ip_profile and self._ip_profile.has_field('ip_profile_params'):
+            vlr_dict['ip_profile_params' ] = self._ip_profile.ip_profile_params.as_dict()
+
         vlr_dict.update(vld_copy_dict)
 
         vlr = RwVlrYang.YangData_Vlr_VlrCatalog_Vlr.from_dict(vlr_dict)
@@ -1418,6 +1434,13 @@ class VirtualNetworkFunctionRecord(object):
         self._log.debug("Published VNFR path = [%s], record = [%s]",
                         self.xpath, self.msg)
 
+    def resolve_vld_ip_profile(self, vnfd_msg, vld):
+        self._log.debug("Receieved ip profile ref is %s",vld.ip_profile_ref)
+        if not vld.has_field('ip_profile_ref'):
+            return None
+        profile = [profile for profile in vnfd_msg.ip_profiles if profile.name == vld.ip_profile_ref]
+        return profile[0] if profile else None
+
     @asyncio.coroutine
     def create_vls(self):
         """ Publish The VLs associated with this VNF """
@@ -1433,7 +1456,8 @@ class VirtualNetworkFunctionRecord(object):
                                             loop=self._loop,
                                             ivld_msg=ivld_msg,
                                             vnfr_name=self.name,
-                                            cloud_account_name=self.cloud_account_name
+                                            cloud_account_name=self.cloud_account_name,
+                                            ip_profile=self.resolve_vld_ip_profile(self.vnfd, ivld_msg)
                                             )
             self._vlrs.append(vlr)
 
@@ -1512,6 +1536,10 @@ class VirtualNetworkFunctionRecord(object):
 
         return placement_groups
 
+    @asyncio.coroutine
+    def vdu_cloud_init_instantiation(self):
+        [vdu.vdud_cloud_init for vdu in self._vdus]
+
     @asyncio.coroutine
     def create_vdus(self, vnfr, restart_mode=False):
         """ Create the VDUs associated with this VNF """
@@ -1576,8 +1604,8 @@ class VirtualNetworkFunctionRecord(object):
         vdu_id_pattern = re.compile(r"\{\{ vdu\[([^]]+)\]\S* \}\}")
 
         for vdu in self._vdus:
-            if vdu.vdud_cloud_init is not None:
-                for vdu_id in vdu_id_pattern.findall(vdu.vdud_cloud_init):
+            if vdu._vdud_cloud_init is not None:
+                for vdu_id in vdu_id_pattern.findall(vdu._vdud_cloud_init):
                     if vdu_id != vdu.vdu_id:
                         # This means that vdu.vdu_id depends upon vdu_id,
                         # i.e. vdu_id must be instantiated before
@@ -1605,7 +1633,6 @@ class VirtualNetworkFunctionRecord(object):
             # wait for the VDUR to enter a terminal state
             while vdu._state not in terminal:
                 yield from asyncio.sleep(1, loop=self._loop)
-
             # update the datastore
             datastore.update(vdu)
 
@@ -1846,6 +1873,7 @@ class VirtualNetworkFunctionRecord(object):
         self._log.debug("VNFR-ID %s: Publish VNFR", self._vnfr_id)
         yield from self.publish(xact)
 
+
         # instantiate VLs
         self._log.debug("VNFR-ID %s: Instantiate VLs", self._vnfr_id)
         try:
@@ -1861,6 +1889,13 @@ class VirtualNetworkFunctionRecord(object):
         self._log.debug("VNFR-ID %s: Create VDUs", self._vnfr_id)
         yield from self.create_vdus(self, restart_mode)
 
+        try:
+            yield from self.vdu_cloud_init_instantiation()
+        except Exception as e:
+            self.set_state(VirtualNetworkFunctionRecordState.FAILED)
+            self._state_failed_reason = str(e)
+            yield from self.publish(xact)
+
         # publish the VNFR
         self._log.debug("VNFR-ID %s: Publish VNFR", self._vnfr_id)
         yield from self.publish(xact)
@@ -2067,7 +2102,7 @@ class VnfrConsoleOperdataDtsHandler(object):
             if action == rwdts.QueryAction.READ:
                 schema = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur.schema()
                 path_entry = schema.keyspec_to_entry(ks_path)
-                self._log.debug("VDU Opdata path is {}".format(path_entry))
+                self._log.debug("VDU Opdata path is {}".format(path_entry.key00.id))
                 try:
                     vnfr = self._vnfm.get_vnfr(self._vnfr_id)
                 except VnfRecordError as e: