1 # Copyright 2019 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from drf_yasg.utils import swagger_auto_schema
17 from rest_framework import status
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
21 from lcm.ns.biz.ns_terminate import TerminateNsService
22 from lcm.pub.utils.jobutil import JobUtil
23 from lcm.jobs.enum import JOB_TYPE, JOB_ACTION
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.ns.serializers.sol.terminate_ns_serializers import TerminateNsReqSerializer
26 from lcm.pub.exceptions import BadRequestException
27 from lcm.ns.const import NS_OCC_BASE_URI
28 from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
29 from .common import view_safe_call_with_log
31 logger = logging.getLogger(__name__)
34 class TerminateNsView(APIView):
36 This task resource represents the "Terminate NS" operation.
37 The client can use this resource to terminate a NS instance.
41 request_body=TerminateNsReqSerializer(),
43 status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
44 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
47 @view_safe_call_with_log(logger=logger)
48 def post(self, request, ns_instance_id):
50 The POST method terminates a NS instance.
51 This method can only be used with a NS instance in the INSTANTIATEDstate.
52 Terminating a NS instance does not delete the NS instance identifier, but rather transitions the NS into the NOT_INSTANTIATED state.
54 :param ns_instance_id:
57 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.TERMINATE, ns_instance_id)
59 logger.debug("Enter TerminateNSView::post %s", request.data)
60 req_serializer = TerminateNsReqSerializer(data=request.data)
61 if not req_serializer.is_valid():
62 logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
63 raise BadRequestException(req_serializer.errors)
64 termination_time = ignore_case_get(request.data, 'terminationTime')
65 logger.debug("terminationTime is %s" % termination_time)
66 # todo terminationTime
67 terminate_ns_service = TerminateNsService(ns_instance_id, job_id, request.data)
68 terminate_ns_service.start()
69 logger.debug("Location: %s" % terminate_ns_service.occ_id)
70 response = Response(data={'jobId': job_id}, status=status.HTTP_202_ACCEPTED)
71 response["Location"] = NS_OCC_BASE_URI % terminate_ns_service.occ_id
72 logger.debug("Leave TerminateNSView")