6b3b31397e9b2569a0a6c2decad683d0e04a6a5b
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / heal_vnf_view.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved.\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 import traceback\r
17 \r
18 from drf_yasg.utils import swagger_auto_schema\r
19 from rest_framework import status\r
20 from rest_framework.response import Response\r
21 from rest_framework.views import APIView\r
22 \r
23 from lcm.nf.biz.heal_vnf import HealVnf\r
24 from lcm.nf.serializers.heal_vnf_req import HealVnfRequestSerializer\r
25 from lcm.nf.serializers.response import ProblemDetailsSerializer\r
26 from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound, NFLCMExceptionConflict\r
27 from lcm.pub.utils.jobutil import JobUtil\r
28 from lcm.pub.database.models import NfInstModel\r
29 from lcm.nf.const import VNF_STATUS\r
30 \r
31 logger = logging.getLogger(__name__)\r
32 \r
33 \r
34 class HealVnfView(APIView):\r
35     @swagger_auto_schema(\r
36         request_body=HealVnfRequestSerializer(),\r
37         responses={\r
38             status.HTTP_202_ACCEPTED: "Success",\r
39             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),\r
40             status.HTTP_409_CONFLICT: ProblemDetailsSerializer(),\r
41             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"\r
42         }\r
43     )\r
44     def post(self, request, instanceid):\r
45         logger.debug("HealVnf--post::> %s" % request.data)\r
46         try:\r
47             heal_vnf_request_serializer = HealVnfRequestSerializer(data=request.data)\r
48             if not heal_vnf_request_serializer.is_valid():\r
49                 raise NFLCMException(heal_vnf_request_serializer.errors)\r
50 \r
51             job_id = JobUtil.create_job('NF', 'HEAL', instanceid)\r
52             JobUtil.add_job_status(job_id, 0, "HEAL_VNF_READY")\r
53             self.heal_pre_check(instanceid, job_id)\r
54             HealVnf(heal_vnf_request_serializer.data, instanceid, job_id).start()\r
55             response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
56             # todo, heal_vnf codes uses job as the status storage, not in VNFLcmOpOccModel.\r
57             # response["Location"] = "/api/vnflcm/v1/vnf_lc_ops/%s" % lcmopoccid\r
58             return response\r
59         except NFLCMExceptionNotFound as e:\r
60             probDetail = ProblemDetailsSerializer(data={"status": status.HTTP_404_NOT_FOUND, "detail": "VNF Instance not found"})\r
61             resp_isvalid = probDetail.is_valid()\r
62             if not resp_isvalid:\r
63                 raise NFLCMException(probDetail.errors)\r
64             return Response(data=probDetail.data, status=status.HTTP_404_NOT_FOUND)\r
65         except NFLCMExceptionConflict as e:\r
66             probDetail = ProblemDetailsSerializer(data={"status": status.HTTP_409_CONFLICT, "detail": "VNF Instance not in Instantiated State"})\r
67             resp_isvalid = probDetail.is_valid()\r
68             if not resp_isvalid:\r
69                     raise NFLCMException(probDetail.errors)\r
70             return Response(data=probDetail.data, status=status.HTTP_409_CONFLICT)\r
71         except NFLCMException as e:\r
72             logger.error(e.message)\r
73             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
74         except Exception as e:\r
75             logger.error(e.message)\r
76             logger.error(traceback.format_exc())\r
77             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
78 \r
79     def heal_pre_check(self, nf_inst_id, job_id):\r
80         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)\r
81         if not vnf_insts.exists():\r
82             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")\r
83 \r
84         if vnf_insts[0].status != 'INSTANTIATED':\r
85             raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")\r
86 \r
87         NfInstModel.objects.filter(nfinstid=nf_inst_id).update(status=VNF_STATUS.HEALING)\r
88         JobUtil.add_job_status(job_id, 15, 'Nf healing pre-check finish')\r
89         logger.info("Nf healing pre-check finish")\r