Merge "Descriptor Unification"
[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 logger = logging.getLogger("rift2openmano.py")
40
41
42 class VNFNotFoundError(Exception):
43 pass
44
45
46 class RiftNSD(object):
47 model = RwYang.Model.create_libncx()
48 model.load_module('nsd')
49 model.load_module('rw-nsd')
50
51 def __init__(self, descriptor):
52 self._nsd = descriptor
53
54 def __str__(self):
55 return str(self._nsd)
56
57 @property
58 def name(self):
59 return self._nsd.name
60
61 @property
62 def id(self):
63 return self._nsd.id
64
65 @property
66 def vnfd_ids(self):
67 return [c.vnfd_id_ref for c in self._nsd.constituent_vnfd]
68
69 @property
70 def constituent_vnfds(self):
71 return self._nsd.constituent_vnfd
72
73 @property
74 def vlds(self):
75 return self._nsd.vld
76
77 @property
78 def cps(self):
79 return self._nsd.connection_point
80
81 @property
82 def description(self):
83 return self._nsd.description
84
85 @classmethod
86 def from_xml_file_hdl(cls, hdl):
87 hdl.seek(0)
88 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
89 descriptor.from_xml_v2(RiftNSD.model, hdl.read())
90 return cls(descriptor)
91
92 @classmethod
93 def from_yaml_file_hdl(cls, hdl):
94 hdl.seek(0)
95 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor.from_yaml(RiftNSD.model, hdl.read())
97 return cls(descriptor)
98
99 @classmethod
100 def from_dict(cls, nsd_dict):
101 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd.from_dict(nsd_dict)
102 return cls(descriptor)
103
104
105 class RiftVNFD(object):
106 model = RwYang.Model.create_libncx()
107 model.load_module('vnfd')
108 model.load_module('rw-vnfd')
109
110 def __init__(self, descriptor):
111 self._vnfd = descriptor
112
113 def __str__(self):
114 return str(self._vnfd)
115
116 @property
117 def id(self):
118 return self._vnfd.id
119
120 @property
121 def name(self):
122 return self._vnfd.name
123
124 @property
125 def description(self):
126 return self._vnfd.description
127
128 @property
129 def cps(self):
130 return self._vnfd.connection_point
131
132 @property
133 def vdus(self):
134 return self._vnfd.vdu
135
136 @property
137 def internal_vlds(self):
138 return self._vnfd.internal_vld
139
140 @classmethod
141 def from_xml_file_hdl(cls, hdl):
142 hdl.seek(0)
143 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
144 descriptor.from_xml_v2(RiftVNFD.model, hdl.read())
145 return cls(descriptor)
146
147 @classmethod
148 def from_yaml_file_hdl(cls, hdl):
149 hdl.seek(0)
150 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor.from_yaml(RiftVNFD.model, hdl.read())
152 return cls(descriptor)
153
154 @classmethod
155 def from_dict(cls, vnfd_dict):
156 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd.from_dict(vnfd_dict)
157 return cls(descriptor)
158
159
160 def is_writable_directory(dir_path):
161 """ Returns True if dir_path is writable, False otherwise
162
163 Arguments:
164 dir_path - A directory path
165 """
166 if not os.path.exists(dir_path):
167 raise ValueError("Directory does not exist: %s", dir_path)
168
169 try:
170 testfile = tempfile.TemporaryFile(dir=dir_path)
171 testfile.close()
172 except OSError:
173 return False
174
175 return True
176
177
178 def create_vnfd_from_xml_files(vnfd_file_hdls):
179 """ Create a list of RiftVNFD instances from xml file handles
180
181 Arguments:
182 vnfd_file_hdls - Rift VNFD XML file handles
183
184 Returns:
185 A list of RiftVNFD instances
186 """
187 vnfd_dict = {}
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
191
192 return vnfd_dict
193
194 def create_vnfd_from_yaml_files(vnfd_file_hdls):
195 """ Create a list of RiftVNFD instances from xml file handles
196
197 Arguments:
198 vnfd_file_hdls - Rift VNFD YAML file handles
199
200 Returns:
201 A list of RiftVNFD instances
202 """
203 vnfd_dict = {}
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
207
208 return vnfd_dict
209
210
211 def create_nsd_from_xml_file(nsd_file_hdl):
212 """ Create a list of RiftNSD instances from xml file handles
213
214 Arguments:
215 nsd_file_hdls - Rift NSD XML file handles
216
217 Returns:
218 A list of RiftNSD instances
219 """
220 nsd = RiftNSD.from_xml_file_hdl(nsd_file_hdl)
221 return nsd
222
223 def create_nsd_from_yaml_file(nsd_file_hdl):
224 """ Create a list of RiftNSD instances from yaml file handles
225
226 Arguments:
227 nsd_file_hdls - Rift NSD XML file handles
228
229 Returns:
230 A list of RiftNSD instances
231 """
232 nsd = RiftNSD.from_yaml_file_hdl(nsd_file_hdl)
233 return nsd
234
235
236 def ddict():
237 return collections.defaultdict(dict)
238
239 def convert_vnfd_name(vnfd_name, member_idx):
240 return vnfd_name + "__" + str(member_idx)
241
242
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)
247
248 openmano = {}
249 openmano["name"] = rift_nsd.name
250 openmano["description"] = rift_nsd.description
251 topology = {}
252 openmano["topology"] = topology
253
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)
260 if openmano_vnfd_id:
261 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
262 "type": "VNF",
263 "vnf_id": openmano_vnfd_id
264 }
265 else:
266 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
267 "type": "VNF",
268 "VNF model": rift_vnfd.name
269 }
270
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"}
276 pass
277
278 topology["connections"] = {}
279 for vld in rift_nsd.vlds:
280
281 # Create a connections entry for each external VLD
282 topology["connections"][vld.name] = {}
283 topology["connections"][vld.name]["nodes"] = []
284
285 #if vld.vim_network_name:
286 if True:
287 if vld.name not in topology["nodes"]:
288 topology["nodes"][vld.name] = {
289 "type": "external_network",
290 "model": vld.name,
291 }
292
293 # Add the external network to the list of connection points
294 topology["connections"][vld.name]["nodes"].append(
295 {vld.name: "0"}
296 )
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)
304
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,
309 }
310
311 # Add the external network to the list of connection points
312 topology["connections"][vld.name]["nodes"].append(
313 {ext_net_name: "0"}
314 )
315
316
317 for vnfd_cp in vld.vnfd_connection_point_ref:
318
319 # Get the RIFT VNF for this external VLD connection point
320 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
321
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(
328 {
329 vnf_ref: vnfd_cp.vnfd_connection_point_ref
330 }
331 )
332
333 return openmano
334
335
336 def rift2openmano_vnfd(rift_vnfd, rift_nsd):
337 openmano_vnf = {"vnf":{}}
338 vnf = openmano_vnf["vnf"]
339
340 vnf["name"] = rift_vnfd.name
341 vnf["description"] = rift_vnfd.description
342
343 vnf["external-connections"] = []
344
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:
349 return vdu, ext_if
350
351 raise ValueError("External connection point reference %s not found" % cp_ref_name)
352
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:
357 return vdu, int_if
358
359 raise ValueError("Internal connection point reference %s not found" % cp_ref_id)
360
361 def rift2openmano_if_type(ext_if):
362
363 cp_ref_name = ext_if.vnfd_connection_point_ref
364 for vld in rift_nsd.vlds:
365
366 # if it is an explicit mgmt_network then check if the given
367 # cp_ref is a part of it
368 if not vld.mgmt_network:
369 continue
370
371 for vld_cp in vld.vnfd_connection_point_ref:
372 if vld_cp.vnfd_connection_point_ref == cp_ref_name:
373 return "mgmt"
374
375
376 rift_type = ext_if.virtual_interface.type_yang
377 # Retaining it for backward compatibility!
378 if rift_type == "OM_MGMT":
379 return "mgmt"
380 elif rift_type == "VIRTIO" or rift_type == "E1000":
381 return "bridge"
382 else:
383 return "data"
384
385 def rift2openmano_vif(rift_type):
386 if rift_type == "VIRTIO":
387 return "virtio"
388 elif rift_type == "E1000":
389 return "e1000"
390 else:
391 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type))
392
393 # Add all external connections
394 for cp in rift_vnfd.cps:
395 # Find the VDU and and external interface for this connection point
396 vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name)
397 connection = {
398 "name": cp.name,
399 "type": rift2openmano_if_type(ext_if),
400 "VNFC": vdu.name,
401 "local_iface_name": ext_if.name,
402 "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
403 }
404
405 vnf["external-connections"].append(connection)
406
407 # Add all internal networks
408 for vld in rift_vnfd.internal_vlds:
409 connection = {
410 "name": vld.name,
411 "description": vld.description,
412 "type": "data",
413 "elements": [],
414 }
415
416 # Add the specific VDU connection points
417 for int_cp in vld.internal_connection_point:
418 vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref)
419 connection["elements"].append({
420 "VNFC": vdu.name,
421 "local_iface_name": int_if.name,
422 })
423 if "internal-connections" not in vnf:
424 vnf["internal-connections"] = []
425
426 vnf["internal-connections"].append(connection)
427
428 # Add VDU's
429 vnf["VNFC"] = []
430 for vdu in rift_vnfd.vdus:
431 vnfc = {
432 "name": vdu.name,
433 "description": vdu.name,
434 "bridge-ifaces": [],
435 }
436
437 if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
438 vnfc["disk"] = vdu.vm_flavor.storage_gb
439
440 if os.path.isabs(vdu.image):
441 vnfc["VNFC image"] = vdu.image
442 else:
443 vnfc["image name"] = vdu.image
444 if vdu.has_field("image_checksum"):
445 vnfc["image checksum"] = vdu.image_checksum
446
447 if vdu.guest_epa.has_field("numa_node_policy"):
448 vnfc["numas"] = [{
449 "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
450 "interfaces":[],
451 }]
452 numa_node_policy = vdu.guest_epa.numa_node_policy
453 if numa_node_policy.has_field("node"):
454 numa_node = numa_node_policy.node[0]
455
456 if numa_node.has_field("paired_threads"):
457 if numa_node.paired_threads.has_field("num_paired_threads"):
458 vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
459 if len(numa_node.paired_threads.paired_thread_ids) > 0:
460 vnfc["numas"][0]["paired-threads-id"] = []
461 for pair in numa_node.paired_threads.paired_thread_ids:
462 vnfc["numas"][0]["paired-threads-id"].append(
463 [pair.thread_a, pair.thread_b]
464 )
465
466 else:
467 if vdu.vm_flavor.has_field("vcpu_count"):
468 vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
469
470 else:
471 if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count:
472 vnfc["vcpus"] = vdu.vm_flavor.vcpu_count
473
474 if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb:
475 vnfc["ram"] = vdu.vm_flavor.memory_mb
476
477
478 if vdu.has_field("hypervisor_epa"):
479 vnfc["hypervisor"] = {}
480 if vdu.hypervisor_epa.has_field("type"):
481 if vdu.hypervisor_epa.type_yang == "REQUIRE_KVM":
482 vnfc["hypervisor"]["type"] = "QEMU-kvm"
483
484 if vdu.hypervisor_epa.has_field("version"):
485 vnfc["hypervisor"]["version"] = vdu.hypervisor_epa.version
486
487 if vdu.has_field("host_epa"):
488 vnfc["processor"] = {}
489 if vdu.host_epa.has_field("om_cpu_model_string"):
490 vnfc["processor"]["model"] = vdu.host_epa.om_cpu_model_string
491 if vdu.host_epa.has_field("om_cpu_feature"):
492 vnfc["processor"]["features"] = []
493 for feature in vdu.host_epa.om_cpu_feature:
494 vnfc["processor"]["features"].append(feature.feature)
495
496
497 vnf["VNFC"].append(vnfc)
498
499 for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
500 intf = {
501 "name": int_if.name,
502 }
503 if int_if.virtual_interface.has_field("vpci"):
504 intf["vpci"] = int_if.virtual_interface.vpci
505
506 if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]:
507 intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang)
508 vnfc["bridge-ifaces"].append(intf)
509
510 elif int_if.virtual_interface.type_yang in ["OM_MGMT"]:
511 vnfc["bridge-ifaces"].append(intf)
512
513 elif int_if.virtual_interface.type_yang == "SR-IOV":
514 intf["bandwidth"] = "10 Gbps"
515 intf["dedicated"] = "yes:sriov"
516 vnfc["numas"][0]["interfaces"].append(intf)
517
518 elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH":
519 intf["bandwidth"] = "10 Gbps"
520 intf["dedicated"] = "yes"
521 if "interfaces" not in vnfc["numas"][0]:
522 vnfc["numas"][0]["interfaces"] = []
523 vnfc["numas"][0]["interfaces"].append(intf)
524 else:
525 raise ValueError("Interface type %s not supported" % int_if.virtual_interface)
526
527 if int_if.virtual_interface.has_field("bandwidth"):
528 if int_if.virtual_interface.bandwidth != 0:
529 bps = int_if.virtual_interface.bandwidth
530
531 # Calculate the bits per second conversion
532 for x in [('M', 1000000), ('G', 1000000000)]:
533 if bps/x[1] >= 1:
534 intf["bandwidth"] = "{} {}bps".format(math.ceil(bps/x[1]), x[0])
535
536
537 return openmano_vnf
538
539
540 def parse_args(argv=sys.argv[1:]):
541 """ Parse the command line arguments
542
543 Arguments:
544 arv - The list of arguments to parse
545
546 Returns:
547 Argparse Namespace instance
548 """
549 parser = argparse.ArgumentParser()
550 parser.add_argument(
551 '-o', '--outdir',
552 default='-',
553 help="Directory to output converted descriptors. Default is stdout",
554 )
555
556 parser.add_argument(
557 '-n', '--nsd-file-hdl',
558 metavar="nsd_xml_file",
559 type=argparse.FileType('r'),
560 help="Rift NSD Descriptor File",
561 )
562
563 parser.add_argument(
564 '-v', '--vnfd-file-hdls',
565 metavar="vnfd_xml_file",
566 action='append',
567 type=argparse.FileType('r'),
568 help="Rift VNFD Descriptor File",
569 )
570
571 args = parser.parse_args(argv)
572
573 if not os.path.exists(args.outdir):
574 os.makedirs(args.outdir)
575
576 if not is_writable_directory(args.outdir):
577 logging.error("Directory %s is not writable", args.outdir)
578 sys.exit(1)
579
580 return args
581
582
583 def write_yaml_to_file(name, outdir, desc_dict):
584 file_name = "%s.yaml" % name
585 yaml_str = yaml.dump(desc_dict)
586 if outdir == "-":
587 sys.stdout.write(yaml_str)
588 return
589
590 file_path = os.path.join(outdir, file_name)
591 dir_path = os.path.dirname(file_path)
592 if not os.path.exists(dir_path):
593 os.makedirs(dir_path)
594
595 with open(file_path, "w") as hdl:
596 hdl.write(yaml_str)
597
598 logger.info("Wrote descriptor to %s", file_path)
599
600
601 def main(argv=sys.argv[1:]):
602 args = parse_args(argv)
603
604 nsd = None
605 if args.vnfd_file_hdls is not None:
606 vnf_dict = create_vnfd_from_xml_files(args.vnfd_file_hdls)
607
608 if args.nsd_file_hdl is not None:
609 nsd = create_nsd_from_xml_file(args.nsd_file_hdl)
610
611 openmano_nsd = rift2openmano_nsd(nsd, vnf_dict)
612
613 write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd)
614
615 for vnf in vnf_dict.values():
616 openmano_vnf = rift2openmano_vnfd(vnf)
617 write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf)
618
619
620 if __name__ == "__main__":
621 logging.basicConfig(level=logging.WARNING)
622 main()