fix swagger error
[vfc/nfvo/lcm.git] / lcm / ns / views / sol / terminate_ns_view.py
1 # Copyright 2019 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
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
20
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
28 from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
29
30 logger = logging.getLogger(__name__)
31
32
33 class TerminateNsView(APIView):
34
35     @swagger_auto_schema(
36         request_body=TerminateNsReqSerializer(),
37         responses={
38             status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
39             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
40         }
41     )
42     def post(self, request, ns_instance_id):
43         job_id = JobUtil.create_job("NS", JOB_TYPE.TERMINATE_NS, ns_instance_id)
44         try:
45             logger.debug("Enter TerminateNSView::post %s", request.data)
46             req_serializer = TerminateNsReqSerializer(data=request.data)
47             if not req_serializer.is_valid():
48                 logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
49                 raise BadRequestException(req_serializer.errors)
50             terminationTime = ignore_case_get(request.data, 'terminationTime')
51             logger.debug("terminationTime is %s" % terminationTime)
52             # todo terminationTime
53             terminateNsService = TerminateNsService(ns_instance_id, job_id, request.data)
54             terminateNsService.start()
55             logger.debug("Location: %s" % terminateNsService.occ_id)
56             response = Response(data={}, status=status.HTTP_202_ACCEPTED)
57             response["Location"] = NS_OCC_BASE_URI % terminateNsService.occ_id
58             logger.debug("Leave TerminateNSView")
59             return response
60         except BadRequestException as e:
61             logger.error("Exception in TerminateNsView: %s", e.message)
62             JobUtil.add_job_status(job_id, 255, 'NS termination failed: %s' % e.message)
63             data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
64             return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
65         except Exception as e:
66             logger.error("Exception in TerminateNsView: %s", e.message)
67             JobUtil.add_job_status(job_id, 255, 'NS termination failed: %s' % e.message)
68             data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
69             return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)