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