blob: 95eb1954efe8c6562b1a49277a53359aa8c4fcf9 [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
kelvin-onlabd9e23de2015-08-06 10:34:44 -070012def testTopology( main, topoFile='', args='', mnCmd='', timeout=300, clean=True ):
kelvin-onlab1d381fe2015-07-14 16:24:56 -070013 """
14 Description:
15 This function combines different wrapper functions in this module
16 to simulate a topology test
17 Test Steps:
18 - Load topology
19 - Discover topology
20 - Compare topology
21 - pingall
22 - Bring links down
23 - Compare topology
24 - pingall
25 - Bring links up
26 - Compare topology
27 - pingall
28 Options:
29 clean: Does sudo mn -c to clean mininet residue
30 Please read mininetclidriver.py >> startNet( .. ) function for details
31 Returns:
32 Returns main.TRUE if the test is successful, main.FALSE otherwise
33 """
34 testTopoResult = main.TRUE
35 compareTopoResult = main.TRUE
36 topoObjectResult = main.TRUE
37 stopResult = main.TRUE
38
39 if clean:
40 # Cleans minient
41 stopResult = stopMininet( main )
42
43 # Restart ONOS to clear hosts test new mininet topology
44 reinstallOnosResult = reinstallOnos( main )
45
46 # Starts topology
kelvin-onlabd9e23de2015-08-06 10:34:44 -070047 startResult = startNewTopology( main, topoFile, args, mnCmd, timeout=timeout )
kelvin-onlab1d381fe2015-07-14 16:24:56 -070048
49 # Gets list of switches in mininet
kelvin-onlabd9e23de2015-08-06 10:34:44 -070050 #assignSwitch( main )
kelvin-onlab1d381fe2015-07-14 16:24:56 -070051
kelvin-onlabd9e23de2015-08-06 10:34:44 -070052 testTopoResult = startResult and topoObjectResult
kelvin-onlab1d381fe2015-07-14 16:24:56 -070053
54
55 return testTopoResult
56
kelvin-onlabd9e23de2015-08-06 10:34:44 -070057def startNewTopology( main, topoFile='', args='', mnCmd='', timeout=900 ):
kelvin-onlab1d381fe2015-07-14 16:24:56 -070058 """
59 Description:
60 This wrapper function starts new topology
61 Options:
62 Please read mininetclidriver.py >> startNet( .. ) function for details
63 Return:
64 Returns main.TRUE if topology is successfully created by mininet,
65 main.FALSE otherwise
66 NOTE:
67 Assumes Mininet1 is the name of the handler
68 """
69 assert main, "There is no main variable"
70 assert main.Mininet1, "Mininet 1 is not created"
71 result = main.TRUE
72
73 main.log.info( main.topoName + ": Starting new Mininet topology" )
74
75 # log which method is being used
76 if topoFile:
77 main.log.info( main.topoName + ": Starting topology with " +
78 topoFile + "topology file" )
79 elif not topoFile and not mnCmd:
80 main.log.info( main.topoName + ": Starting topology using" +
81 " the topo file" )
82 elif topoFile and mnCmd:
83 main.log.error( main.topoName + ": You can only use one " +
84 "method to start a topology" )
85 elif mnCmd:
86 main.log.info( main.topoName + ": Starting topology with '" +
87 mnCmd + "' Mininet command" )
88
89
90 result = main.Mininet1.startNet( topoFile=topoFile,
91 args=args,
kelvin-onlabd9e23de2015-08-06 10:34:44 -070092 mnCmd=mnCmd,
93 timeout=timeout)
kelvin-onlab1d381fe2015-07-14 16:24:56 -070094
95 return result
96
97def stopMininet( main ):
98 """
99 Stops current topology and execute mn -c basically triggers
100 stopNet in mininetclidrivers
101
102 NOTE: Mininet should be running when issuing this command other wise
103 the this function will cause the test to stop
104 """
105 stopResult = main.TRUE
106 stopResult = main.Mininet1.stopNet()
107 time.sleep( 30 )
108 if not stopResult:
109 main.log.info( main.topoName + ": Did not stop Mininet topology" )
110 return stopResult
111
112def compareTopo( main ):
113 """
114 Compare topology( devices, links, ports, hosts ) between ONOS and
115 mininet using sts
116 """
117 devices = []
118 links = []
119 ports = []
120 hosts = []
121 switchResult = []
122 linksResult = []
123 portsResult = []
124 hostsResult = []
125 mnSwitches = main.Mininet1.getSwitches()
126 mnLinks = main.Mininet1.getLinks()
127 mnHosts = main.Mininet1.getHosts()
128 compareTopoResult = main.TRUE
129
130 for i in range( main.numCtrls ):
131 devices.append( json.loads( main.CLIs[ i ].devices() ) )
132 links.append( json.loads( main.CLIs[ i ].links() ) )
133 ports.append( json.loads( main.CLIs[ i ].ports() ) )
134 hosts.append( json.loads( main.CLIs[ i ].hosts() ) )
135
136 # Comparing switches
137 main.log.info( main.topoName + ": Comparing switches in each ONOS nodes" +
138 " with Mininet" )
139 for i in range( main.numCtrls ):
140 tempResult = main.Mininet1.compareSwitches( mnSwitches,
141 devices[ i ],
142 ports[ i ] )
143 switchResult.append( tempResult )
144 if tempResult == main.FALSE:
145 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
146 " switch view is incorrect " )
147
148 if all( result == main.TRUE for result in switchResult ):
149 main.log.info( main.topoName + ": Switch view in all ONOS nodes "+
150 "are correct " )
151 else:
152 compareTopoResult = main.FALSE
153
154 # Comparing links
155 main.log.info( main.topoName + ": Comparing links in each ONOS nodes" +
156 " with Mininet" )
157 for i in range( main.numCtrls ):
158 tempResult = main.Mininet1.compareLinks( mnSwitches,
159 mnLinks,
160 links[ i ] )
161 linksResult.append( tempResult )
162 if tempResult == main.FALSE:
163 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
164 " links view are incorrect " )
165
166 if all( result == main.TRUE for result in linksResult ):
167 main.log.info( main.topoName + ": Links view in all ONOS nodes "+
168 "are correct " )
169 else:
170 compareTopoResult = main.FALSE
171
172 # Comparing hosts
173 main.log.info( main.topoName + ": Comparing hosts in each ONOS nodes" +
174 " with Mininet" )
175 for i in range( main.numCtrls ):
176 tempResult = main.Mininet1.compareHosts( mnHosts, hosts[ i ] )
177 hostsResult.append( tempResult )
178 if tempResult == main.FALSE:
179 main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) +
180 " hosts view are incorrect " )
181
182 if all( result == main.TRUE for result in hostsResult ):
183 main.log.info( main.topoName + ": Hosts view in all ONOS nodes "+
184 "are correct " )
185 else:
186 compareTopoResult = main.FALSE
187
188 return compareTopoResult
189
190def assignSwitch( main ):
191 """
192 Returns switch list using getSwitch in Mininet driver
193 """
194 switchList = []
195 assignResult = main.TRUE
196 switchList = main.Mininet1.getSwitch()
197 assignResult = main.Mininet1.assignSwController( sw=switchList,
198 ip=main.ONOSip[ 0 ],
199 port=6633 )
200
201 for sw in switchList:
202 response = main.Mininet1.getSwController( sw )
203 if re.search( "tcp:" + main.ONOSip[ 0 ], response ):
204 assignResult = assignResult and main.TRUE
205 else:
206 assignResult = main.FALSE
207
208 return switchList
209
kelvin-onlabd9e23de2015-08-06 10:34:44 -0700210def connectivity( main, timeout=900, shortCircuit=True, acceptableFailed=20 ):
211 """
212 Use fwd app and pingall to discover all the hosts
213 """
214 activateResult = main.TRUE
215 appCheck = main.TRUE
216 getDataResult = main.TRUE
217 main.log.info( main.topoName + ": Activating reactive forwarding app " )
218 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
219
220 if main.hostsData:
221 main.hostsData = {}
222 for i in range( main.numCtrls ):
223 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
224 if appCheck != main.TRUE:
225 main.log.warn( main.CLIs[ i ].apps() )
226 main.log.warn( main.CLIs[ i ].appIDs() )
227
228 time.sleep( main.fwdSleep )
229
230 # Discover hosts using pingall
231 pingResult = main.Mininet1.pingall( timeout=timeout,
232 shortCircuit=shortCircuit,
233 acceptableFailed=acceptableFailed )
234
235 main.log.info( main.topoName + ": Deactivate reactive forwarding app " )
236 activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
237 for i in range( main.numCtrls ):
238 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
239 if appCheck != main.TRUE:
240 main.log.warn( main.CLIs[ i ].apps() )
241 main.log.warn( main.CLIs[ i ].appIDs() )
242
243 return pingResult
244
kelvin-onlab1d381fe2015-07-14 16:24:56 -0700245def getHostsData( main ):
246 """
247 Use fwd app and pingall to discover all the hosts
248 """
249 activateResult = main.TRUE
250 appCheck = main.TRUE
251 getDataResult = main.TRUE
252 main.log.info( main.topoName + ": Activating reactive forwarding app " )
253 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
254
255 if main.hostsData:
256 main.hostsData = {}
257 for i in range( main.numCtrls ):
258 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
259 if appCheck != main.TRUE:
260 main.log.warn( main.CLIs[ i ].apps() )
261 main.log.warn( main.CLIs[ i ].appIDs() )
262
263 time.sleep( main.fwdSleep )
264 # Discover hosts using pingall
265 pingResult = main.Mininet1.pingall( timeout=900 )
266
267 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
268 hosts = main.Mininet1.getHosts().keys()
269
270 for host in hosts:
271 main.hostsData[ host ] = {}
272 main.hostsData[ host ][ 'mac' ] = \
273 main.Mininet1.getMacAddress( host ).upper()
274 for hostj in hostsJson:
275 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
276 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
277 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
278 main.hostsData[ host ][ 'location' ] = \
279 hostj[ 'location' ][ 'elementId' ] + '/' + \
280 hostj[ 'location' ][ 'port' ]
281 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
282
283 if activateResult and main.hostsData:
284 main.log.info( main.topoName + ": Successfully used fwd app" +
285 " to discover hosts " )
286 getDataResult = main.TRUE
287 else:
288 main.log.info( main.topoName + ": Failed to use fwd app" +
289 " to discover hosts " )
290 getDataResult = main.FALSE
291
292 main.log.info( main.topoName + ": Deactivate reactive forwarding app " )
293 activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
294 for i in range( main.numCtrls ):
295 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
296 if appCheck != main.TRUE:
297 main.log.warn( main.CLIs[ i ].apps() )
298 main.log.warn( main.CLIs[ i ].appIDs() )
299
300 # This data can be use later for intents
301 print main.hostsData
302
303 return getDataResult
304
305def reinstallOnos( main ):
306 """
307 Description:
308 Stop and start ONOS that clears hosts,devices etc. in order to test
309 new mininet topology
310 Return:
311 Retruns main.TRUE for a successful restart, main.FALSE otherwise.
312 """
313 uninstallResult = []
314 installResult = []
315 stopResult = []
316 startResult = []
317 onosIsUpResult = []
318 restartResult = main.TRUE
319
320 main.log.info( main.topoName + ": Uninstall ONOS cluster" )
321 for ip in main.ONOSip:
322 uninstallResult.append( main.ONOSbench.onosUninstall( nodeIp=ip ) )
323
324 if all( result == main.TRUE for result in uninstallResult ):
325 main.log.info( main.topoName + ": Successfully uninstall ONOS cluster" )
326 else:
327 restartResult = main.FALSE
328 main.log.error( main.topoName + ": Failed to uninstall ONOS cluster" )
329
330 time.sleep( main.startUpSleep )
331
332 main.log.info( main.topoName + ": Installing ONOS cluster" )
333
334 for i in range( main.numCtrls ):
335 installResult.append( main.ONOSbench.onosInstall(
336 node=main.ONOSip[ i ] ) )
337
338 if all( result == main.TRUE for result in installResult ):
339 main.log.info( main.topoName + ": Successfully installed ONOS cluster" )
340 else:
341 restartResult = main.FALSE
342 main.log.error( main.topoName + ": Failed to install ONOS cluster" )
343
344 for i in range( main.numCtrls ):
345 onosIsUpResult.append( main.ONOSbench.isup( main.ONOSip[ i ] ) )
346
347 if all( result == main.TRUE for result in onosIsUpResult ):
348 main.log.report( "ONOS instance is up and ready" )
349 else:
350 main.log.report( "ONOS instance may not be up, stop and " +
351 "start ONOS again " )
352 for i in range( main.numCtrls ):
353 stopResult.append( main.ONOSbench.onosStop( main.ONOSip[ i ] ) )
354
355 if all( result == main.TRUE for result in stopResult ):
356 main.log.info( main.topoName + ": Successfully stop ONOS cluster" )
357 else:
358 main.log.error( main.topoName + ": Failed to stop ONOS cluster" )
359
360 for i in range( main.numCtrls ):
361 startResult.append( main.ONOSbench.onosStart( main.ONOSip[ i ] ) )
362
363 if all( result == main.TRUE for result in startResult ):
364 main.log.info( main.topoName + ": Successfully start ONOS cluster" )
365 else:
366 main.log.error( main.topoName + ": Failed to start ONOS cluster" )
367
368 main.log.info( main.topoName + ": Starting ONOS CLI" )
369 cliResult = []
370 for i in range( main.numCtrls ):
371 cliResult.append( main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] ) )
372
373 if all( result == main.TRUE for result in cliResult ):
374 main.log.info( main.topoName + ": Successfully start ONOS cli" )
375 else:
376 main.log.error( main.topoName + ": Failed to start ONOS cli" )
377 restartResult = main.FALSE
378
379
380 return restartResult
381
382
383