Fix bug 1613 - fixing build to allow python3 -m build
[osm/IM.git] / osm_im / im_translation.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2020 Whitestack, LLC
4 # *************************************************************
5 #
6 # This file is part of OSM common repository.
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: agarcia@whitestack.com
23 ##
24
25 from osm_im.validation import Validation, ValidationException
26
27
28 class TranslationException(Exception):
29 pass
30
31
32 # ******************** Translation public functions ********************
33
34 def translate_im_model_to_sol006(im_model_data):
35 _validate_im_model(im_model_data)
36 descriptor_type = _get_im_model_descriptor_type(im_model_data)
37 if descriptor_type == "vnfd":
38 return translate_im_vnfd_to_sol006(im_model_data)
39 if descriptor_type == "nsd":
40 return translate_im_nsd_to_sol006(im_model_data)
41 # For sanity, should not happen
42 raise TranslationException("Error in translation: cannot determine the type of OSM-IM descriptor. Found {}, "
43 "expected one of: vnfd:vnfd-catalog, vnfd-catalog, nsd:nsd-catalog, nsd-catalog."
44 .format(descriptor_type))
45
46
47 def translate_im_vnfd_to_sol006(im_vnfd):
48 im_vnfd = _remove_im_vnfd_envelope(im_vnfd)
49 sol006_vnfd = {}
50 _add_im_vnfd_basic_data_to_sol006_vnfd(im_vnfd, sol006_vnfd)
51 _add_im_vnfd_mgmt_interface_cp_to_sol006_vnfd(im_vnfd, sol006_vnfd)
52 _add_im_vdu_flavors_to_sol006_vnfd(im_vnfd, sol006_vnfd)
53 _add_im_vdu_guest_epa_to_sol006_vnfd(im_vnfd, sol006_vnfd)
54 _add_im_vdu_images_to_sol006_vnfd(im_vnfd, sol006_vnfd)
55 _add_im_vdus_to_sol006_vnfd(im_vnfd, sol006_vnfd)
56 _add_im_internal_vlds_to_sol006_vfnd(im_vnfd, sol006_vnfd)
57 _add_im_vnf_configuration_to_sol006_vnfd(im_vnfd, sol006_vnfd)
58 _add_im_ip_profiles_to_sol006_vnfd(im_vnfd, sol006_vnfd)
59 _add_im_vdu_monitoring_params_to_sol006_vnfd(im_vnfd, sol006_vnfd)
60 _add_im_scaling_group_descriptors_to_sol006_vnfd(im_vnfd, sol006_vnfd)
61 _add_im_kdus_to_sol006_vnfd(im_vnfd, sol006_vnfd)
62 _add_im_k8s_clusters_to_sol006_vnfd(im_vnfd, sol006_vnfd)
63 _add_im_placement_groups_to_sol006_vnfd(im_vnfd, sol006_vnfd)
64 _cleanup_juju_in_configurations(sol006_vnfd)
65 return {"vnfd": sol006_vnfd}
66
67
68 def translate_im_nsd_to_sol006(im_nsd):
69 im_nsd = _remove_im_nsd_envelope(im_nsd)
70 sol006_nsd = {}
71 _add_im_nsd_basic_data_to_sol006_nsd(im_nsd, sol006_nsd)
72 _add_im_constituent_vnfds_to_sol006_nsd(im_nsd, sol006_nsd)
73 _add_im_vlds_to_sol006_nsd(im_nsd, sol006_nsd)
74 return {"nsd": {"nsd": [sol006_nsd]}}
75
76
77 # ******************** Common translation private functions ********************
78
79 def _validate_im_model(im_model_data):
80 descriptor_type = _get_im_model_descriptor_type(im_model_data)
81 try:
82 Validation().pyangbind_validation(descriptor_type, im_model_data)
83 except ValidationException as e:
84 raise TranslationException("Error on input model validation: {}".format(str(e)))
85
86
87 def _get_im_model_descriptor_type(im_model_data):
88 data_root = list(im_model_data.keys())[0]
89 if "vnfd" in data_root:
90 return "vnfd"
91 if "nsd" in data_root:
92 return "nsd"
93 raise TranslationException("Error in translation: cannot determine the type of OSM-IM descriptor. Found {}, "
94 "expected one of: vnfd:vnfd-catalog, vnfd-catalog, nsd:nsd-catalog, nsd-catalog."
95 .format(data_root))
96
97
98 # ******************** VNFD translation private functions ********************
99
100 def _remove_im_vnfd_envelope(im_vnfd):
101 # Data is wrapped as { vnfd-catalog: { vnfd: [ <data> ] } }
102 return list(im_vnfd.values())[0]["vnfd"][0]
103
104
105 def _add_im_vnfd_basic_data_to_sol006_vnfd(im_vnfd, sol006_vnfd):
106 sol006_vnfd["id"] = im_vnfd["id"]
107 if im_vnfd.get("name"):
108 sol006_vnfd["product-name"] = im_vnfd["name"]
109 if im_vnfd.get("description"):
110 sol006_vnfd["description"] = im_vnfd["description"]
111 if im_vnfd.get("vendor"):
112 sol006_vnfd["provider"] = im_vnfd["vendor"]
113 if im_vnfd.get("version"):
114 sol006_vnfd["version"] = im_vnfd["version"]
115
116
117 def _add_im_vnfd_mgmt_interface_cp_to_sol006_vnfd(im_vnfd, sol006_vnfd):
118 sol006_vnfd["mgmt-cp"] = "{}-ext".format(im_vnfd["mgmt-interface"]["cp"])
119
120
121 def _add_im_vdu_flavors_to_sol006_vnfd(im_vnfd, sol006_vnfd):
122 storage_descriptors = []
123 compute_descriptors = []
124 for vdu in im_vnfd.get("vdu", ()):
125 vdu_id = vdu.get("id")
126 vdu_flavor = vdu.get("vm-flavor")
127 if not vdu_flavor:
128 continue
129 storage_descriptor = {"id": "{}-storage".format(vdu_id)}
130 compute_descriptor = {"id": "{}-compute".format(vdu_id)}
131 if vdu_flavor.get("storage-gb"):
132 storage_descriptor["size-of-storage"] = int(vdu_flavor["storage-gb"])
133 storage_descriptors.append(storage_descriptor)
134 if vdu_flavor.get("vcpu-count"):
135 compute_descriptor["virtual-cpu"] = {"num-virtual-cpu": int(vdu_flavor["vcpu-count"])}
136 if vdu_flavor.get("memory-mb"):
137 compute_descriptor["virtual-memory"] = {"size": float(vdu_flavor["memory-mb"]) / 1024.0}
138 if len(compute_descriptor) > 1:
139 compute_descriptors.append(compute_descriptor)
140 if len(storage_descriptors) > 0:
141 sol006_vnfd["virtual-storage-desc"] = storage_descriptors
142 if len(compute_descriptors) > 0:
143 sol006_vnfd["virtual-compute-desc"] = compute_descriptors
144
145
146 def _add_im_vdu_guest_epa_to_sol006_vnfd(im_vnfd, sol006_vnfd):
147 for vdu in im_vnfd.get("vdu", ()):
148 vdu_guest_epa = vdu.get("guest-epa")
149 if not vdu_guest_epa:
150 continue
151
152 _add_im_vdu_guest_epa_memory_and_cpu_to_sol006_vnfd(vdu, sol006_vnfd)
153 _add_im_vdu_guest_epa_disk_io_to_sol006_vnfd(vdu, sol006_vnfd)
154
155
156 def _add_im_vdu_guest_epa_memory_and_cpu_to_sol006_vnfd(im_vdu, sol006_vnfd):
157 vdu_guest_epa = im_vdu.get("guest-epa")
158 virtual_memory = _get_virtual_memory_from_im_vdu_guest_epa(vdu_guest_epa)
159 virtual_cpu = _get_virtual_cpu_from_im_vdu_guest_epa(vdu_guest_epa)
160 # Find this vdu compute descriptor and update it with the EPA options. If the
161 # vdu compute descriptor does not exist, create one with the EPA options only.
162 compute_descriptor_id = "{}-compute".format(im_vdu["id"])
163 compute_descriptor = {"id": compute_descriptor_id}
164 compute_descriptor_found = False
165 for vcd in sol006_vnfd.get("virtual-compute-desc", ()):
166 if vcd.get("id") == compute_descriptor_id:
167 compute_descriptor = vcd
168 compute_descriptor_found = True
169
170 compute_descriptor_virtual_memory = compute_descriptor.get("virtual-memory", {})
171 compute_descriptor_virtual_cpu = compute_descriptor.get("virtual-cpu", {})
172 compute_descriptor_virtual_memory.update(virtual_memory)
173 compute_descriptor_virtual_cpu.update(virtual_cpu)
174 if compute_descriptor_virtual_memory:
175 compute_descriptor["virtual-memory"] = compute_descriptor_virtual_memory
176 if compute_descriptor_virtual_cpu:
177 compute_descriptor["virtual-cpu"] = compute_descriptor_virtual_cpu
178
179 if not compute_descriptor_found:
180 if sol006_vnfd.get("virtual-compute-desc"):
181 sol006_vnfd["virtual-compute-desc"].append(compute_descriptor)
182 else:
183 sol006_vnfd["virtual-compute-desc"] = [compute_descriptor]
184
185
186 def _add_im_vdu_guest_epa_disk_io_to_sol006_vnfd(im_vdu, sol006_vnfd):
187 vdu_guest_epa = im_vdu.get("guest-epa")
188 disk_io_quota = vdu_guest_epa.get("disk-io-quota", {})
189 if not disk_io_quota:
190 return
191 # Find this vdu storage descriptor and update it with the EPA options. If the
192 # vdu storage descriptor does not exist, create one with the EPA options only.
193 storage_descriptor_id = "{}-storage".format(im_vdu["id"])
194 storage_descriptor = {"id": storage_descriptor_id}
195 storage_descriptor_found = False
196 for vsd in sol006_vnfd.get("virtual-storage-desc", ()):
197 if vsd.get("id") == storage_descriptor_id:
198 storage_descriptor = vsd
199 storage_descriptor_found = True
200
201 storage_descriptor["disk-io-quota"] = disk_io_quota
202 if not storage_descriptor_found:
203 if sol006_vnfd.get("virtual-storage-desc"):
204 sol006_vnfd["virtual-storage-desc"].append(storage_descriptor)
205 else:
206 sol006_vnfd["virtual-storage-desc"] = [storage_descriptor]
207
208
209 def _get_virtual_memory_from_im_vdu_guest_epa(im_vdu_guest_epa):
210 virtual_memory = {}
211 if im_vdu_guest_epa.get("mempage-size"):
212 virtual_memory["mempage-size"] = im_vdu_guest_epa["mempage-size"]
213 if im_vdu_guest_epa.get("numa-node-policy"):
214 virtual_memory["numa-enabled"] = True
215 virtual_memory["numa-node-policy"] = im_vdu_guest_epa["numa-node-policy"]
216 if im_vdu_guest_epa.get("mem-quota"):
217 virtual_memory["mem-quota"] = im_vdu_guest_epa["mem-quota"]
218 return virtual_memory
219
220
221 def _get_virtual_cpu_from_im_vdu_guest_epa(im_vdu_guest_epa):
222 virtual_cpu = {}
223 if im_vdu_guest_epa.get("cpu-pinning-policy") or im_vdu_guest_epa.get("cpu-thread-pinning-policy"):
224 virtual_cpu["pinning"] = {}
225 if im_vdu_guest_epa.get("cpu-pinning-policy"):
226 if im_vdu_guest_epa["cpu-pinning-policy"] == "SHARED":
227 virtual_cpu["pinning"]["policy"] = "dynamic"
228 else:
229 virtual_cpu["pinning"]["policy"] = "static"
230 if im_vdu_guest_epa.get("cpu-thread-pinning-policy"):
231 virtual_cpu["pinning"]["thread-policy"] = im_vdu_guest_epa["cpu-thread-pinning-policy"]
232 if im_vdu_guest_epa.get("cpu-quota"):
233 virtual_cpu["cpu-quota"] = im_vdu_guest_epa["cpu-quota"]
234 return virtual_cpu
235
236
237 def _add_im_vdu_images_to_sol006_vnfd(im_vnfd, sol006_vnfd):
238 image_descriptors = []
239 all_images = set() # To avoid duplicated images
240 for vdu in im_vnfd.get("vdu", ()):
241 vdu_image = vdu.get("image")
242 if vdu_image and vdu_image not in all_images:
243 image_descriptors.append({"id": vdu_image, "name": vdu_image, "image": vdu_image})
244 all_images.add(vdu_image)
245 for alternative_image in vdu.get("alternative-images", ()):
246 alt_image = alternative_image.get("image")
247 alt_image_descriptor = {"id": alt_image, "name": alt_image, "image": alt_image}
248 if alternative_image.get("vim-type"):
249 alt_image_descriptor["vim-type"] = alternative_image["vim-type"]
250 if alt_image not in all_images:
251 image_descriptors.append(alt_image_descriptor)
252 all_images.add(alt_image)
253
254 if len(image_descriptors) > 0:
255 sol006_vnfd["sw-image-desc"] = image_descriptors
256
257
258 def _add_im_vdus_to_sol006_vnfd(im_vnfd, sol006_vnfd):
259 vdus = []
260 ext_cpds = []
261 vdu_configurations = []
262 df_instantiation_level = {"id": "default-instantiation-level", "vdu-level": []}
263 df = {"id": "default-df", "vdu-profile": [], "instantiation-level": [df_instantiation_level]}
264 for vdu in im_vnfd.get("vdu", ()):
265 vdu_descriptor = {"id": vdu["id"]}
266 if vdu.get("description"):
267 vdu_descriptor["description"] = vdu["description"]
268 if vdu.get("name"):
269 vdu_descriptor["name"] = vdu["name"]
270 if vdu.get("cloud-init-file"):
271 vdu_descriptor["cloud-init-file"] = vdu["cloud-init-file"]
272 if vdu.get("supplemental-boot-data"):
273 vdu_descriptor["supplemental-boot-data"] = vdu["supplemental-boot-data"]
274 if vdu.get("alarm"):
275 vdu_descriptor["alarm"] = vdu["alarm"]
276 if vdu.get("pdu-type"):
277 vdu_descriptor["pdu-type"] = vdu["pdu-type"]
278
279 _add_im_vdu_images_to_sol006_vdu(vdu, vdu_descriptor)
280 _add_im_vdu_flavor_to_sol006_vdu(vdu, vdu_descriptor)
281
282 vdu_int_cpds, vdu_ext_cpds = _get_int_and_ext_cpds_from_im_vdu(vdu, im_vnfd)
283 vdu_descriptor["int-cpd"] = vdu_int_cpds
284 ext_cpds.extend(vdu_ext_cpds)
285
286 vdu_profile = _get_vdu_profile_from_im_vdu(vdu, im_vnfd)
287 vdu_level = _get_instantiation_level_vdu_level_from_im_vdu(vdu)
288 if vdu.get("vdu-configuration"):
289 vdu_configuration = vdu["vdu-configuration"]
290 vdu_configuration["id"] = vdu["id"]
291 vdu_configurations.append(vdu_configuration)
292 df["vdu-profile"].append(vdu_profile)
293 df["instantiation-level"][0]["vdu-level"].append(vdu_level)
294
295 vdus.append(vdu_descriptor)
296
297 if len(vdus) > 0:
298 sol006_vnfd["vdu"] = vdus
299 sol006_vnfd["df"] = [df]
300 if len(ext_cpds) > 0:
301 sol006_vnfd["ext-cpd"] = ext_cpds
302 if len(vdu_configurations) > 0:
303 _prepare_dict_entries_for_configurations(sol006_vnfd)
304 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].extend(vdu_configurations)
305
306
307 def _add_im_vdu_images_to_sol006_vdu(im_vdu, sol006_vdu):
308 if im_vdu.get("image"):
309 sol006_vdu["sw-image-desc"] = im_vdu["image"]
310 alternative_images = []
311 for alternative_image in im_vdu.get("alternative-images", ()):
312 alternative_images.append(alternative_image.get("image"))
313 if len(alternative_images) > 0:
314 sol006_vdu["alternative-sw-image-desc"] = alternative_images
315
316
317 def _add_im_vdu_flavor_to_sol006_vdu(im_vdu, sol006_vdu):
318 vdu_flavor = im_vdu.get("vm-flavor")
319 if vdu_flavor:
320 if vdu_flavor.get("vcpu-count") or vdu_flavor.get("memory-mb"):
321 sol006_vdu["virtual-compute-desc"] = "{}-compute".format(im_vdu["id"])
322 if vdu_flavor.get("storage-gb"):
323 sol006_vdu["virtual-storage-desc"] = ["{}-storage".format(im_vdu["id"])]
324
325
326 def _get_int_and_ext_cpds_from_im_vdu(im_vdu, im_vnfd):
327 int_cpds = []
328 ext_cpds = []
329 for interface in im_vdu.get("interface", ()):
330 int_cpd = {"id": "{}-int".format(interface["name"])}
331 virtual_network_interface_requirement = {"name": interface["name"]}
332 if interface.get("virtual-interface"):
333 virtual_network_interface_requirement["virtual-interface"] = interface["virtual-interface"]
334 if "position" in interface:
335 virtual_network_interface_requirement["position"] = int(interface["position"])
336 int_cpd["virtual-network-interface-requirement"] = [virtual_network_interface_requirement]
337 if interface.get("external-connection-point-ref"):
338 ext_cpd = {
339 "id": "{}-ext".format(interface["external-connection-point-ref"]),
340 "int-cpd": {
341 "vdu-id": im_vdu["id"],
342 "cpd": int_cpd["id"]
343 }
344 }
345 for cp in im_vnfd.get("connection-point", ()):
346 if cp.get("name", "") != interface["external-connection-point-ref"]:
347 continue
348 if "port-security-enabled" in cp:
349 ext_cpd["port-security-enabled"] = cp["port-security-enabled"]
350 if cp.get("port-security-disable-strategy"):
351 ext_cpd["port-security-disable-strategy"] = cp["port-security-disable-strategy"]
352 ext_cpds.append(ext_cpd)
353
354 int_cpds.append(int_cpd)
355
356 return int_cpds, ext_cpds
357
358
359 def _get_vdu_profile_from_im_vdu(im_vdu, im_vnfd):
360 vdu_profile = {"id": im_vdu["id"]}
361 initial_instances = int(im_vdu.get("count", 1))
362 vdu_profile["min-number-of-instances"] = initial_instances
363 for scaling_group_descriptor in im_vnfd.get("scaling-group-descriptor", ()):
364 for sgd_vdu in scaling_group_descriptor.get("vdu", []):
365 if sgd_vdu.get("vdu-id-ref") == im_vdu["id"]:
366 sgd_max_instances = int(scaling_group_descriptor.get("max-instance-count", 1))
367 sgd_min_instances = int(scaling_group_descriptor.get("min-instance-count", 0))
368 vdu_profile["min-number-of-instances"] = sgd_min_instances + initial_instances
369 vdu_profile["max-number-of-instances"] = sgd_max_instances + initial_instances
370 return vdu_profile
371
372
373 def _get_instantiation_level_vdu_level_from_im_vdu(im_vdu):
374 vdu_level = {"vdu-id": im_vdu["id"]}
375 vdu_level["number-of-instances"] = int(im_vdu.get("count", 1))
376 return vdu_level
377
378
379 def _add_im_internal_vlds_to_sol006_vfnd(im_vnfd, sol006_vnfd):
380 int_virtual_link_descs = []
381 for ivld in im_vnfd.get("internal-vld", ()):
382 int_virtual_link_desc = {"id": ivld["id"]}
383 _add_im_internal_vld_connection_point_refs_to_sol006_vnfd(ivld, im_vnfd, sol006_vnfd)
384
385 int_virtual_link_descs.append(int_virtual_link_desc)
386
387 if len(int_virtual_link_descs) > 0:
388 sol006_vnfd["int-virtual-link-desc"] = int_virtual_link_descs
389
390
391 def _add_im_internal_vld_connection_point_refs_to_sol006_vnfd(ivld, im_vnfd, sol006_vnfd):
392 all_int_cp_refs_interfaces = {}
393 for vdu in im_vnfd.get("vdu", ()):
394 for interface in vdu.get("interface", ()):
395 int_cp_ref = interface.get("internal-connection-point-ref")
396 if not int_cp_ref:
397 continue
398 all_int_cp_refs_interfaces[int_cp_ref] = (vdu["id"], interface["name"])
399
400 for int_cp in ivld.get("internal-connection-point", ()):
401 int_cp_ref = int_cp["id-ref"]
402 (vdu_id, interface_name) = all_int_cp_refs_interfaces[int_cp_ref]
403 sol006_int_cpd_id = "{}-int".format(interface_name)
404 # Search for this int_cp on sol006_vnfd and update it
405 for vdu in sol006_vnfd.get("vdu", ()):
406 if vdu["id"] != vdu_id:
407 continue
408 for int_cpd in vdu.get("int-cpd", ()):
409 if int_cpd["id"] == sol006_int_cpd_id:
410 int_cpd["int-virtual-link-desc"] = ivld["id"]
411
412 def _prepare_dict_entries_for_configurations(sol006_vnfd):
413 sol006_vnfd["df"] = sol006_vnfd.get("df", [{}])
414 if not sol006_vnfd["df"][0].get("lcm-operations-configuration"):
415 sol006_vnfd["df"][0]["lcm-operations-configuration"] = {
416 "operate-vnf-op-config": {
417 "day1-2": []
418 }
419 }
420
421 def _cleanup_juju_in_configurations(sol006_vnfd):
422 configs = sol006_vnfd["df"][0].get("lcm-operations-configuration")
423 if configs:
424 for day12_config in configs["operate-vnf-op-config"]["day1-2"]:
425 if "juju" in day12_config:
426 ee_name = _create_execution_environment_for_juju(day12_config, day12_config["juju"]["charm"])
427 for primitive in day12_config.get("config-primitive", []):
428 primitive["execution-environment-ref"] = ee_name
429 for primitive in day12_config.get("initial-config-primitive", []):
430 primitive["execution-environment-ref"] = ee_name
431 for primitive in day12_config.get("terminate-config-primitive", []):
432 primitive["execution-environment-ref"] = ee_name
433 day12_config.pop("juju", None)
434
435 def _create_execution_environment_for_juju(day12_config, charm_name):
436 if not "execution-environment-list" in day12_config:
437 day12_config["execution-environment-list"] = []
438
439 day12_config["execution-environment-list"].append(
440 {
441 "id": charm_name + "-ee",
442 "juju": {
443 "charm": charm_name
444 }
445 }
446 )
447
448 return charm_name + "-ee"
449
450 def _add_im_vnf_configuration_to_sol006_vnfd(im_vnfd, sol006_vnfd):
451 vnf_configuration = im_vnfd.get("vnf-configuration")
452 if not vnf_configuration:
453 return
454 vnf_configuration["id"] = im_vnfd.get("id")
455 _prepare_dict_entries_for_configurations(sol006_vnfd)
456 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].append(vnf_configuration)
457
458
459 def _add_im_ip_profiles_to_sol006_vnfd(im_vnfd, sol006_vnfd):
460 all_ivlds_ip_profiles = {}
461 for ivld in im_vnfd.get("internal-vld", ()):
462 if ivld.get("ip-profile-ref"):
463 all_ivlds_ip_profiles[ivld["ip-profile-ref"]] = ivld["id"]
464
465 virtual_link_profiles = []
466 for ip_profile in im_vnfd.get("ip-profiles", ()):
467 virtual_link_profile = {"id": all_ivlds_ip_profiles[ip_profile["name"]], "flavour": ""}
468 ip_profile_params = ip_profile.get("ip-profile-params")
469 if ip_profile_params:
470 l3_protocol_data = {"name": "{}-l3-protocol-data".format(virtual_link_profile["id"])}
471 if ip_profile_params.get("ip-version"):
472 l3_protocol_data["ip-version"] = ip_profile_params["ip-version"]
473 if ip_profile_params.get("subnet-address"):
474 l3_protocol_data["cidr"] = ip_profile_params["subnet-address"]
475 if ip_profile_params.get("gateway-address"):
476 l3_protocol_data["gateway-ip"] = ip_profile_params["gateway-address"]
477 if ip_profile_params.get("security-group"):
478 l3_protocol_data["security-group"] = ip_profile_params["security-group"]
479 if ip_profile_params.get("dhcp-params", {}).get("enabled"):
480 l3_protocol_data["dhcp-enabled"] = ip_profile_params["dhcp-params"]["enabled"]
481 if ip_profile.get("description"):
482 l3_protocol_data["description"] = ip_profile["description"]
483
484 virtual_link_profile["virtual-link-protocol-data"] = {"l3-protocol-data": l3_protocol_data}
485
486 virtual_link_profiles.append(virtual_link_profile)
487
488 if len(virtual_link_profiles) > 0:
489 sol006_vnfd["df"][0]["virtual-link-profile"] = virtual_link_profiles
490
491
492 def _add_im_vdu_monitoring_params_to_sol006_vnfd(im_vnfd, sol006_vnfd):
493 all_vdu_monitoring_param_metrics = {}
494 for vdu in im_vnfd.get("vdu", ()):
495 for monitoring_param in vdu.get("monitoring-param", ()):
496 monitoring_param_metric = monitoring_param.get("nfvi-metric")
497 if monitoring_param_metric:
498 all_vdu_monitoring_param_metrics[(vdu["id"], monitoring_param["id"])] = monitoring_param_metric
499
500 for monitoring_param in im_vnfd.get("monitoring-param", ()):
501 sol006_mp = {"id": monitoring_param["id"]}
502 if monitoring_param.get("name"):
503 sol006_mp["name"] = monitoring_param["name"]
504 if monitoring_param.get("vdu-monitoring-param"):
505 monitoring_param_vdu_id = monitoring_param["vdu-monitoring-param"].get("vdu-ref")
506 monitoring_param_id = monitoring_param["vdu-monitoring-param"].get("vdu-monitoring-param-ref")
507 metric = all_vdu_monitoring_param_metrics.get((monitoring_param_vdu_id, monitoring_param_id))
508 if metric:
509 sol006_mp["performance-metric"] = metric
510 # Find that vdu inside sol006_vnfd and update its monitoring-parameter list
511 for vdu in sol006_vnfd.get("vdu", ()):
512 if vdu["id"] != monitoring_param_vdu_id:
513 continue
514 if vdu.get("monitoring-parameter"):
515 vdu["monitoring-parameter"].append(sol006_mp)
516 else:
517 vdu["monitoring-parameter"] = [sol006_mp]
518
519
520 def _add_im_scaling_group_descriptors_to_sol006_vnfd(im_vnfd, sol006_vnfd):
521 scaling_aspects = []
522 for scaling_group_descriptor in im_vnfd.get("scaling-group-descriptor", ()):
523 scaling_aspect = {"id": scaling_group_descriptor["name"], "name": scaling_group_descriptor["name"]}
524 if scaling_group_descriptor.get("max-instance-count"):
525 scaling_aspect["max-scale-level"] = int(scaling_group_descriptor["max-instance-count"])
526 if scaling_group_descriptor.get("scaling-policy"):
527 scaling_aspect["scaling-policy"] = scaling_group_descriptor["scaling-policy"]
528 if scaling_group_descriptor.get("scaling-config-action"):
529 scaling_aspect["scaling-config-action"] = scaling_group_descriptor["scaling-config-action"]
530
531 delta = {"id": "{}-delta".format(scaling_aspect["id"])}
532 vdu_deltas = []
533 for vdu in scaling_group_descriptor.get("vdu", ()):
534 vdu_delta = {}
535 if vdu.get("count"):
536 vdu_delta["number-of-instances"] = int(vdu["count"])
537 if vdu.get("vdu-id-ref"):
538 vdu_delta["id"] = vdu["vdu-id-ref"]
539 vdu_deltas.append(vdu_delta)
540 if len(vdu_deltas) > 0:
541 delta["vdu-delta"] = vdu_deltas
542 scaling_aspect["aspect-delta-details"] = {"deltas": [delta]}
543
544 scaling_aspects.append(scaling_aspect)
545
546 if len(scaling_aspects) > 0:
547 sol006_vnfd["df"][0]["scaling-aspect"] = scaling_aspects
548
549
550 def _add_im_kdus_to_sol006_vnfd(im_vnfd, sol006_vnfd):
551 if im_vnfd.get("kdu"):
552 sol006_vnfd["kdu"] = im_vnfd["kdu"]
553 kdu_configs = []
554 for a_kdu in sol006_vnfd["kdu"]:
555 if "kdu-configuration" in a_kdu:
556 kdu_config = a_kdu.pop("kdu-configuration")
557 kdu_config["id"] = a_kdu["name"]
558 kdu_configs.append(kdu_config)
559 if len(sol006_vnfd.get("df", ())) == 0:
560 sol006_vnfd["df"] = [{"id": "default-df"}]
561 if len(kdu_configs) > 0:
562 _prepare_dict_entries_for_configurations(sol006_vnfd)
563 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].extend(kdu_configs)
564
565
566
567 def _add_im_k8s_clusters_to_sol006_vnfd(im_vnfd, sol006_vnfd):
568 im_k8s_cluster = im_vnfd.get("k8s-cluster")
569 if not im_k8s_cluster:
570 return
571
572 sol006_k8s_cluster = {}
573 if im_k8s_cluster.get("version"):
574 sol006_k8s_cluster["version"] = im_k8s_cluster["version"]
575 if im_k8s_cluster.get("cni"):
576 sol006_k8s_cluster["cni"] = im_k8s_cluster["cni"]
577
578 nets = []
579 k8s_cluster_ext_cpds = []
580 for net in im_k8s_cluster.get("nets", ()):
581 if net.get("external-connection-point-ref"):
582 ext_cpd = {"id": "{}-ext".format(net["external-connection-point-ref"]), "k8s-cluster-net": net["id"]}
583 k8s_cluster_ext_cpds.append(ext_cpd)
584 nets.append({"id": net["id"]})
585 if len(nets) > 0:
586 sol006_k8s_cluster["nets"] = nets
587
588 sol006_vnfd["k8s-cluster"] = sol006_k8s_cluster
589 if len(k8s_cluster_ext_cpds) > 0:
590 if not sol006_vnfd.get("ext-cpd"):
591 sol006_vnfd["ext-cpd"] = []
592 sol006_vnfd["ext-cpd"].extend(k8s_cluster_ext_cpds)
593
594
595 def _add_im_placement_groups_to_sol006_vnfd(im_vnfd, sol006_vnfd):
596 if im_vnfd.get("placement-groups"):
597 sol006_vnfd["placement-groups"] = im_vnfd["placement-groups"]
598
599
600 # ******************** NSD translation private functions ********************
601
602 def _remove_im_nsd_envelope(im_nsd):
603 # Data is wrapped as { nsd-catalog: { nsd: [ <data> ] } }
604 return list(im_nsd.values())[0]["nsd"][0]
605
606
607 def _add_im_nsd_basic_data_to_sol006_nsd(im_nsd, sol006_nsd):
608 sol006_nsd["id"] = im_nsd["id"]
609 if im_nsd.get("name"):
610 sol006_nsd["name"] = im_nsd["name"]
611 if im_nsd.get("description"):
612 sol006_nsd["description"] = im_nsd["description"]
613 if im_nsd.get("vendor"):
614 sol006_nsd["designer"] = im_nsd["vendor"]
615 if im_nsd.get("version"):
616 sol006_nsd["version"] = im_nsd["version"]
617
618
619 def _add_im_constituent_vnfds_to_sol006_nsd(im_nsd, sol006_nsd):
620 vnfd_ids = set()
621 for constituent_vnfd in im_nsd.get("constituent-vnfd", ()):
622 if constituent_vnfd.get("vnfd-id-ref"):
623 vnfd_ids.add(constituent_vnfd["vnfd-id-ref"])
624
625 if len(vnfd_ids) > 0:
626 sol006_nsd["vnfd-id"] = list(vnfd_ids)
627
628
629 def _add_im_vlds_to_sol006_nsd(im_nsd, sol006_nsd):
630 vlds = im_nsd.get("vld", [])
631 flattened_vlds = [{**vld, **cp_ref} for vld in vlds for cp_ref in vld.get("vnfd-connection-point-ref", ())]
632 all_vlds_by_member_vnf_index = {}
633 for vld in flattened_vlds:
634 member_vnf_index = str(vld.get("member-vnf-index-ref", ""))
635 if member_vnf_index in all_vlds_by_member_vnf_index:
636 all_vlds_by_member_vnf_index[member_vnf_index].append(vld)
637 else:
638 all_vlds_by_member_vnf_index[member_vnf_index] = [vld]
639
640 df = {"id": "default-df", "vnf-profile": []}
641
642 for member_vnf_index in all_vlds_by_member_vnf_index:
643 vnf_profile = {"id": member_vnf_index}
644 vnf_profile["vnfd-id"] = all_vlds_by_member_vnf_index.get(member_vnf_index, [{}])[0].get("vnfd-id-ref")
645 vnf_profile["virtual-link-connectivity"] = []
646 for vld in all_vlds_by_member_vnf_index[member_vnf_index]:
647 virtual_link_connectivity = {"virtual-link-profile-id": vld["id"]}
648 virtual_link_connectivity["constituent-cpd-id"] = [{
649 "constituent-base-element-id": member_vnf_index,
650 "constituent-cpd-id": "{}-ext".format(vld.get("vnfd-connection-point-ref"))
651 }]
652 if vld.get("ip-address"):
653 virtual_link_connectivity["constituent-cpd-id"][0]["ip-address"] = vld["ip-address"]
654 vnf_profile["virtual-link-connectivity"].append(virtual_link_connectivity)
655
656 vlcs_by_virtual_link_profile = {}
657 for vlc in vnf_profile.get("virtual-link-connectivity"):
658 vl_profile_id = vlc.get("virtual-link-profile-id")
659 if vl_profile_id in vlcs_by_virtual_link_profile:
660 vlc_constituent_cpds = vlcs_by_virtual_link_profile[vl_profile_id].get("constituent-cpd-id")
661 vlc_constituent_cpds.extend(vlc.get("constituent-cpd-id"))
662 else:
663 vlcs_by_virtual_link_profile[vl_profile_id] = vlc
664
665 vnf_profile["virtual-link-connectivity"] = list(vlcs_by_virtual_link_profile.values())
666
667 df["vnf-profile"].append(vnf_profile)
668
669 sol006_nsd["df"] = [df]
670
671 virtual_link_descs = []
672 for vld in im_nsd.get("vld", ()):
673 virtual_link_desc = {"id": vld["id"]}
674 if vld.get("mgmt-network"):
675 virtual_link_desc["mgmt-network"] = vld["mgmt-network"]
676 if vld.get("vim-network-name"):
677 virtual_link_desc["vim-network-name"] = vld["vim-network-name"]
678 virtual_link_descs.append(virtual_link_desc)
679
680 if len(virtual_link_descs) > 0:
681 sol006_nsd["virtual-link-desc"] = virtual_link_descs