blob: 122e7bce0d1d4118e1e7eeae3c7f79af0be26b10 [file] [log] [blame]
kelvin-onlab1d381fe2015-07-14 16:24:56 -07001"""
2 Wrapper function for FuncTopo
3 Includes onosclidriver and mininetclidriver functions
4"""
5import time
6import json
7import re
8
9def __init__( self ):
10 self.default = ''
11
Chiyu Chengb8c2c842016-10-05 12:40:49 -070012def getTimestampFromString( main, targetString ):
13 #Get time string from the target string
14 try:
15 assert type( targetString ) is str
16 timeString = targetString.split( ' | ' )
17 timeString = timeString[ 0 ]
18 from datetime import datetime
19 # convert time string to timestamp
20 t = datetime.strptime( timeString, "%Y-%m-%d %H:%M:%S,%f" )
21 import time
22 timestamp = time.mktime( t.timetuple() )
23 timestamp += int( t.microsecond / 1000 ) / 1000.0
24 return timestamp
25 except AssertionError:
26 main.log.error( "Got nothing firom log" )
27 return -1
28 except IndexError:
29 main.log.error( "Time string index error" )
30 return -1
31 except ValueError:
32 main.log.error( "Got wrong string from log" )
33 return -1
34
Chiyu Cheng899621b2016-11-14 11:14:48 -080035def getRoleRequestTimeFromTshark( main ):
36 try:
37 main.log.info( "Get role request time" )
38 with open(main.tsharkResultPath, "r" ) as resultFile:
39 resultText = resultFile.readlines()
40 # select the last role request string
41 roleRequestString = resultText[ len( resultText ) - 1 ]
42 main.log.info( roleRequestString )
43 # get timestamp from role request string
44 roleRequestTime = roleRequestString.split( " " )
45 resultFile.close()
46 return float(roleRequestTime[1])
47 except IndexError:
48 main.log.error("Got wrong role request string from Tshark file")
49 return -1
50
51def compareTimeDiffWithRoleRequest(main, term, Mode, index=0 ):
52 '''
53 Description:
54 Compare the time difference between the time of target term and the time of role request
55 Inclides onosclidriver functions
56
57 '''
58 try:
Chiyu Chengec63bde2016-11-17 18:11:36 -080059 termInfo = main.CLIs[ index ].logSearch( mode=Mode, term=term )
Chiyu Cheng899621b2016-11-14 11:14:48 -080060 termTime = getTimestampFromString( main, termInfo[ 0 ] )
61 roleRequestTime = getRoleRequestTimeFromTshark( main )
62 if termTime == -1 or roleRequestTime == -1:
63 main.writeData = -1
64 main.log.error( "Can't compare the difference with role request time" )
65 return -1
Jon Hall465d56c2016-12-05 10:03:02 -080066 # Only concern about the absolute value of difference.
Chiyu Cheng899621b2016-11-14 11:14:48 -080067 return abs( roleRequestTime - termTime )
68 except IndexError:
69 main.log.error( "Catch the wrong information of search term " )
70 main.writeData = -1
71 return -1
72
Chiyu Chengb8c2c842016-10-05 12:40:49 -070073def getInfoFromLog( main, term1, mode1, term2, mode2, index=0, funcMode='TD' ):
74 '''
75 Description:
76 Get needed informations of the search term from karaf.log
77 Includes onosclidriver functions
78 Function mode:
79 TD (time difference):
80 Get time difference between start and end
81 Term1: startTerm
82 Term2: endTerm
83 DR (disconnect rate):
84 Get switch disconnect rate
85 Term1: disconnectTerm
86 Term2: connectTerm
87
88 '''
89 try:
Chiyu Chengec63bde2016-11-17 18:11:36 -080090 termInfo1 = main.CLIs[ index ].logSearch( mode=mode1,term=term1 )
91 termInfo2 = main.CLIs[ index ].logSearch( mode=mode2,term=term2 )
Chiyu Chengb8c2c842016-10-05 12:40:49 -070092 if funcMode == 'TD':
93 startTime = getTimestampFromString( main, termInfo1[0] )
94 endTime = getTimestampFromString ( main, termInfo2[0] )
95 if startTime == -1 or endTime == -1:
96 main.log.error( "Wrong Time!" )
97 main.writeData = -1
98 return -1
99 return endTime - startTime
100 if funcMode == 'DR':
101 #In this mode, termInfo1 means the total number of switch disconnection and
102 #termInfo2 means the total number of new switch connection
103 #termInfo2 - termInfo1 means the actual real number of switch connection.
104 disconnection = int( termInfo1 ) * 1.0
105 expectConnection = int( main.currScale ) ** 2
106 realConnection = int( termInfo2 ) - int( termInfo1 )
107 if expectConnection != realConnection:
108 main.log.error( "The number of real switch connection doesn't match the number of expected connection" )
109 main.writeData = -1
110 return -1
111 rate = disconnection / expectConnection
112 return rate
113 except IndexError:
114 main.log.error( "Catch the wrong information of search term" )
115 main.writeData = -1
116 return -1
117
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700118def testTopology( main, topoFile='', args='', mnCmd='', timeout=300, clean=True ):
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700119 """
120 Description:
121 This function combines different wrapper functions in this module
122 to simulate a topology test
123 Test Steps:
124 - Load topology
125 - Discover topology
126 - Compare topology
127 - pingall
128 - Bring links down
129 - Compare topology
130 - pingall
131 - Bring links up
132 - Compare topology
133 - pingall
134 Options:
135 clean: Does sudo mn -c to clean mininet residue
136 Please read mininetclidriver.py >> startNet( .. ) function for details
137 Returns:
138 Returns main.TRUE if the test is successful, main.FALSE otherwise
139 """
140 testTopoResult = main.TRUE
141 compareTopoResult = main.TRUE
142 topoObjectResult = main.TRUE
143 stopResult = main.TRUE
144
145 if clean:
146 # Cleans minient
147 stopResult = stopMininet( main )
148
149 # Restart ONOS to clear hosts test new mininet topology
150 reinstallOnosResult = reinstallOnos( main )
151
152 # Starts topology
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700153 startResult = startNewTopology( main, topoFile, args, mnCmd, timeout=timeout )
GlennRC1c5df3c2015-08-27 16:12:09 -0700154 # onos needs time to see the links
155 time.sleep(15)
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700156
157 # Gets list of switches in mininet
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700158 #assignSwitch( main )
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700159
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700160 testTopoResult = startResult and topoObjectResult
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700161
162
163 return testTopoResult
164
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700165def startNewTopology( main, topoFile='', args='', mnCmd='', timeout=900 ):
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700166 """
167 Description:
168 This wrapper function starts new topology
169 Options:
170 Please read mininetclidriver.py >> startNet( .. ) function for details
171 Return:
172 Returns main.TRUE if topology is successfully created by mininet,
173 main.FALSE otherwise
174 NOTE:
175 Assumes Mininet1 is the name of the handler
176 """
177 assert main, "There is no main variable"
178 assert main.Mininet1, "Mininet 1 is not created"
179 result = main.TRUE
180
181 main.log.info( main.topoName + ": Starting new Mininet topology" )
182
183 # log which method is being used
184 if topoFile:
185 main.log.info( main.topoName + ": Starting topology with " +
186 topoFile + "topology file" )
187 elif not topoFile and not mnCmd:
188 main.log.info( main.topoName + ": Starting topology using" +
189 " the topo file" )
190 elif topoFile and mnCmd:
191 main.log.error( main.topoName + ": You can only use one " +
192 "method to start a topology" )
193 elif mnCmd:
194 main.log.info( main.topoName + ": Starting topology with '" +
195 mnCmd + "' Mininet command" )
196
197
198 result = main.Mininet1.startNet( topoFile=topoFile,
199 args=args,
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700200 mnCmd=mnCmd,
201 timeout=timeout)
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700202
203 return result
204
205def stopMininet( main ):
206 """
207 Stops current topology and execute mn -c basically triggers
208 stopNet in mininetclidrivers
209
210 NOTE: Mininet should be running when issuing this command other wise
211 the this function will cause the test to stop
212 """
213 stopResult = main.TRUE
214 stopResult = main.Mininet1.stopNet()
215 time.sleep( 30 )
216 if not stopResult:
217 main.log.info( main.topoName + ": Did not stop Mininet topology" )
218 return stopResult
219
220def compareTopo( main ):
221 """
222 Compare topology( devices, links, ports, hosts ) between ONOS and
223 mininet using sts
224 """
225 devices = []
226 links = []
227 ports = []
228 hosts = []
229 switchResult = []
230 linksResult = []
231 portsResult = []
232 hostsResult = []
233 mnSwitches = main.Mininet1.getSwitches()
234 mnLinks = main.Mininet1.getLinks()
235 mnHosts = main.Mininet1.getHosts()
236 compareTopoResult = main.TRUE
237
238 for i in range( main.numCtrls ):
239 devices.append( json.loads( main.CLIs[ i ].devices() ) )
240 links.append( json.loads( main.CLIs[ i ].links() ) )
241 ports.append( json.loads( main.CLIs[ i ].ports() ) )
242 hosts.append( json.loads( main.CLIs[ i ].hosts() ) )
243
244 # Comparing switches
245 main.log.info( main.topoName + ": Comparing switches in each ONOS nodes" +
246 " with Mininet" )
247 for i in range( main.numCtrls ):
248 tempResult = main.Mininet1.compareSwitches( mnSwitches,
249 devices[ i ],
250 ports[ i ] )
251 switchResult.append( tempResult )
252 if tempResult == main.FALSE:
253 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
254 " switch view is incorrect " )
255
256 if all( result == main.TRUE for result in switchResult ):
257 main.log.info( main.topoName + ": Switch view in all ONOS nodes "+
258 "are correct " )
259 else:
260 compareTopoResult = main.FALSE
261
262 # Comparing links
263 main.log.info( main.topoName + ": Comparing links in each ONOS nodes" +
264 " with Mininet" )
265 for i in range( main.numCtrls ):
266 tempResult = main.Mininet1.compareLinks( mnSwitches,
267 mnLinks,
268 links[ i ] )
269 linksResult.append( tempResult )
270 if tempResult == main.FALSE:
271 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
272 " links view are incorrect " )
273
274 if all( result == main.TRUE for result in linksResult ):
275 main.log.info( main.topoName + ": Links view in all ONOS nodes "+
276 "are correct " )
277 else:
278 compareTopoResult = main.FALSE
279
280 # Comparing hosts
281 main.log.info( main.topoName + ": Comparing hosts in each ONOS nodes" +
282 " with Mininet" )
283 for i in range( main.numCtrls ):
284 tempResult = main.Mininet1.compareHosts( mnHosts, hosts[ i ] )
285 hostsResult.append( tempResult )
286 if tempResult == main.FALSE:
287 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
288 " hosts view are incorrect " )
289
290 if all( result == main.TRUE for result in hostsResult ):
291 main.log.info( main.topoName + ": Hosts view in all ONOS nodes "+
292 "are correct " )
293 else:
294 compareTopoResult = main.FALSE
295
296 return compareTopoResult
297
298def assignSwitch( main ):
299 """
300 Returns switch list using getSwitch in Mininet driver
301 """
302 switchList = []
303 assignResult = main.TRUE
304 switchList = main.Mininet1.getSwitch()
305 assignResult = main.Mininet1.assignSwController( sw=switchList,
306 ip=main.ONOSip[ 0 ],
GlennRC632e2892015-10-19 18:58:41 -0700307 port=6633 )
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700308
309 for sw in switchList:
310 response = main.Mininet1.getSwController( sw )
311 if re.search( "tcp:" + main.ONOSip[ 0 ], response ):
312 assignResult = assignResult and main.TRUE
313 else:
314 assignResult = main.FALSE
315
316 return switchList
317
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700318def connectivity( main, timeout=900, shortCircuit=True, acceptableFailed=20 ):
319 """
320 Use fwd app and pingall to discover all the hosts
321 """
322 activateResult = main.TRUE
323 appCheck = main.TRUE
324 getDataResult = main.TRUE
325 main.log.info( main.topoName + ": Activating reactive forwarding app " )
326 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
327
328 if main.hostsData:
329 main.hostsData = {}
330 for i in range( main.numCtrls ):
331 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
332 if appCheck != main.TRUE:
333 main.log.warn( main.CLIs[ i ].apps() )
334 main.log.warn( main.CLIs[ i ].appIDs() )
335
336 time.sleep( main.fwdSleep )
337
338 # Discover hosts using pingall
339 pingResult = main.Mininet1.pingall( timeout=timeout,
340 shortCircuit=shortCircuit,
341 acceptableFailed=acceptableFailed )
342
343 main.log.info( main.topoName + ": Deactivate reactive forwarding app " )
344 activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
345 for i in range( main.numCtrls ):
346 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
347 if appCheck != main.TRUE:
348 main.log.warn( main.CLIs[ i ].apps() )
349 main.log.warn( main.CLIs[ i ].appIDs() )
350
351 return pingResult
352
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700353def getHostsData( main ):
354 """
355 Use fwd app and pingall to discover all the hosts
356 """
357 activateResult = main.TRUE
358 appCheck = main.TRUE
359 getDataResult = main.TRUE
360 main.log.info( main.topoName + ": Activating reactive forwarding app " )
361 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
362
363 if main.hostsData:
364 main.hostsData = {}
365 for i in range( main.numCtrls ):
366 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
367 if appCheck != main.TRUE:
368 main.log.warn( main.CLIs[ i ].apps() )
369 main.log.warn( main.CLIs[ i ].appIDs() )
370
371 time.sleep( main.fwdSleep )
372 # Discover hosts using pingall
373 pingResult = main.Mininet1.pingall( timeout=900 )
374
375 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
376 hosts = main.Mininet1.getHosts().keys()
377
378 for host in hosts:
379 main.hostsData[ host ] = {}
380 main.hostsData[ host ][ 'mac' ] = \
381 main.Mininet1.getMacAddress( host ).upper()
382 for hostj in hostsJson:
383 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
384 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
385 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
386 main.hostsData[ host ][ 'location' ] = \
387 hostj[ 'location' ][ 'elementId' ] + '/' + \
388 hostj[ 'location' ][ 'port' ]
389 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
390
391 if activateResult and main.hostsData:
392 main.log.info( main.topoName + ": Successfully used fwd app" +
393 " to discover hosts " )
394 getDataResult = main.TRUE
395 else:
396 main.log.info( main.topoName + ": Failed to use fwd app" +
397 " to discover hosts " )
398 getDataResult = main.FALSE
399
400 main.log.info( main.topoName + ": Deactivate reactive forwarding app " )
401 activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
402 for i in range( main.numCtrls ):
403 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
404 if appCheck != main.TRUE:
405 main.log.warn( main.CLIs[ i ].apps() )
406 main.log.warn( main.CLIs[ i ].appIDs() )
407
408 # This data can be use later for intents
409 print main.hostsData
410
411 return getDataResult
412
413def reinstallOnos( main ):
414 """
415 Description:
416 Stop and start ONOS that clears hosts,devices etc. in order to test
417 new mininet topology
418 Return:
419 Retruns main.TRUE for a successful restart, main.FALSE otherwise.
420 """
421 uninstallResult = []
422 installResult = []
423 stopResult = []
424 startResult = []
425 onosIsUpResult = []
426 restartResult = main.TRUE
427
428 main.log.info( main.topoName + ": Uninstall ONOS cluster" )
429 for ip in main.ONOSip:
430 uninstallResult.append( main.ONOSbench.onosUninstall( nodeIp=ip ) )
431
432 if all( result == main.TRUE for result in uninstallResult ):
433 main.log.info( main.topoName + ": Successfully uninstall ONOS cluster" )
434 else:
435 restartResult = main.FALSE
436 main.log.error( main.topoName + ": Failed to uninstall ONOS cluster" )
437
438 time.sleep( main.startUpSleep )
439
440 main.log.info( main.topoName + ": Installing ONOS cluster" )
441
442 for i in range( main.numCtrls ):
443 installResult.append( main.ONOSbench.onosInstall(
444 node=main.ONOSip[ i ] ) )
445
446 if all( result == main.TRUE for result in installResult ):
447 main.log.info( main.topoName + ": Successfully installed ONOS cluster" )
448 else:
449 restartResult = main.FALSE
450 main.log.error( main.topoName + ": Failed to install ONOS cluster" )
451
452 for i in range( main.numCtrls ):
453 onosIsUpResult.append( main.ONOSbench.isup( main.ONOSip[ i ] ) )
454
455 if all( result == main.TRUE for result in onosIsUpResult ):
456 main.log.report( "ONOS instance is up and ready" )
457 else:
458 main.log.report( "ONOS instance may not be up, stop and " +
459 "start ONOS again " )
460 for i in range( main.numCtrls ):
461 stopResult.append( main.ONOSbench.onosStop( main.ONOSip[ i ] ) )
462
463 if all( result == main.TRUE for result in stopResult ):
464 main.log.info( main.topoName + ": Successfully stop ONOS cluster" )
465 else:
466 main.log.error( main.topoName + ": Failed to stop ONOS cluster" )
467
468 for i in range( main.numCtrls ):
469 startResult.append( main.ONOSbench.onosStart( main.ONOSip[ i ] ) )
470
471 if all( result == main.TRUE for result in startResult ):
472 main.log.info( main.topoName + ": Successfully start ONOS cluster" )
473 else:
474 main.log.error( main.topoName + ": Failed to start ONOS cluster" )
475
Chiyu Chengef109502016-11-21 15:51:38 -0800476 main.log.info( main.topoName + ": set up ONOS secure SSH" )
477 secureSshResult = []
478 for i in range( int( main.numCtrls ) ):
Chiyu Cheng421ad372016-11-21 18:04:09 -0800479 secureSshResult.append( main.onosSecureSSH( node=main.ONOSip[i] ) )
Chiyu Chengef109502016-11-21 15:51:38 -0800480 if all( result == main.TRUE for result in secureSshResult ):
481 main.log.info( main.topoName + ": Successfully set up ONOS secure SSH" )
482 else:
483 main.log.error( main.topoName + ": Failed to set up ONOS secure SSH" )
484 restartResult = main.FALSE
485
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700486 main.log.info( main.topoName + ": Starting ONOS CLI" )
487 cliResult = []
488 for i in range( main.numCtrls ):
489 cliResult.append( main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] ) )
490
491 if all( result == main.TRUE for result in cliResult ):
492 main.log.info( main.topoName + ": Successfully start ONOS cli" )
493 else:
494 main.log.error( main.topoName + ": Failed to start ONOS cli" )
495 restartResult = main.FALSE
496
497
498 return restartResult
499
500
501