2 # -*- coding: utf-8 -*-
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from __future__
import print_function
27 Tests the format of OSM VNFD and NSD descriptors
29 __author__
= "Alfonso Tierno, Guillermo Calvino"
30 __date__
= "2018-04-16"
32 version_date
= "Apr 2018"
35 class ArgumentParserError(Exception):
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")
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")
49 if __name__
=="__main__":
51 format_output_yaml
= True
52 input_file_name
= None
56 # load parameters and configuration
57 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvi:o:", ["input=", "help", "version",])
60 if o
in ("-v", "--version"):
61 print ("test descriptor version THREE " + __version__
+ ' ' + version_date
)
63 elif o
in ("-h", "--help"):
66 elif o
in ("-i", "--input"):
69 assert False, "Unhandled option"
70 if not input_file_name
:
72 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
73 input_file_name
= args
[0]
76 file_name
= input_file_name
77 with
open(file_name
, 'r') as f
:
78 descriptor_str
= f
.read()
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
)
85 data
= json
.loads(descriptor_str
)
86 format_output_yaml
= False
88 import osm_im
.vnfd
as vnfd_catalog
89 import osm_im
.nsd
as nsd_catalog
90 from pyangbind
.lib
.serialise
import pybindJSONDecoder
92 if "vnfd:vnfd-catalog" in data
or "vnfd-catalog" in data
:
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
:
98 mynsd
= nsd_catalog
.nsd()
99 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=mynsd
)
102 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
105 except yaml
.YAMLError
as exc
:
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
)
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
:
119 print("Error loading file '{}': {}".format(file_name
, str(e
)), file=sys
.stderr
)
122 print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor
, input_file_name
, str(e
)), file=sys
.stderr
)
124 print("Error. Invalid descriptor format in '{}': {}".format(input_file_name
, str(e
)), file=sys
.stderr
)