blob: 254daaadf1e9248f97d92e3038a58fc441a49f55 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070017import pexpect,os
18import time
19import json
20import collections
21import sys
22import robot
23import os.path
24from os.path import expanduser
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070025import uuid
Kailash Khalasi2adbad82017-05-15 14:53:40 -070026import random
27import re
Kailash Khalasi86e231e2017-06-06 13:13:43 -070028import yaml
You Wangaabb2832017-11-16 17:24:09 -080029import glob
Kailash Khalasi8416bfe2017-12-20 13:06:37 -080030import string
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070031
32class utils(object):
33
34 @staticmethod
35 def listToDict(alist, intListIndex):
36 dictInfo = alist[int(intListIndex)]
37 return dictInfo
38
39 @staticmethod
40 def jsonToList(strFile, strListName):
41 data = json.loads(open(strFile).read())
42 #print "data...",data
43 dataList = data[strListName]
44 return dataList
45
You Wangaabb2832017-11-16 17:24:09 -080046 def readFile(self, path, single=True):
47 dataDict = {}
48 for fileName in glob.glob(path):
49 print "Reading ", fileName
50 data = open(fileName).read()
51 dataDict[fileName] = data
52 if bool(single):
53 return data
You Wangf35df4a2018-01-24 11:00:58 -080054 if not dataDict:
55 print "Failed to find the file!"
56 return None
You Wangaabb2832017-11-16 17:24:09 -080057 return dataDict
58
59 def readFiles(self, path):
60 return self.readFile(path, single=False)
61
Suchitra.Vemuri85220062016-10-25 10:44:11 -070062 '''
63 @method compare_dict
64 @Description: validates if contents of dict1 exists in dict2
65 @params: dict1 = input_data entered through api
66 dict2 = retrieved data from GET method
67 returns True if contents of dict1 exists in dict2
68 '''
You Wang507c4562016-11-23 13:36:09 -080069 def compare_dict(self, dict1, dict2):
70 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070071 print "get data", dict2
72 if dict1 == None or dict2 == None:
73 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070074 if type(dict1) is not dict or type(dict2) is not dict:
75 return False
You Wang507c4562016-11-23 13:36:09 -080076 if dict1 == {}:
77 return True
78 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070079
You Wang507c4562016-11-23 13:36:09 -080080 '''
81 @method compare_dict_recursive
82 @Description: recursive function to validate if dict1 is a subset of dict2
83 returns True if contents of dict1 exists in dict2
84 '''
85 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070086 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080087 if key1 not in dict2.keys():
88 print "Missing key", key1, "in dict2"
89 return False
90 value2 = dict2[key1]
91 if type(value1) is dict and type(value2) is dict:
92 if not self.compare_dict_recursive(value1, value2):
93 return False
94 else:
95 if value2 != value1:
96 print "Values of key", key1, "in two dicts are not equal"
97 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070098 return True
99
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700100 '''
You Wang3964e842016-12-09 12:04:32 -0800101 @method compare_list_of_dicts
102 @Description: validates if contents of dicts in list1 exists in dicts of list2
103 returns True if for each dict in list1, there's a dict in list2 that contains its content
104 '''
105 def compare_list_of_dicts(self, list1, list2):
106 for dict1 in list1:
107 if dict1 == {}:
108 continue
109 key = dict1.keys()[0]
110 value = dict1[key]
111 dict2 = self.getDictFromListOfDict(list2, key, value)
112 if dict2 == {}:
113 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
114 return False
115 if self.compare_dict(dict1, dict2) == False:
116 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
117 return False
118 return True
119
120 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700121 @method search_dictionary
122 @Description: Searches for a key in the provided nested dictionary
123 @params: input_dict = dictionary to be searched
124 search_key = name of the key to be searched for
125 returns two values: search_key value and status of the search.
126 True if found (False when not found)
127
128 '''
129 def search_dictionary(self,input_dict, search_key):
130 input_keys = input_dict.keys()
131 key_value = ''
132 found = False
133 for key in input_keys:
134 if key == search_key:
135 key_value = input_dict[key]
136 found = True
137 break
138 elif type(input_dict[key]) == dict:
139 key_value, found = self.search_dictionary(input_dict[key],search_key)
140 if found == True:
141 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800142 elif type(input_dict[key]) == list:
143 if not input_dict[key]:
144 found = False
145 break
146 for item in input_dict[key]:
147 if isinstance(item, dict):
148 key_value, found = self.search_dictionary(item, search_key)
149 if found == True:
150 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700151 return key_value,found
152 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800153 @method getDictFromListOfDict
154 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700155 @Description: Searches for the dictionary in the provided list of dictionaries
156 that matches the value of the key provided
157 @params : List of dictionaries(getResponse Data from the URL),
158 SearchKey - Key that needs to be searched for (ex: account_num)
159 searchKeyValue - Value of the searchKey (ex: 21)
160 @Returns: Dictionary returned when match found for searchKey with the corresponding
161 searchKeyValue provided
162 '''
163
164 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
165 return_dict = {}
166 result = ''
167 for data in getJsonDataList:
Suchitra Vemurif58de922017-06-14 12:53:42 -0700168 print "data", data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700169 return_dict = {}
170 found = False
171 input_keys = data.keys()
172 for key in input_keys:
173 if key == searchKey and str(data[key]) == str(searchKeyValue):
174 found = True
175 return_dict = data
Suchitra Vemurif58de922017-06-14 12:53:42 -0700176 print "return_dict",return_dict
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700177 break
178 elif type(data[key]) == dict:
179 result, found = self.search_dictionary(data[key],searchKey)
180 if found == True and str(result) == str(searchKeyValue):
181 return_dict = data
182 break
183 elif type(data[key]) == list:
184 for item in data[key]:
185 if isinstance(item, dict):
186 result, found = self.search_dictionary(data[key], searchKey)
187 if found == True and str(result) == str(searchKeyValue):
188 return_dict = data
189 break
190 if return_dict:
191 break
192 return return_dict
193
194
195 '''
196 @method getFieldValueFromDict
197 @params : search_dict - Dictionary to be searched
198 field - Key to be searched for (ex: account_num)
199 @Returns: Returns the value of the Key that was provided
200 '''
201 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700202 results = ''
203 found = False
204 input_keys = search_dict.keys()
205 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800206 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700207 if key == field:
208 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800209 if not results:
210 found = True
211 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700212 elif type(search_dict[key]) == dict:
213 results, found = self.search_dictionary(search_dict[key],field)
214 if found == True:
215 break
216 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800217 if not search_dict[key]:
218 found = False
You Wangf462ee92016-12-16 13:11:43 -0800219 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700220 for item in search_dict[key]:
221 if isinstance(item, dict):
222 results, found = self.search_dictionary(item, field)
223 if found == True:
224 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800225 if results:
226 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700227
228 return results
229
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800230 def setFieldValueInDict(self,input_dict,field,field_value):
231 input_dict[field]=field_value
232 return input_dict
233
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800234 '''
235 @method getAllFieldValues
236 @params : getJsonDataDictList - List of dictionaries to be searched
237 fieldName - Key to be searched for (ex: instance_id)
238 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800239 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800240
241 def getAllFieldValues(self, getJsonDataDictList, fieldName):
242 value_list = []
243 uniqValue = ''
244 uniq_list = []
245 for data in getJsonDataDictList:
246 fieldValue = ''
247 fieldValue = self.getFieldValueFromDict(data, fieldName)
248 value_list.append(fieldValue)
249 uniq_list = sorted(set(value_list))
250 if len(uniq_list) == 1:
251 uniqValue = uniq_list[0]
252 else:
253 print "list of values found for ", fieldName, ";", uniq_list
254 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800255
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700256 def generate_uuid(self):
257 return uuid.uuid4()
258
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700259 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
260 num = None
261 while num in blacklist or num is None:
262 num = random.randrange(int(min), int(max))
263 if typeTag:
264 return num
265 else:
266 return str(num)
267
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700268 def get_dynamic_resources(self, inputfile, resource):
269 resourceNames = []
270 names = {}
271 dnames = []
272 with open(inputfile, 'r') as f:
273 contents = yaml.load(f)
274 resources = contents[resource]
275 for i in resources:
276 resourceNames.append(i["name"])
277 for i in resourceNames:
278 names['name']=i
279 dnames.append(names.copy())
280 return dnames
Kailash Khalasi8416bfe2017-12-20 13:06:37 -0800281
282 def generate_random_value(self, value):
283 if value == 'string':
284 return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
285 if value == 'bool':
286 return random.choice([True, False])
287 if value == 'int32' or value == 'uint32':
288 return random.randint(1,10000)
289 if value == 'float':
290 return random.uniform(1,10)
291 else:
292 return None
293
294 def generate_random_slice_name(self):
295 random_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
296 return 'testloginbase' + random_name