RIFT OSM R1 Initial Submission
[osm/SO.git] / common / python / rift / mano / yang_translator / common / utils.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import gettext
19 import numbers
20 import os
21
22 from six.moves.urllib.parse import urlparse
23
24 import yaml
25
26 _localedir = os.environ.get('yang-translator'.upper() + '_LOCALEDIR')
27 _t = gettext.translation('yang-translator', localedir=_localedir,
28 fallback=True)
29
30
31 def _(msg):
32 return _t.gettext(msg)
33
34
35 class CompareUtils(object):
36
37 MISMATCH_VALUE1_LABEL = "<Expected>"
38 MISMATCH_VALUE2_LABEL = "<Provided>"
39 ORDERLESS_LIST_KEYS = ['allowed_values', 'depends_on']
40
41 @staticmethod
42 def compare_dicts(dict1, dict2, log):
43 """Return False if not equal, True if both are equal."""
44
45 if dict1 is None and dict2 is None:
46 return True
47 if dict1 is None or dict2 is None:
48 return False
49
50 both_equal = True
51 for dict1_item, dict2_item in zip(dict1.items(), dict2.items()):
52 if dict1_item != dict2_item:
53 msg = (_("%(label1)s: %(item1)s \n is not equal to \n:"
54 "%(label2)s: %(item2)s")
55 % {'label1': CompareUtils.MISMATCH_VALUE2_LABEL,
56 'item1': dict1_item,
57 'label2': CompareUtils.MISMATCH_VALUE1_LABEL,
58 'item2': dict2_item})
59 log.warning(msg)
60 both_equal = False
61 break
62 return both_equal
63
64 @staticmethod
65 def reorder(dic):
66 '''Canonicalize list items in the dictionary for ease of comparison.
67
68 For properties whose value is a list in which the order does not
69 matter, some pre-processing is required to bring those lists into a
70 canonical format. We use sorting just to make sure such differences
71 in ordering would not cause to a mismatch.
72 '''
73
74 if type(dic) is not dict:
75 return None
76
77 reordered = {}
78 for key in dic.keys():
79 value = dic[key]
80 if type(value) is dict:
81 reordered[key] = CompareUtils.reorder(value)
82 elif type(value) is list \
83 and key in CompareUtils.ORDERLESS_LIST_KEYS:
84 reordered[key] = sorted(value)
85 else:
86 reordered[key] = value
87 return reordered
88
89 @staticmethod
90 def diff_dicts(dict1, dict2, reorder=True):
91 '''Compares two dictionaries and returns their differences.
92
93 Returns a dictionary of mismatches between the two dictionaries.
94 An empty dictionary is returned if two dictionaries are equivalent.
95 The reorder parameter indicates whether reordering is required
96 before comparison or not.
97 '''
98
99 if reorder:
100 dict1 = CompareUtils.reorder(dict1)
101 dict2 = CompareUtils.reorder(dict2)
102
103 if dict1 is None and dict2 is None:
104 return {}
105 if dict1 is None or dict2 is None:
106 return {CompareUtils.MISMATCH_VALUE1_LABEL: dict1,
107 CompareUtils.MISMATCH_VALUE2_LABEL: dict2}
108
109 diff = {}
110 keys1 = set(dict1.keys())
111 keys2 = set(dict2.keys())
112 for key in keys1.union(keys2):
113 if key in keys1 and key not in keys2:
114 diff[key] = {CompareUtils.MISMATCH_VALUE1_LABEL: dict1[key],
115 CompareUtils.MISMATCH_VALUE2_LABEL: None}
116 elif key not in keys1 and key in keys2:
117 diff[key] = {CompareUtils.MISMATCH_VALUE1_LABEL: None,
118 CompareUtils.MISMATCH_VALUE2_LABEL: dict2[key]}
119 else:
120 val1 = dict1[key]
121 val2 = dict2[key]
122 if val1 != val2:
123 if type(val1) is dict and type(val2) is dict:
124 diff[key] = CompareUtils.diff_dicts(val1, val2, False)
125 else:
126 diff[key] = {CompareUtils.MISMATCH_VALUE1_LABEL: val1,
127 CompareUtils.MISMATCH_VALUE2_LABEL: val2}
128 return diff
129
130
131 class YamlUtils(object):
132
133 @staticmethod
134 def get_dict(yaml_file):
135 '''Returns the dictionary representation of the given YAML spec.'''
136 try:
137 return yaml.load(open(yaml_file))
138 except IOError:
139 return None
140
141 @staticmethod
142 def compare_yamls(yaml1_file, yaml2_file):
143 '''Returns true if two dictionaries are equivalent, false otherwise.'''
144 dict1 = YamlUtils.get_dict(yaml1_file)
145 dict2 = YamlUtils.get_dict(yaml2_file)
146 return CompareUtils.compare_dicts(dict1, dict2)
147
148 @staticmethod
149 def compare_yaml_dict(yaml_file, dic):
150 '''Returns true if yaml matches the dictionary, false otherwise.'''
151 return CompareUtils.compare_dicts(YamlUtils.get_dict(yaml_file), dic)
152
153
154 class UrlUtils(object):
155
156 @staticmethod
157 def validate_url(path):
158 """Validates whether the given path is a URL or not.
159
160 If the given path includes a scheme (http, https, ftp, ...) and a net
161 location (a domain name such as www.github.com) it is validated as a
162 URL.
163 """
164 parsed = urlparse(path)
165 return bool(parsed.scheme) and bool(parsed.netloc)
166
167
168 def str_to_num(value):
169 """Convert a string representation of a number into a numeric type."""
170 if isinstance(value, numbers.Number):
171 return value
172 try:
173 return int(value)
174 except ValueError:
175 return float(value)
176
177
178 def map_name_to_python(name):
179 if name == 'type':
180 return 'type_yang'
181 return name.replace('-', '_')
182
183
184 def convert_keys_to_python(d):
185 '''Change all keys from - to _'''
186 if isinstance(d, dict):
187 for key in d.keys():
188 d[map_name_to_python(key)] = convert_keys_to_python(d.pop(key))
189 return d
190 elif isinstance(d, list):
191 arr = []
192 for memb in d:
193 arr.append(convert_keys_to_python(memb))
194 return arr
195 else:
196 return d
197
198 def map_name_to_yang(name):
199 return name.replace('_', '-')
200
201
202 def convert_keys_to_yang(d):
203 '''Change all keys from _ to -'''
204 if isinstance(d, dict):
205 for key in d.keys():
206 d[map_name_to_python(key)] = convert_keys_to_yang(d.pop(key))
207 return d
208 elif isinstance(d, list):
209 arr = []
210 for memb in d:
211 arr.append(convert_keys_to_yang(memb))
212 return arr
213 else:
214 return d
215
216
217 def stringify_dict(d):
218 '''Convert all integer, float, etc to str'''
219 if isinstance(d, dict):
220 for key in d.keys():
221 d[key] = stringify_dict(d[key])
222 return d
223 elif isinstance(d, list):
224 arr = []
225 for memb in d:
226 arr.append(stringify_dict(memb))
227 return arr
228 else:
229 if not isinstance(d, str):
230 return str(d)
231 return d