24b33e34d3d1501b96de6d81651fd0d2c2f1a8d3
[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 \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.heal_vnf import HealVnf\r
23 from lcm.nf.serializers.heal_vnf_req import HealVnfRequestSerializer\r
24 from lcm.nf.serializers.response import ProblemDetailsSerializer\r
25 from lcm.pub.exceptions import NFLCMException\r
26 from lcm.pub.exceptions import NFLCMExceptionNotFound\r
27 from lcm.pub.exceptions import NFLCMExceptionConflict\r
28 from lcm.pub.utils.jobutil import JobUtil\r
29 from lcm.pub.database.models import NfInstModel\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 HealVnfView(APIView):\r
37     @swagger_auto_schema(\r
38         request_body=HealVnfRequestSerializer(),\r
39         responses={\r
40             status.HTTP_202_ACCEPTED: "Success",\r
41             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),\r
42             status.HTTP_409_CONFLICT: ProblemDetailsSerializer(),\r
43             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"\r
44         }\r
45     )\r
46     @view_safe_call_with_log(logger=logger)\r
47     def post(self, request, instanceid):\r
48         logger.debug("HealVnf--post::> %s" % request.data)\r
49 \r
50         heal_vnf_request_serializer = HealVnfRequestSerializer(data=request.data)\r
51         if not heal_vnf_request_serializer.is_valid():\r
52             raise NFLCMException(heal_vnf_request_serializer.errors)\r
53 \r
54         job_id = JobUtil.create_job('NF', 'HEAL', instanceid)\r
55         JobUtil.add_job_status(job_id, 0, "HEAL_VNF_READY")\r
56         self.heal_pre_check(instanceid, job_id)\r
57         HealVnf(heal_vnf_request_serializer.data, instanceid, job_id).start()\r
58         response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
59         # todo, heal_vnf codes uses job as the status storage, not in VNFLcmOpOccModel.\r
60         # response["Location"] = "/api/vnflcm/v1/vnf_lc_ops/%s" % lcmopoccid\r
61         return response\r
62 \r
63     def heal_pre_check(self, nf_inst_id, job_id):\r
64         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)\r
65         if not vnf_insts.exists():\r
66             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")\r
67 \r
68         if vnf_insts[0].status != 'INSTANTIATED':\r
69             raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")\r
70 \r
71         NfInstModel.objects.filter(nfinstid=nf_inst_id).update(status=VNF_STATUS.HEALING)\r
72         JobUtil.add_job_status(job_id, 15, 'Nf healing pre-check finish')\r
73         logger.info("Nf healing pre-check finish")\r