blob: ca5c0f2d8f6dc73e5ceb6a5ebd265d6cd243677d [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001import inspect
2import sys
3import os
Jon Hallfd8ce432015-07-21 13:07:53 -07004import re
adminbae64d82013-08-01 10:50:15 -07005from 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 Hall4ba53f02015-07-29 13:07:41 -070020 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070021
22
23'''
24
25
26class UpdateDriver:
27 def __init__(self):
28 self.default = ''
29 self.configFile = "/home/openflow/TestON/config/ofadriver.cfg"
30 self.methodDict = {}
31 self.fileDict = {}
Jon Hall4ba53f02015-07-29 13:07:41 -070032
adminbae64d82013-08-01 10:50:15 -070033
34 def getmethods(self,modulePath,Class) :
35 '''
36 This will get the list of methods in given module or class.
Jon Hall4ba53f02015-07-29 13:07:41 -070037 It accepts the module path and class name. If there is no
adminbae64d82013-08-01 10:50:15 -070038 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 Hall4ba53f02015-07-29 13:07:41 -070043 print "Message : Method list is being obatined , Please wait ..."
adminbae64d82013-08-01 10:50:15 -070044 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 Hall4ba53f02015-07-29 13:07:41 -070058
59
adminbae64d82013-08-01 10:50:15 -070060 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 Hall4ba53f02015-07-29 13:07:41 -0700104
adminbae64d82013-08-01 10:50:15 -0700105 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 Hall4ba53f02015-07-29 13:07:41 -0700114
115
adminbae64d82013-08-01 10:50:15 -0700116 def getList(self):
117 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700118 This method will maintain the hash with module->class->methodList or
adminbae64d82013-08-01 10:50:15 -0700119 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 Hall4ba53f02015-07-29 13:07:41 -0700125 print "Error : Module Does not Exists"
adminbae64d82013-08-01 10:50:15 -0700126 print e
127 return False
Jon Hall4ba53f02015-07-29 13:07:41 -0700128
adminbae64d82013-08-01 10:50:15 -0700129 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 Hall4ba53f02015-07-29 13:07:41 -0700135 sys.path.extend(pathList)
adminbae64d82013-08-01 10:50:15 -0700136 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 Hall4ba53f02015-07-29 13:07:41 -0700162 It removes the ignored classes for each module mention in ofadriver.cfg
adminbae64d82013-08-01 10:50:15 -0700163 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700164 class_ignoreList = []
adminbae64d82013-08-01 10:50:15 -0700165 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 Hall4ba53f02015-07-29 13:07:41 -0700179 pass
adminbae64d82013-08-01 10:50:15 -0700180 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 Hall4ba53f02015-07-29 13:07:41 -0700198 pass
adminbae64d82013-08-01 10:50:15 -0700199 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 Hall4ba53f02015-07-29 13:07:41 -0700216 else :
adminbae64d82013-08-01 10:50:15 -0700217 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 Hall4ba53f02015-07-29 13:07:41 -0700224 This will add the methods(mentioned in ofadriver.cfg file) into method list if it doesnot exists in list.
adminbae64d82013-08-01 10:50:15 -0700225 '''
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 Hall4ba53f02015-07-29 13:07:41 -0700238
adminbae64d82013-08-01 10:50:15 -0700239 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 Hall4ba53f02015-07-29 13:07:41 -0700244 if className == None :
adminbae64d82013-08-01 10:50:15 -0700245 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 Hall4ba53f02015-07-29 13:07:41 -0700259 methodArgs = self.getargs(newModule,None,self.methodDict[moduleName])
260 self.fileDict[moduleName] = methodArgs
adminbae64d82013-08-01 10:50:15 -0700261 else :
262 methodArgs = self.getargs(newModule,className,self.methodDict[moduleName][className])
263 self.fileDict[className] = methodArgs
264 return self.fileDict
Jon Hall4ba53f02015-07-29 13:07:41 -0700265
adminbae64d82013-08-01 10:50:15 -0700266 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 Hall4ba53f02015-07-29 13:07:41 -0700271
adminbae64d82013-08-01 10:50:15 -0700272 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 Hall4ba53f02015-07-29 13:07:41 -0700297 return False
adminbae64d82013-08-01 10:50:15 -0700298
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 Hall4ba53f02015-07-29 13:07:41 -0700317 if result :
adminbae64d82013-08-01 10:50:15 -0700318 self.getDriverPath()
319 self.appendDriver(self.driverPath + self.driver + ".py")
320 self.driverList.append(self.driverPath + self.driver + ".py")
321 else :
Jon Hall4ba53f02015-07-29 13:07:41 -0700322 return False
adminbae64d82013-08-01 10:50:15 -0700323 else :
324 for index, driverName in enumerate(drivers) :
Jon Hall4ba53f02015-07-29 13:07:41 -0700325
adminbae64d82013-08-01 10:50:15 -0700326 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 Hall4ba53f02015-07-29 13:07:41 -0700333 return False
334
adminbae64d82013-08-01 10:50:15 -0700335 print "=" * 90
336 print " " * 30 + "Output Driver File :"
Jon Hall4ba53f02015-07-29 13:07:41 -0700337 print ",\n".join(self.driverList)
adminbae64d82013-08-01 10:50:15 -0700338 print "=" * 90
Jon Hall4ba53f02015-07-29 13:07:41 -0700339 return True
adminbae64d82013-08-01 10:50:15 -0700340
Jon Hall4ba53f02015-07-29 13:07:41 -0700341
342
adminbae64d82013-08-01 10:50:15 -0700343 def getDriverPath(self):
344 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700345 It will set the driver path and returns it.If driver path is not specified then it will take
adminbae64d82013-08-01 10:50:15 -0700346 default path (/lib/updatedriver/).
Jon Hall4ba53f02015-07-29 13:07:41 -0700347 '''
adminbae64d82013-08-01 10:50:15 -0700348 self.driverPath = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700349 try :
adminbae64d82013-08-01 10:50:15 -0700350 self.driverPath = self.configDict['config-driver']['importTypes'][self.driver]['driver-path']
Jon Hall4ba53f02015-07-29 13:07:41 -0700351
Jon Hallf632d202015-07-30 15:45:11 -0700352 except KeyError:
353 location = os.path.abspath( os.path.dirname( __file__ ) )
354 path = re.sub( "(bin)$", "", location )
adminbae64d82013-08-01 10:50:15 -0700355 self.driverPath = path + "/lib/updatedriver/"
356 return self.driverPath
357
358
359 def printHeader(self,driver):
360 content = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700361
adminbae64d82013-08-01 10:50:15 -0700362 print " " * 10 +"=" * 90 + "\n"
Jon Hall4ba53f02015-07-29 13:07:41 -0700363 content = content + " " * 30 + "*-- Welcome to Updated Driver --*\n"
adminbae64d82013-08-01 10:50:15 -0700364 content = content + "\n" + " " * 10 + " " * 10 + "Config File : " + "/home/openflow/TestON/config/ofadriver.py"
Jon Hall4ba53f02015-07-29 13:07:41 -0700365 content = content + "\n" + " " * 10 + " " * 10 + "Drivers Name : " + driver
366 print content
367 print " " * 10 + "=" * 90
adminbae64d82013-08-01 10:50:15 -0700368
369
370