Bug 299 fixed
[osm/SO.git] / models / openmano / python / rift / openmano / rift2openmano.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import argparse
20 import collections
21 import logging
22 import math
23 import os
24 import sys
25 import tempfile
26 import yaml
27
28 import gi
29 gi.require_version('RwYang', '1.0')
30 gi.require_version('RwVnfdYang', '1.0')
31 gi.require_version('RwNsdYang', '1.0')
32
33 from gi.repository import (
34 RwYang,
35 RwVnfdYang,
36 RwNsdYang,
37 )
38
39 import rift.package.store
40 import rift.package.cloud_init
41
42 logger = logging.getLogger("rift2openmano.py")
43
44
45 class VNFNotFoundError(Exception):
46 pass
47
48
49 class RiftNSD(object):
50 model = RwYang.Model.create_libncx()
51 model.load_module('nsd')
52 model.load_module('rw-nsd')
53
54 def __init__(self, descriptor):
55 self._nsd = descriptor
56
57 def __str__(self):
58 return str(self._nsd)
59
60 @property
61 def name(self):
62 return self._nsd.name
63
64 @property
65 def id(self):
66 return self._nsd.id
67
68 @property
69 def vnfd_ids(self):
70 return [c.vnfd_id_ref for c in self._nsd.constituent_vnfd]
71
72 @property
73 def constituent_vnfds(self):
74 return self._nsd.constituent_vnfd
75
76 @property
77 def scaling_group_descriptor(self):
78 return self._nsd.scaling_group_descriptor
79
80 @property
81 def vlds(self):
82 return self._nsd.vld
83
84 @property
85 def cps(self):
86 return self._nsd.connection_point
87
88 @property
89 def description(self):
90 return self._nsd.description
91
92 @classmethod
93 def from_xml_file_hdl(cls, hdl):
94 hdl.seek(0)
95 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor.from_xml_v2(RiftNSD.model, hdl.read())
97 return cls(descriptor)
98
99 @classmethod
100 def from_yaml_file_hdl(cls, hdl):
101 hdl.seek(0)
102 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
103 descriptor.from_yaml(RiftNSD.model, hdl.read())
104 return cls(descriptor)
105
106 @classmethod
107 def from_dict(cls, nsd_dict):
108 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd.from_dict(nsd_dict)
109 return cls(descriptor)
110
111
112 class RiftVNFD(object):
113 model = RwYang.Model.create_libncx()
114 model.load_module('vnfd')
115 model.load_module('rw-vnfd')
116
117 def __init__(self, descriptor):
118 self._vnfd = descriptor
119
120 def __str__(self):
121 return str(self._vnfd)
122
123 @property
124 def id(self):
125 return self._vnfd.id
126
127 @property
128 def name(self):
129 return self._vnfd.name
130
131 @property
132 def description(self):
133 return self._vnfd.description
134
135 @property
136 def cps(self):
137 return self._vnfd.connection_point
138
139 @property
140 def vdus(self):
141 return self._vnfd.vdu
142
143 @property
144 def internal_vlds(self):
145 return self._vnfd.internal_vld
146
147 @classmethod
148 def from_xml_file_hdl(cls, hdl):
149 hdl.seek(0)
150 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor.from_xml_v2(RiftVNFD.model, hdl.read())
152 return cls(descriptor)
153
154 @classmethod
155 def from_yaml_file_hdl(cls, hdl):
156 hdl.seek(0)
157 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
158 descriptor.from_yaml(RiftVNFD.model, hdl.read())
159 return cls(descriptor)
160
161 @classmethod
162 def from_dict(cls, vnfd_dict):
163 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd.from_dict(vnfd_dict)
164 return cls(descriptor)
165
166
167 def is_writable_directory(dir_path):
168 """ Returns True if dir_path is writable, False otherwise
169
170 Arguments:
171 dir_path - A directory path
172 """
173 if not os.path.exists(dir_path):
174 raise ValueError("Directory does not exist: %s", dir_path)
175
176 try:
177 testfile = tempfile.TemporaryFile(dir=dir_path)
178 testfile.close()
179 except OSError:
180 return False
181
182 return True
183
184
185 def create_vnfd_from_files(vnfd_file_hdls):
186 """ Create a list of RiftVNFD instances from xml/yaml file handles
187
188 Arguments:
189 vnfd_file_hdls - Rift VNFD XML/YAML file handles
190
191 Returns:
192 A list of RiftVNFD instances
193 """
194 vnfd_dict = {}
195 for vnfd_file_hdl in vnfd_file_hdls:
196 if vnfd_file_hdl.name.endswith("yaml") or vnfd_file_hdl.name.endswith("yaml"):
197 vnfd = RiftVNFD.from_yaml_file_hdl(vnfd_file_hdl)
198 else:
199 vnfd = RiftVNFD.from_xml_file_hdl(vnfd_file_hdl)
200 vnfd_dict[vnfd.id] = vnfd
201
202 return vnfd_dict
203
204
205 def create_nsd_from_file(nsd_file_hdl):
206 """ Create a list of RiftNSD instances from yaml/xml file handles
207
208 Arguments:
209 nsd_file_hdls - Rift NSD XML/yaml file handles
210
211 Returns:
212 A list of RiftNSD instances
213 """
214 if nsd_file_hdl.name.endswith("yaml") or nsd_file_hdl.name.endswith("yaml"):
215 nsd = RiftNSD.from_yaml_file_hdl(nsd_file_hdl)
216 else:
217 nsd = RiftNSD.from_xml_file_hdl(nsd_file_hdl)
218 return nsd
219
220
221 def ddict():
222 return collections.defaultdict(dict)
223
224 def convert_vnfd_name(vnfd_name, member_idx):
225 return vnfd_name + "__" + str(member_idx)
226
227
228 def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids, rift_vnfd_id=None):
229 if rift_vnfd_id is None:
230 for vnfd_id in rift_nsd.vnfd_ids:
231 if vnfd_id not in rift_vnfds:
232 raise VNFNotFoundError("VNF id %s not provided" % vnfd_id)
233
234 openmano = {}
235 openmano["name"] = rift_nsd.name
236 if rift_vnfd_id is not None:
237 for scaling_groups in rift_nsd.scaling_group_descriptor:
238 openmano["name"] += scaling_groups.name
239 openmano["description"] = rift_nsd.description
240 topology = {}
241 openmano["topology"] = topology
242
243 topology["nodes"] = {}
244 for vnfd in rift_nsd.constituent_vnfds:
245 vnfd_id = vnfd.vnfd_id_ref
246 if rift_vnfd_id is not None and rift_vnfd_id != vnfd_id:
247 continue
248 rift_vnfd = rift_vnfds[vnfd_id]
249 member_idx = vnfd.member_vnf_index
250 openmano_vnfd_id = openmano_vnfd_ids.get(vnfd_id,None)
251 if openmano_vnfd_id:
252 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
253 "type": "VNF",
254 "vnf_id": openmano_vnfd_id
255 }
256 else:
257 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
258 "type": "VNF",
259 "VNF model": rift_vnfd.name
260 }
261
262 for vld in rift_nsd.vlds:
263 # Openmano has both bridge_net and dataplane_net models for network types
264 # For now, since we are using openmano in developer mode lets just hardcode
265 # to bridge_net since it won't matter anyways.
266 # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"}
267 pass
268
269 topology["connections"] = {}
270 for vld in rift_nsd.vlds:
271
272 # Create a connections entry for each external VLD
273 topology["connections"][vld.name] = {}
274 topology["connections"][vld.name]["nodes"] = []
275
276 #if vld.vim_network_name:
277 if True:
278 if vld.name not in topology["nodes"]:
279 topology["nodes"][vld.name] = {
280 "type": "external_network",
281 "model": vld.name,
282 }
283
284 # Add the external network to the list of connection points
285 topology["connections"][vld.name]["nodes"].append(
286 {vld.name: "0"}
287 )
288 elif vld.provider_network.has_field("physical_network"):
289 # Add the external datacenter network to the topology
290 # node list if it isn't already added
291 ext_net_name = vld.provider_network.physical_network
292 ext_net_name_with_seg = ext_net_name
293 if vld.provider_network.has_field("segmentation_id"):
294 ext_net_name_with_seg += ":{}".format(vld.provider_network.segmentation_id)
295
296 if ext_net_name not in topology["nodes"]:
297 topology["nodes"][ext_net_name] = {
298 "type": "external_network",
299 "model": ext_net_name_with_seg,
300 }
301
302 # Add the external network to the list of connection points
303 topology["connections"][vld.name]["nodes"].append(
304 {ext_net_name: "0"}
305 )
306
307
308 for vnfd_cp in vld.vnfd_connection_point_ref:
309
310 # Get the RIFT VNF for this external VLD connection point
311 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
312
313 # For each VNF in this connection, use the same interface name
314 topology["connections"][vld.name]["type"] = "link"
315 # Vnf ref is the vnf name with the member_vnf_idx appended
316 member_idx = vnfd_cp.member_vnf_index_ref
317 vnf_ref = vnfd.name + "__" + str(member_idx)
318 topology["connections"][vld.name]["nodes"].append(
319 {
320 vnf_ref: vnfd_cp.vnfd_connection_point_ref
321 }
322 )
323 return openmano
324
325 def rift2openmano_vnfd_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids,rift_vnfd_id=None):
326
327 if rift_vnfd_id not in rift_vnfds:
328 raise VNFNotFoundError("VNF id %s not provided" % rift_vnfd_id)
329
330 openmano_vnfd_nsd = {}
331 for groups in rift_nsd.scaling_group_descriptor:
332 openmano_vnfd_nsd["name"] = rift_vnfd_id+'__'+'scaling_group'+'__'+groups.name
333 openmano_vnfd_nsd["description"] = "Scaling Group"
334 topology = {}
335 openmano_vnfd_nsd["topology"] = topology
336 topology["connections"] = {}
337 topology["nodes"] = {}
338 tst_index = []
339 openmano_vnfd_id = openmano_vnfd_ids.get(rift_vnfd_id,None)
340 for rvnfd_id in rift_nsd.constituent_vnfds:
341 if rvnfd_id.vnfd_id_ref == rift_vnfd_id:
342 rift_vnfd = rift_vnfds[rift_vnfd_id]
343 topology["nodes"][rift_vnfd.name +'__'+str(rvnfd_id.member_vnf_index)] = {
344 "type": "VNF",
345 "vnf_id": openmano_vnfd_id
346 }
347
348 for vld in rift_nsd.vlds:
349
350 # Create a connections entry for each external VLD
351 topology["connections"][vld.name] = {}
352 topology["connections"][vld.name]["nodes"] = []
353 if True:
354 if vld.name not in topology["nodes"]:
355 topology["nodes"][vld.name] = {
356 "type": "external_network",
357 "model": vld.name,
358 }
359 topology["connections"][vld.name]["nodes"].append(
360 {vld.name: "0"}
361 )
362
363
364
365 for vnfd_cp in vld.vnfd_connection_point_ref:
366 if not rift_vnfd_id in vnfd_cp.vnfd_id_ref:
367 continue
368 if rift_vnfd_id in vnfd_cp.vnfd_id_ref:
369
370 # Get the RIFT VNF for this external VLD connection point
371 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
372
373
374 # For each VNF in this connection, use the same interface name
375 topology["connections"][vld.name]["type"] = "link"
376 # Vnf ref is the vnf name with the member_vnf_idx appended
377 member_idx = vnfd_cp.member_vnf_index_ref
378 vnf_ref = vnfd.name + "__" + str(member_idx)
379 topology["connections"][vld.name]["nodes"].append(
380 {
381 vnf_ref: vnfd_cp.vnfd_connection_point_ref
382 }
383 )
384 return openmano_vnfd_nsd
385
386
387 def cloud_init(rift_vnfd_id, vdu):
388 """ Populate cloud_init with script from
389 either the inline contents or from the file provided
390 """
391 vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger)
392
393 cloud_init_msg = None
394 if vdu.cloud_init is not None:
395 logger.debug("cloud_init script provided inline %s", vdu.cloud_init)
396 cloud_init_msg = vdu.cloud_init
397 elif vdu.cloud_init_file is not None:
398 # Get cloud-init script contents from the file provided in the cloud_init_file param
399 logger.debug("cloud_init script provided in file %s", vdu.cloud_init_file)
400 filename = vdu.cloud_init_file
401 vnfd_package_store.refresh()
402 stored_package = vnfd_package_store.get_package(rift_vnfd_id)
403 cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger)
404 try:
405 cloud_init_msg = cloud_init_extractor.read_script(stored_package, filename)
406 except rift.package.cloud_init.CloudInitExtractionError as e:
407 raise ValueError(e)
408 else:
409 logger.debug("VDU translation: cloud-init script not provided")
410 return
411
412 logger.debug("Current cloud init msg is {}".format(cloud_init_msg))
413 return cloud_init_msg
414
415 def config_file_init(rift_vnfd_id, vdu, cfg_file):
416 """ Populate config file init with file provided
417 """
418 vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger)
419
420 # Get script contents from the file provided in the cloud_init directory
421 logger.debug("config file script provided in file {}".format(cfg_file))
422 filename = cfg_file
423 vnfd_package_store.refresh()
424 stored_package = vnfd_package_store.get_package(rift_vnfd_id)
425 cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger)
426 try:
427 cfg_file_msg = cloud_init_extractor.read_script(stored_package, filename)
428 except rift.package.cloud_init.CloudInitExtractionError as e:
429 raise ValueError(e)
430
431 logger.debug("Current config file msg is {}".format(cfg_file_msg))
432 return cfg_file_msg
433
434 def rift2openmano_vnfd(rift_vnfd, rift_nsd):
435 openmano_vnf = {"vnf":{}}
436 vnf = openmano_vnf["vnf"]
437
438 vnf["name"] = rift_vnfd.name
439 vnf["description"] = rift_vnfd.description
440
441 vnf["external-connections"] = []
442
443 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name):
444 for vdu in rift_vnfd.vdus:
445 for ext_if in vdu.external_interface:
446 if ext_if.vnfd_connection_point_ref == cp_ref_name:
447 return vdu, ext_if
448
449 raise ValueError("External connection point reference %s not found" % cp_ref_name)
450
451 def find_vdu_and_int_if_by_cp_ref(cp_ref_id):
452 for vdu in rift_vnfd.vdus:
453 for int_if in vdu.internal_interface:
454 if int_if.vdu_internal_connection_point_ref == cp_ref_id:
455 return vdu, int_if
456
457 raise ValueError("Internal connection point reference %s not found" % cp_ref_id)
458
459 def rift2openmano_if_type(ext_if):
460
461 cp_ref_name = ext_if.vnfd_connection_point_ref
462 for vld in rift_nsd.vlds:
463
464 # if it is an explicit mgmt_network then check if the given
465 # cp_ref is a part of it
466 if not vld.mgmt_network:
467 continue
468
469 for vld_cp in vld.vnfd_connection_point_ref:
470 if vld_cp.vnfd_connection_point_ref == cp_ref_name:
471 return "mgmt"
472
473
474 rift_type = ext_if.virtual_interface.type_yang
475 # Retaining it for backward compatibility!
476 if rift_type == "OM_MGMT":
477 return "mgmt"
478 elif rift_type == "VIRTIO" or rift_type == "E1000":
479 return "bridge"
480 else:
481 return "data"
482
483 def rift2openmano_vif(rift_type):
484 if rift_type == "VIRTIO":
485 return "virtio"
486 elif rift_type == "E1000":
487 return "e1000"
488 else:
489 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type))
490
491 # Add all external connections
492 cp_to_port_security_map = {}
493
494 for cp in rift_vnfd.cps:
495 # Find the VDU and and external interface for this connection point
496 vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name)
497 connection = {
498 "name": cp.name,
499 "type": rift2openmano_if_type(ext_if),
500 "VNFC": vdu.name,
501 "local_iface_name": ext_if.name,
502 "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
503 }
504
505 if cp.has_field('port_security_enabled'):
506 cp_to_port_security_map[cp.name] = cp.port_security_enabled
507 vnf["external-connections"].append(connection)
508
509 # Add all internal networks
510 for vld in rift_vnfd.internal_vlds:
511 connection = {
512 "name": vld.name,
513 "description": vld.description,
514 "type": "bridge",
515 "elements": [],
516 }
517
518 # Add the specific VDU connection points
519 for int_cp in vld.internal_connection_point:
520 vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref)
521 connection["elements"].append({
522 "VNFC": vdu.name,
523 "local_iface_name": int_if.name,
524 })
525 if "internal-connections" not in vnf:
526 vnf["internal-connections"] = []
527
528 vnf["internal-connections"].append(connection)
529
530 # Add VDU's
531 vnf["VNFC"] = []
532 for vdu in rift_vnfd.vdus:
533 vnfc = {
534 "name": vdu.name,
535 "description": vdu.name,
536 "bridge-ifaces": [],
537 }
538
539 if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
540 vnfc["disk"] = vdu.vm_flavor.storage_gb
541
542 if vdu.has_field("image"):
543 if os.path.isabs(vdu.image):
544 vnfc["VNFC image"] = vdu.image
545 else:
546 vnfc["image name"] = vdu.image
547 if vdu.has_field("image_checksum"):
548 vnfc["image checksum"] = vdu.image_checksum
549
550 dedicated_int = False
551 for intf in list(vdu.internal_interface) + list(vdu.external_interface):
552 if intf.virtual_interface.type_yang in ["SR_IOV", "PCI_PASSTHROUGH"]:
553 dedicated_int = True
554 if vdu.guest_epa.has_field("numa_node_policy") or dedicated_int:
555 vnfc["numas"] = [{
556 "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
557 "interfaces":[],
558 }]
559 numa_node_policy = vdu.guest_epa.numa_node_policy
560 if numa_node_policy.has_field("node"):
561 numa_node = numa_node_policy.node[0]
562
563 if numa_node.has_field("num_cores"):
564 vnfc["numas"][0]["cores"] = numa_node.num_cores
565
566 if numa_node.has_field("paired_threads"):
567 if numa_node.paired_threads.has_field("num_paired_threads"):
568 vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
569 if len(numa_node.paired_threads.paired_thread_ids) > 0:
570 vnfc["numas"][0]["paired-threads-id"] = []
571 for pair in numa_node.paired_threads.paired_thread_ids:
572 vnfc["numas"][0]["paired-threads-id"].append(
573 [pair.thread_a, pair.thread_b]
574 )
575
576 if numa_node.has_field("num_threads"):
577 vnfc["numas"][0]["threads"] = numa_node.num_threads
578 else:
579 if vdu.vm_flavor.has_field("vcpu_count"):
580 vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
581
582 if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count:
583 vnfc["vcpus"] = vdu.vm_flavor.vcpu_count
584
585 if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb:
586 vnfc["ram"] = vdu.vm_flavor.memory_mb
587
588
589 if vdu.has_field("hypervisor_epa"):
590 vnfc["hypervisor"] = {}
591 if vdu.hypervisor_epa.has_field("type"):
592 if vdu.hypervisor_epa.type_yang == "REQUIRE_KVM":
593 vnfc["hypervisor"]["type"] = "QEMU-kvm"
594
595 if vdu.hypervisor_epa.has_field("version"):
596 vnfc["hypervisor"]["version"] = vdu.hypervisor_epa.version
597
598 if vdu.has_field("host_epa"):
599 vnfc["processor"] = {}
600 if vdu.host_epa.has_field("om_cpu_model_string"):
601 vnfc["processor"]["model"] = vdu.host_epa.om_cpu_model_string
602 if vdu.host_epa.has_field("om_cpu_feature"):
603 vnfc["processor"]["features"] = []
604 for feature in vdu.host_epa.om_cpu_feature:
605 vnfc["processor"]["features"].append(feature.feature)
606
607 if vdu.has_field("volumes"):
608 vnfc["devices"] = []
609 # Sort volumes as device-list is implictly ordered by Openmano
610 newvollist = sorted(vdu.volumes, key=lambda k: k.name)
611 for iter_num, volume in enumerate(newvollist):
612 if iter_num == 0:
613 # Convert the first volume to vnfc.image
614 if os.path.isabs(volume.image):
615 vnfc["VNFC image"] = volume.image
616 else:
617 vnfc["image name"] = volume.image
618 if volume.has_field("image_checksum"):
619 vnfc["image checksum"] = volume.image_checksum
620 else:
621 # Add Openmano devices
622 device = {}
623 device["type"] = volume.device_type
624 if volume.has_field("size"):
625 device["size"] = volume.size
626 if volume.has_field("image"):
627 device["image name"] = volume.image
628 if volume.has_field("image_checksum"):
629 device["image checksum"] = volume.image_checksum
630 vnfc["devices"].append(device)
631
632 vnfc_boot_data_init = False
633 if vdu.has_field("cloud_init") or vdu.has_field("cloud_init_file"):
634 vnfc['boot-data'] = dict()
635 vnfc_boot_data_init = True
636 vnfc['boot-data']['user-data'] = cloud_init(rift_vnfd.id, vdu)
637
638 if vdu.has_field("supplemental_boot_data"):
639 if vdu.supplemental_boot_data.has_field('boot_data_drive'):
640 if vdu.supplemental_boot_data.boot_data_drive is True:
641 if vnfc_boot_data_init is False:
642 vnfc['boot-data'] = dict()
643 vnfc_boot_data_init = True
644 vnfc['boot-data']['boot-data-drive'] = vdu.supplemental_boot_data.boot_data_drive
645
646 if vdu.supplemental_boot_data.has_field('config_file'):
647 om_cfgfile_list = list()
648 for custom_config_file in vdu.supplemental_boot_data.config_file:
649 cfg_source = config_file_init(rift_vnfd.id, vdu, custom_config_file.source)
650 om_cfgfile_list.append({"dest":custom_config_file.dest, "content": cfg_source})
651 vnfc['boot-data']['config-files'] = om_cfgfile_list
652
653
654 vnf["VNFC"].append(vnfc)
655
656 for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
657 intf = {
658 "name": int_if.name,
659 }
660 if int_if.virtual_interface.has_field("vpci"):
661 intf["vpci"] = int_if.virtual_interface.vpci
662
663 if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]:
664 intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang)
665 vnfc["bridge-ifaces"].append(intf)
666
667 elif int_if.virtual_interface.type_yang in ["OM_MGMT"]:
668 vnfc["bridge-ifaces"].append(intf)
669
670 elif int_if.virtual_interface.type_yang == "SR_IOV":
671 intf["bandwidth"] = "10 Gbps"
672 intf["dedicated"] = "no"
673 vnfc["numas"][0]["interfaces"].append(intf)
674
675 elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH":
676 intf["bandwidth"] = "10 Gbps"
677 intf["dedicated"] = "yes"
678 if "interfaces" not in vnfc["numas"][0]:
679 vnfc["numas"][0]["interfaces"] = []
680 vnfc["numas"][0]["interfaces"].append(intf)
681 else:
682 raise ValueError("Interface type %s not supported" % int_if.virtual_interface)
683
684 if int_if.virtual_interface.has_field("bandwidth"):
685 if int_if.virtual_interface.bandwidth != 0:
686 bps = int_if.virtual_interface.bandwidth
687
688 # Calculate the bits per second conversion
689 for x in [('M', 1000000), ('G', 1000000000)]:
690 if bps/x[1] >= 1:
691 intf["bandwidth"] = "{} {}bps".format(math.ceil(bps/x[1]), x[0])
692
693 for bridge_iface in vnfc["bridge-ifaces"]:
694 if bridge_iface['name'] in cp_to_port_security_map:
695 bridge_iface['port-security'] = cp_to_port_security_map[bridge_iface['name']]
696 # Sort bridge-ifaces-list TODO sort others
697 newlist = sorted(vnfc["bridge-ifaces"], key=lambda k: k['name'])
698 vnfc["bridge-ifaces"] = newlist
699
700 return openmano_vnf
701
702
703 def parse_args(argv=sys.argv[1:]):
704 """ Parse the command line arguments
705
706 Arguments:
707 arv - The list of arguments to parse
708
709 Returns:
710 Argparse Namespace instance
711 """
712 parser = argparse.ArgumentParser()
713 parser.add_argument(
714 '-o', '--outdir',
715 default='-',
716 help="Directory to output converted descriptors. Default is stdout",
717 )
718
719 parser.add_argument(
720 '-n', '--nsd-file-hdl',
721 metavar="nsd_file",
722 type=argparse.FileType('r'),
723 help="Rift NSD Descriptor File",
724 )
725
726 parser.add_argument(
727 '-v', '--vnfd-file-hdls',
728 metavar="vnfd_file",
729 action='append',
730 type=argparse.FileType('r'),
731 help="Rift VNFD Descriptor File",
732 )
733
734 args = parser.parse_args(argv)
735
736 if not os.path.exists(args.outdir):
737 os.makedirs(args.outdir)
738
739 if not is_writable_directory(args.outdir):
740 logging.error("Directory %s is not writable", args.outdir)
741 sys.exit(1)
742
743 return args
744
745
746 def write_yaml_to_file(name, outdir, desc_dict):
747 file_name = "%s.yaml" % name
748 yaml_str = yaml.dump(desc_dict)
749 if outdir == "-":
750 sys.stdout.write(yaml_str)
751 return
752
753 file_path = os.path.join(outdir, file_name)
754 dir_path = os.path.dirname(file_path)
755 if not os.path.exists(dir_path):
756 os.makedirs(dir_path)
757
758 with open(file_path, "w") as hdl:
759 hdl.write(yaml_str)
760
761 logger.info("Wrote descriptor to %s", file_path)
762
763
764 def main(argv=sys.argv[1:]):
765 args = parse_args(argv)
766 nsd = None
767 openmano_vnfr_ids = dict()
768 vnf_dict = None
769 if args.vnfd_file_hdls is not None:
770 vnf_dict = create_vnfd_from_files(args.vnfd_file_hdls)
771
772 for vnfd in vnf_dict:
773 openmano_vnfr_ids[vnfd] = vnfd
774
775 if args.nsd_file_hdl is not None:
776 nsd = create_nsd_from_file(args.nsd_file_hdl)
777
778 openmano_nsd = rift2openmano_nsd(nsd, vnf_dict, openmano_vnfr_ids)
779 vnfd_nsd = rift2openmano_vnfd_nsd(nsd, vnf_dict, openmano_vnfr_ids)
780 write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd)
781 write_yaml_to_file(vnfd_nsd["name"], args.outdir, vnfd_nsd)
782 for vnf in vnf_dict.values():
783 openmano_vnf = rift2openmano_vnfd(vnf, nsd)
784 write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf)
785
786
787 if __name__ == "__main__":
788 logging.basicConfig(level=logging.WARNING)
789 main()