Changes for IM change 10095: remove kdu-model
[osm/LCM.git] / osm_lcm / data_utils / vnfd.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2020 Whitestack, LLC
4 # *************************************************************
5 #
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 #
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: fbravo@whitestack.com
23 ##
24
25 from osm_lcm.data_utils import list_utils
26 from osm_lcm.lcm_utils import get_iterable
27
28
29 def get_lcm_operations_configuration(vnfd):
30 return vnfd.get("df", ())[0].get("lcm-operations-configuration", ())
31
32
33 def get_vdu_list(vnfd):
34 return vnfd.get("vdu", ())
35
36
37 def get_kdu_list(vnfd):
38 return vnfd.get("kdu", ())
39
40
41 def get_ee_sorted_initial_config_primitive_list(primitive_list, vca_deployed, ee_descriptor_id):
42 """
43 Generates a list of initial-config-primitive based on the list provided by the descriptor. It includes internal
44 primitives as verify-ssh-credentials, or config when needed
45 :param primitive_list: information of the descriptor
46 :param vca_deployed: information of the deployed, needed for known if it is related to an NS, VNF, VDU and if
47 this element contains a ssh public key
48 :param ee_descriptor_id: execution environment descriptor id. It is the value of
49 XXX_configuration.execution-environment-list.INDEX.id; it can be None
50 :return: The modified list. Can ba an empty list, but always a list
51 """
52 primitive_list = primitive_list or []
53 primitive_list = [
54 p for p in primitive_list if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id
55 ]
56 if primitive_list:
57 primitive_list.sort(key=lambda val: int(val['seq']))
58
59 # look for primitive config, and get the position. None if not present
60 config_position = None
61 for index, primitive in enumerate(primitive_list):
62 if primitive["name"] == "config":
63 config_position = index
64 break
65
66 # for NS, add always a config primitive if not present (bug 874)
67 if not vca_deployed["member-vnf-index"] and config_position is None:
68 primitive_list.insert(0, {"name": "config", "parameter": []})
69 config_position = 0
70 # TODO revise if needed: for VNF/VDU add verify-ssh-credentials after config
71 if vca_deployed["member-vnf-index"] and config_position is not None and vca_deployed.get("ssh-public-key"):
72 primitive_list.insert(config_position + 1, {"name": "verify-ssh-credentials", "parameter": []})
73 return primitive_list
74
75
76 def get_ee_sorted_terminate_config_primitive_list(primitive_list, ee_descriptor_id):
77 primitive_list = primitive_list or []
78 primitive_list = [
79 p for p in primitive_list if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id
80 ]
81 if primitive_list:
82 primitive_list.sort(key=lambda val: int(val['seq']))
83 return primitive_list
84
85
86 def get_vdu_profile(vnfd, vdu_profile_id):
87 return list_utils.find_in_list(
88 vnfd.get("df", ())[0]["vdu-profile"],
89 lambda vdu_profile: vdu_profile["id"] == vdu_profile_id)
90
91
92 def get_vnf_configuration(vnfd):
93 if "vnf-configuration-id" not in vnfd.get("df")[0]:
94 return None
95 vnf_config_id = vnfd.get("df")[0]["vnf-configuration-id"]
96 return list_utils.find_in_list(
97 vnfd.get("vnf-configuration", {}),
98 lambda vnf_config: vnf_config["id"] == vnf_config_id)
99
100
101 def get_vdu_configuration(vnfd, vdu_id):
102 vdu_profile = get_vdu_profile(vnfd, vdu_id)
103 return list_utils.find_in_list(
104 vnfd.get("vdu-configuration", ()),
105 lambda vdu_configuration: vdu_configuration["id"] == vdu_profile["vdu-configuration-id"])
106
107
108 def get_kdu_configuration(vnfd, kdu_name):
109 for kdu in get_iterable(vnfd, "kdu"):
110 if kdu_name == kdu["name"]:
111 return kdu.get("kdu-configuration")
112
113
114 def get_virtual_link_profiles(vnfd):
115 return vnfd.get("df")[0].get("virtual-link-profile", ())
116
117
118 def get_vdu(vnfd, vdu_id):
119 return list_utils.find_in_list(
120 vnfd.get("vdu", ()),
121 lambda vdu: vdu["id"] == vdu_id)
122
123
124 def get_vdu_index(vnfd, vdu_id):
125 target_vdu = list_utils.find_in_list(
126 vnfd.get("vdu", ()),
127 lambda vdu: vdu["id"] == vdu_id)
128 if target_vdu:
129 return vnfd.get("vdu", ()).index(target_vdu)
130 else:
131 return -1
132
133
134 def get_scaling_aspect(vnfd):
135 return vnfd.get("df", ())[0].get("scaling-aspect", ())
136
137
138 def get_number_of_instances(vnfd, vdu_id):
139 return list_utils.find_in_list(
140 vnfd.get(
141 "df",
142 ()
143 )[0].get(
144 "instantiation-level",
145 ()
146 )[0].get(
147 "vdu-level",
148 ()
149 ),
150 lambda a_vdu: a_vdu["vdu-id"] == vdu_id
151 )["number-of-instances"]