4259ece6b0fd6a05f04cf0e547ea0ea9e18915fa
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
29 Converts OSM VNFD, NSD descriptor from release TWO to release THREE format
31 __author__
= "Alfonso Tierno"
32 __date__
= "2017-10-14"
34 version_date
= "Nov 2017"
37 class ArgumentParserError(Exception):
42 print("Usage: {} [options] FILE".format(sys
.argv
[0]))
43 print(" EXPERIMENTAL: Upgrade vnfd, nsd descriptor from old versions to release THREE version")
44 print(" FILE: a yaml or json vnfd-catalog or nsd-catalog descriptor")
46 print(" -v|--version: prints current version")
47 print(" -h|--help: shows this help")
48 print(" -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
49 print(" -o|--output FILE: where to write generated descriptor. By default stdout")
50 # print(" --test: Content is tested to check wrong format or unknown keys")
54 def remove_prefix(desc
, prefix
):
56 Recursively removes prefix from keys
57 :param desc: dictionary or list to change
58 :param prefix: prefix to remove. Must
59 :return: None, param desc is changed
61 prefix_len
= len(prefix
)
62 if isinstance(desc
, dict):
64 for k
,v
in desc
.items():
65 if isinstance(v
, (list, tuple, dict)):
66 remove_prefix(v
, prefix
)
67 if isinstance(k
, str) and k
.startswith(prefix
) and k
!= prefix
:
68 prefixed_list
.append(k
)
69 for k
in prefixed_list
:
70 desc
[k
[prefix_len
:]] = desc
.pop(k
)
71 elif isinstance(desc
, (list, tuple)):
73 if isinstance(desc
, (list, tuple, dict)):
74 remove_prefix(i
, prefix
)
77 if __name__
=="__main__":
79 format_output_yaml
= True
80 input_file_name
= None
81 output_file_name
= None
84 # load parameters and configuration
85 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvi:o:", ["input=", "help", "version", "output=", "test",])
88 if o
in ("-v", "--version"):
89 print ("upgrade descriptor version " + __version__
+ ' ' + version_date
)
91 elif o
in ("-h", "--help"):
94 elif o
in ("-i", "--input"):
96 elif o
in ("-o", "--output"):
101 assert False, "Unhandled option"
102 if not input_file_name
:
104 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
105 input_file_name
= args
[0]
108 file_name
= input_file_name
109 with
open(file_name
, 'r') as f
:
110 descriptor_str
= f
.read()
112 file_name
= output_file_name
113 output
= open(file_name
, 'w')
117 if input_file_name
.endswith('.yaml') or input_file_name
.endswith('.yml') or not \
118 (input_file_name
.endswith('.json') or '\t' in descriptor_str
):
119 data
= yaml
.load(descriptor_str
)
121 data
= json
.loads(descriptor_str
)
122 format_output_yaml
= False
125 if "vnfd:vnfd-catalog" in data
or "vnfd-catalog" in data
:
126 remove_prefix(data
, "vnfd:")
127 error_position
.append("vnfd-catalog")
128 vnfd_descriptor
= data
["vnfd-catalog"]
130 vnfd_list
= vnfd_descriptor
["vnfd"]
131 error_position
.append("vnfd")
132 for vnfd
in vnfd_list
:
133 error_position
[-1] = "vnfd[{}]".format(vnfd
["id"])
135 # Remove vnf-configuration:config-attributes
136 if "vnf-configuration" in vnfd
and "config-attributes" in vnfd
["vnf-configuration"]:
137 del vnfd
["vnf-configuration"]["config-attributes"]
139 # Iterate with vdu:interfaces
140 vdu_list
= vnfd
["vdu"]
141 error_position
.append("vdu")
143 error_position
[-1] = "vdu[{}]".format(vdu
["id"])
145 # Change exernal/internal interface
147 external_interface_list
= vdu
.pop("external-interface", ())
148 error_position
.append("external-interface")
149 for external_interface
in external_interface_list
:
150 error_position
[-1] = "external-interface[{}]".format(external_interface
["name"])
151 external_interface
["type"] = "EXTERNAL"
152 external_interface
["external-connection-point-ref"] = \
153 external_interface
.pop("vnfd-connection-point-ref")
154 interface_list
.append(external_interface
)
157 internal_interface_list
= vdu
.pop("internal-interface", ())
158 error_position
.append("internal-interface")
159 for internal_interface
in internal_interface_list
:
160 error_position
[-1] = "internal-interface[{}]".format(internal_interface
["name"])
161 internal_interface
["type"] = "INTERNAL"
162 internal_interface
["internal-connection-point-ref"] = \
163 internal_interface
.pop("vdu-internal-connection-point-ref")
164 interface_list
.append(internal_interface
)
168 vdu
["interface"] = interface_list
171 elif "nsd:nsd-catalog" in data
or "nsd-catalog" in data
:
172 remove_prefix(data
, "nsd:")
173 error_position
.append("nsd-catalog")
174 nsd_descriptor
= data
["nsd-catalog"]
176 nsd_list
= nsd_descriptor
["nsd"]
177 error_position
.append("nsd")
179 error_position
[-1] = "nsd[{}]".format(nsd
["id"])
181 # Change initial-config-primitive into initial-service-primitive
182 if "initial-config-primitive" in nsd
:
183 nsd
['initial-service-primitive'] = nsd
.pop("initial-config-primitive")
186 error_position
= ["global"]
187 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
189 if format_output_yaml
:
190 yaml
.dump(data
, output
, indent
=4, default_flow_style
=False)
192 json
.dump(data
, output
)
194 except yaml
.YAMLError
as exc
:
196 if hasattr(exc
, 'problem_mark'):
197 mark
= exc
.problem_mark
198 error_pos
= "at line:%s column:%s" % (mark
.line
+ 1, mark
.column
+ 1)
199 print("Error loading file '{}'. yaml format error {}".format(input_file_name
, error_pos
), file=sys
.stderr
)
201 except json
.decoder
.JSONDecodeError
as e
:
202 print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name
, message
=str(e
)),
204 except ArgumentParserError
as e
:
205 print(str(e
), file=sys
.stderr
)
207 print("Error loading file '{}': {}".format(file_name
, e
), file=sys
.stderr
)
208 except Exception as e
:
210 print("Descriptor error at '{}': {}".format(":".join(error_position
), e
), file=sys
.stderr
)
213 # print("Unexpected exception {}".format(e), file=sys.stderr)