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("cloud-init"):

273 vdu_descriptor["cloud-init"] = vdu["cloud-init"]

274 if vdu.get("supplemental-boot-data"):

275 vdu_descriptor["supplemental-boot-data"] = vdu["supplemental-boot-data"]

276 if vdu.get("alarm"):

277 vdu_descriptor["alarm"] = vdu["alarm"]

278 if vdu.get("pdu-type"):

279 vdu_descriptor["pdu-type"] = vdu["pdu-type"]

280

281 _add_im_vdu_images_to_sol006_vdu(vdu, vdu_descriptor)

282 _add_im_vdu_flavor_to_sol006_vdu(vdu, vdu_descriptor)

283

284 vdu_int_cpds, vdu_ext_cpds = _get_int_and_ext_cpds_from_im_vdu(vdu, im_vnfd)

285 vdu_descriptor["int-cpd"] = vdu_int_cpds

286 ext_cpds.extend(vdu_ext_cpds)

287

288 vdu_profile = _get_vdu_profile_from_im_vdu(vdu, im_vnfd)

289 vdu_level = _get_instantiation_level_vdu_level_from_im_vdu(vdu)

290 if vdu.get("vdu-configuration"):

291 vdu_configuration = vdu["vdu-configuration"]

292 vdu_configuration["id"] = vdu["id"]

293 vdu_configurations.append(vdu_configuration)

294 df["vdu-profile"].append(vdu_profile)

295 df["instantiation-level"][0]["vdu-level"].append(vdu_level)

296

297 vdus.append(vdu_descriptor)

298

299 if len(vdus) > 0:

300 sol006_vnfd["vdu"] = vdus

301 sol006_vnfd["df"] = [df]

302 if len(ext_cpds) > 0:

303 sol006_vnfd["ext-cpd"] = ext_cpds

304 if len(vdu_configurations) > 0:

305 _prepare_dict_entries_for_configurations(sol006_vnfd)

306 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].extend(vdu_configurations)

307

308

309 def _add_im_vdu_images_to_sol006_vdu(im_vdu, sol006_vdu):

310 if im_vdu.get("image"):

311 sol006_vdu["sw-image-desc"] = im_vdu["image"]

312 alternative_images = []

313 for alternative_image in im_vdu.get("alternative-images", ()):

314 alternative_images.append(alternative_image.get("image"))

315 if len(alternative_images) > 0:

316 sol006_vdu["alternative-sw-image-desc"] = alternative_images

317

318

319 def _add_im_vdu_flavor_to_sol006_vdu(im_vdu, sol006_vdu):

320 vdu_flavor = im_vdu.get("vm-flavor")

321 if vdu_flavor:

322 if vdu_flavor.get("vcpu-count") or vdu_flavor.get("memory-mb"):

323 sol006_vdu["virtual-compute-desc"] = "{}-compute".format(im_vdu["id"])

324 if vdu_flavor.get("storage-gb"):

325 sol006_vdu["virtual-storage-desc"] = ["{}-storage".format(im_vdu["id"])]

326

327

328 def _get_int_and_ext_cpds_from_im_vdu(im_vdu, im_vnfd):

329 int_cpds = []

330 ext_cpds = []

331 for interface in im_vdu.get("interface", ()):

332 int_cpd = {"id": "{}-int".format(interface["name"])}

333 virtual_network_interface_requirement = {"name": interface["name"]}

334 if interface.get("virtual-interface"):

335 virtual_network_interface_requirement["virtual-interface"] = interface["virtual-interface"]

336 if "position" in interface:

337 virtual_network_interface_requirement["position"] = int(interface["position"])

338 int_cpd["virtual-network-interface-requirement"] = [virtual_network_interface_requirement]

339 if interface.get("external-connection-point-ref"):

340 ext_cpd = {

341 "id": "{}-ext".format(interface["external-connection-point-ref"]),

342 "int-cpd": {

343 "vdu-id": im_vdu["id"],

344 "cpd": int_cpd["id"]

345 }

346 }

347 for cp in im_vnfd.get("connection-point", ()):

348 if cp.get("name", "") != interface["external-connection-point-ref"]:

349 continue

350 if "port-security-enabled" in cp:

351 ext_cpd["port-security-enabled"] = cp["port-security-enabled"]

352 if cp.get("port-security-disable-strategy"):

353 ext_cpd["port-security-disable-strategy"] = cp["port-security-disable-strategy"]

354 ext_cpds.append(ext_cpd)

355

356 int_cpds.append(int_cpd)

357

358 return int_cpds, ext_cpds

359

360

361 def _get_vdu_profile_from_im_vdu(im_vdu, im_vnfd):

362 vdu_profile = {"id": im_vdu["id"]}

363 initial_instances = int(im_vdu.get("count", 1))

364 vdu_profile["min-number-of-instances"] = initial_instances

365 for scaling_group_descriptor in im_vnfd.get("scaling-group-descriptor", ()):

366 for sgd_vdu in scaling_group_descriptor.get("vdu", []):

367 if sgd_vdu.get("vdu-id-ref") == im_vdu["id"]:

368 sgd_max_instances = int(scaling_group_descriptor.get("max-instance-count", 1))

369 sgd_min_instances = int(scaling_group_descriptor.get("min-instance-count", 0))

370 vdu_profile["min-number-of-instances"] = sgd_min_instances + initial_instances

371 vdu_profile["max-number-of-instances"] = sgd_max_instances + initial_instances

372 return vdu_profile

373

374

375 def _get_instantiation_level_vdu_level_from_im_vdu(im_vdu):

376 vdu_level = {"vdu-id": im_vdu["id"]}

377 vdu_level["number-of-instances"] = int(im_vdu.get("count", 1))

378 return vdu_level

379

380

381 def _add_im_internal_vlds_to_sol006_vfnd(im_vnfd, sol006_vnfd):

382 int_virtual_link_descs = []

383 for ivld in im_vnfd.get("internal-vld", ()):

384 int_virtual_link_desc = {"id": ivld["id"]}

385 _add_im_internal_vld_connection_point_refs_to_sol006_vnfd(ivld, im_vnfd, sol006_vnfd)

386

387 int_virtual_link_descs.append(int_virtual_link_desc)

388

389 if len(int_virtual_link_descs) > 0:

390 sol006_vnfd["int-virtual-link-desc"] = int_virtual_link_descs

391

392

393 def _add_im_internal_vld_connection_point_refs_to_sol006_vnfd(ivld, im_vnfd, sol006_vnfd):

394 all_int_cp_refs_interfaces = {}

395 for vdu in im_vnfd.get("vdu", ()):

396 for interface in vdu.get("interface", ()):

397 int_cp_ref = interface.get("internal-connection-point-ref")

398 if not int_cp_ref:

399 continue

400 all_int_cp_refs_interfaces[int_cp_ref] = (vdu["id"], interface["name"])

401

402 for int_cp in ivld.get("internal-connection-point", ()):

403 int_cp_ref = int_cp["id-ref"]

404 (vdu_id, interface_name) = all_int_cp_refs_interfaces[int_cp_ref]

405 sol006_int_cpd_id = "{}-int".format(interface_name)

406 # Search for this int_cp on sol006_vnfd and update it

407 for vdu in sol006_vnfd.get("vdu", ()):

408 if vdu["id"] != vdu_id:

409 continue

410 for int_cpd in vdu.get("int-cpd", ()):

411 if int_cpd["id"] == sol006_int_cpd_id:

412 int_cpd["int-virtual-link-desc"] = ivld["id"]

413

414 def _prepare_dict_entries_for_configurations(sol006_vnfd):

415 sol006_vnfd["df"] = sol006_vnfd.get("df", [{}])

416 if not sol006_vnfd["df"][0].get("lcm-operations-configuration"):

417 sol006_vnfd["df"][0]["lcm-operations-configuration"] = {

418 "operate-vnf-op-config": {

419 "day1-2": []

420 }

421 }

422

423 def _cleanup_juju_in_configurations(sol006_vnfd):

424 configs = sol006_vnfd["df"][0].get("lcm-operations-configuration")

425 if configs:

426 for day12_config in configs["operate-vnf-op-config"]["day1-2"]:

427 if "juju" in day12_config:

428 ee_name = _create_execution_environment_for_juju(day12_config, day12_config["juju"]["charm"])

429 for primitive in day12_config.get("config-primitive", []):

430 primitive["execution-environment-ref"] = ee_name

431 for primitive in day12_config.get("initial-config-primitive", []):

432 primitive["execution-environment-ref"] = ee_name

433 for primitive in day12_config.get("terminate-config-primitive", []):

434 primitive["execution-environment-ref"] = ee_name

435 day12_config.pop("juju", None)

436

437 def _create_execution_environment_for_juju(day12_config, charm_name):

438 if not "execution-environment-list" in day12_config:

439 day12_config["execution-environment-list"] = []

440

441 day12_config["execution-environment-list"].append(

442 {

443 "id": charm_name + "-ee",

444 "juju": {

445 "charm": charm_name

446 }

447 }

448 )

449

450 return charm_name + "-ee"

451

452 def _add_im_vnf_configuration_to_sol006_vnfd(im_vnfd, sol006_vnfd):

453 vnf_configuration = im_vnfd.get("vnf-configuration")

454 if not vnf_configuration:

455 return

456 vnf_configuration["id"] = im_vnfd.get("id")

457 _prepare_dict_entries_for_configurations(sol006_vnfd)

458 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].append(vnf_configuration)

459

460

461 def _add_im_ip_profiles_to_sol006_vnfd(im_vnfd, sol006_vnfd):

462 all_ivlds_ip_profiles = {}

463 for ivld in im_vnfd.get("internal-vld", ()):

464 if ivld.get("ip-profile-ref"):

465 all_ivlds_ip_profiles[ivld["ip-profile-ref"]] = ivld["id"]

466

467 virtual_link_profiles = []

468 for ip_profile in im_vnfd.get("ip-profiles", ()):

469 virtual_link_profile = {"id": all_ivlds_ip_profiles[ip_profile["name"]], "flavour": ""}

470 ip_profile_params = ip_profile.get("ip-profile-params")

471 if ip_profile_params:

472 l3_protocol_data = {"name": "{}-l3-protocol-data".format(virtual_link_profile["id"])}

473 if ip_profile_params.get("ip-version"):

474 l3_protocol_data["ip-version"] = ip_profile_params["ip-version"]

475 if ip_profile_params.get("subnet-address"):

476 l3_protocol_data["cidr"] = ip_profile_params["subnet-address"]

477 if ip_profile_params.get("gateway-address"):

478 l3_protocol_data["gateway-ip"] = ip_profile_params["gateway-address"]

479 if ip_profile_params.get("security-group"):

480 l3_protocol_data["security-group"] = ip_profile_params["security-group"]

481 if ip_profile_params.get("dhcp-params", {}).get("enabled"):

482 l3_protocol_data["dhcp-enabled"] = ip_profile_params["dhcp-params"]["enabled"]

483 if ip_profile.get("description"):

484 l3_protocol_data["description"] = ip_profile["description"]

485

486 virtual_link_profile["virtual-link-protocol-data"] = {"l3-protocol-data": l3_protocol_data}

487

488 virtual_link_profiles.append(virtual_link_profile)

489

490 if len(virtual_link_profiles) > 0:

491 sol006_vnfd["df"][0]["virtual-link-profile"] = virtual_link_profiles

492

493

494 def _add_im_vdu_monitoring_params_to_sol006_vnfd(im_vnfd, sol006_vnfd):

495 all_vdu_monitoring_param_metrics = {}

496 for vdu in im_vnfd.get("vdu", ()):

497 for monitoring_param in vdu.get("monitoring-param", ()):

498 monitoring_param_metric = monitoring_param.get("nfvi-metric")

499 if monitoring_param_metric:

500 all_vdu_monitoring_param_metrics[(vdu["id"], monitoring_param["id"])] = monitoring_param_metric

501

502 for monitoring_param in im_vnfd.get("monitoring-param", ()):

503 sol006_mp = {"id": monitoring_param["id"]}

504 if monitoring_param.get("name"):

505 sol006_mp["name"] = monitoring_param["name"]

506 if monitoring_param.get("vdu-monitoring-param"):

507 monitoring_param_vdu_id = monitoring_param["vdu-monitoring-param"].get("vdu-ref")

508 monitoring_param_id = monitoring_param["vdu-monitoring-param"].get("vdu-monitoring-param-ref")

509 metric = all_vdu_monitoring_param_metrics.get((monitoring_param_vdu_id, monitoring_param_id))

510 if metric:

511 sol006_mp["performance-metric"] = metric

512 # Find that vdu inside sol006_vnfd and update its monitoring-parameter list

513 for vdu in sol006_vnfd.get("vdu", ()):

514 if vdu["id"] != monitoring_param_vdu_id:

515 continue

516 if vdu.get("monitoring-parameter"):

517 vdu["monitoring-parameter"].append(sol006_mp)

518 else:

519 vdu["monitoring-parameter"] = [sol006_mp]

520

521

522 def _add_im_scaling_group_descriptors_to_sol006_vnfd(im_vnfd, sol006_vnfd):

523 scaling_aspects = []

524 for scaling_group_descriptor in im_vnfd.get("scaling-group-descriptor", ()):

525 scaling_aspect = {"id": scaling_group_descriptor["name"], "name": scaling_group_descriptor["name"]}

526 if scaling_group_descriptor.get("max-instance-count"):

527 scaling_aspect["max-scale-level"] = int(scaling_group_descriptor["max-instance-count"])

528 if scaling_group_descriptor.get("scaling-policy"):

529 scaling_aspect["scaling-policy"] = scaling_group_descriptor["scaling-policy"]

530 if scaling_group_descriptor.get("scaling-config-action"):

531 scaling_aspect["scaling-config-action"] = scaling_group_descriptor["scaling-config-action"]

532

533 delta = {"id": "{}-delta".format(scaling_aspect["id"])}

534 vdu_deltas = []

535 for vdu in scaling_group_descriptor.get("vdu", ()):

536 vdu_delta = {}

537 if vdu.get("count"):

538 vdu_delta["number-of-instances"] = int(vdu["count"])

539 if vdu.get("vdu-id-ref"):

540 vdu_delta["id"] = vdu["vdu-id-ref"]

541 vdu_deltas.append(vdu_delta)

542 if len(vdu_deltas) > 0:

543 delta["vdu-delta"] = vdu_deltas

544 scaling_aspect["aspect-delta-details"] = {"deltas": [delta]}

545

546 scaling_aspects.append(scaling_aspect)

547

548 if len(scaling_aspects) > 0:

549 sol006_vnfd["df"][0]["scaling-aspect"] = scaling_aspects

550

551

552 def _add_im_kdus_to_sol006_vnfd(im_vnfd, sol006_vnfd):

553 if im_vnfd.get("kdu"):

554 sol006_vnfd["kdu"] = im_vnfd["kdu"]

555 kdu_configs = []

556 for a_kdu in sol006_vnfd["kdu"]:

557 if "kdu-configuration" in a_kdu:

558 kdu_config = a_kdu.pop("kdu-configuration")

559 kdu_config["id"] = a_kdu["name"]

560 kdu_configs.append(kdu_config)

561 if len(sol006_vnfd.get("df", ())) == 0:

562 sol006_vnfd["df"] = [{"id": "default-df"}]

563 if len(kdu_configs) > 0:

564 _prepare_dict_entries_for_configurations(sol006_vnfd)

565 sol006_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"].extend(kdu_configs)

566

567

568

569 def _add_im_k8s_clusters_to_sol006_vnfd(im_vnfd, sol006_vnfd):

570 im_k8s_cluster = im_vnfd.get("k8s-cluster")

571 if not im_k8s_cluster:

572 return

573

574 sol006_k8s_cluster = {}

575 if im_k8s_cluster.get("version"):

576 sol006_k8s_cluster["version"] = im_k8s_cluster["version"]

577 if im_k8s_cluster.get("cni"):

578 sol006_k8s_cluster["cni"] = im_k8s_cluster["cni"]

579

580 nets = []

581 k8s_cluster_ext_cpds = []

582 for net in im_k8s_cluster.get("nets", ()):

583 if net.get("external-connection-point-ref"):

584 ext_cpd = {"id": "{}-ext".format(net["external-connection-point-ref"]), "k8s-cluster-net": net["id"]}

585 k8s_cluster_ext_cpds.append(ext_cpd)

586 nets.append({"id": net["id"]})

587 if len(nets) > 0:

588 sol006_k8s_cluster["nets"] = nets

589

590 sol006_vnfd["k8s-cluster"] = sol006_k8s_cluster

591 if len(k8s_cluster_ext_cpds) > 0:

592 if not sol006_vnfd.get("ext-cpd"):

593 sol006_vnfd["ext-cpd"] = []

594 sol006_vnfd["ext-cpd"].extend(k8s_cluster_ext_cpds)

595

596

597 def _add_im_placement_groups_to_sol006_vnfd(im_vnfd, sol006_vnfd):

598 if im_vnfd.get("placement-groups"):

599 sol006_vnfd["placement-groups"] = im_vnfd["placement-groups"]

600

601

602# ******************** NSD translation private functions ********************

603

604 def _remove_im_nsd_envelope(im_nsd):

605 # Data is wrapped as { nsd-catalog: { nsd: [ <data> ] } }

606 return list(im_nsd.values())[0]["nsd"][0]

607

608

609 def _add_im_nsd_basic_data_to_sol006_nsd(im_nsd, sol006_nsd):

610 sol006_nsd["id"] = im_nsd["id"]

611 if im_nsd.get("name"):

612 sol006_nsd["name"] = im_nsd["name"]

613 if im_nsd.get("description"):

614 sol006_nsd["description"] = im_nsd["description"]

615 if im_nsd.get("vendor"):

616 sol006_nsd["designer"] = im_nsd["vendor"]

617 if im_nsd.get("version"):

618 sol006_nsd["version"] = im_nsd["version"]

619

620

621 def _add_im_constituent_vnfds_to_sol006_nsd(im_nsd, sol006_nsd):

622 vnfd_ids = set()

623 for constituent_vnfd in im_nsd.get("constituent-vnfd", ()):

624 if constituent_vnfd.get("vnfd-id-ref"):

625 vnfd_ids.add(constituent_vnfd["vnfd-id-ref"])

626

627 if len(vnfd_ids) > 0:

628 sol006_nsd["vnfd-id"] = list(vnfd_ids)

629

630

631 def _add_im_vlds_to_sol006_nsd(im_nsd, sol006_nsd):

632 vlds = im_nsd.get("vld", [])

633 flattened_vlds = [{**vld, **cp_ref} for vld in vlds for cp_ref in vld.get("vnfd-connection-point-ref", ())]

634 all_vlds_by_member_vnf_index = {}

635 for vld in flattened_vlds:

636 member_vnf_index = str(vld.get("member-vnf-index-ref", ""))

637 if member_vnf_index in all_vlds_by_member_vnf_index:

638 all_vlds_by_member_vnf_index[member_vnf_index].append(vld)

639 else:

640 all_vlds_by_member_vnf_index[member_vnf_index] = [vld]

641

642 df = {"id": "default-df", "vnf-profile": []}

643

644 for member_vnf_index in all_vlds_by_member_vnf_index:

645 vnf_profile = {"id": member_vnf_index}

646 vnf_profile["vnfd-id"] = all_vlds_by_member_vnf_index.get(member_vnf_index, [{}])[0].get("vnfd-id-ref")

647 vnf_profile["virtual-link-connectivity"] = []

648 for vld in all_vlds_by_member_vnf_index[member_vnf_index]:

649 virtual_link_connectivity = {"virtual-link-profile-id": vld["id"]}

650 virtual_link_connectivity["constituent-cpd-id"] = [{

651 "constituent-base-element-id": member_vnf_index,

652 "constituent-cpd-id": "{}-ext".format(vld.get("vnfd-connection-point-ref"))

653 }]

654 if vld.get("ip-address"):

655 virtual_link_connectivity["constituent-cpd-id"][0]["ip-address"] = vld["ip-address"]

656 vnf_profile["virtual-link-connectivity"].append(virtual_link_connectivity)

657

658 vlcs_by_virtual_link_profile = {}

659 for vlc in vnf_profile.get("virtual-link-connectivity"):

660 vl_profile_id = vlc.get("virtual-link-profile-id")

661 if vl_profile_id in vlcs_by_virtual_link_profile:

662 vlc_constituent_cpds = vlcs_by_virtual_link_profile[vl_profile_id].get("constituent-cpd-id")

663 vlc_constituent_cpds.extend(vlc.get("constituent-cpd-id"))

664 else:

665 vlcs_by_virtual_link_profile[vl_profile_id] = vlc

666

667 vnf_profile["virtual-link-connectivity"] = list(vlcs_by_virtual_link_profile.values())

668

669 df["vnf-profile"].append(vnf_profile)

670

671 sol006_nsd["df"] = [df]

672

673 virtual_link_descs = []

674 for vld in im_nsd.get("vld", ()):

675 virtual_link_desc = {"id": vld["id"]}

676 if vld.get("mgmt-network"):

677 virtual_link_desc["mgmt-network"] = vld["mgmt-network"]

678 if vld.get("vim-network-name"):

679 virtual_link_desc["vim-network-name"] = vld["vim-network-name"]

680 virtual_link_descs.append(virtual_link_desc)

681

682 if len(virtual_link_descs) > 0:

683 sol006_nsd["virtual-link-desc"] = virtual_link_descs