Merge "Bug 129 RIFT-15122"
[osm/SO.git] / models / openmano / python / rift / openmano / rift2openmano.py
index 3edbbb0..c29818f 100755 (executable)
@@ -25,6 +25,11 @@ import sys
 import tempfile
 import yaml
 
+import gi
+gi.require_version('RwYang', '1.0')
+gi.require_version('RwVnfdYang', '1.0')
+gi.require_version('RwNsdYang', '1.0')
+
 from gi.repository import (
     RwYang,
     RwVnfdYang,
@@ -328,7 +333,7 @@ def rift2openmano_nsd(rift_nsd, rift_vnfds,openmano_vnfd_ids):
     return openmano
 
 
-def rift2openmano_vnfd(rift_vnfd):
+def rift2openmano_vnfd(rift_vnfd, rift_nsd):
     openmano_vnf = {"vnf":{}}
     vnf = openmano_vnf["vnf"]
 
@@ -353,21 +358,45 @@ def rift2openmano_vnfd(rift_vnfd):
 
         raise ValueError("Internal connection point reference %s not found" % cp_ref_id)
 
-    def rift2openmano_if_type(rift_type):
+    def rift2openmano_if_type(ext_if):
+
+        cp_ref_name = ext_if.vnfd_connection_point_ref
+        for vld in rift_nsd.vlds:
+
+            # if it is an explicit mgmt_network then check if the given
+            # cp_ref is a part of it
+            if not vld.mgmt_network:
+                continue
+
+            for vld_cp in vld.vnfd_connection_point_ref:
+                if vld_cp.vnfd_connection_point_ref == cp_ref_name:
+                    return "mgmt"
+
+
+        rift_type = ext_if.virtual_interface.type_yang
+        # Retaining it for backward compatibility!
         if rift_type == "OM_MGMT":
             return "mgmt"
-        elif rift_type == "VIRTIO":
+        elif rift_type == "VIRTIO" or rift_type == "E1000":
             return "bridge"
         else:
             return "data"
 
+    def rift2openmano_vif(rift_type):
+        if rift_type == "VIRTIO":
+            return "virtio"
+        elif rift_type == "E1000":
+            return "e1000"
+        else:
+            raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type))
+
     # Add all external connections
     for cp in rift_vnfd.cps:
         # Find the VDU and and external interface for this connection point
         vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name)
         connection = {
             "name": cp.name,
-            "type": rift2openmano_if_type(ext_if.virtual_interface.type_yang),
+            "type": rift2openmano_if_type(ext_if),
             "VNFC": vdu.name,
             "local_iface_name": ext_if.name,
             "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
@@ -385,8 +414,8 @@ def rift2openmano_vnfd(rift_vnfd):
             }
 
         # Add the specific VDU connection points
-        for int_cp_ref in vld.internal_connection_point_ref:
-            vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp_ref)
+        for int_cp in vld.internal_connection_point:
+            vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref)
             connection["elements"].append({
                 "VNFC": vdu.name,
                 "local_iface_name": int_if.name,
@@ -402,37 +431,52 @@ def rift2openmano_vnfd(rift_vnfd):
         vnfc = {
             "name": vdu.name,
             "description": vdu.name,
-            "numas": [{
-                "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
-                "interfaces":[],
-                }],
             "bridge-ifaces": [],
             }
 
+        if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
+            vnfc["disk"] = vdu.vm_flavor.storage_gb
+
         if os.path.isabs(vdu.image):
             vnfc["VNFC image"] = vdu.image
         else:
             vnfc["image name"] = vdu.image
             if vdu.has_field("image_checksum"):
                 vnfc["image checksum"] = vdu.image_checksum
+        dedicated_int = False
+        for intf in list(vdu.internal_interface) + list(vdu.external_interface):
+            if intf.virtual_interface.type_yang in ["SR_IOV", "PCI_PASSTHROUGH"]:
+                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":[],
+                           }]
+            numa_node_policy = vdu.guest_epa.numa_node_policy
+            if numa_node_policy.has_field("node"):
+                numa_node = numa_node_policy.node[0]
+
+                if numa_node.has_field("paired_threads"):
+                    if numa_node.paired_threads.has_field("num_paired_threads"):
+                        vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
+                    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]
+                                     )
 
-        numa_node_policy = vdu.guest_epa.numa_node_policy
-        if numa_node_policy.has_field("node"):
-            numa_node = numa_node_policy.node[0]
-
-            if numa_node.has_field("paired_threads"):
-                if numa_node.paired_threads.has_field("num_paired_threads"):
-                    vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
-                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]
-                                 )
+            else:
+                if vdu.vm_flavor.has_field("vcpu_count"):
+                    vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
 
         else:
-            if vdu.vm_flavor.has_field("vcpu_count"):
-                vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
+            if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count:
+                vnfc["vcpus"] = vdu.vm_flavor.vcpu_count
+
+            if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb:
+                vnfc["ram"] = vdu.vm_flavor.memory_mb
+
 
         if vdu.has_field("hypervisor_epa"):
             vnfc["hypervisor"] = {}
@@ -450,12 +494,9 @@ def rift2openmano_vnfd(rift_vnfd):
             if vdu.host_epa.has_field("om_cpu_feature"):
                 vnfc["processor"]["features"] = []
                 for feature in vdu.host_epa.om_cpu_feature:
-                    vnfc["processor"]["features"].append(feature)
+                    vnfc["processor"]["features"].append(feature.feature)
 
 
-        if vdu.vm_flavor.has_field("storage_gb"):
-            vnfc["disk"] = vdu.vm_flavor.storage_gb
-
         vnf["VNFC"].append(vnfc)
 
         for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
@@ -465,12 +506,16 @@ def rift2openmano_vnfd(rift_vnfd):
             if int_if.virtual_interface.has_field("vpci"):
                 intf["vpci"] = int_if.virtual_interface.vpci
 
-            if int_if.virtual_interface.type_yang in ["VIRTIO", "OM_MGMT"]:
+            if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]:
+                intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang)
+                vnfc["bridge-ifaces"].append(intf)
+
+            elif int_if.virtual_interface.type_yang in ["OM_MGMT"]:
                 vnfc["bridge-ifaces"].append(intf)
 
-            elif int_if.virtual_interface.type_yang == "SR-IOV":
+            elif int_if.virtual_interface.type_yang == "SR_IOV":
                 intf["bandwidth"] = "10 Gbps"
-                intf["dedicated"] = "yes:sriov"
+                intf["dedicated"] = "no"
                 vnfc["numas"][0]["interfaces"].append(intf)
 
             elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH":