Test functionality added to tool to upgrade vnf,ns descriptors to R. THREE 25/5725/7
authorgcalvino <guillermo.calvinosanchez@altran.com>
Thu, 23 Nov 2017 12:38:32 +0000 (13:38 +0100)
committergcalvino <guillermo.calvinosanchez@altran.com>
Mon, 27 Nov 2017 16:25:10 +0000 (17:25 +0100)
Change-Id: Ib1ea634fe8421ab4281e737f181edebf37beec09
Signed-off-by: gcalvino <guillermo.calvinosanchez@altran.com>
descriptor-packages/tools/upgrade_descriptor_version.py

index 4259ece..c830e52 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python2
 # -*- coding: utf-8 -*-
 
 ##
 # under the License.
 #
 ##
-
+from __future__ import print_function
 import json
 import yaml
 # import logging
 import sys
 import getopt
 
-
 """
 Converts OSM VNFD, NSD descriptor from release TWO to release THREE format
 """
-__author__ = "Alfonso Tierno"
+__author__ = "Alfonso Tierno, Guillermo Calvino"
 __date__ = "2017-10-14"
-__version__ = "0.0.1"
+__version__ = "0.0.2"
 version_date = "Nov 2017"
 
 
@@ -47,7 +46,7 @@ def usage():
     print("      -h|--help: shows this help")
     print("      -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
     print("      -o|--output FILE: where to write generated descriptor. By default stdout")
-    print("      --test: Content is tested to check wrong format or unknown keys")
+    print("      --test: Content is tested to check wrong format or unknown keys")
     return
 
 
@@ -80,6 +79,7 @@ if __name__=="__main__":
     input_file_name = None
     output_file_name = None
     test_file = None
+    file_name = None
     try:
         # load parameters and configuration
         opts, args = getopt.getopt(sys.argv[1:], "hvi:o:", ["input=", "help", "version", "output=", "test",])
@@ -113,6 +113,7 @@ if __name__=="__main__":
             output = open(file_name, 'w')
         else:
             output = sys.stdout
+        file_name = None
 
         if input_file_name.endswith('.yaml') or input_file_name.endswith('.yml') or not \
             (input_file_name.endswith('.json') or '\t' in descriptor_str):
@@ -121,28 +122,70 @@ if __name__=="__main__":
             data = json.loads(descriptor_str)
             format_output_yaml = False
 
+        if test_file:
+            import osm_im.vnfd as vnfd_catalog
+            import osm_im.nsd as nsd_catalog
+            from pyangbind.lib.serialise import pybindJSONDecoder
+
+            if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
+                descriptor = "VNF"
+                myvnfd = vnfd_catalog.vnfd()
+                pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd)
+            elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
+                descriptor = "NS"
+                mynsd = nsd_catalog.nsd()
+                pybindJSONDecoder.load_ietf_json(data, None, None, obj=mynsd)
+            else:
+                descriptor = None
+                raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
+            exit(0)
+
         # Convert version
         if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
             remove_prefix(data, "vnfd:")
             error_position.append("vnfd-catalog")
             vnfd_descriptor = data["vnfd-catalog"]
-
             vnfd_list = vnfd_descriptor["vnfd"]
             error_position.append("vnfd")
             for vnfd in vnfd_list:
                 error_position[-1] = "vnfd[{}]".format(vnfd["id"])
-
                 # Remove vnf-configuration:config-attributes
                 if "vnf-configuration" in vnfd and "config-attributes" in vnfd["vnf-configuration"]:
                     del vnfd["vnf-configuration"]["config-attributes"]
+                # Remove interval-vld:vendor
+                if "internal-vld" in vnfd:
+                    internal_vld_list = vnfd.get("internal-vld", ())
+                    for internal_vld in internal_vld_list:
+                        if "vendor" in internal_vld:
+                            del internal_vld["vendor"]
+                # Remove "rw-nsd:meta"
+                if "rw-vnfd:meta" in vnfd:
+                    del vnfd["rw-vnfd:meta"]
+                # Change vnf-configuration:service-primitive into vnf-configuration:config-primitive
+                if "vnf-configuration" in vnfd and "service-primitive" in vnfd["vnf-configuration"]:
+                    vnfd["vnf-configuration"]["config-primitive"] = vnfd["vnf-configuration"].pop("service-primitive")
+
+                #Convert to capital letters vnf-configuration:service-primitive:parameter:data-type
+                if "vnf-configuration" in vnfd and "config-primitive" in vnfd["vnf-configuration"]:
+                    error_position.append("vnf-configuration")
+                    error_position.append("config-primitive")
+                    primitive_list = vnfd["vnf-configuration"].get("config-primitive", ())
 
+                    for primitive in primitive_list:
+                        if "parameter" in primitive:
+                            parameter_list = primitive.get("parameter", ())
+                            for parameter in parameter_list:
+                                parameter["data-type"] = str(parameter["data-type"]).upper()
+
+                    vnfd["vnf-configuration"]["config-primitive"] = primitive_list
+                    error_position.pop()
+                    error_position.pop()
                 # Iterate with vdu:interfaces
                 vdu_list = vnfd["vdu"]
                 error_position.append("vdu")
                 for vdu in vdu_list:
                     error_position[-1] = "vdu[{}]".format(vdu["id"])
-
-                    # Change exernal/internal interface
+                    # Change external/internal interface
                     interface_list = []
                     external_interface_list = vdu.pop("external-interface", ())
                     error_position.append("external-interface")
@@ -153,7 +196,6 @@ if __name__=="__main__":
                             external_interface.pop("vnfd-connection-point-ref")
                         interface_list.append(external_interface)
                     error_position.pop()
-
                     internal_interface_list = vdu.pop("internal-interface", ())
                     error_position.append("internal-interface")
                     for internal_interface in internal_interface_list:
@@ -164,8 +206,6 @@ if __name__=="__main__":
                         interface_list.append(internal_interface)
                     error_position.pop()
 
-                    if interface_list:
-                        vdu["interface"] = interface_list
                 error_position.pop()
             error_position = []
         elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
@@ -181,6 +221,22 @@ if __name__=="__main__":
                 # Change initial-config-primitive into initial-service-primitive
                 if "initial-config-primitive" in nsd:
                     nsd['initial-service-primitive'] = nsd.pop("initial-config-primitive")
+                # Remove "rw-nsd:meta"
+                if "rw-nsd:meta" in nsd:
+                    del nsd["rw-nsd:meta"]
+                # Remove "rw-meta"
+                if "rw-meta" in nsd:
+                    del nsd["rw-meta"]
+                # Iterate with vld:id
+                error_position.append("vld")
+                vld_list = nsd.get("vld",())
+                for vld in vld_list:
+                    error_position[-1] = "vld[{}]".format(vld["id"])
+                    if "provider-network" in vld and "overlay-type" in vld["provider-network"]:
+                        del vld["provider-network"]["overlay-type"]
+                error_position.pop()
+                if vld_list:
+                    nsd["vld"] = vld_list
             error_position = []
         else:
             error_position = ["global"]
@@ -190,6 +246,7 @@ if __name__=="__main__":
             yaml.dump(data, output, indent=4, default_flow_style=False)
         else:
             json.dump(data, output)
+        exit(0)
 
     except yaml.YAMLError as exc:
         error_pos = ""
@@ -198,16 +255,26 @@ if __name__=="__main__":
             error_pos = "at line:%s column:%s" % (mark.line + 1, mark.column + 1)
         print("Error loading file '{}'. yaml format error {}".format(input_file_name, error_pos), file=sys.stderr)
 
-    except json.decoder.JSONDecodeError as e:
-        print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name, message=str(e)),
-              file=sys.stderr)
+#    except json.decoder.JSONDecodeError as e:
+#        print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name, message=str(e)),
+#              file=sys.stderr)
     except ArgumentParserError as e:
         print(str(e), file=sys.stderr)
     except IOError as e:
-        print("Error loading file '{}': {}".format(file_name, e), file=sys.stderr)
+            print("Error loading file '{}': {}".format(file_name, e), file=sys.stderr)
+    except ImportError as e:
+        print ("Package python-osm-im not installed: {}".format(e), file=sys.stderr)
     except Exception as e:
-        if error_position:
+        if test_file:
+            if descriptor:
+                print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor, input_file_name, str(e)), file=sys.stderr)
+            else:
+                print("Error. Invalid descriptor format in '{}': {}".format(input_file_name, str(e)),
+                      file=sys.stderr)
+        elif error_position:
             print("Descriptor error at '{}': {}".format(":".join(error_position), e), file=sys.stderr)
+        elif file_name:
+            print ("Error loading file '{}': {}".format(file_name, str(e)), file=sys.stderr)
         else:
             raise
             # print("Unexpected exception {}".format(e), file=sys.stderr)