9ae2cd36667295d7db56d1581568539c9b684344
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / scale_to_level_view.py
1 # Copyright (C) 2019 ZTE. All Rights Reserved.
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 import traceback
17
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.nf.serializers.scale_vnf_to_level_request_serializer import ScaleVnfToLevelRequestSerializer
24 from lcm.nf.serializers.response import ProblemDetailsSerializer
25 from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound, NFLCMExceptionConflict
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.database.models import NfInstModel
28 from lcm.nf.const import VNF_STATUS
29 from lcm.nf.biz.scale_vnf_to_level import ScaleVnfToLevel
30
31 logger = logging.getLogger(__name__)
32
33
34 class ScaleVnfToLevelView(APIView):
35     @swagger_auto_schema(
36         request_body=ScaleVnfToLevelRequestSerializer(),
37         responses={
38             status.HTTP_202_ACCEPTED: "Success",
39             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
40             status.HTTP_409_CONFLICT: ProblemDetailsSerializer(),
41             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
42         }
43     )
44     def post(self, request, instanceid):
45         logger.debug("ScaleVnfToLevel--post::> %s" % request.data)
46         try:
47             scale_to_level_serializer = ScaleVnfToLevelRequestSerializer(data=request.data)
48             if not scale_to_level_serializer.is_valid():
49                 raise NFLCMException(scale_to_level_serializer.errors)
50
51             job_id = JobUtil.create_job('NF', 'SCALE_TO_LEVEL', instanceid)
52             JobUtil.add_job_status(job_id, 0, "SCALE_VNF_TO_LEVEL_READY")
53             self.scale_pre_check(instanceid, job_id)
54
55             ScaleVnfToLevel(scale_to_level_serializer.data, instanceid, job_id).start()
56
57             response = Response(data={"jobId": job_id},
58                                 status=status.HTTP_202_ACCEPTED)
59             return response
60         except NFLCMExceptionNotFound as e:
61             probDetail = ProblemDetailsSerializer(data={"status": status.HTTP_404_NOT_FOUND,
62                                                         "detail": "VNF Instance not found"})
63             resp_isvalid = probDetail.is_valid()
64             if not resp_isvalid:
65                 raise NFLCMException(probDetail.errors)
66             return Response(data=probDetail.data,
67                             status=status.HTTP_404_NOT_FOUND)
68         except NFLCMExceptionConflict as e:
69             probDetail = ProblemDetailsSerializer(data={"status": status.HTTP_409_CONFLICT,
70                                                         "detail": "VNF Instance not in Instantiated State"})
71             resp_isvalid = probDetail.is_valid()
72             if not resp_isvalid:
73                 raise NFLCMException(probDetail.errors)
74             return Response(data=probDetail.data,
75                             status=status.HTTP_409_CONFLICT)
76         except NFLCMException as e:
77             logger.error(e.message)
78             return Response(data={'error': '%s' % e.message},
79                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
80         except Exception as e:
81             logger.error(e.message)
82             logger.error(traceback.format_exc())
83             return Response(data={'error': 'unexpected exception'},
84                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
85
86     def scale_pre_check(self, nf_inst_id, job_id):
87         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
88         if not vnf_insts.exists():
89             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")
90
91         vnf_insts.update(status=VNF_STATUS.SCALING)
92         JobUtil.add_job_status(job_id, 15, 'Nf scaling to level pre-check finish')
93         logger.info("Nf scaling to level pre-check finish")