diff options
-rw-r--r-- | lcm/ns/biz/ns_instant.py | 36 | ||||
-rw-r--r-- | lcm/ns/biz/ns_instantiate_flow.py | 13 | ||||
-rw-r--r-- | lcm/ns/views/sol/instantiate_ns_views.py | 4 | ||||
-rw-r--r-- | lcm/workflows/build_in.py | 8 | ||||
-rw-r--r-- | lcm/workflows/tests.py | 13 |
5 files changed, 45 insertions, 29 deletions
diff --git a/lcm/ns/biz/ns_instant.py b/lcm/ns/biz/ns_instant.py index c9146ed2..9a3fb1d3 100644 --- a/lcm/ns/biz/ns_instant.py +++ b/lcm/ns/biz/ns_instant.py @@ -34,17 +34,19 @@ 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 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): @@ -54,6 +56,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) try: logger.debug('ns-instant(%s) workflow starting...' % self.ns_inst_id) @@ -157,21 +160,23 @@ class InstantNSService(object): 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(job_id, plan_input) + 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)) + NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.message) JobUtil.add_job_status(job_id, 255, 'NS instantiation failed') return dict(data={'error': e.message}, 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}} @@ -185,7 +190,8 @@ class InstantNSService(object): return dict(data={'jobId': job_id}, status=status.HTTP_200_OK) 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!") @@ -198,18 +204,18 @@ 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): diff --git a/lcm/ns/biz/ns_instantiate_flow.py b/lcm/ns/biz/ns_instantiate_flow.py index 88b04fe7..913e8686 100644 --- a/lcm/ns/biz/ns_instantiate_flow.py +++ b/lcm/ns/biz/ns_instantiate_flow.py @@ -22,6 +22,7 @@ from lcm.pub.utils.values import ignore_case_get from lcm.pub.utils import restcall from lcm.pub.exceptions import NSLCMException from lcm.workflows.graphflow.flow.flow import GraphFlow +from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc logger = logging.getLogger(__name__) @@ -36,15 +37,16 @@ config = { class NsInstantiateWorkflowThread(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): - run_ns_instantiate(self.plan_input) + run_ns_instantiate(self.plan_input, self.occ_id) -def run_ns_instantiate(input_data): +def run_ns_instantiate(input_data, occ_id): """ format of input_data { @@ -78,14 +80,17 @@ def run_ns_instantiate(input_data): update_job(job_id, 90, "true", "Start to post deal") post_deal(ns_inst_id, "true") update_job(job_id, 100, "true", "Create NS successfully.") + NsLcmOpOcc.update(occ_id, "COMPLETED") ns_instantiate_ok = True except NSLCMException as e: logger.error("Failded to Create NS: %s", e.message) update_job(job_id, JOB_ERROR, "255", "Failded to Create NS.") + NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.message) post_deal(ns_inst_id, "false") - except: + except Exception as e: logger.error(traceback.format_exc()) update_job(job_id, JOB_ERROR, "255", "Failded to Create NS.") + NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.message) post_deal(ns_inst_id, "false") return ns_instantiate_ok diff --git a/lcm/ns/views/sol/instantiate_ns_views.py b/lcm/ns/views/sol/instantiate_ns_views.py index b2582337..83331007 100644 --- a/lcm/ns/views/sol/instantiate_ns_views.py +++ b/lcm/ns/views/sol/instantiate_ns_views.py @@ -43,9 +43,9 @@ class InstantiateNsView(APIView): logger.debug("request.data is not valid,error: %s" % req_serializer.errors) raise BadRequestException(req_serializer.errors) ack = InstantNSService(ns_instance_id, request.data).do_biz() - nsLcmOpOccId = ack['nsLcmOpOccId'] + nsLcmOpOccId = ack['occ_id'] response = Response(data={}, status=status.HTTP_202_ACCEPTED) - logger.debug("Location: %s" % ack['nsLcmOpOccId']) + logger.debug("Location: %s" % ack['occ_id']) response["Location"] = NS_OCC_BASE_URI % nsLcmOpOccId logger.debug("Leave NSInstView::post::ack=%s", ack) return response diff --git a/lcm/workflows/build_in.py b/lcm/workflows/build_in.py index e47a6db9..80060605 100644 --- a/lcm/workflows/build_in.py +++ b/lcm/workflows/build_in.py @@ -21,6 +21,7 @@ from lcm.pub.utils.syscomm import fun_name from lcm.pub.utils.values import ignore_case_get from lcm.pub.utils import restcall from lcm.pub.exceptions import NSLCMException +from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc logger = logging.getLogger(__name__) @@ -45,7 +46,7 @@ format of input_data """ -def run_ns_instantiate(input_data): +def run_ns_instantiate(input_data, occ_id): logger.debug("Enter %s, input_data is %s", fun_name(), input_data) ns_instantiate_ok = False job_id = ignore_case_get(input_data, "jobId") @@ -84,14 +85,17 @@ def run_ns_instantiate(input_data): post_deal(ns_inst_id, "true") update_job(job_id, 100, "true", "Create NS successfully.") + NsLcmOpOcc.update(occ_id, "COMPLETED") ns_instantiate_ok = True except NSLCMException as e: logger.error("Failded to Create NS: %s", e.message) update_job(job_id, JOB_ERROR, "255", "Failded to Create NS.") + NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.message) post_deal(ns_inst_id, "false") - except: + except Exception as e: logger.error(traceback.format_exc()) update_job(job_id, JOB_ERROR, "255", "Failded to Create NS.") + NsLcmOpOcc.update(occ_id, operationState="FAILED", error=e.message) post_deal(ns_inst_id, "false") finally: g_jobs_status.pop(job_id) diff --git a/lcm/workflows/tests.py b/lcm/workflows/tests.py index 53a4a628..08e3477e 100644 --- a/lcm/workflows/tests.py +++ b/lcm/workflows/tests.py @@ -23,6 +23,7 @@ from rest_framework import status from lcm.pub.database.models import WFPlanModel from lcm.pub.utils import restcall from lcm.workflows import build_in +from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc class WorkflowViewTest(unittest.TestCase): @@ -135,8 +136,8 @@ class WorkflowViewTest(unittest.TestCase): def side_effect(*args): return mock_vals[args[4]] mock_call_req.side_effect = side_effect - - self.assertTrue(build_in.run_ns_instantiate(wf_input)) + occ_id = NsLcmOpOcc.create(ns_inst_id, "INSTANTIATE", "PROCESSING", False, wf_input) + self.assertTrue(build_in.run_ns_instantiate(wf_input, occ_id)) @mock.patch.object(restcall, 'call_req') def test_buildin_workflow_when_create_vl_failed(self, mock_call_req): @@ -169,8 +170,8 @@ class WorkflowViewTest(unittest.TestCase): def side_effect(*args): return mock_vals[args[4]] mock_call_req.side_effect = side_effect - - self.assertFalse(build_in.run_ns_instantiate(wf_input)) + occ_id = NsLcmOpOcc.create(ns_inst_id, "INSTANTIATE", "PROCESSING", False, wf_input) + self.assertFalse(build_in.run_ns_instantiate(wf_input, occ_id)) @mock.patch.object(restcall, 'call_req') def test_buildin_workflow_when_create_vnf_failed(self, mock_call_req): @@ -218,5 +219,5 @@ class WorkflowViewTest(unittest.TestCase): def side_effect(*args): return mock_vals[args[4]] mock_call_req.side_effect = side_effect - - self.assertFalse(build_in.run_ns_instantiate(wf_input)) + occ_id = NsLcmOpOcc.create(ns_inst_id, "INSTANTIATE", "PROCESSING", False, wf_input) + self.assertFalse(build_in.run_ns_instantiate(wf_input, occ_id)) |