Bug: #240 - NS Scaling Basic Changes
[osm/SO.git] / models / openmano / python / rift / openmano / rift2openmano.py
index 2ce17c0..178cc1e 100755 (executable)
@@ -34,7 +34,7 @@ from gi.repository import (
     RwYang,
     RwVnfdYang,
     RwNsdYang,
-    )
+)
 
 import rift.package.store
 import rift.package.cloud_init
@@ -221,13 +221,16 @@ def convert_vnfd_name(vnfd_name, member_idx):
     return vnfd_name + "__" + str(member_idx)
 
 
-def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
-    for vnfd_id in rift_nsd.vnfd_ids:
-        if vnfd_id not in rift_vnfds:
-            raise VNFNotFoundError("VNF id %s not provided" % vnfd_id)
+def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids, rift_vnfd_id=None):
+    if rift_vnfd_id is None:
+        for vnfd_id in rift_nsd.vnfd_ids:
+            if vnfd_id not in rift_vnfds:
+                raise VNFNotFoundError("VNF id %s not provided" % vnfd_id)
 
     openmano = {}
     openmano["name"] = rift_nsd.name
+    if rift_vnfd_id is not None:
+        openmano["name"] += "scale1"
     openmano["description"] = rift_nsd.description
     topology = {}
     openmano["topology"] = topology
@@ -235,6 +238,8 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
     topology["nodes"] = {}
     for vnfd in rift_nsd.constituent_vnfds:
         vnfd_id = vnfd.vnfd_id_ref
+        if rift_vnfd_id is not None and rift_vnfd_id != vnfd_id:
+            continue
         rift_vnfd = rift_vnfds[vnfd_id]
         member_idx = vnfd.member_vnf_index
         openmano_vnfd_id = openmano_vnfd_ids.get(vnfd_id,None)
@@ -242,12 +247,12 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
             topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
                 "type": "VNF",
                 "vnf_id": openmano_vnfd_id
-                }
+            }
         else:
             topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
                 "type": "VNF",
                 "VNF model": rift_vnfd.name
-                }
+            }
 
     for vld in rift_nsd.vlds:
         # Openmano has both bridge_net and dataplane_net models for network types
@@ -267,14 +272,14 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
         if True:
             if vld.name not in topology["nodes"]:
                 topology["nodes"][vld.name] = {
-                        "type": "external_network",
-                        "model": vld.name,
-                        }
+                    "type": "external_network",
+                    "model": vld.name,
+                }
 
             # Add the external network to the list of connection points
             topology["connections"][vld.name]["nodes"].append(
-                    {vld.name: "0"}
-                    )
+                {vld.name: "0"}
+            )
         elif vld.provider_network.has_field("physical_network"):
             # Add the external datacenter network to the topology
             # node list if it isn't already added
@@ -285,14 +290,14 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
 
             if ext_net_name not in topology["nodes"]:
                 topology["nodes"][ext_net_name] = {
-                        "type": "external_network",
-                        "model": ext_net_name_with_seg,
-                        }
+                    "type": "external_network",
+                    "model": ext_net_name_with_seg,
+                }
 
             # Add the external network to the list of connection points
             topology["connections"][vld.name]["nodes"].append(
-                    {ext_net_name: "0"}
-                    )
+                {ext_net_name: "0"}
+            )
 
 
         for vnfd_cp in vld.vnfd_connection_point_ref:
@@ -310,9 +315,53 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids):
                     vnf_ref: vnfd_cp.vnfd_connection_point_ref
                 }
             )
-
     return openmano
 
+def rift2openmano_vnfd_nsd(rift_nsd, rift_vnfds, rift_vnfd_id):
+
+    if rift_vnfd_id not in rift_vnfds:
+        print ("IDS", rift_vnfds)
+        raise VNFNotFoundError("VNF id %s not provided" % rift_vnfd_id)
+
+    openmano_vnfd_nsd = {}
+    openmano_vnfd_nsd["name"] = rift_vnfd_id+'-'+'scaling_group'
+    openmano_vnfd_nsd["description"] = "Scaling Group"
+    topology = {}
+    openmano_vnfd_nsd["topology"] = topology
+
+    topology["nodes"] = {}
+    openmano_vnfd_nsd["topology"] = topology
+    topology["connections"] = {}
+    topology["nodes"] = {}
+    topology["nodes"] = {
+        "type": "VNF",
+        "vnf_id": rift_vnfd_id
+    }
+
+    for vld in rift_nsd.vlds:
+
+        # Create a connections entry for each external VLD
+        topology["connections"][vld.name] = {}
+        topology["connections"][vld.name]["nodes"] = []
+
+    for vnfd_cp in vld.vnfd_connection_point_ref:
+        if not rit_vnfd_id in vnfd_cp.vnfd_id_ref:
+            continue
+        if rift_vnfd_id in vnfd_cp.vnfd_id_ref:
+            # Get the RIFT VNF for this external VLD connection point
+            vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
+            # For each VNF in this connection, use the same interface name
+            topology["connections"][vld.name]["type"] = "link"
+            # Vnf ref is the vnf name with the member_vnf_idx appended
+            member_idx = vnfd_cp.member_vnf_index_ref
+            vnf_ref = rift_vnfd_id + "__" + str(member_idx)
+            topology["connections"][vld.name]["nodes"].append(
+                {
+                    vnf_ref: vnfd_cp.vnfd_connection_point_ref
+                }
+            )
+    return openmano_vnfd_nsd
+
 def cloud_init(rift_vnfd_id, vdu):
     """ Populate cloud_init with script from
          either the inline contents or from the file provided
@@ -324,7 +373,7 @@ def cloud_init(rift_vnfd_id, vdu):
         logger.debug("cloud_init script provided inline %s", vdu.cloud_init)
         cloud_init_msg = vdu.cloud_init
     elif vdu.cloud_init_file is not None:
-       # Get cloud-init script contents from the file provided in the cloud_init_file param
+        # Get cloud-init script contents from the file provided in the cloud_init_file param
         logger.debug("cloud_init script provided in file %s", vdu.cloud_init_file)
         filename = vdu.cloud_init_file
         vnfd_package_store.refresh()
@@ -353,9 +402,9 @@ def config_file_init(rift_vnfd_id, vdu, cfg_file):
     stored_package = vnfd_package_store.get_package(rift_vnfd_id)
     cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger)
     try:
-            cfg_file_msg = cloud_init_extractor.read_script(stored_package, filename)
+        cfg_file_msg = cloud_init_extractor.read_script(stored_package, filename)
     except rift.package.cloud_init.CloudInitExtractionError as e:
-            raise ValueError(e)
+        raise ValueError(e)
 
     logger.debug("Current config file msg is {}".format(cfg_file_msg))
     return cfg_file_msg
@@ -427,7 +476,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
             "VNFC": vdu.name,
             "local_iface_name": ext_if.name,
             "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
-            }
+        }
 
         vnf["external-connections"].append(connection)
 
@@ -438,7 +487,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
             "description": vld.description,
             "type": "bridge",
             "elements": [],
-            }
+        }
 
         # Add the specific VDU connection points
         for int_cp in vld.internal_connection_point:
@@ -446,7 +495,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
             connection["elements"].append({
                 "VNFC": vdu.name,
                 "local_iface_name": int_if.name,
-                })
+            })
         if "internal-connections" not in vnf:
             vnf["internal-connections"] = []
 
@@ -459,7 +508,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
             "name": vdu.name,
             "description": vdu.name,
             "bridge-ifaces": [],
-            }
+        }
 
         if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
             vnfc["disk"] = vdu.vm_flavor.storage_gb
@@ -478,9 +527,9 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
                 dedicated_int = True
         if vdu.guest_epa.has_field("numa_node_policy") or dedicated_int:
             vnfc["numas"] = [{
-                           "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
-                           "interfaces":[],
-                           }]
+                "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
+                "interfaces":[],
+            }]
             numa_node_policy = vdu.guest_epa.numa_node_policy
             if numa_node_policy.has_field("node"):
                 numa_node = numa_node_policy.node[0]
@@ -491,9 +540,9 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
                     if len(numa_node.paired_threads.paired_thread_ids) > 0:
                         vnfc["numas"][0]["paired-threads-id"] = []
                         for pair in numa_node.paired_threads.paired_thread_ids:
-                             vnfc["numas"][0]["paired-threads-id"].append(
-                                     [pair.thread_a, pair.thread_b]
-                                     )
+                            vnfc["numas"][0]["paired-threads-id"].append(
+                                [pair.thread_a, pair.thread_b]
+                            )
 
             else:
                 if vdu.vm_flavor.has_field("vcpu_count"):
@@ -528,7 +577,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
         if vdu.has_field("volumes"):
             vnfc["devices"] = []
             # Sort volumes as device-list is implictly ordered by Openmano
-            newvollist = sorted(vdu.volumes, key=lambda k: k.name) 
+            newvollist = sorted(vdu.volumes, key=lambda k: k.name)
             for iter_num, volume in enumerate(newvollist):
                 if iter_num == 0:
                     # Convert the first volume to vnfc.image
@@ -543,7 +592,7 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
                     device = {}
                     device["type"] = volume.device_type
                     device["image"] = volume.image
-                    vnfc["devices"].append(device)   
+                    vnfc["devices"].append(device)
 
         vnfc_boot_data_init = False
         if vdu.has_field("cloud_init") or vdu.has_field("cloud_init_file"):
@@ -565,14 +614,14 @@ def rift2openmano_vnfd(rift_vnfd, rift_nsd):
                     cfg_source = config_file_init(rift_vnfd.id, vdu, custom_config_file.source)
                     om_cfgfile_list.append({"dest":custom_config_file.dest, "content": cfg_source})
                 vnfc['boot-data']['config-files'] = om_cfgfile_list
+
 
         vnf["VNFC"].append(vnfc)
 
         for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
             intf = {
                 "name": int_if.name,
-                }
+            }
             if int_if.virtual_interface.has_field("vpci"):
                 intf["vpci"] = int_if.virtual_interface.vpci
 
@@ -627,14 +676,14 @@ def parse_args(argv=sys.argv[1:]):
         '-o', '--outdir',
         default='-',
         help="Directory to output converted descriptors. Default is stdout",
-        )
+    )
 
     parser.add_argument(
         '-n', '--nsd-file-hdl',
         metavar="nsd_file",
         type=argparse.FileType('r'),
         help="Rift NSD Descriptor File",
-        )
+    )
 
     parser.add_argument(
         '-v', '--vnfd-file-hdls',
@@ -642,7 +691,7 @@ def parse_args(argv=sys.argv[1:]):
         action='append',
         type=argparse.FileType('r'),
         help="Rift VNFD Descriptor File",
-        )
+    )
 
     args = parser.parse_args(argv)
 
@@ -676,8 +725,8 @@ def write_yaml_to_file(name, outdir, desc_dict):
 
 def main(argv=sys.argv[1:]):
     args = parse_args(argv)
-
     nsd = None
+    rift_vnfd_id = 'test_vnfd'
     openmano_vnfr_ids = dict()
     vnf_dict = None
     if args.vnfd_file_hdls is not None:
@@ -689,10 +738,10 @@ def main(argv=sys.argv[1:]):
     if args.nsd_file_hdl is not None:
         nsd = create_nsd_from_file(args.nsd_file_hdl)
 
-    openmano_nsd = rift2openmano_nsd(nsd, vnf_dict, openmano_vnfr_ids)
-
+    openmano_nsd = rift2openmano_nsd(nsd, vnf_dict, openmano_vnfr_ids,rift_vnfd_id)
+    vnfd_nsd = rift2openmano_vnfd_nsd(nsd, vnf_dict, openmano_vnfr_ids, rift_vnfd_id)
     write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd)
-
+    write_yaml_to_file(vnfd_nsd["name"], args.outdir, vnfd_nsd)
     for vnf in vnf_dict.values():
         openmano_vnf = rift2openmano_vnfd(vnf, nsd)
         write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf)
@@ -700,4 +749,4 @@ def main(argv=sys.argv[1:]):
 
 if __name__ == "__main__":
     logging.basicConfig(level=logging.WARNING)
-    main()
+    main()
\ No newline at end of file