blob: bb825a0f79a6ad4d0ba5af175e95ca19395537a3 [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 Wang304d3f92017-12-14 16:13:31 -080029import glob
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070030
31class utils(object):
32
33 @staticmethod
34 def listToDict(alist, intListIndex):
35 dictInfo = alist[int(intListIndex)]
36 return dictInfo
37
38 @staticmethod
39 def jsonToList(strFile, strListName):
40 data = json.loads(open(strFile).read())
41 #print "data...",data
42 dataList = data[strListName]
43 return dataList
44
You Wang304d3f92017-12-14 16:13:31 -080045 def readFile(self, path, single=True):
46 dataDict = {}
47 for fileName in glob.glob(path):
48 print "Reading ", fileName
49 data = open(fileName).read()
50 dataDict[fileName] = data
51 if bool(single):
52 return data
53 return dataDict
54
55 def readFiles(self, path):
56 return self.readFile(path, single=False)
57
Suchitra.Vemuri85220062016-10-25 10:44:11 -070058 '''
59 @method compare_dict
60 @Description: validates if contents of dict1 exists in dict2
61 @params: dict1 = input_data entered through api
62 dict2 = retrieved data from GET method
63 returns True if contents of dict1 exists in dict2
64 '''
You Wang507c4562016-11-23 13:36:09 -080065 def compare_dict(self, dict1, dict2):
66 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070067 print "get data", dict2
68 if dict1 == None or dict2 == None:
69 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070070 if type(dict1) is not dict or type(dict2) is not dict:
71 return False
You Wang507c4562016-11-23 13:36:09 -080072 if dict1 == {}:
73 return True
74 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070075
You Wang507c4562016-11-23 13:36:09 -080076 '''
77 @method compare_dict_recursive
78 @Description: recursive function to validate if dict1 is a subset of dict2
79 returns True if contents of dict1 exists in dict2
80 '''
81 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070082 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080083 if key1 not in dict2.keys():
84 print "Missing key", key1, "in dict2"
85 return False
86 value2 = dict2[key1]
87 if type(value1) is dict and type(value2) is dict:
88 if not self.compare_dict_recursive(value1, value2):
89 return False
90 else:
91 if value2 != value1:
92 print "Values of key", key1, "in two dicts are not equal"
93 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070094 return True
95
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070096 '''
You Wang3964e842016-12-09 12:04:32 -080097 @method compare_list_of_dicts
98 @Description: validates if contents of dicts in list1 exists in dicts of list2
99 returns True if for each dict in list1, there's a dict in list2 that contains its content
100 '''
101 def compare_list_of_dicts(self, list1, list2):
102 for dict1 in list1:
103 if dict1 == {}:
104 continue
105 key = dict1.keys()[0]
106 value = dict1[key]
107 dict2 = self.getDictFromListOfDict(list2, key, value)
108 if dict2 == {}:
109 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
110 return False
111 if self.compare_dict(dict1, dict2) == False:
112 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
113 return False
114 return True
115
116 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700117 @method search_dictionary
118 @Description: Searches for a key in the provided nested dictionary
119 @params: input_dict = dictionary to be searched
120 search_key = name of the key to be searched for
121 returns two values: search_key value and status of the search.
122 True if found (False when not found)
123
124 '''
125 def search_dictionary(self,input_dict, search_key):
126 input_keys = input_dict.keys()
127 key_value = ''
128 found = False
129 for key in input_keys:
130 if key == search_key:
131 key_value = input_dict[key]
132 found = True
133 break
134 elif type(input_dict[key]) == dict:
135 key_value, found = self.search_dictionary(input_dict[key],search_key)
136 if found == True:
137 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800138 elif type(input_dict[key]) == list:
139 if not input_dict[key]:
140 found = False
141 break
142 for item in input_dict[key]:
143 if isinstance(item, dict):
144 key_value, found = self.search_dictionary(item, search_key)
145 if found == True:
146 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700147 return key_value,found
148 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800149 @method getDictFromListOfDict
150 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700151 @Description: Searches for the dictionary in the provided list of dictionaries
152 that matches the value of the key provided
153 @params : List of dictionaries(getResponse Data from the URL),
154 SearchKey - Key that needs to be searched for (ex: account_num)
155 searchKeyValue - Value of the searchKey (ex: 21)
156 @Returns: Dictionary returned when match found for searchKey with the corresponding
157 searchKeyValue provided
158 '''
159
160 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
161 return_dict = {}
162 result = ''
163 for data in getJsonDataList:
Suchitra Vemurif58de922017-06-14 12:53:42 -0700164 print "data", data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700165 return_dict = {}
166 found = False
167 input_keys = data.keys()
168 for key in input_keys:
169 if key == searchKey and str(data[key]) == str(searchKeyValue):
170 found = True
171 return_dict = data
Suchitra Vemurif58de922017-06-14 12:53:42 -0700172 print "return_dict",return_dict
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700173 break
174 elif type(data[key]) == dict:
175 result, found = self.search_dictionary(data[key],searchKey)
176 if found == True and str(result) == str(searchKeyValue):
177 return_dict = data
178 break
179 elif type(data[key]) == list:
180 for item in data[key]:
181 if isinstance(item, dict):
182 result, found = self.search_dictionary(data[key], searchKey)
183 if found == True and str(result) == str(searchKeyValue):
184 return_dict = data
185 break
186 if return_dict:
187 break
188 return return_dict
189
190
191 '''
192 @method getFieldValueFromDict
193 @params : search_dict - Dictionary to be searched
194 field - Key to be searched for (ex: account_num)
195 @Returns: Returns the value of the Key that was provided
196 '''
197 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700198 results = ''
199 found = False
200 input_keys = search_dict.keys()
201 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800202 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700203 if key == field:
204 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800205 if not results:
206 found = True
207 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700208 elif type(search_dict[key]) == dict:
209 results, found = self.search_dictionary(search_dict[key],field)
210 if found == True:
211 break
212 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800213 if not search_dict[key]:
214 found = False
You Wangf462ee92016-12-16 13:11:43 -0800215 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700216 for item in search_dict[key]:
217 if isinstance(item, dict):
218 results, found = self.search_dictionary(item, field)
219 if found == True:
220 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800221 if results:
222 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700223
224 return results
225
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800226 def setFieldValueInDict(self,input_dict,field,field_value):
227 input_dict[field]=field_value
228 return input_dict
229
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800230 '''
231 @method getAllFieldValues
232 @params : getJsonDataDictList - List of dictionaries to be searched
233 fieldName - Key to be searched for (ex: instance_id)
234 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800235 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800236
237 def getAllFieldValues(self, getJsonDataDictList, fieldName):
238 value_list = []
239 uniqValue = ''
240 uniq_list = []
241 for data in getJsonDataDictList:
242 fieldValue = ''
243 fieldValue = self.getFieldValueFromDict(data, fieldName)
244 value_list.append(fieldValue)
245 uniq_list = sorted(set(value_list))
246 if len(uniq_list) == 1:
247 uniqValue = uniq_list[0]
248 else:
249 print "list of values found for ", fieldName, ";", uniq_list
250 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800251
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700252 def generate_uuid(self):
253 return uuid.uuid4()
254
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700255 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
256 num = None
257 while num in blacklist or num is None:
258 num = random.randrange(int(min), int(max))
259 if typeTag:
260 return num
261 else:
262 return str(num)
263
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700264 def get_dynamic_resources(self, inputfile, resource):
265 resourceNames = []
266 names = {}
267 dnames = []
268 with open(inputfile, 'r') as f:
269 contents = yaml.load(f)
270 resources = contents[resource]
271 for i in resources:
272 resourceNames.append(i["name"])
273 for i in resourceNames:
274 names['name']=i
275 dnames.append(names.copy())
276 return dnames