Merge "save artifacts on stage_4 success"
[osm/devops.git] / descriptor-packages / tools / upgrade_descriptor_version.py
1 #!/usr/bin/env python3
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
21 import json
22 import yaml
23 # import logging
24 import sys
25 import getopt
26
27
28 """
29 Converts OSM VNFD, NSD descriptor from release TWO to release THREE format
30 """
31 __author__ = "Alfonso Tierno"
32 __date__ = "2017-10-14"
33 __version__ = "0.0.1"
34 version_date = "Nov 2017"
35
36
37 class ArgumentParserError(Exception):
38 pass
39
40
41 def usage():
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")
45 print(" OPTIONS:")
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")
51 return
52
53
54 def remove_prefix(desc, prefix):
55 """
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
60 """
61 prefix_len = len(prefix)
62 if isinstance(desc, dict):
63 prefixed_list=[]
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)):
72 for i in desc:
73 if isinstance(desc, (list, tuple, dict)):
74 remove_prefix(i, prefix)
75
76
77 if __name__=="__main__":
78 error_position = []
79 format_output_yaml = True
80 input_file_name = None
81 output_file_name = None
82 test_file = None
83 try:
84 # load parameters and configuration
85 opts, args = getopt.getopt(sys.argv[1:], "hvi:o:", ["input=", "help", "version", "output=", "test",])
86
87 for o, a in opts:
88 if o in ("-v", "--version"):
89 print ("upgrade descriptor version " + __version__ + ' ' + version_date)
90 sys.exit()
91 elif o in ("-h", "--help"):
92 usage()
93 sys.exit()
94 elif o in ("-i", "--input"):
95 input_file_name = a
96 elif o in ("-o", "--output"):
97 output_file_name = a
98 elif o == "--test":
99 test_file = True
100 else:
101 assert False, "Unhandled option"
102 if not input_file_name:
103 if not args:
104 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
105 input_file_name = args[0]
106
107 # Open files
108 file_name = input_file_name
109 with open(file_name, 'r') as f:
110 descriptor_str = f.read()
111 if output_file_name:
112 file_name = output_file_name
113 output = open(file_name, 'w')
114 else:
115 output = sys.stdout
116
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)
120 else: # json
121 data = json.loads(descriptor_str)
122 format_output_yaml = False
123
124 # Convert version
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"]
129
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"])
134
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"]
138
139 # Iterate with vdu:interfaces
140 vdu_list = vnfd["vdu"]
141 error_position.append("vdu")
142 for vdu in vdu_list:
143 error_position[-1] = "vdu[{}]".format(vdu["id"])
144
145 # Change exernal/internal interface
146 interface_list = []
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)
155 error_position.pop()
156
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)
165 error_position.pop()
166
167 if interface_list:
168 vdu["interface"] = interface_list
169 error_position.pop()
170 error_position = []
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"]
175
176 nsd_list = nsd_descriptor["nsd"]
177 error_position.append("nsd")
178 for nsd in nsd_list:
179 error_position[-1] = "nsd[{}]".format(nsd["id"])
180
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")
184 error_position = []
185 else:
186 error_position = ["global"]
187 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
188
189 if format_output_yaml:
190 yaml.dump(data, output, indent=4, default_flow_style=False)
191 else:
192 json.dump(data, output)
193
194 except yaml.YAMLError as exc:
195 error_pos = ""
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)
200
201 except json.decoder.JSONDecodeError as e:
202 print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name, message=str(e)),
203 file=sys.stderr)
204 except ArgumentParserError as e:
205 print(str(e), file=sys.stderr)
206 except IOError as e:
207 print("Error loading file '{}': {}".format(file_name, e), file=sys.stderr)
208 except Exception as e:
209 if error_position:
210 print("Descriptor error at '{}': {}".format(":".join(error_position), e), file=sys.stderr)
211 else:
212 raise
213 # print("Unexpected exception {}".format(e), file=sys.stderr)
214 exit(1)