322180677e9ec7d2f5206be2029460d9bf174d27
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / operate_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.operate_vnf import OperateVnf\r
23 from lcm.nf.serializers.operate_vnf_req import OperateVnfRequestSerializer\r
24 from lcm.nf.serializers.response import ProblemDetailsSerializer\r
25 from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound, NFLCMExceptionConflict\r
26 from lcm.pub.utils.jobutil import JobUtil\r
27 from lcm.pub.database.models import NfInstModel\r
28 from lcm.nf.const import VNF_STATUS\r
29 from .common import view_safe_call_with_log\r
30 \r
31 logger = logging.getLogger(__name__)\r
32 \r
33 \r
34 class OperateVnfView(APIView):\r
35     @swagger_auto_schema(\r
36         request_body=OperateVnfRequestSerializer(),\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     @view_safe_call_with_log(logger=logger)\r
45     def post(self, request, instanceid):\r
46         logger.debug("OperateVnf--post::> %s" % request.data)\r
47 \r
48         operate_vnf_request_serializer = OperateVnfRequestSerializer(data=request.data)\r
49         if not operate_vnf_request_serializer.is_valid():\r
50             raise NFLCMException(operate_vnf_request_serializer.errors)\r
51 \r
52         job_id = JobUtil.create_job('NF', 'OPERATE', instanceid)\r
53         JobUtil.add_job_status(job_id, 0, "OPERATE_VNF_READY")\r
54         self.operate_pre_check(instanceid, job_id)\r
55         OperateVnf(operate_vnf_request_serializer.data, instanceid, job_id).start()\r
56         response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
57         # Location todo, it use job as the status storage\r
58         # response["Location"] = "/api/vnflcm/v1/vnf_lcm_op_occs/%s" % lcmopoccid\r
59         return response\r
60 \r
61     def operate_pre_check(self, nfInstId, jobId):\r
62         vnf_insts = NfInstModel.objects.filter(nfinstid=nfInstId)\r
63         if not vnf_insts.exists():\r
64             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")\r
65 \r
66         if vnf_insts[0].status != 'INSTANTIATED':\r
67             raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")\r
68         NfInstModel.objects.filter(nfinstid=nfInstId).update(status=VNF_STATUS.OPERATING)\r
69 \r
70         JobUtil.add_job_status(jobId, 15, 'Nf operating pre-check finish')\r
71         logger.info("Nf operating pre-check finish")\r