Add precheck logic to terminate vnf
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / terminate_vnf_view.py
1 # Copyright 2017 ZTE Corporation.\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #         http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 \r
15 import logging\r
16 \r
17 from drf_yasg.utils import swagger_auto_schema\r
18 from rest_framework import status\r
19 from rest_framework.response import Response\r
20 from rest_framework.views import APIView\r
21 \r
22 from lcm.nf.biz.terminate_vnf import TerminateVnf\r
23 from lcm.nf.serializers.terminate_vnf_req import TerminateVnfRequestSerializer\r
24 from lcm.nf.serializers.job_identifier import JobIdentifierSerializer\r
25 from lcm.pub.database.models import NfInstModel\r
26 from lcm.pub.exceptions import NFLCMException\r
27 from lcm.pub.exceptions import NFLCMExceptionConflict\r
28 from lcm.pub.exceptions import NFLCMExceptionNotFound\r
29 from lcm.pub.utils.jobutil import JobUtil\r
30 from lcm.nf.const import VNF_STATUS\r
31 from .common import view_safe_call_with_log\r
32 \r
33 logger = logging.getLogger(__name__)\r
34 \r
35 \r
36 class TerminateVnfView(APIView):\r
37     @swagger_auto_schema(\r
38         request_body=TerminateVnfRequestSerializer(),\r
39         responses={\r
40             status.HTTP_202_ACCEPTED: JobIdentifierSerializer(),\r
41             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"\r
42         }\r
43     )\r
44     @view_safe_call_with_log(logger=logger)\r
45     def post(self, request, instanceid):\r
46         logger.debug("TerminateVnf--post::> %s" % request.data)\r
47 \r
48         terminate_vnf_request_serializer = TerminateVnfRequestSerializer(data=request.data)\r
49         if not terminate_vnf_request_serializer.is_valid():\r
50             raise NFLCMException(terminate_vnf_request_serializer.errors)\r
51 \r
52         job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)\r
53         JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")\r
54 \r
55         self.terminate_pre_check(instanceid, job_id)\r
56         TerminateVnf(terminate_vnf_request_serializer.data, instanceid, job_id).start()\r
57 \r
58         terminate_vnf_response_serializer = JobIdentifierSerializer(data={"jobId": job_id})\r
59         if not terminate_vnf_response_serializer.is_valid():\r
60             raise NFLCMException(terminate_vnf_response_serializer.errors)\r
61 \r
62         return Response(data=terminate_vnf_response_serializer.data, status=status.HTTP_202_ACCEPTED)\r
63 \r
64     def terminate_pre_check(self, nf_inst_id, job_id):\r
65         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)\r
66         if not vnf_insts.exists():\r
67             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")\r
68 \r
69         if vnf_insts[0].status != 'INSTANTIATED':\r
70             raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")\r
71 \r
72         vnf_insts.update(status=VNF_STATUS.TERMINATING)\r
73         JobUtil.add_job_status(job_id, 15, 'Nf terminating pre-check finish')\r
74         logger.info("Nf terminating pre-check finish")\r