modify ns instance
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_instant.py
index 1588ae9..e6f0d1b 100644 (file)
@@ -35,6 +35,7 @@ from lcm.pub.utils.values import ignore_case_get
 from lcm.workflows import build_in
 from lcm.ns.biz.ns_instantiate_flow import run_ns_instantiate
 from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc
+from lcm.ns.enum import NS_INST_STATUS
 
 logger = logging.getLogger(__name__)
 
@@ -57,6 +58,7 @@ class InstantNSService(object):
     def do_biz(self):
         job_id = JobUtil.create_job("NS", "NS_INST", self.ns_inst_id)
         occ_id = NsLcmOpOcc.create(self.ns_inst_id, "INSTANTIATE", "PROCESSING", False, self.req_data)
+        NSInstModel.objects.filter(id=self.ns_inst_id).update(status=NS_INST_STATUS.INSTANTIATING)
 
         try:
             logger.debug('ns-instant(%s) workflow starting...' % self.ns_inst_id)
@@ -77,9 +79,9 @@ class InstantNSService(object):
             else:
                 params_json = json.JSONEncoder().encode({})
 
-            location_constraints = []
-            if 'locationConstraints' in self.req_data:
-                location_constraints = self.req_data['locationConstraints']
+            location_constraints = [] if not self.req_data.get('locationConstraints')\
+                else self.req_data['locationConstraints']
+            vnf_vim = self.get_vnf_vim_info(location_constraints)
 
             JobUtil.add_job_status(job_id, 5, 'Start query nsd(%s)' % ns_inst.nspackage_id)
             dst_plan = sdc_run_catalog.parse_nsd(ns_inst.nspackage_id, input_parameters)
@@ -91,12 +93,11 @@ class InstantNSService(object):
             plan_dict = json.JSONDecoder().decode(dst_plan)
             for vnf in ignore_case_get(plan_dict, "vnfs"):
                 vnfd_id = vnf['properties']['id']
-                vnfm_type_temp = vnf['properties'].get("vnfm_info", "undefined")
-                logger.debug("vnfd_id: %s, vnfm_type : %s", vnfd_id, vnfm_type_temp)
-                vnfm_type = vnfm_type_temp
-                if isinstance(vnfm_type_temp, list):
-                    vnfm_type = vnfm_type_temp[0]
-                vimid = self.get_vnf_vim_id(vim_id, location_constraints, vnfd_id)
+                vnfm_type = vnf['properties'].get("vnfm_info", "undefined")
+                logger.debug("vnfd_id: %s, vnfm_type : %s", vnfd_id, vnfm_type)
+                if isinstance(vnfm_type, list):
+                    vnfm_type = vnfm_type[0]
+                vimid = self.get_vnf_vim_id(vim_id, vnf_vim, vnfd_id)
                 s_vimid = "%s_%s" % (vimid["cloud_owner"], vimid["cloud_regionid"])
                 vnfm_info = extsys.select_vnfm(vnfm_type=vnfm_type, vim_id=s_vimid)
 
@@ -111,7 +112,7 @@ class InstantNSService(object):
                     }
                 })
 
-            self.set_vl_vim_id(vim_id, location_constraints, plan_dict)
+            self.set_vl_vim_id(vim_id, vnf_vim, plan_dict)
             dst_plan = json.JSONEncoder().encode(plan_dict)
             logger.debug('tosca plan dest add vimid:%s' % dst_plan)
             NSInstModel.objects.filter(id=self.ns_inst_id).update(nsd_model=dst_plan)
@@ -177,6 +178,7 @@ class InstantNSService(object):
             logger.error("ns-instant(%s) workflow error:%s" % (self.ns_inst_id, e.args[0]))
             NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.args[0])
             JobUtil.add_job_status(job_id, 255, 'NS instantiation failed')
+            build_in.post_deal(self.ns_inst_id, "false")
             return dict(data={'error': e.args[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
     def start_wso2_workflow(self, job_id, ns_inst, plan_input, occ_id):
@@ -191,7 +193,7 @@ class InstantNSService(object):
         JobUtil.add_job_status(job_id, 10, 'NS inst(%s) wso2 workflow started: %s' % (
             self.ns_inst_id, ret.get('status')))
         if ret.get('status') == 1:
-            return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
+            return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id)
         return dict(data={'error': ret['message']}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
     def start_activiti_workflow(self, job_id, plan_input, occ_id):
@@ -222,39 +224,18 @@ class InstantNSService(object):
         return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id)
 
     @staticmethod
-    def get_vnf_vim_id(vim_id, location_constraints, vnfdid):
-        for location in location_constraints:
-            if "vnfProfileId" in location and vnfdid == location["vnfProfileId"]:
-                # if 'vimId' in location['locationConstraints']:
-                if len(location['locationConstraints']) == 1:
-                    cloud_owner = location['locationConstraints']["vimId"].split('_')[0]
-                    cloud_regionid = location['locationConstraints']["vimId"].split('_')[1]
-                    vim_id = {"cloud_owner": cloud_owner, "cloud_regionid": cloud_regionid}
-                elif len(location['locationConstraints']) == 2:
-                    cloud_owner = location['locationConstraints']["cloudOwner"]
-                    cloud_regionid = location['locationConstraints']["cloudRegionId"]
-                    vim_id = {"cloud_owner": cloud_owner, "cloud_regionid": cloud_regionid}
-                return vim_id
-        if vim_id:
-            return vim_id
+    def get_vnf_vim_id(vim_id, vnf_vim, vnfdid):
+        new_vim_id = vnf_vim.get(vnfdid) if vnf_vim.get(vnfdid) else vim_id
+        if new_vim_id:
+            return new_vim_id
         raise NSLCMException("No Vim info is found for vnf(%s)." % vnfdid)
 
     @staticmethod
-    def set_vl_vim_id(vim_id, location_constraints, plan_dict):
-        if "vls" not in plan_dict:
-            logger.debug("No vl is found in nsd.")
-            return
-        vl_vnf = {}
-        for vnf in ignore_case_get(plan_dict, "vnfs"):
-            if "dependencies" in vnf:
-                for depend in vnf["dependencies"]:
-                    vl_vnf[depend["vl_id"]] = vnf['properties']['id']
+    def get_vnf_vim_info(location_constraints):
         vnf_vim = {}
-
         for location in location_constraints:
             if "vnfProfileId" in location:
                 vnfd_id = location["vnfProfileId"]
-                # if 'vimId' in location["locationConstraints"]:
                 if len(location['locationConstraints']) == 1:
                     cloud_owner = location["locationConstraints"]["vimId"].split('_')[0]
                     cloud_regionid = location["locationConstraints"]["vimId"].split('_')[1]
@@ -265,13 +246,23 @@ class InstantNSService(object):
                     cloud_regionid = location["locationConstraints"]["cloudRegionId"]
                     vim_id = {"cloud_owner": cloud_owner, "cloud_regionid": cloud_regionid}
                     vnf_vim[vnfd_id] = vim_id
+        return vnf_vim
+
+    @staticmethod
+    def set_vl_vim_id(vim_id, vnf_vim, plan_dict):
+        if "vls" not in plan_dict:
+            logger.debug("No vl is found in nsd.")
+            return
+        vl_vnf = {}
+        for vnf in ignore_case_get(plan_dict, "vnfs"):
+            if "dependencies" in vnf:
+                for depend in vnf["dependencies"]:
+                    vl_vnf[depend["vl_id"]] = vnf['properties']['id']
         for vl in plan_dict["vls"]:
             vnfdid = ignore_case_get(vl_vnf, vl["vl_id"])
             vimid = ignore_case_get(vnf_vim, vnfdid)
             if not vimid:
                 vimid = vim_id
-            if not vimid:
-                raise NSLCMException("No Vim info for vl(%s) of vnf(%s)." % (vl["vl_id"], vnfdid))
             if "location_info" not in vl["properties"]:
                 vl["properties"]["location_info"] = {}
             vl["properties"]["location_info"]["vimid"] = vimid
@@ -283,11 +274,11 @@ class InstantNSService(object):
         sfcs = len(data.get('fps', []))
         vnfs = len(data.get('vnfs', []))
         pnfs = len(data.get('pnfs', []))
-        return {'vlCount': str(vls), 'sfcCount': str(sfcs), 'vnfCount': str(vnfs), 'pnfCount': str(pnfs)}
+        return {'vlCount': vls, 'sfcCount': sfcs, 'vnfCount': vnfs, 'pnfCount': pnfs}
 
     def init_pnf_para(self, plan_dict):
-        pnfs_in_input = ignore_case_get(self.req_data, "addpnfData")
-        pnfs_in_nsd = ignore_case_get(plan_dict, "pnfs")
+        pnfs_in_input = ignore_case_get(self.req_data, "addpnfData", [])
+        pnfs_in_nsd = ignore_case_get(plan_dict, "pnfs", [])
         logger.debug("addpnfData ; %s" % pnfs_in_input)
         logger.debug("pnfs_in_nsd ; %s" % pnfs_in_nsd)
         pnfs = {}