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 | |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 17 | import pexpect,os |
| 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 |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 30 | |
| 31 | class 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 Wang | aabb283 | 2017-11-16 17:24:09 -0800 | [diff] [blame] | 45 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 58 | ''' |
| 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 Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 65 | def compare_dict(self, dict1, dict2): |
| 66 | print "input data", dict1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 67 | print "get data", dict2 |
| 68 | if dict1 == None or dict2 == None: |
| 69 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 70 | if type(dict1) is not dict or type(dict2) is not dict: |
| 71 | return False |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 72 | if dict1 == {}: |
| 73 | return True |
| 74 | return self.compare_dict_recursive(dict1, dict2) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 75 | |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 76 | ''' |
| 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 82 | for key1,value1 in dict1.items(): |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 83 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 94 | return True |
| 95 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 96 | ''' |
You Wang | 3964e84 | 2016-12-09 12:04:32 -0800 | [diff] [blame] | 97 | @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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 117 | @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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 138 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 147 | return key_value,found |
| 148 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 149 | @method getDictFromListOfDict |
| 150 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 151 | @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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 164 | print "data", data |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 165 | 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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 172 | print "return_dict",return_dict |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 173 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 198 | results = '' |
| 199 | found = False |
| 200 | input_keys = search_dict.keys() |
| 201 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 202 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 203 | if key == field: |
| 204 | results = search_dict[key] |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 205 | if not results: |
| 206 | found = True |
| 207 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 208 | 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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 213 | if not search_dict[key]: |
| 214 | found = False |
You Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 215 | continue |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 216 | 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.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 221 | if results: |
| 222 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 223 | |
| 224 | return results |
| 225 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 226 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 227 | input_dict[field]=field_value |
| 228 | return input_dict |
| 229 | |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 230 | ''' |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 235 | ''' |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 236 | |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 251 | |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 252 | def generate_uuid(self): |
| 253 | return uuid.uuid4() |
| 254 | |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 255 | 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 Khalasi | 86e231e | 2017-06-06 13:13:43 -0700 | [diff] [blame] | 264 | 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 |