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):
37 request_body=TerminateNsReqSerializer(),
39 status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
40 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
43 @view_safe_call_with_log(logger=logger)
44 def post(self, request, ns_instance_id):
45 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.TERMINATE, ns_instance_id)
47 logger.debug("Enter TerminateNSView::post %s", request.data)
48 req_serializer = TerminateNsReqSerializer(data=request.data)
49 if not req_serializer.is_valid():
50 logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
51 raise BadRequestException(req_serializer.errors)
52 termination_time = ignore_case_get(request.data, 'terminationTime')
53 logger.debug("terminationTime is %s" % termination_time)
54 # todo terminationTime
55 terminate_ns_service = TerminateNsService(ns_instance_id, job_id, request.data)
56 terminate_ns_service.start()
57 logger.debug("Location: %s" % terminate_ns_service.occ_id)
58 response = Response(data={}, status=status.HTTP_202_ACCEPTED)
59 response["Location"] = NS_OCC_BASE_URI % terminate_ns_service.occ_id
60 logger.debug("Leave TerminateNSView")