blob: 07944aaecb6986427582c90d5dd24dee61530d24 [file] [log] [blame]
kelvin-onlabe5239e52015-05-13 14:46:45 -07001"""
2 Wrapper functions for FuncIntent
3 This functions include Onosclidriver and Mininetclidriver driver functions
4"""
5def __init__( self ):
6 self.default = ''
7
kelvin-onlab2ccad6e2015-05-18 10:36:54 -07008def hostIntent( main,
9 name="",
10 host1="",
11 host2="",
12 host1Id="",
13 host2Id="",
14 mac1="",
15 mac2="",
16 vlan1="-1",
17 vlan2="-1",
18 sw1="",
19 sw2="",
20 expectedLink=0 ):
kelvin-onlabe5239e52015-05-13 14:46:45 -070021 """
22 Add host intents
23 """
24 import time
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070025
26 # Assert variables
27 assert main, "There is no main variable"
28 assert name, "variable name is empty"
29 assert host1 and host2, "You must specify hosts"
30
kelvin-onlabe5239e52015-05-13 14:46:45 -070031 global itemName
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070032 itemName = name
33 h1Name = host1
34 h2Name = host2
35 h1Id = host1Id
36 h2Id = host2Id
37 h1Mac = mac1
38 h2Mac = mac2
39 vlan1 = vlan1
40 vlan2 = vlan2
kelvin-onlabe5239e52015-05-13 14:46:45 -070041 intentsId = []
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070042 stepResult = main.TRUE
kelvin-onlabe5239e52015-05-13 14:46:45 -070043 pingResult = main.TRUE
44 intentResult = main.TRUE
45 flowResult = main.TRUE
46 topoResult = main.TRUE
47 linkDownResult = main.TRUE
48 linkUpResult = main.TRUE
49
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070050 if main.hostsData:
51 if not h1Mac:
52 h1Mac = main.hostsData[ h1Name ][ 'mac' ]
53 if not h2Mac:
54 h2Mac = main.hostsData[ h2Name ][ 'mac' ]
55 if main.hostsData[ h1Name ][ 'vlan' ] != '-1':
56 vlan1 = main.hostsData[ h1Name ][ 'vlan' ]
57 if main.hostsData[ h2Name ][ 'vlan' ] != '-1':
58 vlan2 = main.hostsData[ h2Name ][ 'vlan' ]
59 if not h1Id:
60 h1Id = main.hostsData[ h1Name ][ 'id' ]
61 if not h2Id:
62 h2Id = main.hostsData[ h2Name ][ 'id' ]
63
64 assert h1Id and h2Id, "You must specify host IDs"
65 if not ( h1Id and h2Id ):
66 main.log.info( "There are no host IDs" )
67 return main.FALSE
68
kelvin-onlabe5239e52015-05-13 14:46:45 -070069 # Discover hosts using arping
70 main.log.info( itemName + ": Discover host using arping" )
71 main.Mininet1.arping( host=h1Name )
72 main.Mininet1.arping( host=h2Name )
73 host1 = main.CLIs[ 0 ].getHost( mac=h1Mac )
74 host2 = main.CLIs[ 0 ].getHost( mac=h2Mac )
75
76 # Adding host intents
77 main.log.info( itemName + ": Adding host intents" )
78 intent1 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h1Id,
79 hostIdTwo=h2Id )
80 intentsId.append( intent1 )
81 time.sleep( 5 )
82 intent2 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h2Id,
83 hostIdTwo=h1Id )
84 intentsId.append( intent2 )
85
86 # Check intents state
87 time.sleep( 50 )
88 intentResult = checkIntentState( main, intentsId )
89
90 # Verify flows
91 checkFlowsState( main )
92
93 # Ping hosts
94 pingHost( main, h1Name, h2Name )
95 # Ping hosts again...
96 pingResult = pingHost( main, h1Name, h2Name )
97 time.sleep( 5 )
98
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070099 # Test rerouting if these variables exist
100 if sw1 and sw2 and expectedLink:
101 # link down
102 link( main, sw1, sw2, "down" )
103 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700104
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700105 # Verify flows
106 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700107
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700108 # Check OnosTopology
109 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700110
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700111 # Ping hosts
112 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700113
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700114 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700115
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700116 # link up
117 link( main, sw1, sw2, "up" )
118 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700119
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700120 # Verify flows
121 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700122
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700123 # Check OnosTopology
124 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700125
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700126 # Ping hosts
127 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700128
129 # Remove intents
130 for intent in intentsId:
131 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
132
133 print main.CLIs[ 0 ].intents()
134 stepResult = pingResult and linkDownResult and linkUpResult \
135 and intentResult
136
137 return stepResult
138
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700139def pointIntent( main,
140 name="",
141 host1="",
142 host2="",
143 deviceId1="",
144 deviceId2="",
145 port1="",
146 port2="",
147 ethType="",
148 mac1="",
149 mac2="",
150 bandwidth="",
151 lambdaAlloc=False,
152 ipProto="",
153 ip1="",
154 ip2="",
155 tcp1="",
156 tcp2="",
157 sw1="",
158 sw2="",
159 expectedLink=0 ):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700160 """
161 Add Point intents
162 """
163 import time
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700164 assert main, "There is no main variable"
165 assert name, "variable name is empty"
166 assert host1 and host2, "You must specify hosts"
167
kelvin-onlabe5239e52015-05-13 14:46:45 -0700168 global itemName
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700169 itemName = name
170 h1Name = host1
171 h2Name = host2
kelvin-onlabe5239e52015-05-13 14:46:45 -0700172 intentsId = []
kelvin-onlabb2235602015-05-13 17:51:06 -0700173
kelvin-onlabe5239e52015-05-13 14:46:45 -0700174 pingResult = main.TRUE
175 intentResult = main.TRUE
176 flowResult = main.TRUE
177 topoResult = main.TRUE
178 linkDownResult = main.TRUE
179 linkUpResult = main.TRUE
180
kelvin-onlabb2235602015-05-13 17:51:06 -0700181 # Adding bidirectional point intents
kelvin-onlabe5239e52015-05-13 14:46:45 -0700182 main.log.info( itemName + ": Adding host intents" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700183 intent1 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId1,
184 egressDevice=deviceId2,
185 portIngress=port1,
186 portEgress=port2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700187 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700188 ethSrc=mac1,
189 ethDst=mac2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700190 bandwidth=bandwidth,
191 lambdaAlloc=lambdaAlloc,
192 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700193 ipSrc=ip1,
194 ipDst=ip2,
195 tcpSrc=tcp1,
196 tcpDst=tcp2 )
kelvin-onlabb2235602015-05-13 17:51:06 -0700197
kelvin-onlabe5239e52015-05-13 14:46:45 -0700198 intentsId.append( intent1 )
199 time.sleep( 5 )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700200 intent2 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId2,
201 egressDevice=deviceId1,
202 portIngress=port2,
203 portEgress=port1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700204 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700205 ethSrc=mac2,
206 ethDst=mac1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700207 bandwidth=bandwidth,
208 lambdaAlloc=lambdaAlloc,
209 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700210 ipSrc=ip2,
211 ipDst=ip1,
212 tcpSrc=tcp2,
213 tcpDst=tcp1 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700214 intentsId.append( intent2 )
215
216 # Check intents state
217 time.sleep( 50 )
218 intentResult = checkIntentState( main, intentsId )
219
220 # Verify flows
221 checkFlowsState( main )
222
223 # Ping hosts
224 pingHost( main, h1Name, h2Name )
225 # Ping hosts again...
226 pingResult = pingHost( main, h1Name, h2Name )
227 time.sleep( 5 )
228
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700229 if sw1 and sw2 and expectedLink:
230 # link down
231 link( main, sw1, sw2, "down" )
232 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700233
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700234 # Verify flows
235 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700236
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700237 # Check OnosTopology
238 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700239
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700240 # Ping hosts
241 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700242
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700243 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700244
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700245 # link up
246 link( main, sw1, sw2, "up" )
247 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700248
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700249 # Verify flows
250 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700251
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700252 # Check OnosTopology
253 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700254
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700255 # Ping hosts
256 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700257
258 # Remove intents
259 for intent in intentsId:
260 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
261
262 print main.CLIs[ 0 ].intents()
263 stepResult = pingResult and linkDownResult and linkUpResult \
264 and intentResult
265
266 return stepResult
267
268def link( main, sw1, sw2, option):
269
270 # link down
271 main.log.info( itemName + ": Bring link " + option + "between " +
272 sw1 + " and " + sw2 )
273 main.Mininet1.link( end1=sw1, end2=sw2, option=option )
274
275def pingHost( main, h1Name, h2Name ):
276
277 # Ping hosts
278 main.log.info( itemName + ": Ping " + h1Name + " and " +
279 h2Name )
280 pingResult1 = main.Mininet1.pingHost( src=h1Name , target=h2Name )
281 if not pingResult1:
282 main.log.info( itemName + ": " + h1Name + " cannot ping "
283 + h2Name )
284 pingResult2 = main.Mininet1.pingHost( src=h2Name , target=h1Name )
285 if not pingResult2:
286 main.log.info( itemName + ": " + h2Name + " cannot ping "
287 + h1Name )
288 pingResult = pingResult1 and pingResult2
289 if pingResult:
290 main.log.info( itemName + ": Successfully pinged " +
291 "both hosts" )
292 else:
293 main.log.info( itemName + ": Failed to ping " +
294 "both hosts" )
295 return pingResult
296
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700297def getHostsData( main ):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700298 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700299 Use fwd app and pingall to discover all the hosts
kelvin-onlabe5239e52015-05-13 14:46:45 -0700300 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700301 """
302 hosts json format:
303 """
304 import json
305 activateResult = main.TRUE
306 appCheck = main.TRUE
307 main.log.info( "Activating reactive forwarding app " )
308 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
309
310 for i in range( main.numCtrls ):
311 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
312
313 if appCheck != main.TRUE:
314 main.log.warn( main.CLIs[ 0 ].apps() )
315 main.log.warn( main.CLIs[ 0 ].appIDs() )
316
317 pingResult = main.Mininet1.pingall()
318 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
319 hosts = main.Mininet1.getHosts()
320 for host in hosts:
321 main.hostsData[ host ] = {}
322 main.hostsData[ host ][ 'mac' ] = main.Mininet1.getMacAddress( host )
323 for hostj in hostsJson:
324 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
325 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
326 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
327 main.hostsData[ host ][ 'location' ] = \
328 hostj[ 'location' ][ 'elementId' ] + '/' + \
329 hostj[ 'location' ][ 'port' ]
330 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
331
332 print main.hostsData
333 return pingResult
kelvin-onlabe5239e52015-05-13 14:46:45 -0700334
335def checkTopology( main, expectedLink ):
336 statusResult = main.TRUE
337 # Check onos topology
338 main.log.info( itemName + ": Checking ONOS topology " )
339
340 for i in range( main.numCtrls ):
341 topologyResult = main.CLIs[ i ].topology()
342 statusResult = main.ONOSbench.checkStatus( topologyResult,
343 main.numSwitch,
344 expectedLink )\
345 and statusResult
346 if not statusResult:
347 main.log.info( itemName + ": Topology mismatch" )
348 else:
349 main.log.info( itemName + ": Topology match" )
350 return statusResult
351
352def checkIntentState( main, intentsId ):
353
354 main.log.info( itemName + ": Check host intents state" )
355 for i in range( main.numCtrls ):
356 intentResult = main.CLIs[ i ].checkIntentState( intentsId=intentsId )
357 if not intentResult:
358 main.log.info( itemName + ": Check host intents state again")
359 for i in range( main.numCtrls ):
360 intentResult = main.CLIs[ i ].checkIntentState(
361 intentsId=intentsId )
362 return intentResult
363
364def checkFlowsState( main ):
365
366 main.log.info( itemName + ": Check flows state" )
367 checkFlowsResult = main.CLIs[ 0 ].checkFlowsState()
368 return checkFlowsResult
369
370def printMsg( main, h1Name, h2Name ):
371 main.log.info("PINGING HOST INSIDE printMSG")
372 pingHost( main, itemName, h1Name, h2Name )
373 print 'lala'
374