Fix vfc-nslcm heal bug
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / create_vnfs.py
index f7b9491..9315ef9 100644 (file)
@@ -71,6 +71,9 @@ class CreateVnfs(Thread):
             self.get_params()
             self.check_nf_name_exist()
             self.get_vnfd_id()
+            if REPORT_TO_AAI:
+                self.create_vnf_in_aai()
+                # self.create_vserver_in_aai()
             self.check_nf_package_valid()
             self.send_nf_init_request_to_vnfm()
             self.send_get_vnfm_request_to_extsys()
@@ -78,9 +81,7 @@ class CreateVnfs(Thread):
             self.wait_vnfm_job_finish()
             self.write_vnf_creation_info()
             self.save_info_to_db()
-            if REPORT_TO_AAI:
-                self.create_vnf_in_aai()
-                self.create_vserver_in_aai()
+            JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0)
         except NSLCMException as e:
             self.vnf_inst_failed_handle(e.message)
         except Exception:
@@ -119,7 +120,8 @@ class CreateVnfs(Thread):
             if self.vnf_id == vnf_info['vnf_id']:
                 self.vnfd_id = vnf_info['properties']['id']
                 if 'name' not in vnf_info['properties']:
-                    self.vnf_inst_name = self.vnfd_id + str(uuid.uuid4())
+                    # HW vnf instance name must start with alphabet
+                    self.vnf_inst_name = 'vnf' + self.vnfd_id + str(uuid.uuid4())
                 else:
                     self.vnf_inst_name = vnf_info['properties']['name'] + str(uuid.uuid4())
                 self.vnf_inst_name = self.vnf_inst_name[:30]
@@ -131,6 +133,7 @@ class CreateVnfs(Thread):
         nfpackage_info = query_vnfpackage_by_id(self.vnfd_id)
         self.nf_package_info = nfpackage_info["packageInfo"]
         self.vnfd_model = ignore_case_get(self.nf_package_info, "vnfdModel")
+        self.vnfd_model = json.loads(self.vnfd_model)
 
     def get_virtual_link_info(self, vnf_id):
         virtual_link_list, ext_virtual_link = [], []
@@ -235,7 +238,7 @@ class CreateVnfs(Thread):
                               timeout=NFVO_VNF_INST_TIMEOUT_SECOND)
 
         if ret != JOB_MODEL_STATUS.FINISHED:
-            logger.error('VNF instantiation failed on VNFM side.')
+            logger.error('VNF instantiation failed on VNFM side. ret=[%s]', ret)
             raise NSLCMException('VNF instantiation failed on VNFM side.')
 
     def write_vnf_creation_info(self):
@@ -254,7 +257,6 @@ class CreateVnfs(Thread):
         logger.debug("save_info_to_db start")
         do_biz_with_share_lock("set-vnflist-in-vnffginst-%s" % self.ns_inst_id, self.save_vnf_inst_id_in_vnffg)
         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.ACTIVE, lastuptime=now_time())
-        JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0)
         logger.debug("save_info_to_db end")
 
     def vnf_inst_failed_handle(self, error_msg):
@@ -277,80 +279,88 @@ class CreateVnfs(Thread):
 
     def create_vnf_in_aai(self):
         logger.debug("CreateVnfs::create_vnf_in_aai::report vnf instance[%s] to aai." % self.nf_inst_id)
-        data = {
-            "vnf-id": self.nf_inst_id,
-            "vnf-name": self.vnf_inst_name,
-            "vnf-type": "vnf-type-test111",
-            "service-id": self.ns_inst_id,
-            "in-maint": True,
-            "is-closed-loop-disabled": False,
-            "relationship-list": {
-                "relationship": [
-                    {
-                        "related-to": "service-instance",
-                        "relationship-data": [
-                            {
-                                "relationship-key": "customer.global-customer-id",
-                                "relationship-value": "global-customer-id-" + self.ns_inst_id
-                            },
-                            {
-                                "relationship-key": "service-subscription.service-type",
-                                "relationship-value": "Network"
-                            },
-                            {
-                                "relationship-key": "service-instance.service-instance-id",
-                                "relationship-value": self.ns_inst_id
-                            }
-                        ]
-                    }
-                ]
-            }
-        }
-        resp_data, resp_status = create_vnf_aai(self.nf_inst_id, data)
-        if resp_data:
-            logger.debug("Fail to create vnf instance[%s] to aai, vnf instance=[%s], resp_status: [%s]."
-                         % (self.nf_inst_id, self.ns_inst_id, resp_status))
-        else:
-            logger.debug("Success to create vnf instance[%s] to aai, vnf instance=[%s], resp_status: [%s]."
-                         % (self.nf_inst_id, self.ns_inst_id, resp_status))
-
-    def create_vserver_in_aai(self):
-        logger.debug("CreateVnfs::create_vserver_in_aai::report vserver instance to aai.")
-        cloud_owner, cloud_region_id = split_vim_to_owner_region(self.vim_id)
-
-        # query vim_info from aai
-        vim_info = get_vim_by_id(self.vim_id)
-        tenant_id = vim_info["tenant"]
-        vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.nf_inst_id)
-        for vm_inst_info in vm_inst_infos:
-            vserver_id = vm_inst_info.resouceid
+        try:
+            ns_insts = NSInstModel.objects.filter(id=self.ns_inst_id)
+            self.global_customer_id = ns_insts[0].global_customer_id
+            self.service_type = ns_insts[0].service_type
             data = {
-                "vserver-id": vm_inst_info.resouceid,
-                "vserver-name": vm_inst_info.vmname,
-                "prov-status": "ACTIVE",
-                "vserver-selflink": "",
+                "vnf-id": self.nf_inst_id,
+                "vnf-name": self.vnf_inst_name,
+                "vnf-type": "vnf-type-test111",
+                "service-id": self.ns_inst_id,
                 "in-maint": True,
                 "is-closed-loop-disabled": False,
                 "relationship-list": {
                     "relationship": [
                         {
-                            "related-to": "generic-vnf",
+                            "related-to": "service-instance",
                             "relationship-data": [
                                 {
-                                    "relationship-key": "generic-vnf.vnf-id",
-                                    "relationship-value": self.nf_inst_id
+                                    "relationship-key": "customer.global-customer-id",
+                                    "relationship-value": self.global_customer_id
+                                },
+                                {
+                                    "relationship-key": "service-subscription.service-type",
+                                    "relationship-value": self.service_type
+                                },
+                                {
+                                    "relationship-key": "service-instance.service-instance-id",
+                                    "relationship-value": self.ns_inst_id
                                 }
                             ]
                         }
                     ]
                 }
             }
+            resp_data, resp_status = create_vnf_aai(self.nf_inst_id, data)
+            logger.debug("Success to create vnf[%s] to aai, ns instance=[%s], resp_status: [%s]."
+                         % (self.nf_inst_id, self.ns_inst_id, resp_status))
+        except NSLCMException as e:
+            logger.debug("Fail to create vnf[%s] to aai, ns instance=[%s], detail message: %s"
+                         % (self.nf_inst_id, self.ns_inst_id, e.message))
+        except:
+            logger.error(traceback.format_exc())
 
-            # create vserver instance in aai
-            resp_data, resp_status = create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data)
-            if resp_data:
-                logger.debug("Fail to create vserver instance[%s] to aai, vnf instance=[%s], resp_status: [%s]."
-                             % (vserver_id, self.nf_inst_id, resp_status))
-            else:
-                logger.debug("Success to create vserver instance[%s] to aai, vnf instance=[%s], resp_status: [%s]."
+    def create_vserver_in_aai(self):
+        logger.debug("CreateVnfs::create_vserver_in_aai::report vserver instance to aai.")
+        try:
+            cloud_owner, cloud_region_id = split_vim_to_owner_region(self.vim_id)
+
+            # query vim_info from aai
+            vim_info = get_vim_by_id(self.vim_id)
+            tenant_id = vim_info["tenantId"]
+            vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.nf_inst_id)
+            for vm_inst_info in vm_inst_infos:
+                vserver_id = vm_inst_info.resouceid
+                data = {
+                    "vserver-id": vm_inst_info.resouceid,
+                    "vserver-name": vm_inst_info.vmname,
+                    "prov-status": "ACTIVE",
+                    "vserver-selflink": "",
+                    "in-maint": True,
+                    "is-closed-loop-disabled": False,
+                    "relationship-list": {
+                        "relationship": [
+                            {
+                                "related-to": "generic-vnf",
+                                "relationship-data": [
+                                    {
+                                        "relationship-key": "generic-vnf.vnf-id",
+                                        "relationship-value": self.nf_inst_id
+                                    }
+                                ]
+                            }
+                        ]
+                    }
+                }
+
+                # create vserver instance in aai
+                resp_data, resp_status = create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data)
+                logger.debug("Success to create vserver[%s] to aai, vnf instance=[%s], resp_status: [%s]."
                              % (vserver_id, self.nf_inst_id, resp_status))
+            logger.debug("Success to create all vserver to aai.")
+        except NSLCMException as e:
+            logger.debug("Fail to create vserver to aai, vnf instance=[%s], detail message: %s"
+                         % (self.nf_inst_id, e.message))
+        except:
+            logger.error(traceback.format_exc())