Adding missing dependency
[osm/devops.git] / descriptor-packages / tools / validate_descriptor.py
index 1c67195..de958fe 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 ##
@@ -36,6 +36,8 @@ version_date = "Apr 2018"
 class ArgumentParserError(Exception):
     pass
 
+class DescriptorValidationError(Exception):
+    pass
 
 def usage():
     print("Usage: {} [options] FILE".format(sys.argv[0]))
@@ -45,6 +47,7 @@ def usage():
     print("      -v|--version: prints current version")
     print("      -h|--help: shows this help")
     print("      -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
+    print("      -c|--charms: looks for the charms folder and validates its coherency with the descriptor")
     return
 
 
@@ -102,19 +105,22 @@ if __name__ == "__main__":
     input_file_name = None
     test_file = None
     file_name = None
+    validate_charms = False
     try:
         # load parameters and configuration
         opts, args = getopt.getopt(sys.argv[1:], "hvi:o:", ["input=", "help", "version",])
 
         for o, a in opts:
             if o in ("-v", "--version"):
-                print ("test descriptor version THREE " + __version__ + ' ' + version_date)
+                print("test descriptor version THREE " + __version__ + ' ' + version_date)
                 sys.exit()
             elif o in ("-h", "--help"):
                 usage()
                 sys.exit()
             elif o in ("-i", "--input"):
                 input_file_name = a
+            elif o in ("-c", "--charms"):
+                validate_charms = True
             else:
                 assert False, "Unhandled option"
         if not input_file_name:
@@ -148,7 +154,9 @@ if __name__ == "__main__":
             vnfd_list = vnfd_descriptor["vnfd"]
             mgmt_iface = False
             for vnfd in vnfd_list:
-                vdu_list = vnfd["vdu"]
+                if "vdu" not in vnfd and "kdu" not in vnfd:
+                    raise DescriptorValidationError("vdu or kdu not present in the descriptor")
+                vdu_list = vnfd.get("vdu",[])
                 for vdu in vdu_list:
                     interface_list = []
                     external_interface_list = vdu.pop("external-interface", ())
@@ -162,18 +170,19 @@ if __name__ == "__main__":
                             raise KeyError(
                                 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'PARAVIRT' instead")
                     # Mrityunjay yadav: Verify charm if included in vdu
-                    if vdu.get("vdu-configuration", False):
+                    if vdu.get("vdu-configuration", False) and validate_charms:
                         validate_charm(vdu["vdu-configuration"], input_file_name)
                 if vnfd.get("mgmt-interface"):
                     mgmt_iface = True
                     if vnfd["mgmt-interface"].get("vdu-id"):
                         raise KeyError("'mgmt-iface': Deprecated 'vdu-id' field. Please, use 'cp' field instead")
                 # Mrityunjay yadav: Verify charm if included in vnf
-                if vnfd.get("vnf-configuration", False):
+                if vnfd.get("vnf-configuration", False) and validate_charms:
                     validate_charm(vnfd["vnf-configuration"], input_file_name)
+                kdu_list = vnfd.get("kdu",[])
 
             if not mgmt_iface:
-                raise KeyError("'mgmt-iface' is a mandatory field and it is not defined")
+                raise KeyError("'mgmt-interface' 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:
@@ -195,10 +204,12 @@ if __name__ == "__main__":
             mark = exc.problem_mark
             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 DescriptorValidationError as e:
+        print(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: