blob: 4d48d16107bd93271f4ea58cacb708b4b1485051 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Zack Williamsc6722d52020-01-13 16:34:33 -070015from __future__ import absolute_import, print_function
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -070016
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070017import json
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070018import uuid
Kailash Khalasi2adbad82017-05-15 14:53:40 -070019import random
Kailash Khalasi86e231e2017-06-06 13:13:43 -070020import yaml
You Wangaabb2832017-11-16 17:24:09 -080021import glob
Kailash Khalasi8416bfe2017-12-20 13:06:37 -080022import string
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070023
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070024
Zack Williams821c5022020-01-15 15:11:46 -070025class CORDDictUtils(object):
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070026 @staticmethod
27 def listToDict(alist, intListIndex):
28 dictInfo = alist[int(intListIndex)]
29 return dictInfo
30
31 @staticmethod
32 def jsonToList(strFile, strListName):
33 data = json.loads(open(strFile).read())
Zack Williamsc6722d52020-01-13 16:34:33 -070034 # print "data...",data
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070035 dataList = data[strListName]
36 return dataList
37
You Wangaabb2832017-11-16 17:24:09 -080038 def readFile(self, path, single=True):
39 dataDict = {}
40 for fileName in glob.glob(path):
Zack Williamsc6722d52020-01-13 16:34:33 -070041 print("Reading ", fileName)
You Wangaabb2832017-11-16 17:24:09 -080042 data = open(fileName).read()
43 dataDict[fileName] = data
44 if bool(single):
45 return data
You Wangf35df4a2018-01-24 11:00:58 -080046 if not dataDict:
Zack Williamsc6722d52020-01-13 16:34:33 -070047 print("Failed to find the file!")
You Wangf35df4a2018-01-24 11:00:58 -080048 return None
You Wangaabb2832017-11-16 17:24:09 -080049 return dataDict
50
51 def readFiles(self, path):
52 return self.readFile(path, single=False)
53
Zack Williamsc6722d52020-01-13 16:34:33 -070054 """
Suchitra.Vemuri85220062016-10-25 10:44:11 -070055 @method compare_dict
56 @Description: validates if contents of dict1 exists in dict2
57 @params: dict1 = input_data entered through api
58 dict2 = retrieved data from GET method
59 returns True if contents of dict1 exists in dict2
Zack Williamsc6722d52020-01-13 16:34:33 -070060 """
61
You Wang507c4562016-11-23 13:36:09 -080062 def compare_dict(self, dict1, dict2):
Zack Williamsc6722d52020-01-13 16:34:33 -070063 print("input data", dict1)
64 print("get data", dict2)
65 if dict1 is None or dict2 is None:
66 return False
67 if not isinstance(dict1, dict) or not isinstance(dict2, dict):
68 return False
You Wang507c4562016-11-23 13:36:09 -080069 if dict1 == {}:
70 return True
71 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070072
Zack Williamsc6722d52020-01-13 16:34:33 -070073 """
You Wang507c4562016-11-23 13:36:09 -080074 @method compare_dict_recursive
75 @Description: recursive function to validate if dict1 is a subset of dict2
76 returns True if contents of dict1 exists in dict2
Zack Williamsc6722d52020-01-13 16:34:33 -070077 """
78
You Wang507c4562016-11-23 13:36:09 -080079 def compare_dict_recursive(self, dict1, dict2):
Zack Williamsc6722d52020-01-13 16:34:33 -070080 for key1, value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080081 if key1 not in dict2.keys():
Zack Williamsc6722d52020-01-13 16:34:33 -070082 print("Missing key", key1, "in dict2")
You Wang507c4562016-11-23 13:36:09 -080083 return False
84 value2 = dict2[key1]
Zack Williamsc6722d52020-01-13 16:34:33 -070085 if isinstance(value1, dict) and isinstance(value2, dict):
You Wang507c4562016-11-23 13:36:09 -080086 if not self.compare_dict_recursive(value1, value2):
87 return False
88 else:
89 if value2 != value1:
Zack Williamsc6722d52020-01-13 16:34:33 -070090 print("Values of key", key1, "in two dicts are not equal")
You Wang507c4562016-11-23 13:36:09 -080091 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070092 return True
93
Zack Williamsc6722d52020-01-13 16:34:33 -070094 """
You Wang3964e842016-12-09 12:04:32 -080095 @method compare_list_of_dicts
96 @Description: validates if contents of dicts in list1 exists in dicts of list2
97 returns True if for each dict in list1, there's a dict in list2 that contains its content
Zack Williamsc6722d52020-01-13 16:34:33 -070098 """
99
You Wang3964e842016-12-09 12:04:32 -0800100 def compare_list_of_dicts(self, list1, list2):
101 for dict1 in list1:
102 if dict1 == {}:
103 continue
Scott Baker3545ca12020-04-23 17:29:07 -0700104 key = list(dict1.keys())[0]
You Wang3964e842016-12-09 12:04:32 -0800105 value = dict1[key]
106 dict2 = self.getDictFromListOfDict(list2, key, value)
107 if dict2 == {}:
Zack Williamsc6722d52020-01-13 16:34:33 -0700108 print(
109 "Comparison failed: no dictionaries found in list2 with key",
110 key,
111 "and value",
112 value,
113 )
You Wang3964e842016-12-09 12:04:32 -0800114 return False
Zack Williamsc6722d52020-01-13 16:34:33 -0700115 if not self.compare_dict(dict1, dict2):
116 print(
117 "Comparison failed: dictionary",
118 dict1,
119 "is not a subset of dictionary",
120 dict2,
121 )
You Wang3964e842016-12-09 12:04:32 -0800122 return False
123 return True
124
Zack Williamsc6722d52020-01-13 16:34:33 -0700125 """
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700126 @method search_dictionary
127 @Description: Searches for a key in the provided nested dictionary
128 @params: input_dict = dictionary to be searched
129 search_key = name of the key to be searched for
130 returns two values: search_key value and status of the search.
131 True if found (False when not found)
132
Zack Williamsc6722d52020-01-13 16:34:33 -0700133 """
134
135 def search_dictionary(self, input_dict, search_key):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700136 input_keys = input_dict.keys()
Zack Williamsc6722d52020-01-13 16:34:33 -0700137 key_value = ""
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700138 found = False
139 for key in input_keys:
140 if key == search_key:
Zack Williamsc6722d52020-01-13 16:34:33 -0700141 key_value = input_dict[key]
142 found = True
143 break
144 elif isinstance(input_dict[key], dict):
145 key_value, found = self.search_dictionary(
146 input_dict[key], search_key)
147 if found:
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700148 break
Zack Williamsc6722d52020-01-13 16:34:33 -0700149 elif isinstance(input_dict[key], list):
150 if not input_dict[key]:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800151 found = False
152 break
Zack Williamsc6722d52020-01-13 16:34:33 -0700153 for item in input_dict[key]:
154 if isinstance(item, dict):
155 key_value, found = self.search_dictionary(
156 item, search_key)
157 if found:
158 break
159 return key_value, found
160
161 """
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800162 @method getDictFromListOfDict
163 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700164 @Description: Searches for the dictionary in the provided list of dictionaries
165 that matches the value of the key provided
166 @params : List of dictionaries(getResponse Data from the URL),
167 SearchKey - Key that needs to be searched for (ex: account_num)
168 searchKeyValue - Value of the searchKey (ex: 21)
169 @Returns: Dictionary returned when match found for searchKey with the corresponding
170 searchKeyValue provided
Zack Williamsc6722d52020-01-13 16:34:33 -0700171 """
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700172
Zack Williamsc6722d52020-01-13 16:34:33 -0700173 def getDictFromListOfDict(self, getJsonDataList,
174 searchKey, searchKeyValue):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700175 return_dict = {}
Zack Williamsc6722d52020-01-13 16:34:33 -0700176 result = ""
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700177 for data in getJsonDataList:
Zack Williamsc6722d52020-01-13 16:34:33 -0700178 print("data", data)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700179 return_dict = {}
180 found = False
181 input_keys = data.keys()
182 for key in input_keys:
183 if key == searchKey and str(data[key]) == str(searchKeyValue):
Zack Williamsc6722d52020-01-13 16:34:33 -0700184 found = True
185 return_dict = data
186 print("return_dict", return_dict)
187 break
188 elif isinstance(data[key], dict):
189 result, found = self.search_dictionary(
190 data[key], searchKey)
191 if found and str(result) == str(searchKeyValue):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700192 return_dict = data
193 break
Zack Williamsc6722d52020-01-13 16:34:33 -0700194 elif isinstance(data[key], list):
195 for item in data[key]:
196 if isinstance(item, dict):
197 result, found = self.search_dictionary(
198 data[key], searchKey)
199 if found and str(
200 result) == str(searchKeyValue):
201 return_dict = data
202 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700203 if return_dict:
Zack Williamsc6722d52020-01-13 16:34:33 -0700204 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700205 return return_dict
206
Zack Williamsc6722d52020-01-13 16:34:33 -0700207 """
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700208 @method getFieldValueFromDict
209 @params : search_dict - Dictionary to be searched
210 field - Key to be searched for (ex: account_num)
211 @Returns: Returns the value of the Key that was provided
Zack Williamsc6722d52020-01-13 16:34:33 -0700212 """
213
214 def getFieldValueFromDict(self, search_dict, field):
215 results = ""
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700216 found = False
217 input_keys = search_dict.keys()
218 for key in input_keys:
Zack Williamsc6722d52020-01-13 16:34:33 -0700219 print("key...", key)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700220 if key == field:
Zack Williamsc6722d52020-01-13 16:34:33 -0700221 results = search_dict[key]
222 if not results:
223 found = True
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700224 break
Zack Williamsc6722d52020-01-13 16:34:33 -0700225 elif isinstance(search_dict[key], dict):
226 results, found = self.search_dictionary(
227 search_dict[key], field)
228 if found:
229 break
230 elif isinstance(search_dict[key], list):
231 if not search_dict[key]:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800232 found = False
You Wangf462ee92016-12-16 13:11:43 -0800233 continue
Zack Williamsc6722d52020-01-13 16:34:33 -0700234 for item in search_dict[key]:
235 if isinstance(item, dict):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700236 results, found = self.search_dictionary(item, field)
Zack Williamsc6722d52020-01-13 16:34:33 -0700237 if found:
238 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800239 if results:
Zack Williamsc6722d52020-01-13 16:34:33 -0700240 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700241
242 return results
243
Zack Williamsc6722d52020-01-13 16:34:33 -0700244 def setFieldValueInDict(self, input_dict, field, field_value):
245 input_dict[field] = field_value
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800246 return input_dict
247
Zack Williamsc6722d52020-01-13 16:34:33 -0700248 """
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800249 @method getAllFieldValues
250 @params : getJsonDataDictList - List of dictionaries to be searched
251 fieldName - Key to be searched for (ex: instance_id)
252 @Returns: Returns the unique value of the Key that was provided
Zack Williamsc6722d52020-01-13 16:34:33 -0700253 """
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800254
255 def getAllFieldValues(self, getJsonDataDictList, fieldName):
256 value_list = []
Zack Williams821c5022020-01-15 15:11:46 -0700257 # uniqValue = "" - this is unused, commented out
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800258 uniq_list = []
259 for data in getJsonDataDictList:
Zack Williamsc6722d52020-01-13 16:34:33 -0700260 fieldValue = ""
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800261 fieldValue = self.getFieldValueFromDict(data, fieldName)
262 value_list.append(fieldValue)
263 uniq_list = sorted(set(value_list))
264 if len(uniq_list) == 1:
Zack Williams821c5022020-01-15 15:11:46 -0700265 pass # see above, unused?
266 # uniqValue = uniq_list[0]
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800267 else:
Zack Williamsc6722d52020-01-13 16:34:33 -0700268 print("list of values found for ", fieldName, ";", uniq_list)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800269 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800270
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700271 def generate_uuid(self):
272 return uuid.uuid4()
273
Zack Williamsc6722d52020-01-13 16:34:33 -0700274 def generate_random_number_from_blacklist(
275 self, blacklist, min=100, max=500, typeTag=False
276 ):
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700277 num = None
278 while num in blacklist or num is None:
279 num = random.randrange(int(min), int(max))
280 if typeTag:
281 return num
282 else:
283 return str(num)
284
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700285 def get_dynamic_resources(self, inputfile, resource):
286 resourceNames = []
287 names = {}
288 dnames = []
Zack Williamsc6722d52020-01-13 16:34:33 -0700289 with open(inputfile, "r") as f:
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700290 contents = yaml.load(f)
291 resources = contents[resource]
292 for i in resources:
293 resourceNames.append(i["name"])
294 for i in resourceNames:
Zack Williamsc6722d52020-01-13 16:34:33 -0700295 names["name"] = i
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700296 dnames.append(names.copy())
297 return dnames
Kailash Khalasi8416bfe2017-12-20 13:06:37 -0800298
Zack Williamsc6722d52020-01-13 16:34:33 -0700299 def generate_random_value(
300 self, value, max_length=10, min_int=1, max_int=10000):
301 if value == "string":
302 return "".join(
303 random.choice(string.ascii_lowercase + string.digits)
304 for _ in range(max_length)
305 )
306 if value == "bool":
307 return random.choice([True, False])
308 if value == "int32" or value == "uint32":
309 return random.randint(min_int, max_int)
310 if value == "float":
311 return random.uniform(1, 10)
312 if value == "role":
313 return "admin"
314 if value == "direction":
315 return random.choice(["in", "out"])
316 if value == "flavor":
317 return random.choice(["m1.large", "m1.medium", "m1.small"])
318 if value == "vlan_tag":
319 return random.choice(["555", "1-4096", "ANY"])
320 if value == "ip_address":
Kailashbc5245f2019-03-26 13:40:19 -0700321 return ".".join(str(random.randint(0, 255)) for _ in range(4))
Kailash Khalasi8416bfe2017-12-20 13:06:37 -0800322 else:
Kailash Khalasicceec832018-08-10 15:46:50 -0700323 return None
Kailash Khalasi8416bfe2017-12-20 13:06:37 -0800324
325 def generate_random_slice_name(self):
Zack Williamsc6722d52020-01-13 16:34:33 -0700326 random_name = "".join(
327 random.choice(
328 string.ascii_lowercase +
329 string.digits) for _ in range(10))
330 return "testloginbase" + random_name