03189e9a0b7f002cf3005f6fac508659b14d1b03
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / scale_vnf_to_level.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 import json
15 import logging
16
17 from lcm.nf.biz.scale_vnf import ScaleVnf
18 from lcm.nf.const import GRANT_TYPE
19 from lcm.pub.database.models import VmInstModel
20 from lcm.pub.exceptions import NFLCMException
21
22 logger = logging.getLogger(__name__)
23
24
25 class ScaleVnfToLevel(ScaleVnf):
26     def __init__(self, data, nf_inst_id, job_id):
27         super(ScaleVnfToLevel, self).__init__(data=data,
28                                               nf_inst_id=nf_inst_id,
29                                               job_id=job_id)
30         self.aspect_id = ''
31
32     def scale_pre(self):
33         self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
34         scale_in_vms = VmInstModel.objects.filter(instid=self.nf_inst_id)
35         vms_num = scale_in_vms.count()
36         self.instantiation_level_id = self.data.get("instantiationLevelId")
37         self.additional_params = self.data.get("additionalParams", {})
38         if not self.instantiation_level_id:
39             self.scale_info = self.data.get("scaleInfo")
40             self.aspect_id = self.scale_info.get("aspectId")
41             self.instantiation_level_id = self.get_instantiation_level_id()
42         else:
43             self.aspect_id = self.get_aspect_id()
44
45         number_of_instances = self.get_number_of_instances()
46         if number_of_instances == vms_num:
47             raise NFLCMException("Don't need scale in/out.")
48         elif number_of_instances > vms_num:
49             self.scale_type = GRANT_TYPE.SCALE_OUT
50         else:
51             self.scale_type = GRANT_TYPE.SCALE_IN
52
53         self.scale_inst_num = abs(number_of_instances - vms_num)
54         self.is_scale_in = (self.scale_type == GRANT_TYPE.SCALE_IN)
55         self.step_delta = self.get_scale_step_delta()
56         self.target_vdu, self.step_inst_num = self.get_vdu_scale_aspect_deltas()
57         self.min_instance_num, self.max_instance_num = self.get_instance_range()
58         self.check_if_can_scale()
59         self.scale_out_resource = {}
60
61     def get_number_of_instances(self):
62         for policy in self.vnfd_info.get("policies", []):
63             if policy.get("type") != "tosca.policies.nfv.VduInstantiationLevels":
64                 continue
65             # if not self.aspect_id:
66             #     self.aspect_id = policy.get("targets")[0]
67             levels = policy["properties"]["levels"]
68             number_of_instances = levels.get(self.instantiation_level_id)
69             if number_of_instances:
70                 return number_of_instances.get("number_of_instances")
71         raise NFLCMException("InstantiationLevelId(%s) does not exist" % self.instantiation_level_id)
72
73     def get_instantiation_level_id(self):
74         scale_level_in_request = self.scale_info.get("scaleLevel")
75         for policy in self.vnfd_info.get("policies", []):
76             if policy.get("type") != "tosca.policies.nfv.InstantiationLevels":
77                 continue
78             levels = policy["properties"]["levels"]
79             for level_id, level_info in levels.items():
80                 scale_aspect = level_info["scale_info"].get(self.aspect_id)
81                 scale_level_in_vnfd = scale_aspect["scale_level"]
82                 if scale_level_in_request == scale_level_in_vnfd:
83                     return level_id
84         raise NFLCMException("Failed to get scale_info in vnfd")
85
86     def get_aspect_id(self):
87         for policy in self.vnfd_info.get("policies", []):
88             if policy.get("type") != "tosca.policies.nfv.InstantiationLevels":
89                 continue
90             levels = policy["properties"]["levels"]
91             level_info = levels.get(self.instantiation_level_id)
92             return level_info.get("scale_info").keys()[0]
93         raise NFLCMException("Failed to get aspect_id in vnfd")