modify ns instance
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_instant.py
index 1f72b32..e6f0d1b 100644 (file)
@@ -34,17 +34,20 @@ from lcm.pub.utils.jobutil import JobUtil
 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__)
 
 
 class BuildInWorkflowThread(Thread):
-    def __init__(self, plan_input):
+    def __init__(self, plan_input, occ_id):
         Thread.__init__(self)
         self.plan_input = plan_input
+        self.occ_id = occ_id
 
     def run(self):
-        build_in.run_ns_instantiate(self.plan_input)
+        build_in.run_ns_instantiate(self.plan_input, self.occ_id)
 
 
 class InstantNSService(object):
@@ -53,26 +56,32 @@ class InstantNSService(object):
         self.req_data = plan_content
 
     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:
-            job_id = JobUtil.create_job("NS", "NS_INST", self.ns_inst_id)
             logger.debug('ns-instant(%s) workflow starting...' % self.ns_inst_id)
             logger.debug('req_data=%s' % self.req_data)
             ns_inst = NSInstModel.objects.get(id=self.ns_inst_id)
-            vim_id = ''
+            vim_id = {}
 
             input_parameters = []
             if 'additionalParamForNs' in self.req_data:
-                for key, val in self.req_data['additionalParamForNs'].items():
+                for key, val in list(self.req_data['additionalParamForNs'].items()):
                     input_parameters.append({"key": key, "value": val})
+
                 if 'location' in self.req_data['additionalParamForNs']:
-                    vim_id = self.req_data['additionalParamForNs']['location']
+                    cloud_owner = self.req_data['additionalParamForNs']['location'].split('_')[0]
+                    cloud_regionid = self.req_data["additionalParamForNs"]["location"].split('_')[1]
+                    vim_id = {"cloud_owner": cloud_owner, "cloud_regionid": cloud_regionid}
                 params_json = json.JSONEncoder().encode(self.req_data["additionalParamForNs"])
             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)
@@ -84,15 +93,13 @@ 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)
-                if vnfm_type_temp != "undefined":
-                    if isinstance(vnfm_type_temp, list):
-                        vnfm_type = vnfm_type_temp[0]
-                    if isinstance(vnfm_type_temp, str):
-                        vnfm_type = vnfm_type_temp
-                vimid = self.get_vnf_vim_id(vim_id, location_constraints, vnfd_id)
-                vnfm_info = extsys.select_vnfm(vnfm_type=vnfm_type, vim_id=vimid)
+                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)
 
                 params_vnf.append({
                     "vnfProfileId": vnf["vnf_id"],
@@ -105,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)
@@ -143,7 +150,7 @@ class InstantNSService(object):
                                    template_name=service_tpl['templateName'],
                                    template_id=service_tpl['serviceTemplateId']).save()
 
-                for key, val in self.req_data['additionalParamForNs'].items():
+                for key, val in list(self.req_data['additionalParamForNs'].items()):
                     InputParamMappingModel(service_id=self.ns_inst_id,
                                            input_key=key,
                                            input_value=val).save()
@@ -154,25 +161,28 @@ class InstantNSService(object):
                                    nsinstid=self.ns_inst_id,
                                    endpointnumber=0).save()
             else:
-                # TODO:
+                # TODO
                 pass
             logger.debug("workflow option: %s" % config.WORKFLOW_OPTION)
             if config.WORKFLOW_OPTION == "wso2":
-                return self.start_wso2_workflow(job_id, ns_inst, plan_input)
+                return self.start_wso2_workflow(job_id, ns_inst, plan_input, occ_id=occ_id)
             elif config.WORKFLOW_OPTION == "activiti":
-                return self.start_activiti_workflow()
+                return self.start_activiti_workflow(job_id, plan_input, occ_id=occ_id)
             elif config.WORKFLOW_OPTION == "grapflow":
-                return self.start_buildin_grapflow(job_id, plan_input)
+                return self.start_buildin_grapflow(job_id, plan_input, occ_id=occ_id)
             else:
-                return self.start_buildin_workflow(job_id, plan_input)
+                return self.start_buildin_workflow(job_id, plan_input, occ_id=occ_id)
 
         except Exception as e:
             logger.error(traceback.format_exc())
-            logger.error("ns-instant(%s) workflow error:%s" % (self.ns_inst_id, e.message))
+            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')
-            return dict(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+            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):
+    def start_wso2_workflow(self, job_id, ns_inst, plan_input, occ_id):
+        # todo occ_id
         servicetemplate_id = get_servicetemplate_id(ns_inst.nsd_id)
         process_id = get_process_id('init', servicetemplate_id)
         data = {"processId": process_id, "params": {"planInput": plan_input}}
@@ -183,10 +193,11 @@ 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):
+    def start_activiti_workflow(self, job_id, plan_input, occ_id):
+        # todo occ_id
         plans = WFPlanModel.objects.filter()
         if not plans:
             raise NSLCMException("No plan is found, you should deploy plan first!")
@@ -199,30 +210,46 @@ class InstantNSService(object):
         JobUtil.add_job_status(job_id, 10, 'NS inst(%s) activiti 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_buildin_workflow(self, job_id, plan_input):
+    def start_buildin_workflow(self, job_id, plan_input, occ_id):
         JobUtil.add_job_status(job_id, 10, 'NS inst(%s) buildin workflow started.' % self.ns_inst_id)
-        BuildInWorkflowThread(plan_input).start()
-        return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
+        BuildInWorkflowThread(plan_input, occ_id).start()
+        return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id)
 
-    def start_buildin_grapflow(self, job_id, plan_input):
+    def start_buildin_grapflow(self, job_id, plan_input, occ_id):
         JobUtil.add_job_status(job_id, 10, 'NS inst(%s) buildin grap workflow started.' % self.ns_inst_id)
-        run_ns_instantiate(plan_input)
-        return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
+        run_ns_instantiate(plan_input, occ_id)
+        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"]:
-                return location["locationConstraints"]["vimId"]
-        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):
+    def get_vnf_vim_info(location_constraints):
+        vnf_vim = {}
+        for location in location_constraints:
+            if "vnfProfileId" in location:
+                vnfd_id = location["vnfProfileId"]
+                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}
+                    vnf_vim[vnfd_id] = vim_id
+                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}
+                    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
@@ -231,18 +258,11 @@ class InstantNSService(object):
             if "dependencies" in vnf:
                 for depend in vnf["dependencies"]:
                     vl_vnf[depend["vl_id"]] = vnf['properties']['id']
-        vnf_vim = {}
-        for location in location_constraints:
-            if "vnfProfileId" in location:
-                vnfd_id = location["vnfProfileId"]
-                vnf_vim[vnfd_id] = location["locationConstraints"]["vimId"]
         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
@@ -254,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 = {}
@@ -267,7 +287,6 @@ class InstantNSService(object):
                 if pnfd["properties"]["descriptor_id"] == pnf["pnfdId"]:
                     k = pnfd["pnf_id"]
                     pnf["nsInstances"] = self.ns_inst_id
-                    # todo pnf["pnfdInfoId"]
                     pnfs[k] = {
                         "type": "CreatePnf",
                         "input": {