6c58ce8002ef7e7523f6edc4cbc15c5c08c3e096
[vfc/nfvo/lcm.git] / lcm / ns / views / deprecated / term_ns_view.py
1 # Copyright 2016-2017 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 import logging
15 import time
16
17 from drf_yasg.utils import swagger_auto_schema
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21
22 from lcm.ns.biz.ns_terminate import TerminateNsService
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.exceptions import BadRequestException
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.jobs.enum import JOB_TYPE, JOB_ACTION
27 from lcm.ns.serializers.deprecated.ns_serializers import _TerminateNsReqSerializer
28 from lcm.ns.serializers.deprecated.ns_serializers import _NsOperateJobSerializer
29 from .common import view_safe_call_with_log
30
31 logger = logging.getLogger(__name__)
32
33
34 class NSTerminateView(APIView):
35     @swagger_auto_schema(
36         request_body=_TerminateNsReqSerializer(),
37         responses={
38             status.HTTP_202_ACCEPTED: _NsOperateJobSerializer(),
39             status.HTTP_400_BAD_REQUEST: "Bad Request",
40             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
41         }
42     )
43     @view_safe_call_with_log(logger=logger)
44     def post(self, request, ns_instance_id):
45         """
46         Terminate a NS instance
47         :param request:
48         :param ns_instance_id:
49         :return:
50         """
51         logger.debug("Enter TerminateNSView::post %s", request.data)
52         req_serializer = _TerminateNsReqSerializer(data=request.data)
53         if not req_serializer.is_valid():
54             raise BadRequestException(req_serializer.errors)
55
56         job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.TERMINATE, ns_instance_id)
57         time.sleep(2)
58         TerminateNsService(ns_instance_id, job_id, request.data).start()
59
60         resp_serializer = _NsOperateJobSerializer(data={'jobId': job_id})
61         if not resp_serializer.is_valid():
62             raise NSLCMException(resp_serializer.errors)
63
64         logger.debug("Leave TerminateNSView::post ret=%s", {'jobId': job_id})
65         return Response(data={'jobId': job_id}, status=status.HTTP_202_ACCEPTED)