4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
29 gi
.require_version('RwYang', '1.0')
30 gi
.require_version('RwVnfdYang', '1.0')
31 gi
.require_version('RwNsdYang', '1.0')
33 from gi
.repository
import (
39 logger
= logging
.getLogger("rift2openmano.py")
42 class VNFNotFoundError(Exception):
46 class RiftNSD(object):
47 model
= RwYang
.Model
.create_libncx()
48 model
.load_module('nsd')
49 model
.load_module('rw-nsd')
51 def __init__(self
, descriptor
):
52 self
._nsd
= descriptor
67 return [c
.vnfd_id_ref
for c
in self
._nsd
.constituent_vnfd
]
70 def constituent_vnfds(self
):
71 return self
._nsd
.constituent_vnfd
79 return self
._nsd
.connection_point
82 def description(self
):
83 return self
._nsd
.description
86 def from_xml_file_hdl(cls
, hdl
):
88 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
89 descriptor
.from_xml_v2(RiftNSD
.model
, hdl
.read())
90 return cls(descriptor
)
93 def from_yaml_file_hdl(cls
, hdl
):
95 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor
.from_yaml(RiftNSD
.model
, hdl
.read())
97 return cls(descriptor
)
100 def from_dict(cls
, nsd_dict
):
101 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
.from_dict(nsd_dict
)
102 return cls(descriptor
)
105 class RiftVNFD(object):
106 model
= RwYang
.Model
.create_libncx()
107 model
.load_module('vnfd')
108 model
.load_module('rw-vnfd')
110 def __init__(self
, descriptor
):
111 self
._vnfd
= descriptor
114 return str(self
._vnfd
)
122 return self
._vnfd
.name
125 def description(self
):
126 return self
._vnfd
.description
130 return self
._vnfd
.connection_point
134 return self
._vnfd
.vdu
137 def internal_vlds(self
):
138 return self
._vnfd
.internal_vld
141 def from_xml_file_hdl(cls
, hdl
):
143 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
144 descriptor
.from_xml_v2(RiftVNFD
.model
, hdl
.read())
145 return cls(descriptor
)
148 def from_yaml_file_hdl(cls
, hdl
):
150 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor
.from_yaml(RiftVNFD
.model
, hdl
.read())
152 return cls(descriptor
)
155 def from_dict(cls
, vnfd_dict
):
156 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
.from_dict(vnfd_dict
)
157 return cls(descriptor
)
160 def is_writable_directory(dir_path
):
161 """ Returns True if dir_path is writable, False otherwise
164 dir_path - A directory path
166 if not os
.path
.exists(dir_path
):
167 raise ValueError("Directory does not exist: %s", dir_path
)
170 testfile
= tempfile
.TemporaryFile(dir=dir_path
)
178 def create_vnfd_from_xml_files(vnfd_file_hdls
):
179 """ Create a list of RiftVNFD instances from xml file handles
182 vnfd_file_hdls - Rift VNFD XML file handles
185 A list of RiftVNFD instances
188 for vnfd_file_hdl
in vnfd_file_hdls
:
189 vnfd
= RiftVNFD
.from_xml_file_hdl(vnfd_file_hdl
)
190 vnfd_dict
[vnfd
.id] = vnfd
194 def create_vnfd_from_yaml_files(vnfd_file_hdls
):
195 """ Create a list of RiftVNFD instances from xml file handles
198 vnfd_file_hdls - Rift VNFD YAML file handles
201 A list of RiftVNFD instances
204 for vnfd_file_hdl
in vnfd_file_hdls
:
205 vnfd
= RiftVNFD
.from_yaml_file_hdl(vnfd_file_hdl
)
206 vnfd_dict
[vnfd
.id] = vnfd
211 def create_nsd_from_xml_file(nsd_file_hdl
):
212 """ Create a list of RiftNSD instances from xml file handles
215 nsd_file_hdls - Rift NSD XML file handles
218 A list of RiftNSD instances
220 nsd
= RiftNSD
.from_xml_file_hdl(nsd_file_hdl
)
223 def create_nsd_from_yaml_file(nsd_file_hdl
):
224 """ Create a list of RiftNSD instances from yaml file handles
227 nsd_file_hdls - Rift NSD XML file handles
230 A list of RiftNSD instances
232 nsd
= RiftNSD
.from_yaml_file_hdl(nsd_file_hdl
)
237 return collections
.defaultdict(dict)
239 def convert_vnfd_name(vnfd_name
, member_idx
):
240 return vnfd_name
+ "__" + str(member_idx
)
243 def rift2openmano_nsd(rift_nsd
, rift_vnfds
,openmano_vnfd_ids
):
244 for vnfd_id
in rift_nsd
.vnfd_ids
:
245 if vnfd_id
not in rift_vnfds
:
246 raise VNFNotFoundError("VNF id %s not provided" % vnfd_id
)
249 openmano
["name"] = rift_nsd
.name
250 openmano
["description"] = rift_nsd
.description
252 openmano
["topology"] = topology
254 topology
["nodes"] = {}
255 for vnfd
in rift_nsd
.constituent_vnfds
:
256 vnfd_id
= vnfd
.vnfd_id_ref
257 rift_vnfd
= rift_vnfds
[vnfd_id
]
258 member_idx
= vnfd
.member_vnf_index
259 openmano_vnfd_id
= openmano_vnfd_ids
.get(vnfd_id
,None)
261 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
263 "vnf_id": openmano_vnfd_id
266 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
268 "VNF model": rift_vnfd
.name
271 for vld
in rift_nsd
.vlds
:
272 # Openmano has both bridge_net and dataplane_net models for network types
273 # For now, since we are using openmano in developer mode lets just hardcode
274 # to bridge_net since it won't matter anyways.
275 # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"}
278 topology
["connections"] = {}
279 for vld
in rift_nsd
.vlds
:
281 # Create a connections entry for each external VLD
282 topology
["connections"][vld
.name
] = {}
283 topology
["connections"][vld
.name
]["nodes"] = []
285 #if vld.vim_network_name:
287 if vld
.name
not in topology
["nodes"]:
288 topology
["nodes"][vld
.name
] = {
289 "type": "external_network",
293 # Add the external network to the list of connection points
294 topology
["connections"][vld
.name
]["nodes"].append(
297 elif vld
.provider_network
.has_field("physical_network"):
298 # Add the external datacenter network to the topology
299 # node list if it isn't already added
300 ext_net_name
= vld
.provider_network
.physical_network
301 ext_net_name_with_seg
= ext_net_name
302 if vld
.provider_network
.has_field("segmentation_id"):
303 ext_net_name_with_seg
+= ":{}".format(vld
.provider_network
.segmentation_id
)
305 if ext_net_name
not in topology
["nodes"]:
306 topology
["nodes"][ext_net_name
] = {
307 "type": "external_network",
308 "model": ext_net_name_with_seg
,
311 # Add the external network to the list of connection points
312 topology
["connections"][vld
.name
]["nodes"].append(
317 for vnfd_cp
in vld
.vnfd_connection_point_ref
:
319 # Get the RIFT VNF for this external VLD connection point
320 vnfd
= rift_vnfds
[vnfd_cp
.vnfd_id_ref
]
322 # For each VNF in this connection, use the same interface name
323 topology
["connections"][vld
.name
]["type"] = "link"
324 # Vnf ref is the vnf name with the member_vnf_idx appended
325 member_idx
= vnfd_cp
.member_vnf_index_ref
326 vnf_ref
= vnfd
.name
+ "__" + str(member_idx
)
327 topology
["connections"][vld
.name
]["nodes"].append(
329 vnf_ref
: vnfd_cp
.vnfd_connection_point_ref
336 def rift2openmano_vnfd(rift_vnfd
):
337 openmano_vnf
= {"vnf":{}}
338 vnf
= openmano_vnf
["vnf"]
340 vnf
["name"] = rift_vnfd
.name
341 vnf
["description"] = rift_vnfd
.description
343 vnf
["external-connections"] = []
345 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name
):
346 for vdu
in rift_vnfd
.vdus
:
347 for ext_if
in vdu
.external_interface
:
348 if ext_if
.vnfd_connection_point_ref
== cp_ref_name
:
351 raise ValueError("External connection point reference %s not found" % cp_ref_name
)
353 def find_vdu_and_int_if_by_cp_ref(cp_ref_id
):
354 for vdu
in rift_vnfd
.vdus
:
355 for int_if
in vdu
.internal_interface
:
356 if int_if
.vdu_internal_connection_point_ref
== cp_ref_id
:
359 raise ValueError("Internal connection point reference %s not found" % cp_ref_id
)
361 def rift2openmano_if_type(rift_type
):
362 if rift_type
== "OM_MGMT":
364 elif rift_type
== "VIRTIO" or rift_type
== "E1000":
369 def rift2openmano_vif(rift_type
):
370 if rift_type
== "VIRTIO":
372 elif rift_type
== "E1000":
375 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type
))
377 # Add all external connections
378 for cp
in rift_vnfd
.cps
:
379 # Find the VDU and and external interface for this connection point
380 vdu
, ext_if
= find_vdu_and_ext_if_by_cp_ref(cp
.name
)
383 "type": rift2openmano_if_type(ext_if
.virtual_interface
.type_yang
),
385 "local_iface_name": ext_if
.name
,
386 "description": "%s iface on VDU %s" % (ext_if
.name
, vdu
.name
),
389 vnf
["external-connections"].append(connection
)
391 # Add all internal networks
392 for vld
in rift_vnfd
.internal_vlds
:
395 "description": vld
.description
,
400 # Add the specific VDU connection points
401 for int_cp
in vld
.internal_connection_point
:
402 vdu
, int_if
= find_vdu_and_int_if_by_cp_ref(int_cp
.id_ref
)
403 connection
["elements"].append({
405 "local_iface_name": int_if
.name
,
407 if "internal-connections" not in vnf
:
408 vnf
["internal-connections"] = []
410 vnf
["internal-connections"].append(connection
)
414 for vdu
in rift_vnfd
.vdus
:
417 "description": vdu
.name
,
421 if vdu
.vm_flavor
.has_field("storage_gb") and vdu
.vm_flavor
.storage_gb
:
422 vnfc
["disk"] = vdu
.vm_flavor
.storage_gb
424 if os
.path
.isabs(vdu
.image
):
425 vnfc
["VNFC image"] = vdu
.image
427 vnfc
["image name"] = vdu
.image
428 if vdu
.has_field("image_checksum"):
429 vnfc
["image checksum"] = vdu
.image_checksum
431 if vdu
.guest_epa
.has_field("numa_node_policy"):
433 "memory": max(int(vdu
.vm_flavor
.memory_mb
/1024), 1),
436 numa_node_policy
= vdu
.guest_epa
.numa_node_policy
437 if numa_node_policy
.has_field("node"):
438 numa_node
= numa_node_policy
.node
[0]
440 if numa_node
.has_field("paired_threads"):
441 if numa_node
.paired_threads
.has_field("num_paired_threads"):
442 vnfc
["numas"][0]["paired-threads"] = numa_node
.paired_threads
.num_paired_threads
443 if len(numa_node
.paired_threads
.paired_thread_ids
) > 0:
444 vnfc
["numas"][0]["paired-threads-id"] = []
445 for pair
in numa_node
.paired_threads
.paired_thread_ids
:
446 vnfc
["numas"][0]["paired-threads-id"].append(
447 [pair
.thread_a
, pair
.thread_b
]
451 if vdu
.vm_flavor
.has_field("vcpu_count"):
452 vnfc
["numas"][0]["cores"] = max(vdu
.vm_flavor
.vcpu_count
, 1)
455 if vdu
.vm_flavor
.has_field("vcpu_count") and vdu
.vm_flavor
.vcpu_count
:
456 vnfc
["vcpus"] = vdu
.vm_flavor
.vcpu_count
458 if vdu
.vm_flavor
.has_field("memory_mb") and vdu
.vm_flavor
.memory_mb
:
459 vnfc
["ram"] = vdu
.vm_flavor
.memory_mb
462 if vdu
.has_field("hypervisor_epa"):
463 vnfc
["hypervisor"] = {}
464 if vdu
.hypervisor_epa
.has_field("type"):
465 if vdu
.hypervisor_epa
.type_yang
== "REQUIRE_KVM":
466 vnfc
["hypervisor"]["type"] = "QEMU-kvm"
468 if vdu
.hypervisor_epa
.has_field("version"):
469 vnfc
["hypervisor"]["version"] = vdu
.hypervisor_epa
.version
471 if vdu
.has_field("host_epa"):
472 vnfc
["processor"] = {}
473 if vdu
.host_epa
.has_field("om_cpu_model_string"):
474 vnfc
["processor"]["model"] = vdu
.host_epa
.om_cpu_model_string
475 if vdu
.host_epa
.has_field("om_cpu_feature"):
476 vnfc
["processor"]["features"] = []
477 for feature
in vdu
.host_epa
.om_cpu_feature
:
478 vnfc
["processor"]["features"].append(feature
.feature
)
481 vnf
["VNFC"].append(vnfc
)
483 for int_if
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
487 if int_if
.virtual_interface
.has_field("vpci"):
488 intf
["vpci"] = int_if
.virtual_interface
.vpci
490 if int_if
.virtual_interface
.type_yang
in ["VIRTIO", "E1000"]:
491 intf
["model"] = rift2openmano_vif(int_if
.virtual_interface
.type_yang
)
492 vnfc
["bridge-ifaces"].append(intf
)
494 elif int_if
.virtual_interface
.type_yang
in ["OM_MGMT"]:
495 vnfc
["bridge-ifaces"].append(intf
)
497 elif int_if
.virtual_interface
.type_yang
== "SR-IOV":
498 intf
["bandwidth"] = "10 Gbps"
499 intf
["dedicated"] = "yes:sriov"
500 vnfc
["numas"][0]["interfaces"].append(intf
)
502 elif int_if
.virtual_interface
.type_yang
== "PCI_PASSTHROUGH":
503 intf
["bandwidth"] = "10 Gbps"
504 intf
["dedicated"] = "yes"
505 if "interfaces" not in vnfc
["numas"][0]:
506 vnfc
["numas"][0]["interfaces"] = []
507 vnfc
["numas"][0]["interfaces"].append(intf
)
509 raise ValueError("Interface type %s not supported" % int_if
.virtual_interface
)
511 if int_if
.virtual_interface
.has_field("bandwidth"):
512 if int_if
.virtual_interface
.bandwidth
!= 0:
513 bps
= int_if
.virtual_interface
.bandwidth
515 # Calculate the bits per second conversion
516 for x
in [('M', 1000000), ('G', 1000000000)]:
518 intf
["bandwidth"] = "{} {}bps".format(math
.ceil(bps
/x
[1]), x
[0])
524 def parse_args(argv
=sys
.argv
[1:]):
525 """ Parse the command line arguments
528 arv - The list of arguments to parse
531 Argparse Namespace instance
533 parser
= argparse
.ArgumentParser()
537 help="Directory to output converted descriptors. Default is stdout",
541 '-n', '--nsd-file-hdl',
542 metavar
="nsd_xml_file",
543 type=argparse
.FileType('r'),
544 help="Rift NSD Descriptor File",
548 '-v', '--vnfd-file-hdls',
549 metavar
="vnfd_xml_file",
551 type=argparse
.FileType('r'),
552 help="Rift VNFD Descriptor File",
555 args
= parser
.parse_args(argv
)
557 if not os
.path
.exists(args
.outdir
):
558 os
.makedirs(args
.outdir
)
560 if not is_writable_directory(args
.outdir
):
561 logging
.error("Directory %s is not writable", args
.outdir
)
567 def write_yaml_to_file(name
, outdir
, desc_dict
):
568 file_name
= "%s.yaml" % name
569 yaml_str
= yaml
.dump(desc_dict
)
571 sys
.stdout
.write(yaml_str
)
574 file_path
= os
.path
.join(outdir
, file_name
)
575 dir_path
= os
.path
.dirname(file_path
)
576 if not os
.path
.exists(dir_path
):
577 os
.makedirs(dir_path
)
579 with
open(file_path
, "w") as hdl
:
582 logger
.info("Wrote descriptor to %s", file_path
)
585 def main(argv
=sys
.argv
[1:]):
586 args
= parse_args(argv
)
589 if args
.vnfd_file_hdls
is not None:
590 vnf_dict
= create_vnfd_from_xml_files(args
.vnfd_file_hdls
)
592 if args
.nsd_file_hdl
is not None:
593 nsd
= create_nsd_from_xml_file(args
.nsd_file_hdl
)
595 openmano_nsd
= rift2openmano_nsd(nsd
, vnf_dict
)
597 write_yaml_to_file(openmano_nsd
["name"], args
.outdir
, openmano_nsd
)
599 for vnf
in vnf_dict
.values():
600 openmano_vnf
= rift2openmano_vnfd(vnf
)
601 write_yaml_to_file(openmano_vnf
["vnf"]["name"], args
.outdir
, openmano_vnf
)
604 if __name__
== "__main__":
605 logging
.basicConfig(level
=logging
.WARNING
)