diff options
author | 2019-03-25 11:06:25 +0800 | |
---|---|---|
committer | 2019-03-25 11:06:25 +0800 | |
commit | 4792c0f41b781d21047df7df8109c1c041fdf1ee (patch) | |
tree | 50bad33a41c2df3caa6edfb9e6fe8d30b3ce893e | |
parent | c1a85aa8898222ab09dc2be5109eca92c45b2a4f (diff) |
create op_occs
create op_occs
Change-Id: Ideea8b353fa1541c157a6138710611fc6ccb22b4
Issue-ID: VFC-1214
Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
-rw-r--r-- | lcm/ns/biz/ns_instant.py | 7 | ||||
-rw-r--r-- | lcm/ns/biz/ns_lcm_op_occ.py | 61 | ||||
-rw-r--r-- | lcm/ns/const.py | 1 | ||||
-rw-r--r-- | lcm/ns/views/sol/instantiate_ns_views.py | 39 | ||||
-rw-r--r-- | lcm/pub/utils/fileutil.py | 2 | ||||
-rw-r--r-- | lcm/pub/utils/jobutil.py | 1 | ||||
-rw-r--r-- | lcm/pub/utils/values.py | 23 |
7 files changed, 128 insertions, 6 deletions
diff --git a/lcm/ns/biz/ns_instant.py b/lcm/ns/biz/ns_instant.py index 2670e250..c9146ed2 100644 --- a/lcm/ns/biz/ns_instant.py +++ b/lcm/ns/biz/ns_instant.py @@ -53,8 +53,9 @@ class InstantNSService(object): self.req_data = plan_content def do_biz(self): + job_id = JobUtil.create_job("NS", "NS_INST", self.ns_inst_id) + 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) @@ -152,13 +153,13 @@ 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) elif config.WORKFLOW_OPTION == "activiti": - return self.start_activiti_workflow() + return self.start_activiti_workflow(job_id, plan_input) elif config.WORKFLOW_OPTION == "grapflow": return self.start_buildin_grapflow(job_id, plan_input) else: diff --git a/lcm/ns/biz/ns_lcm_op_occ.py b/lcm/ns/biz/ns_lcm_op_occ.py new file mode 100644 index 00000000..f808cd2d --- /dev/null +++ b/lcm/ns/biz/ns_lcm_op_occ.py @@ -0,0 +1,61 @@ +# Copyright 2019 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import datetime +import uuid + +from lcm.pub.database.models import NSLcmOpOccModel +from lcm.pub.utils.values import update_value + + +logger = logging.getLogger(__name__) + + +class NsLcmOpOcc(object): + @staticmethod + def create(nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams): + logger.debug("lcm_op_occ(%s,%s,%s,%s,%s) create begin." % (nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams)) + cur_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + lcm_op_occ = NSLcmOpOccModel.objects.create( + id=str(uuid.uuid4()), + operation_state=operationState, + state_entered_time=cur_time, + start_time=cur_time, + ns_instance_id=nsInstanceId, + operation=lcmOperationType, + is_automatic_invocation=isAutomaticInvocation, + operation_params=operationParams, + is_cancel_pending=False + ) + logger.debug("lcm_op_occ(%s) create successfully." % lcm_op_occ.id) + return lcm_op_occ.id + + @staticmethod + def update(occ_id, operationState=None, isCancelPending=None, cancelMode=None, error=None, resourceChanges=None): + lcm_op_occ = NSLcmOpOccModel.objects.get(id=occ_id) + if operationState: + lcm_op_occ.operation_state = operationState + lcm_op_occ.state_entered_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + if isCancelPending: + lcm_op_occ.is_cancel_pending = isCancelPending + if cancelMode: + lcm_op_occ.cancel_mode = cancelMode + if error: + lcm_op_occ.error = error + if resourceChanges: + lcm_op_occ.resource_changes = update_value(lcm_op_occ.resourceChanges if lcm_op_occ.resourceChanges else {}, resourceChanges) + lcm_op_occ.save() + logger.debug("lcm_op_occ(%s) update successfully." % lcm_op_occ.id) + return lcm_op_occ.id diff --git a/lcm/ns/const.py b/lcm/ns/const.py index 684c47f7..7fd0fc9f 100644 --- a/lcm/ns/const.py +++ b/lcm/ns/const.py @@ -169,3 +169,4 @@ CHANGE_RESULT = [ ] NS_INSTANCE_BASE_URI = MSB_BASE_URL + '/api/nslcm/v1/ns_instances/%s' +NS_OCC_BASE_URI = MSB_BASE_URL + '/api/nslcm/v1/ns_lcm_op_occs/%s' diff --git a/lcm/ns/views/sol/instantiate_ns_views.py b/lcm/ns/views/sol/instantiate_ns_views.py index c19becea..b2582337 100644 --- a/lcm/ns/views/sol/instantiate_ns_views.py +++ b/lcm/ns/views/sol/instantiate_ns_views.py @@ -13,12 +13,47 @@ # limitations under the License. import logging +from drf_yasg.utils import swagger_auto_schema +from rest_framework import status +from rest_framework.response import Response from rest_framework.views import APIView +from lcm.ns.biz.ns_instant import InstantNSService +from lcm.ns.serializers.sol.inst_ns_serializers import InstantNsReqSerializer +from lcm.pub.exceptions import BadRequestException +from lcm.ns.const import NS_OCC_BASE_URI + logger = logging.getLogger(__name__) class InstantiateNsView(APIView): + @swagger_auto_schema( + request_body=InstantNsReqSerializer(), + responses={ + status.HTTP_202_ACCEPTED: None, + status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error" + } + ) def post(self, request, ns_instance_id): - # todo - return + logger.debug("Enter NSInstView::post::ns_instance_id=%s", ns_instance_id) + logger.debug("request.data=%s", request.data) + try: + req_serializer = InstantNsReqSerializer(data=request.data) + if not req_serializer.is_valid(): + 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'] + response = Response(data={}, status=status.HTTP_202_ACCEPTED) + logger.debug("Location: %s" % ack['nsLcmOpOccId']) + response["Location"] = NS_OCC_BASE_URI % nsLcmOpOccId + logger.debug("Leave NSInstView::post::ack=%s", ack) + return response + except BadRequestException as e: + logger.error("Exception in CreateNS: %s", e.message) + data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message} + return Response(data=data, status=status.HTTP_400_BAD_REQUEST) + except Exception as e: + logger.error("Exception in CreateNS: %s", e.message) + data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message} + return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/lcm/pub/utils/fileutil.py b/lcm/pub/utils/fileutil.py index 3568e5ee..87cdfd9b 100644 --- a/lcm/pub/utils/fileutil.py +++ b/lcm/pub/utils/fileutil.py @@ -22,7 +22,7 @@ logger = logging.getLogger(__name__) def make_dirs(path): if not os.path.exists(path): - os.makedirs(path, 0777) + os.makedirs(path, 0o777) def delete_dirs(path): diff --git a/lcm/pub/utils/jobutil.py b/lcm/pub/utils/jobutil.py index 89a3af02..3aa56354 100644 --- a/lcm/pub/utils/jobutil.py +++ b/lcm/pub/utils/jobutil.py @@ -18,6 +18,7 @@ import traceback from lcm.pub.database.models import JobStatusModel, JobModel from lcm.pub.utils import idutil +from functools import reduce logger = logging.getLogger(__name__) diff --git a/lcm/pub/utils/values.py b/lcm/pub/utils/values.py index 10700d02..fbb75f68 100644 --- a/lcm/pub/utils/values.py +++ b/lcm/pub/utils/values.py @@ -12,6 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + +logger = logging.getLogger(__name__) + def ignore_case_get(args, key, def_val=""): if not key: @@ -22,3 +26,22 @@ def ignore_case_get(args, key, def_val=""): if old_key.upper() == key.upper(): return args[old_key] return def_val + + +def update_value(origin_data, new_data): + logger.debug(origin_data) + if not isinstance(origin_data, dict): + str_data = origin_data.encode('utf-8') + logger.debug(str_data) + origin_data = eval(str_data) + logger.debug(isinstance(origin_data, dict)) + logger.debug(new_data) + for k, v in new_data.iteritems(): + if k not in origin_data: + origin_data[k] = v + else: + if isinstance(origin_data[k], list): + origin_data[k] = origin_data[k].extend(v) + else: + origin_data[k] = v + return origin_data |