1 # Copyright (C) 2018 Verizon. All Rights Reserved.
\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
7 # http://www.apache.org/licenses/LICENSE-2.0
\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
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
23 from lcm.nf.biz.operate_vnf import OperateVnf
\r
24 from lcm.nf.serializers.operate_vnf_req import OperateVnfRequestSerializer
\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
31 logger = logging.getLogger(__name__)
\r
34 class OperateVnfView(APIView):
\r
35 @swagger_auto_schema(
\r
36 request_body=OperateVnfRequestSerializer(),
\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
44 def post(self, request, instanceid):
\r
45 logger.debug("OperateVnf--post::> %s" % request.data)
\r
47 operate_vnf_request_serializer = OperateVnfRequestSerializer(data=request.data)
\r
48 if not operate_vnf_request_serializer.is_valid():
\r
49 raise NFLCMException(operate_vnf_request_serializer.errors)
\r
51 job_id = JobUtil.create_job('NF', 'OPERATE', instanceid)
\r
52 JobUtil.add_job_status(job_id, 0, "OPERATE_VNF_READY")
\r
53 self.operate_pre_check(instanceid, job_id)
\r
54 OperateVnf(operate_vnf_request_serializer.data, instanceid, job_id).start()
\r
55 response = Response(data=None, status=status.HTTP_202_ACCEPTED)
\r
56 response["Location"] = "/vnf_lc_ops/%s" % job_id
\r
58 except NFLCMExceptionNotFound as e:
\r
59 probDetail = ProblemDetailsSerializer(data={"status": 404, "detail": "VNF Instance not found"})
\r
60 resp_isvalid = probDetail.is_valid()
\r
61 if not resp_isvalid:
\r
62 raise NFLCMException(probDetail.errors)
\r
63 return Response(data=probDetail.data, status=status.HTTP_404_NOT_FOUND)
\r
64 except NFLCMExceptionConflict as e:
\r
65 probDetail = ProblemDetailsSerializer(data={"status": 409, "detail": "VNF Instance not in Instantiated State"})
\r
66 resp_isvalid = probDetail.is_valid()
\r
67 if not resp_isvalid:
\r
68 raise NFLCMException(probDetail.errors)
\r
69 return Response(data=probDetail.data, status=status.HTTP_409_CONFLICT)
\r
70 except NFLCMException as e:
\r
71 logger.error(e.message)
\r
72 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r
73 except Exception as e:
\r
74 logger.error(e.message)
\r
75 logger.error(traceback.format_exc())
\r
76 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r
78 def operate_pre_check(self, nfInstId, jobId):
\r
79 vnf_insts = NfInstModel.objects.filter(nfinstid=nfInstId)
\r
80 if not vnf_insts.exists():
\r
81 raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")
\r
83 if vnf_insts[0].status != 'INSTANTIATED':
\r
84 raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")
\r
85 NfInstModel.objects.filter(nfinstid=nfInstId).update(status=VNF_STATUS.OPERATING)
\r
87 JobUtil.add_job_status(jobId, 15, 'Nf operating pre-check finish')
\r
88 logger.info("Nf operating pre-check finish")
\r