Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 | |
Kailash Khalasi | 69977fe | 2018-04-07 14:49:55 -0700 | [diff] [blame] | 17 | import os |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 18 | import time |
| 19 | import json |
| 20 | import collections |
| 21 | import sys |
| 22 | import robot |
| 23 | import os.path |
| 24 | from os.path import expanduser |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 25 | import uuid |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 26 | import random |
| 27 | import re |
Kailash Khalasi | 86e231e | 2017-06-06 13:13:43 -0700 | [diff] [blame] | 28 | import yaml |
You Wang | aabb283 | 2017-11-16 17:24:09 -0800 | [diff] [blame] | 29 | import glob |
Kailash Khalasi | 8416bfe | 2017-12-20 13:06:37 -0800 | [diff] [blame] | 30 | import string |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 31 | |
| 32 | class 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 Wang | aabb283 | 2017-11-16 17:24:09 -0800 | [diff] [blame] | 46 | 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 Wang | f35df4a | 2018-01-24 11:00:58 -0800 | [diff] [blame] | 54 | if not dataDict: |
| 55 | print "Failed to find the file!" |
| 56 | return None |
You Wang | aabb283 | 2017-11-16 17:24:09 -0800 | [diff] [blame] | 57 | return dataDict |
| 58 | |
| 59 | def readFiles(self, path): |
| 60 | return self.readFile(path, single=False) |
| 61 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 62 | ''' |
| 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 Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 69 | def compare_dict(self, dict1, dict2): |
| 70 | print "input data", dict1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 71 | print "get data", dict2 |
| 72 | if dict1 == None or dict2 == None: |
| 73 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 74 | if type(dict1) is not dict or type(dict2) is not dict: |
| 75 | return False |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 76 | if dict1 == {}: |
| 77 | return True |
| 78 | return self.compare_dict_recursive(dict1, dict2) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 79 | |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 80 | ''' |
| 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 86 | for key1,value1 in dict1.items(): |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 87 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 98 | return True |
| 99 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 100 | ''' |
You Wang | 3964e84 | 2016-12-09 12:04:32 -0800 | [diff] [blame] | 101 | @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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 121 | @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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 142 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 151 | return key_value,found |
| 152 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 153 | @method getDictFromListOfDict |
| 154 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 155 | @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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 168 | print "data", data |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 169 | 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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 176 | print "return_dict",return_dict |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 177 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 202 | results = '' |
| 203 | found = False |
| 204 | input_keys = search_dict.keys() |
| 205 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 206 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 207 | if key == field: |
| 208 | results = search_dict[key] |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 209 | if not results: |
| 210 | found = True |
| 211 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 212 | 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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 217 | if not search_dict[key]: |
| 218 | found = False |
You Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 219 | continue |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 220 | 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.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 225 | if results: |
| 226 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 227 | |
| 228 | return results |
| 229 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 230 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 231 | input_dict[field]=field_value |
| 232 | return input_dict |
| 233 | |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 234 | ''' |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 239 | ''' |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 240 | |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 255 | |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 256 | def generate_uuid(self): |
| 257 | return uuid.uuid4() |
| 258 | |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 259 | 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 Khalasi | 86e231e | 2017-06-06 13:13:43 -0700 | [diff] [blame] | 268 | 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 Khalasi | 8416bfe | 2017-12-20 13:06:37 -0800 | [diff] [blame] | 281 | |
| 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) |
Kailash Khalasi | cceec83 | 2018-08-10 15:46:50 -0700 | [diff] [blame] | 289 | if value == 'float': |
| 290 | return random.uniform(1,10) |
Kailash Khalasi | 91d9dc0 | 2018-08-27 10:33:12 -0700 | [diff] [blame] | 291 | if value == 'role': |
Kailash Khalasi | 3aa5874 | 2018-08-27 13:32:00 -0700 | [diff] [blame] | 292 | return 'admin' |
Kailash Khalasi | 91d9dc0 | 2018-08-27 10:33:12 -0700 | [diff] [blame] | 293 | if value == 'direction': |
| 294 | return random.choice(['in', 'out']) |
| 295 | if value == 'flavor': |
| 296 | return random.choice(['m1.large', 'm1.medium', 'm1.small']) |
Kailash Khalasi | b987e9b | 2018-10-11 11:12:35 -0700 | [diff] [blame^] | 297 | if value == 'vlan_tag': |
| 298 | return random.choice(['555', '1-4096', 'ANY']) |
Kailash Khalasi | 8416bfe | 2017-12-20 13:06:37 -0800 | [diff] [blame] | 299 | else: |
Kailash Khalasi | cceec83 | 2018-08-10 15:46:50 -0700 | [diff] [blame] | 300 | return None |
Kailash Khalasi | 8416bfe | 2017-12-20 13:06:37 -0800 | [diff] [blame] | 301 | |
| 302 | def generate_random_slice_name(self): |
| 303 | random_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) |
Kailash Khalasi | cceec83 | 2018-08-10 15:46:50 -0700 | [diff] [blame] | 304 | return 'testloginbase' + random_name |