Separated tool to test descriptors format
[osm/devops.git] / descriptor-packages / tools / validate_descriptor.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 ##
5 # All Rights Reserved.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18 #
19 ##
20 from __future__ import print_function
21 import json
22 import yaml
23 import sys
24 import getopt
25
26 """
27 Tests the format of OSM VNFD and NSD descriptors
28 """
29 __author__ = "Alfonso Tierno, Guillermo Calvino"
30 __date__ = "2018-04-16"
31 __version__ = "0.0.1"
32 version_date = "Apr 2018"
33
34
35 class ArgumentParserError(Exception):
36 pass
37
38
39 def usage():
40 print("Usage: {} [options] FILE".format(sys.argv[0]))
41 print(" EXPERIMENTAL: Validates vnfd, nsd descriptors format")
42 print(" FILE: a yaml or json vnfd-catalog or nsd-catalog descriptor")
43 print(" OPTIONS:")
44 print(" -v|--version: prints current version")
45 print(" -h|--help: shows this help")
46 print(" -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
47 return
48
49 if __name__=="__main__":
50 error_position = []
51 format_output_yaml = True
52 input_file_name = None
53 test_file = None
54 file_name = None
55 try:
56 # load parameters and configuration
57 opts, args = getopt.getopt(sys.argv[1:], "hvi:o:", ["input=", "help", "version",])
58
59 for o, a in opts:
60 if o in ("-v", "--version"):
61 print ("test descriptor version THREE " + __version__ + ' ' + version_date)
62 sys.exit()
63 elif o in ("-h", "--help"):
64 usage()
65 sys.exit()
66 elif o in ("-i", "--input"):
67 input_file_name = a
68 else:
69 assert False, "Unhandled option"
70 if not input_file_name:
71 if not args:
72 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
73 input_file_name = args[0]
74
75 # Open files
76 file_name = input_file_name
77 with open(file_name, 'r') as f:
78 descriptor_str = f.read()
79 file_name = None
80
81 if input_file_name.endswith('.yaml') or input_file_name.endswith('.yml') or not \
82 (input_file_name.endswith('.json') or '\t' in descriptor_str):
83 data = yaml.load(descriptor_str)
84 else: # json
85 data = json.loads(descriptor_str)
86 format_output_yaml = False
87
88 import osm_im.vnfd as vnfd_catalog
89 import osm_im.nsd as nsd_catalog
90 from pyangbind.lib.serialise import pybindJSONDecoder
91
92 if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
93 descriptor = "VNF"
94 myvnfd = vnfd_catalog.vnfd()
95 pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd)
96 elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
97 descriptor = "NS"
98 mynsd = nsd_catalog.nsd()
99 pybindJSONDecoder.load_ietf_json(data, None, None, obj=mynsd)
100 else:
101 descriptor = None
102 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
103 exit(0)
104
105 except yaml.YAMLError as exc:
106 error_pos = ""
107 if hasattr(exc, 'problem_mark'):
108 mark = exc.problem_mark
109 error_pos = "at line:%s column:%s" % (mark.line + 1, mark.column + 1)
110 print("Error loading file '{}'. yaml format error {}".format(input_file_name, error_pos), file=sys.stderr)
111 except ArgumentParserError as e:
112 print(str(e), file=sys.stderr)
113 except IOError as e:
114 print("Error loading file '{}': {}".format(file_name, e), file=sys.stderr)
115 except ImportError as e:
116 print ("Package python-osm-im not installed: {}".format(e), file=sys.stderr)
117 except Exception as e:
118 if file_name:
119 print("Error loading file '{}': {}".format(file_name, str(e)), file=sys.stderr)
120 else:
121 if descriptor:
122 print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor, input_file_name, str(e)), file=sys.stderr)
123 else:
124 print("Error. Invalid descriptor format in '{}': {}".format(input_file_name, str(e)), file=sys.stderr)
125 exit(1)