blob: d4d33928030d0f76a0f672b9d588d221e874f66e [file] [log] [blame]
kelvin-onlab9f108d72015-06-08 15:58:43 -07001"""
2 Wrapper function for FuncTopo
3 Includes onosclidriver and mininetclidriver functions
4"""
5import time
6import json
7
8def __init__( self ):
9 self.default = ''
10
11def testTopology( main, topoFile='', args='', mnCmd='', clean=True ):
12 """
13 Description:
14 This function combines different wrapper functions in this module
15 to simulate a topology test
16 Test Steps:
17 - Load topology
18 - Discover topology
19 - Compare topology
20 - pingall
21 - Bring links down
22 - Compare topology
23 - pingall
24 - Bring links up
25 - Compare topology
26 - pingall
27
28 Returns:
29 Returns main.TRUE if the test is successful, main.FALSE otherwise
30 """
31 testTopoResult = main.TRUE
32 compareTopoResult = main.TRUE
33 topoObjectResult = main.TRUE
34
35 # Starts topology
36 startResult = startNewTopology( main, topoFile, args, mnCmd, clean )
37
38 # Create topology object using sts
39 topoObjectResult = createTopoObject( main )
40
41 # Compare Topology
42 compareTopoResult = compareTopo( main )
43
44 # This function activates fwd app then does pingall as well as store
45 # hosts data in a variable main.hostsData
46 getHostsResult = getHostsData( main )
47
48 testTopoResult = startResult and topoObjectResult and \
49 compareTopoResult and getHostsResult
50
kelvin-onlabf512e942015-06-08 19:42:59 -070051 # Restart ONOS to clear hosts test new mininet topology
52 restartResult = restartONOS( main )
53
kelvin-onlab9f108d72015-06-08 15:58:43 -070054 return testTopoResult
55
56def startNewTopology( main, topoFile='', args='', mnCmd='', clean=True ):
57 """
58 Description:
59 This wrapper function starts new topology
60 Optional:
61 clean - Stops current topology and execute mn -c basically triggers
62 stopNet in mininetclidrivers
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 if clean:
90 main.Mininet1.stopNet()
91 time.sleep( 30 )
92 else:
93 main.log.info( main.topoName + ": Did not stop Mininet topology" )
94
95 result = main.Mininet1.startNet( topoFile=topoFile,
96 args=args,
97 mnCmd=mnCmd )
98
99 return result
100
101def createTopoObject( main ):
102 """
103 Creates topology object using sts module
104 """
105 from sts.topology.teston_topology import TestONTopology
106 global MNTopo
107 try:
108 ctrls = []
109 main.log.info( main.topoName + ": Creating topology object" +
110 " from mininet" )
111 for node in main.nodes:
112 temp = ( node, node.name, node.ip_address, 6633 )
113 ctrls.append( temp )
114 MNTopo = TestONTopology( main.Mininet1, ctrls )
115 except Exception:
116 objResult = main.FALSE
117 else:
118 objResult = main.TRUE
119
120 return objResult
121
122def compareTopo( main ):
123 """
124 Compare topology( devices, links, ports, hosts ) between ONOS and
125 mininet using sts
126 """
127 assert MNTopo, "There is no MNTopo object"
128 devices = []
129 links = []
130 ports = []
131 hosts = []
132 switchResult = []
133 linksResult = []
134 portsResult = []
135 hostsResult = []
136 compareTopoResult = main.TRUE
137
138 for i in range( main.numCtrls ):
139 devices.append( json.loads( main.CLIs[ i ].devices() ) )
140 links.append( json.loads( main.CLIs[ i ].links() ) )
141 ports.append( json.loads( main.CLIs[ i ].ports() ) )
142 hosts.append( json.loads( main.CLIs[ i ].hosts() ) )
143
144 # Comparing switches
145 main.log.info( main.topoName + ": Comparing switches in each ONOS nodes" +
146 " with Mininet" )
147 for i in range( main.numCtrls ):
148 tempResult = main.Mininet1.compareSwitches( MNTopo, devices[ i ] )
149 switchResult.append( tempResult )
150 if tempResult == main.FALSE:
151 main.log.error( main.topoName + ": ONOS-" + str( i ) +
152 " switch view is incorrect " )
153
154 if all( result == main.TRUE for result in switchResult ):
155 main.log.info( main.topoName + ": Switch view in all ONOS nodes "+
156 "are correct " )
157 else:
158 compareTopoResult = main.FALSE
159
160 # Comparing ports
161 main.log.info( main.topoName + ": Comparing ports in each ONOS nodes" +
162 " with Mininet" )
163 for i in range( main.numCtrls ):
164 tempResult = main.Mininet1.comparePorts( MNTopo, ports[ i ] )
165 portsResult.append( tempResult )
166 if tempResult == main.FALSE:
167 main.log.error( main.topoName + ": ONOS-" + str( i ) +
168 " ports view are incorrect " )
169
170 if all( result == main.TRUE for result in portsResult ):
171 main.log.info( main.topoName + ": Ports view in all ONOS nodes "+
172 "are correct " )
173 else:
174 compareTopoResult = main.FALSE
175
176 # Comparing links
177 main.log.info( main.topoName + ": Comparing links in each ONOS nodes" +
178 " with Mininet" )
179 for i in range( main.numCtrls ):
180 tempResult = main.Mininet1.compareLinks( MNTopo, links[ i ] )
181 linksResult.append( tempResult )
182 if tempResult == main.FALSE:
183 main.log.error( main.topoName + ": ONOS-" + str( i ) +
184 " links view are incorrect " )
185
186 if all( result == main.TRUE for result in linksResult ):
187 main.log.info( main.topoName + ": Links view in all ONOS nodes "+
188 "are correct " )
189 else:
190 compareTopoResult = main.FALSE
191
192 # Comparing hosts
193 main.log.info( main.topoName + ": Comparing hosts in each ONOS nodes" +
194 " with Mininet" )
195 for i in range( main.numCtrls ):
196 tempResult = main.Mininet1.compareHosts( MNTopo, hosts[ i ] )
197 hostsResult.append( tempResult )
198 if tempResult == main.FALSE:
199 main.log.error( main.topoName + ": ONOS-" + str( i ) +
200 " hosts view are incorrect " )
201
202 if all( result == main.TRUE for result in hostsResult ):
203 main.log.info( main.topoName + ": Hosts view in all ONOS nodes "+
204 "are correct " )
205 else:
206 compareTopoResult = main.FALSE
207
208 return compareTopoResult
209
210def getHostsData( main ):
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 if main.hostsData:
220 main.hostsData = {}
221 for i in range( main.numCtrls ):
222 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
223 if appCheck != main.TRUE:
224 main.log.warn( main.CLIs[ i ].apps() )
225 main.log.warn( main.CLIs[ i ].appIDs() )
226
227 pingResult = main.Mininet1.pingall( timeout=900 )
228 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
229 hosts = main.Mininet1.getHosts()
230 for host in hosts:
231 main.hostsData[ host ] = {}
232 main.hostsData[ host ][ 'mac' ] = \
233 main.Mininet1.getMacAddress( host ).upper()
234 for hostj in hostsJson:
235 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
236 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
237 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
238 main.hostsData[ host ][ 'location' ] = \
239 hostj[ 'location' ][ 'elementId' ] + '/' + \
240 hostj[ 'location' ][ 'port' ]
241 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
242
243 if activateResult and main.hostsData:
244 main.log.info( main.topoName + ": Successfully used fwd app" +
245 " to discover hosts " )
246 getDataResult = main.TRUE
247 else:
248 main.log.info( main.topoName + ": Failed to use fwd app" +
249 " to discover hosts " )
250 getDataResult = main.FALSE
251
252 # This data can be use later for intents
253 print main.hostsData
254
255 return getDataResult
kelvin-onlabf512e942015-06-08 19:42:59 -0700256
257def restartONOS( main ):
258 """
259 Description:
260 Stop and start ONOS that clears hosts,devices etc. in order to test
261 new mininet topology
262 Return:
263 Retruns main.TRUE for a successful restart, main.FALSE otherwise.
264 """
265 stopResult = []
266 startResult = []
267 restartResult = main.TRUE
268
269 main.log.info( main.topoName + ": Stopping ONOS cluster" )
270 for node in main.nodes:
271 startResult.append( main.ONOSbench.onosStop( nodeIp=node.ip_address ) )
272
273 if all( result == main.TRUE for result in stopResult ):
274 main.log.info( main.topoName + ": Successfully stopped ONOS cluster" )
275 else:
276 restartResult = main.FALSE
277 main.log.error( main.topoName + ": Failed to stop ONOS cluster" )
278
279 time.sleep( 15 )
280
281 main.log.info( main.topoName + ": Starting ONOS cluster" )
282 for node in main.nodes:
283 startResult.append( main.ONOSbench.onosStart( nodeIp=node.ip_address ) )
284
285 if all( result == main.TRUE for result in startResult ):
286 main.log.info( main.topoName + ": Successfully start ONOS cluster" )
287 else:
288 restartResult = main.FALSE
289 main.log.error( main.topoName + ": Failed to start ONOS cluster" )
290
291 # Start ONOS CLIs again
292 main.log.info( main.topoName + ": Starting ONOS CLI" )
293 cliResult = main.TRUE
294 for i in range( main.numCtrls ):
295 cliResult = cliResult and \
296 main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] )
297
298 time.sleep( 15 )
299
300 return restartResult
301
302
303