admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | import inspect |
| 2 | import sys |
| 3 | import os |
Jon Hall | fd8ce43 | 2015-07-21 13:07:53 -0700 | [diff] [blame] | 4 | import re |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | from core import xmldict |
| 6 | ''' |
| 7 | @author: Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | ''' |
| 24 | |
| 25 | |
| 26 | class UpdateDriver: |
| 27 | def __init__(self): |
| 28 | self.default = '' |
| 29 | self.configFile = "/home/openflow/TestON/config/ofadriver.cfg" |
| 30 | self.methodDict = {} |
| 31 | self.fileDict = {} |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 32 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | |
| 34 | def getmethods(self,modulePath,Class) : |
| 35 | ''' |
| 36 | This will get the list of methods in given module or class. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 37 | It accepts the module path and class name. If there is no |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 38 | class name then it has be mentioned as None. |
| 39 | ''' |
| 40 | methodList = [] |
| 41 | moduleList = modulePath.split("/") |
| 42 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 43 | print "Message : Method list is being obatined , Please wait ..." |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | try : |
| 45 | if Class : |
| 46 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [Class], -1) |
| 47 | ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] |
| 48 | self.ClassList = ClassList |
| 49 | Class = vars(Module)[Class] |
| 50 | methodList = [x.__name__ for x in Class.__dict__.values() if inspect.isfunction(x)] |
| 51 | else : |
| 52 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(),[moduleList[len(moduleList) - 2]], -1) |
| 53 | methodList = [x.__name__ for x in Module.__dict__.values() if inspect.isfunction(x)] |
| 54 | ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] |
| 55 | self.ClassList = ClassList |
| 56 | except : |
| 57 | print "Error : " +str(sys.exc_info()[1]) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 58 | |
| 59 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | self.method = methodList |
| 61 | return self.method |
| 62 | |
| 63 | def echo(self) : |
| 64 | print "Echoing !!!!!!" |
| 65 | |
| 66 | def getargs(self,moduleName,className,method) : |
| 67 | ''' |
| 68 | This will return the list of arguments in a method of python module of class. |
| 69 | It accepts method list as an argument. |
| 70 | ''' |
| 71 | print "Message : Argument list is being obtained for each method" |
| 72 | methodArgsDict = {} |
| 73 | if className == None: |
| 74 | moduleList = moduleName.split(".") |
| 75 | for index,name in enumerate(method) : |
| 76 | Module = __import__(moduleList[len(moduleList) -1], globals(), locals(), [moduleList[len(moduleList) -2]], -1) |
| 77 | try : |
| 78 | names = vars(Module)[name] |
| 79 | except KeyError: |
| 80 | print "Message : method '" + name + "'does not exists,Continued with including it. " |
| 81 | return False |
| 82 | argumentList = inspect.getargspec(names) #inspect.getargvalues(name) |
| 83 | methodArgsDict[name] = argumentList[0] |
| 84 | else : |
| 85 | moduleList = moduleName.split(".") |
| 86 | for index,name in enumerate(method) : |
| 87 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [className], -1) |
| 88 | Class = getattr(Module, className) |
| 89 | try : |
| 90 | names = vars(Class)[name] |
| 91 | except KeyError : |
| 92 | print "Message : method '" + name + "'does not exists,Continued with include it." |
| 93 | return False |
| 94 | |
| 95 | argumentList = inspect.getargspec(names) #inspect.getargvalues(name) |
| 96 | methodArgsDict[name] = argumentList[0] |
| 97 | |
| 98 | return methodArgsDict |
| 99 | |
| 100 | def configparser(self,fileName): |
| 101 | ''' |
| 102 | It will parse the config file (ofa.cfg) and return as dictionary |
| 103 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 104 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | matchFileName = re.match(r'(.*)\.cfg', fileName, re.M | re.I) |
| 106 | if matchFileName: |
| 107 | self.configFile = fileName |
| 108 | try : |
| 109 | xml = open(fileName).read() |
| 110 | self.configDict = xmldict.xml_to_dict(xml) |
| 111 | return self.configDict |
| 112 | except : |
| 113 | print "Error : Config file " + self.configFile + " not defined properly or file path error" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 114 | |
| 115 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | def getList(self): |
| 117 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 118 | This method will maintain the hash with module->class->methodList or |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | module -> methodList .It will return the same Hash. |
| 120 | ''' |
| 121 | classList = [] |
| 122 | try : |
| 123 | moduleList = self.configDict['config-driver']['importTypes'][self.driver]['modules'].keys() |
| 124 | except KeyError,e: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 125 | print "Error : Module Does not Exists" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 126 | print e |
| 127 | return False |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 128 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 129 | for index,value in enumerate(moduleList): |
| 130 | modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['path'] |
| 131 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['name'] |
| 132 | |
| 133 | try : |
| 134 | pathList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['set-path'].split(",") |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 135 | sys.path.extend(pathList) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 136 | except KeyError : |
| 137 | print "Error : No System Path is given " |
| 138 | pass |
| 139 | try : |
| 140 | Class = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'] |
| 141 | except : |
| 142 | Class = None |
| 143 | if Class == None : |
| 144 | methodList = self.getmethods(modulePath,None) |
| 145 | self.methodDict[moduleName] = methodList |
| 146 | self.method_ignoreList(value,None) |
| 147 | self.getMethodArgsHash(moduleName,value,None) |
| 148 | else : |
| 149 | classList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'].keys() |
| 150 | for indx,className in enumerate(classList): |
| 151 | if className == 'ignore-list' : |
| 152 | pass |
| 153 | else : |
| 154 | methodList = self.getmethods(modulePath,className) |
| 155 | self.methodDict[moduleName] = {className : methodList} |
| 156 | self.method_ignoreList(value,className) |
| 157 | self.class_ignoreList(value) |
| 158 | self.getMethodArgsHash(moduleName,value,className) |
| 159 | |
| 160 | def class_ignoreList(self,module) : |
| 161 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 162 | It removes the ignored classes for each module mention in ofadriver.cfg |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 164 | class_ignoreList = [] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 165 | if self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'] == None : |
| 166 | pass |
| 167 | else : |
| 168 | try : |
| 169 | class_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes']['ignore-list']).split(",") |
| 170 | except KeyError : |
| 171 | print "Message : No Class Ignore List present" |
| 172 | return True |
| 173 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 174 | try : |
| 175 | for index,className in enumerate(class_ignoreList): |
| 176 | if className in self.methodDict[moduleName].keys(): |
| 177 | del self.methodDict[moduleName][className] |
| 178 | except AttributeError: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 179 | pass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | return self.methodDict |
| 181 | |
| 182 | def method_ignoreList(self,module,className): |
| 183 | ''' |
| 184 | It removes the ignored methods of each module or class mentioned in ofadriver.cfg. |
| 185 | ''' |
| 186 | method_ignoreList = [] |
| 187 | |
| 188 | try : |
| 189 | if className == None : |
| 190 | try : |
| 191 | method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['ignore-list']).split(",") |
| 192 | except TypeError : |
| 193 | pass |
| 194 | else : |
| 195 | try : |
| 196 | method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['ignore-list']).split(",") |
| 197 | except TypeError : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 198 | pass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 199 | except KeyError : |
| 200 | print "Message : No Ignore-List Exists , proceeding for looking add method" |
| 201 | self.add_method(module,className) |
| 202 | return True |
| 203 | |
| 204 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 205 | #import pprint |
| 206 | #pprint.pprint(self.methodDict[moduleName]) |
| 207 | for index, method in enumerate(method_ignoreList) : |
| 208 | if className == None : |
| 209 | try : |
| 210 | self.methodDict[moduleName].remove(method) |
| 211 | #pprint.pprint(self.methodDict) |
| 212 | except ValueError: |
| 213 | print "Message : Method " + method + "Does not exist in module " + moduleName + ", Continue to rest execution" |
| 214 | pass |
| 215 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 216 | else : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | if method in self.methodDict[moduleName][className] : |
| 218 | self.methodDict[moduleName][className].remove(method) |
| 219 | self.add_method(module,className) |
| 220 | return self.methodDict |
| 221 | |
| 222 | def add_method(self,module,className) : |
| 223 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 224 | This will add the methods(mentioned in ofadriver.cfg file) into method list if it doesnot exists in list. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | ''' |
| 226 | method_List = [] |
| 227 | try : |
| 228 | if className == None : |
| 229 | try : |
| 230 | method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['add-list']).split(",") |
| 231 | except TypeError : |
| 232 | pass |
| 233 | else : |
| 234 | try : |
| 235 | method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['add-list']).split(",") |
| 236 | except TypeError : |
| 237 | pass |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 238 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 239 | except KeyError : |
| 240 | print "Message : No Add-List Exists , Proceeding with all available methods" |
| 241 | return True |
| 242 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 243 | for index, method in enumerate(method_List) : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 244 | if className == None : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | self.methodDict[moduleName] = [] |
| 246 | self.methodDict[moduleName].append(method) |
| 247 | else : |
| 248 | self.methodDict[moduleName][className] = [] |
| 249 | self.methodDict[moduleName][className].append(method) |
| 250 | |
| 251 | def getMethodArgsHash(self,moduleName,module,className): |
| 252 | ''' |
| 253 | This will maintain a Hash of class->method->argumentsList |
| 254 | ''' |
| 255 | modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['path'] |
| 256 | moduleList = modulePath.split("/") |
| 257 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 258 | if className == None : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 259 | methodArgs = self.getargs(newModule,None,self.methodDict[moduleName]) |
| 260 | self.fileDict[moduleName] = methodArgs |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 261 | else : |
| 262 | methodArgs = self.getargs(newModule,className,self.methodDict[moduleName][className]) |
| 263 | self.fileDict[className] = methodArgs |
| 264 | return self.fileDict |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 265 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 266 | def appendDriver(self,fileName): |
| 267 | ''' |
| 268 | This will append the given driver file with methods along with arguments. |
| 269 | ''' |
| 270 | matchFileName = re.match(r'(.*)\.py', fileName, re.M | re.I) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 271 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 272 | if matchFileName: |
| 273 | fileHandle = None |
| 274 | try : |
| 275 | print "Message : Writing Driver file at " + fileName |
| 276 | fileHandle = open(fileName,"a") |
| 277 | content = '' |
| 278 | |
| 279 | for index, key in enumerate(self.fileDict.keys()): |
| 280 | try : |
| 281 | for ind, method in enumerate(self.fileDict[key].keys()): |
| 282 | if not method == "__init__" : |
| 283 | args = '' |
| 284 | args = ",".join(self.fileDict[key][method]) |
| 285 | content = content + "\n" + " " * 4 + "def " + method + "(self," + args + ") :" |
| 286 | content = content + "\n" + " " * 8 + "return " + key + "." + method + "(" + args + ")\n" |
| 287 | except AttributeError : |
| 288 | pass |
| 289 | fileHandle.write(content) |
| 290 | fileHandle.close() |
| 291 | return content |
| 292 | |
| 293 | except : |
| 294 | print "Error : Driver file " + fileName + "does not exists" |
| 295 | else : |
| 296 | print "Error : File name " + fileName + "is not python module" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 297 | return False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 298 | |
| 299 | |
| 300 | def writeDriver(self, driver) : |
| 301 | ''' |
| 302 | This will accept the List of driver name and write those drivers if no driver name is specified |
| 303 | then it will write all of the driver specified in the ofadriver.cfg. |
| 304 | ''' |
| 305 | self.printHeader(driver) |
| 306 | drivers = [] |
| 307 | commaMatch = re.search(",", driver, flags=0) |
| 308 | if commaMatch: |
| 309 | drivers = driver.split(",") |
| 310 | else : |
| 311 | drivers.append(driver) |
| 312 | self.driverList = [] |
| 313 | if len(drivers) == 0: |
| 314 | for index, driverName in enumerate(self.configDict['config-driver']['importTypes'].keys()): |
| 315 | self.driver = driverName |
| 316 | result = self.getList() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 317 | if result : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 318 | self.getDriverPath() |
| 319 | self.appendDriver(self.driverPath + self.driver + ".py") |
| 320 | self.driverList.append(self.driverPath + self.driver + ".py") |
| 321 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 322 | return False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 323 | else : |
| 324 | for index, driverName in enumerate(drivers) : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 325 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 326 | self.driver = driverName |
| 327 | result = self.getList() |
| 328 | if result : |
| 329 | self.getDriverPath() |
| 330 | self.appendDriver(self.driverPath + self.driver + ".py") |
| 331 | self.driverList.append(self.driverPath + self.driver + ".py") |
| 332 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 333 | return False |
| 334 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 335 | print "=" * 90 |
| 336 | print " " * 30 + "Output Driver File :" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 337 | print ",\n".join(self.driverList) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 338 | print "=" * 90 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 339 | return True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 340 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 341 | |
| 342 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 343 | def getDriverPath(self): |
| 344 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 345 | It will set the driver path and returns it.If driver path is not specified then it will take |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 346 | default path (/lib/updatedriver/). |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 347 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 348 | self.driverPath = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 349 | try : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 350 | self.driverPath = self.configDict['config-driver']['importTypes'][self.driver]['driver-path'] |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 351 | |
Jon Hall | f632d20 | 2015-07-30 15:45:11 -0700 | [diff] [blame] | 352 | except KeyError: |
| 353 | location = os.path.abspath( os.path.dirname( __file__ ) ) |
| 354 | path = re.sub( "(bin)$", "", location ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 355 | self.driverPath = path + "/lib/updatedriver/" |
| 356 | return self.driverPath |
| 357 | |
| 358 | |
| 359 | def printHeader(self,driver): |
| 360 | content = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 361 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 362 | print " " * 10 +"=" * 90 + "\n" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 363 | content = content + " " * 30 + "*-- Welcome to Updated Driver --*\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 364 | content = content + "\n" + " " * 10 + " " * 10 + "Config File : " + "/home/openflow/TestON/config/ofadriver.py" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 365 | content = content + "\n" + " " * 10 + " " * 10 + "Drivers Name : " + driver |
| 366 | print content |
| 367 | print " " * 10 + "=" * 90 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 368 | |
| 369 | |
| 370 | |