Adding checks to upgrade and validation tools 99/6299/3
authorgcalvino <guillermo.calvinosanchez@altran.com>
Fri, 15 Jun 2018 07:51:34 +0000 (09:51 +0200)
committercalvinosanch <guillermo.calvinosanchez@altran.com>
Fri, 17 Aug 2018 08:10:09 +0000 (09:10 +0100)
Signed-off-by: gcalvino <guillermo.calvinosanchez@altran.com>
descriptor-packages/tools/upgrade_descriptor_version.py
descriptor-packages/tools/validate_descriptor.py

index e33d234..66bdbb1 100755 (executable)
@@ -129,6 +129,16 @@ if __name__=="__main__":
 
             if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
                 descriptor = "VNF"
+                #Check if mgmt-interface is defined:
+                remove_prefix(data, "vnfd:")
+                vnfd_descriptor = data["vnfd-catalog"]
+                vnfd_list = vnfd_descriptor["vnfd"]
+                mgmt_iface = False
+                for vnfd in vnfd_list:
+                    if vnfd.get("mgmt-interface"):
+                        mgmt_iface = True
+                if not mgmt_iface:
+                    raise KeyError("'mgmt-iface' is a mandatory field and it is not defined")
                 myvnfd = vnfd_catalog.vnfd()
                 pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd)
             elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
@@ -192,6 +202,8 @@ if __name__=="__main__":
                     error_position.append("external-interface")
                     for external_interface in external_interface_list:
                         error_position[-1] = "external-interface[{}]".format(external_interface["name"])
+                        if "rw-vnfd:floating-ip-needed" in external_interface:
+                            del external_interface["rw-vnfd:floating-ip-needed"]
                         external_interface["type"] = "EXTERNAL"
                         external_interface["external-connection-point-ref"] = \
                             external_interface.pop("vnfd-connection-point-ref")
@@ -210,6 +222,14 @@ if __name__=="__main__":
                             internal_interface.pop("vdu-internal-connection-point-ref")
                         interface_list.append(internal_interface)
                     error_position.pop()
+
+                    #Removing "rw-vnfd:floating-ip-needed" items from V3 descriptors
+                    interfaces = vdu.pop("interface", ())
+                    for iface in interfaces:
+                        if "rw-vnfd:floating-ip-needed" in iface:
+                            del iface["rw-vnfd:floating-ip-needed"]
+                        interface_list.append(iface)
+
                     # order interface alphabetically and set position
                     if interface_list:
                         interface_list = sorted(interface_list,
index 36e7c8d..d2888c3 100755 (executable)
@@ -46,6 +46,28 @@ def usage():
     print("      -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
     return
 
+def remove_prefix(desc, prefix):
+    """
+    Recursively removes prefix from keys
+    :param desc: dictionary or list to change
+    :param prefix: prefix to remove. Must
+    :return: None, param desc is changed
+    """
+    prefix_len = len(prefix)
+    if isinstance(desc, dict):
+        prefixed_list=[]
+        for k,v in desc.items():
+            if isinstance(v, (list, tuple, dict)):
+                remove_prefix(v, prefix)
+            if isinstance(k, str) and k.startswith(prefix) and k != prefix:
+                prefixed_list.append(k)
+        for k in prefixed_list:
+            desc[k[prefix_len:]] = desc.pop(k)
+    elif isinstance(desc, (list, tuple)):
+        for i in desc:
+            if isinstance(desc, (list, tuple, dict)):
+                remove_prefix(i, prefix)
+
 if __name__=="__main__":
     error_position = []
     format_output_yaml = True
@@ -91,6 +113,16 @@ if __name__=="__main__":
 
         if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
             descriptor = "VNF"
+            # Check if mgmt-interface is defined:
+            remove_prefix(data, "vnfd:")
+            vnfd_descriptor = data["vnfd-catalog"]
+            vnfd_list = vnfd_descriptor["vnfd"]
+            mgmt_iface = False
+            for vnfd in vnfd_list:
+                if vnfd.get("mgmt-interface"):
+                    mgmt_iface = True
+            if not mgmt_iface:
+                raise KeyError("'mgmt-iface' is a mandatory field and it is not defined")
             myvnfd = vnfd_catalog.vnfd()
             pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd)
         elif "nsd:nsd-catalog" in data or "nsd-catalog" in data: