8d4f41cba862138c893712dda976c790a53571db
[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
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.scale_vnf_to_level_request_serializer import ScaleVnfToLevelRequestSerializer
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 lcm.nf.biz.scale_vnf_to_level import ScaleVnfToLevel
31 from .common import view_safe_call_with_log
32
33 logger = logging.getLogger(__name__)
34
35
36 class ScaleVnfToLevelView(APIView):
37     @swagger_auto_schema(
38         request_body=ScaleVnfToLevelRequestSerializer(),
39         responses={
40             status.HTTP_202_ACCEPTED: "Success",
41             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
42             status.HTTP_409_CONFLICT: ProblemDetailsSerializer(),
43             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
44         }
45     )
46     @view_safe_call_with_log(logger=logger)
47     def post(self, request, instanceid):
48         logger.debug("ScaleVnfToLevel--post::> %s" % request.data)
49
50         scale_to_level_serializer = ScaleVnfToLevelRequestSerializer(data=request.data)
51         if not scale_to_level_serializer.is_valid():
52             raise NFLCMException(scale_to_level_serializer.errors)
53
54         job_id = JobUtil.create_job('NF', 'SCALE_TO_LEVEL', instanceid)
55         JobUtil.add_job_status(job_id, 0, "SCALE_VNF_TO_LEVEL_READY")
56         self.scale_pre_check(instanceid, job_id)
57
58         ScaleVnfToLevel(scale_to_level_serializer.data, instanceid, job_id).start()
59
60         response = Response(data={"jobId": job_id},
61                             status=status.HTTP_202_ACCEPTED)
62         return response
63
64     def scale_pre_check(self, nf_inst_id, job_id):
65         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
66         if not vnf_insts.exists():
67             raise NFLCMExceptionNotFound("VNF nf_inst_id does not exist.")
68
69         if vnf_insts[0].status != 'INSTANTIATED':
70             raise NFLCMExceptionConflict("VNF instantiationState is not INSTANTIATED.")
71
72         vnf_insts.update(status=VNF_STATUS.SCALING)
73         JobUtil.add_job_status(job_id, 15, 'Nf scaling to level pre-check finish')
74         logger.info("Nf scaling to level pre-check finish")