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.pub.utils.jobutil import JOB_TYPE
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
29 logger = logging.getLogger(__name__)
32 class TerminateNsView(APIView):
35 request_body=TerminateNsReqSerializer(),
37 status.HTTP_202_ACCEPTED: None,
38 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
41 def post(self, request, ns_instance_id):
43 logger.debug("Enter TerminateNSView::post %s", request.data)
44 req_serializer = TerminateNsReqSerializer(data=request.data)
45 if not req_serializer.is_valid():
46 logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
47 raise BadRequestException(req_serializer.errors)
48 terminationTime = ignore_case_get(request.data, 'terminationTime')
49 logger.debug("terminationTime is %s" % terminationTime)
50 # todo terminationTime
51 job_id = JobUtil.create_job("NS", JOB_TYPE.TERMINATE_NS, ns_instance_id)
52 terminateNsService = TerminateNsService(ns_instance_id, job_id, request.data)
53 terminateNsService.start()
54 logger.debug("Location: %s" % terminateNsService.occ_id)
55 response = Response(data={}, status=status.HTTP_202_ACCEPTED)
56 response["Location"] = NS_OCC_BASE_URI % terminateNsService.occ_id
58 except BadRequestException as e:
59 logger.error("Exception in CreateNS: %s", e.message)
60 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
61 return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
62 except Exception as e:
63 logger.error("Exception in CreateNS: %s", e.message)
64 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
65 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)