diff options
-rw-r--r-- | lcm/ns/biz/ns_terminate.py | 17 | ||||
-rw-r--r-- | lcm/ns/serializers/sol/terminate_ns_serializers.py | 19 | ||||
-rw-r--r-- | lcm/ns/tests/tests_ns_terminate.py | 6 | ||||
-rw-r--r-- | lcm/ns/views/deprecated/term_ns_view.py | 5 | ||||
-rw-r--r-- | lcm/ns/views/sol/ns_instances_views.py | 6 | ||||
-rw-r--r-- | lcm/ns/views/sol/terminate_ns_view.py | 45 |
6 files changed, 82 insertions, 16 deletions
diff --git a/lcm/ns/biz/ns_terminate.py b/lcm/ns/biz/ns_terminate.py index a8831cb2..4bc9d430 100644 --- a/lcm/ns/biz/ns_terminate.py +++ b/lcm/ns/biz/ns_terminate.py @@ -25,6 +25,7 @@ from lcm.pub.utils.values import ignore_case_get from lcm.pub.utils import restcall from lcm.ns.const import OWNER_TYPE from lcm.pub.database.models import PNFInstModel +from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc JOB_ERROR = 255 @@ -32,12 +33,13 @@ logger = logging.getLogger(__name__) class TerminateNsService(threading.Thread): - def __init__(self, ns_inst_id, terminate_type, terminate_timeout, job_id): + def __init__(self, ns_inst_id, job_id, request_data): threading.Thread.__init__(self) - self.ns_inst_id = ns_inst_id - self.terminate_type = terminate_type - self.terminate_timeout = terminate_timeout + self.terminate_type = request_data.get('terminationType', 'GRACEFUL') + self.terminate_timeout = request_data.get('gracefulTerminationTimeout', 600) self.job_id = job_id + self.ns_inst_id = ns_inst_id + self.occ_id = NsLcmOpOcc.create(ns_inst_id, "TERMINATE", "PROCESSING", False, request_data) def run(self): try: @@ -53,12 +55,15 @@ class TerminateNsService(threading.Thread): NSInstModel.objects.filter(id=self.ns_inst_id).update(status='null') JobUtil.add_job_status(self.job_id, 100, "ns terminate ends.", '') + NsLcmOpOcc.update(self.occ_id, "COMPLETED") except NSLCMException as e: JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) - except Exception as ex: - logger.error(ex.message) + NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.message) + except Exception as e: + logger.error(e.message) logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, JOB_ERROR, "ns terminate fail.") + NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.message) def cancel_vl_list(self): array_vlinst = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=self.ns_inst_id) diff --git a/lcm/ns/serializers/sol/terminate_ns_serializers.py b/lcm/ns/serializers/sol/terminate_ns_serializers.py new file mode 100644 index 00000000..582ad963 --- /dev/null +++ b/lcm/ns/serializers/sol/terminate_ns_serializers.py @@ -0,0 +1,19 @@ +# Copyright (c) 2019, ZTE Technologies Co., Ltd. + +# 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. + +from rest_framework import serializers + + +class TerminateNsReqSerializer(serializers.Serializer): + terminationTime = serializers.CharField(help_text="Timestamp indicating the end time of the NS.", required=False) diff --git a/lcm/ns/tests/tests_ns_terminate.py b/lcm/ns/tests/tests_ns_terminate.py index 7de09589..e49dafcf 100644 --- a/lcm/ns/tests/tests_ns_terminate.py +++ b/lcm/ns/tests/tests_ns_terminate.py @@ -90,8 +90,10 @@ class TestTerminateNsViews(TestCase): return mock_vals[args[4]] mock_call_req.side_effect = side_effect - - TerminateNsService(self.nf_inst_id, "forceful", "600", job_id).run() + req_data = { + "terminationType": "FORCEFUL", + "gracefulTerminationTimeout": "600"} + TerminateNsService(self.nf_inst_id, job_id, req_data).run() nsinst = NSInstModel.objects.get(id=self.ns_inst_id) if nsinst: self.assertTrue(1, 0) diff --git a/lcm/ns/views/deprecated/term_ns_view.py b/lcm/ns/views/deprecated/term_ns_view.py index fd78b95a..b61d6d0c 100644 --- a/lcm/ns/views/deprecated/term_ns_view.py +++ b/lcm/ns/views/deprecated/term_ns_view.py @@ -21,7 +21,6 @@ from rest_framework.views import APIView from lcm.ns.biz.ns_terminate import TerminateNsService from lcm.pub.exceptions import NSLCMException from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE -from lcm.pub.utils.values import ignore_case_get from lcm.ns.serializers.deprecated.ns_serializers import _TerminateNsReqSerializer, _NsOperateJobSerializer logger = logging.getLogger(__name__) @@ -42,10 +41,8 @@ class TerminateNSView(APIView): if not req_serializer.is_valid(): raise NSLCMException(req_serializer.errors) - termination_type = ignore_case_get(request.data, 'terminationType') - graceful_termination_timeout = ignore_case_get(request.data, 'gracefulTerminationTimeout') job_id = JobUtil.create_job("NS", JOB_TYPE.TERMINATE_NS, ns_instance_id) - TerminateNsService(ns_instance_id, termination_type, graceful_termination_timeout, job_id).start() + TerminateNsService(ns_instance_id, job_id, request.data).start() resp_serializer = _NsOperateJobSerializer(data={'jobId': job_id}) if not resp_serializer.is_valid(): diff --git a/lcm/ns/views/sol/ns_instances_views.py b/lcm/ns/views/sol/ns_instances_views.py index 324e6f7a..1af7cf78 100644 --- a/lcm/ns/views/sol/ns_instances_views.py +++ b/lcm/ns/views/sol/ns_instances_views.py @@ -69,10 +69,12 @@ class NSInstancesView(APIView): globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID", None) if not globalCustomerId: raise BadRequestException("Not found globalCustomerId in header") + serviceType = request.META.get("HTTP_SERVICETYPE", None) + if not serviceType: + serviceType = "NetworkService" req_serializer = CreateNsRequestSerializer(data=request.data) if not req_serializer.is_valid(): raise BadRequestException(req_serializer.errors) - if ignore_case_get(request.data, 'test') == "test": return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED) csar_id = ignore_case_get(request.data, 'nsdId') @@ -80,7 +82,7 @@ class NSInstancesView(APIView): description = ignore_case_get(request.data, 'description') context = { "globalCustomerId": globalCustomerId, - "serviceType": "NetworkService" + "serviceType": serviceType } ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz() logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id) diff --git a/lcm/ns/views/sol/terminate_ns_view.py b/lcm/ns/views/sol/terminate_ns_view.py index c387bd49..c6213820 100644 --- a/lcm/ns/views/sol/terminate_ns_view.py +++ b/lcm/ns/views/sol/terminate_ns_view.py @@ -13,12 +13,53 @@ # 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_terminate import TerminateNsService +from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.utils.jobutil import JOB_TYPE +from lcm.pub.utils.values import ignore_case_get +from lcm.ns.serializers.sol.terminate_ns_serializers import TerminateNsReqSerializer +from lcm.pub.exceptions import BadRequestException +from lcm.ns.const import NS_OCC_BASE_URI + logger = logging.getLogger(__name__) class TerminateNsView(APIView): + + @swagger_auto_schema( + request_body=TerminateNsReqSerializer(), + responses={ + status.HTTP_202_ACCEPTED: None, + status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error" + } + ) def post(self, request, ns_instance_id): - # todo - return + try: + logger.debug("Enter TerminateNSView::post %s", request.data) + req_serializer = TerminateNsReqSerializer(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) + terminationTime = ignore_case_get(request.data, 'terminationTime') + logger.debug("terminationTime is %s" % terminationTime) + # todo terminationTime + job_id = JobUtil.create_job("NS", JOB_TYPE.TERMINATE_NS, ns_instance_id) + terminateNsService = TerminateNsService(ns_instance_id, job_id, request.data) + terminateNsService.start() + logger.debug("Location: %s" % terminateNsService.occ_id) + response = Response(data={}, status=status.HTTP_202_ACCEPTED) + response["Location"] = NS_OCC_BASE_URI % terminateNsService.occ_id + 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_400_BAD_REQUEST, 'detail': e.message} + return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |