blob: 0045c643444012dfc74c94e0ef3cc4e7ea466686 [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' ]
kelvin-onlabe7270fa2015-05-18 14:35:03 -070055 print '1', main.hostsData[ h1Name ]
56 print '2', main.hostsData[ h2Name ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070057 if main.hostsData[ h1Name ][ 'vlan' ] != '-1':
kelvin-onlabe7270fa2015-05-18 14:35:03 -070058 print 'vlan1', main.hostsData[ h1Name ][ 'vlan' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070059 vlan1 = main.hostsData[ h1Name ][ 'vlan' ]
60 if main.hostsData[ h2Name ][ 'vlan' ] != '-1':
kelvin-onlabe7270fa2015-05-18 14:35:03 -070061 print 'vlan2', main.hostsData[ h2Name ][ 'vlan' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070062 vlan2 = main.hostsData[ h2Name ][ 'vlan' ]
63 if not h1Id:
64 h1Id = main.hostsData[ h1Name ][ 'id' ]
65 if not h2Id:
66 h2Id = main.hostsData[ h2Name ][ 'id' ]
67
68 assert h1Id and h2Id, "You must specify host IDs"
69 if not ( h1Id and h2Id ):
70 main.log.info( "There are no host IDs" )
71 return main.FALSE
72
kelvin-onlabe5239e52015-05-13 14:46:45 -070073 # Discover hosts using arping
74 main.log.info( itemName + ": Discover host using arping" )
75 main.Mininet1.arping( host=h1Name )
76 main.Mininet1.arping( host=h2Name )
77 host1 = main.CLIs[ 0 ].getHost( mac=h1Mac )
78 host2 = main.CLIs[ 0 ].getHost( mac=h2Mac )
79
80 # Adding host intents
81 main.log.info( itemName + ": Adding host intents" )
82 intent1 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h1Id,
83 hostIdTwo=h2Id )
84 intentsId.append( intent1 )
85 time.sleep( 5 )
86 intent2 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h2Id,
87 hostIdTwo=h1Id )
88 intentsId.append( intent2 )
89
90 # Check intents state
91 time.sleep( 50 )
92 intentResult = checkIntentState( main, intentsId )
93
94 # Verify flows
95 checkFlowsState( main )
96
97 # Ping hosts
98 pingHost( main, h1Name, h2Name )
99 # Ping hosts again...
100 pingResult = pingHost( main, h1Name, h2Name )
101 time.sleep( 5 )
102
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700103 # Test rerouting if these variables exist
104 if sw1 and sw2 and expectedLink:
105 # link down
106 link( main, sw1, sw2, "down" )
107 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700108
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700109 # Verify flows
110 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700111
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700112 # Check OnosTopology
113 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700114
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700115 # Ping hosts
116 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700117
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700118 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700119
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700120 # link up
121 link( main, sw1, sw2, "up" )
122 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700123
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700124 # Verify flows
125 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700126
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700127 # Check OnosTopology
128 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700129
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700130 # Ping hosts
131 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700132
133 # Remove intents
134 for intent in intentsId:
135 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
136
137 print main.CLIs[ 0 ].intents()
138 stepResult = pingResult and linkDownResult and linkUpResult \
139 and intentResult
140
141 return stepResult
142
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700143def pointIntent( main,
144 name="",
145 host1="",
146 host2="",
147 deviceId1="",
148 deviceId2="",
149 port1="",
150 port2="",
151 ethType="",
152 mac1="",
153 mac2="",
154 bandwidth="",
155 lambdaAlloc=False,
156 ipProto="",
157 ip1="",
158 ip2="",
159 tcp1="",
160 tcp2="",
161 sw1="",
162 sw2="",
163 expectedLink=0 ):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700164 """
165 Add Point intents
166 """
167 import time
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700168 assert main, "There is no main variable"
169 assert name, "variable name is empty"
170 assert host1 and host2, "You must specify hosts"
171
kelvin-onlabe5239e52015-05-13 14:46:45 -0700172 global itemName
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700173 itemName = name
174 h1Name = host1
175 h2Name = host2
kelvin-onlabe5239e52015-05-13 14:46:45 -0700176 intentsId = []
kelvin-onlabb2235602015-05-13 17:51:06 -0700177
kelvin-onlabe5239e52015-05-13 14:46:45 -0700178 pingResult = main.TRUE
179 intentResult = main.TRUE
180 flowResult = main.TRUE
181 topoResult = main.TRUE
182 linkDownResult = main.TRUE
183 linkUpResult = main.TRUE
184
kelvin-onlabb2235602015-05-13 17:51:06 -0700185 # Adding bidirectional point intents
kelvin-onlabe5239e52015-05-13 14:46:45 -0700186 main.log.info( itemName + ": Adding host intents" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700187 intent1 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId1,
188 egressDevice=deviceId2,
189 portIngress=port1,
190 portEgress=port2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700191 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700192 ethSrc=mac1,
193 ethDst=mac2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700194 bandwidth=bandwidth,
195 lambdaAlloc=lambdaAlloc,
196 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700197 ipSrc=ip1,
198 ipDst=ip2,
199 tcpSrc=tcp1,
200 tcpDst=tcp2 )
kelvin-onlabb2235602015-05-13 17:51:06 -0700201
kelvin-onlabe5239e52015-05-13 14:46:45 -0700202 intentsId.append( intent1 )
203 time.sleep( 5 )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700204 intent2 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId2,
205 egressDevice=deviceId1,
206 portIngress=port2,
207 portEgress=port1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700208 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700209 ethSrc=mac2,
210 ethDst=mac1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700211 bandwidth=bandwidth,
212 lambdaAlloc=lambdaAlloc,
213 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700214 ipSrc=ip2,
215 ipDst=ip1,
216 tcpSrc=tcp2,
217 tcpDst=tcp1 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700218 intentsId.append( intent2 )
219
220 # Check intents state
221 time.sleep( 50 )
222 intentResult = checkIntentState( main, intentsId )
223
224 # Verify flows
225 checkFlowsState( main )
226
227 # Ping hosts
228 pingHost( main, h1Name, h2Name )
229 # Ping hosts again...
230 pingResult = pingHost( main, h1Name, h2Name )
231 time.sleep( 5 )
232
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700233 if sw1 and sw2 and expectedLink:
234 # link down
235 link( main, sw1, sw2, "down" )
236 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700237
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700238 # Verify flows
239 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700240
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700241 # Check OnosTopology
242 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700243
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700244 # Ping hosts
245 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700246
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700247 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700248
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700249 # link up
250 link( main, sw1, sw2, "up" )
251 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700252
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700253 # Verify flows
254 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700255
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700256 # Check OnosTopology
257 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700258
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700259 # Ping hosts
260 pingResult = pingResult and pingHost( main, h1Name, h2Name )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700261
262 # Remove intents
263 for intent in intentsId:
264 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
265
266 print main.CLIs[ 0 ].intents()
267 stepResult = pingResult and linkDownResult and linkUpResult \
268 and intentResult
269
270 return stepResult
271
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700272def singleToMultiIntent( main,
273 name="",
274 hostNames=None,
275 devices=None,
276 ports=None,
277 ethType="",
278 macs=None,
279 bandwidth="",
280 lambdaAlloc=False,
281 ipProto="",
282 ipAddresses="",
283 tcp="",
284 sw1="",
285 sw2="",
286 expectedLink=0 ):
287 """
288 Add Single to Multi Point intents
289 """
290 import time
291 assert main, "There is no main variable"
292 assert name, "variable name is empty"
293 assert hostNames, "You must specify hosts"
294 assert devices or main.hostsData, "You must specify devices"
295
296 global itemName
297 itemName = name
298 h1Name = host1
299 h2Name = host2
300 intentsId = []
301
302 if devices and ports:
303 if len( devices ) and len( ports ):
304 main.log.info( itemName +
305 ": devices and ports are not the same lenght " +
306 "devices - " + str( len( devices ) ) + " ports - "
307 + str( len( ports ) ) )
308
309
310
311 pingResult = main.TRUE
312 intentResult = main.TRUE
313 flowResult = main.TRUE
314 topoResult = main.TRUE
315 linkDownResult = main.TRUE
316 linkUpResult = main.TRUE
317
318 # Adding bidirectional point intents
319 main.log.info( itemName + ": Adding host intents" )
320 intent1 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId1,
321 egressDevice=deviceId2,
322 portIngress=port1,
323 portEgress=port2,
324 ethType=ethType,
325 ethSrc=mac1,
326 ethDst=mac2,
327 bandwidth=bandwidth,
328 lambdaAlloc=lambdaAlloc,
329 ipProto=ipProto,
330 ipSrc=ip1,
331 ipDst=ip2,
332 tcpSrc=tcp1,
333 tcpDst=tcp2 )
334
335 intentsId.append( intent1 )
336 time.sleep( 5 )
337 intent2 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId2,
338 egressDevice=deviceId1,
339 portIngress=port2,
340 portEgress=port1,
341 ethType=ethType,
342 ethSrc=mac2,
343 ethDst=mac1,
344 bandwidth=bandwidth,
345 lambdaAlloc=lambdaAlloc,
346 ipProto=ipProto,
347 ipSrc=ip2,
348 ipDst=ip1,
349 tcpSrc=tcp2,
350 tcpDst=tcp1 )
351 intentsId.append( intent2 )
352
353 # Check intents state
354 time.sleep( 50 )
355 intentResult = checkIntentState( main, intentsId )
356
357 # Verify flows
358 checkFlowsState( main )
359
360 # Ping hosts
361 pingHost( main, h1Name, h2Name )
362 # Ping hosts again...
363 pingResult = pingHost( main, h1Name, h2Name )
364 time.sleep( 5 )
365
366 if sw1 and sw2 and expectedLink:
367 # link down
368 link( main, sw1, sw2, "down" )
369 intentResult = intentResult and checkIntentState( main, intentsId )
370
371 # Verify flows
372 checkFlowsState( main )
373
374 # Check OnosTopology
375 topoResult = checkTopology( main, expectedLink )
376
377 # Ping hosts
378 pingResult = pingResult and pingHost( main, h1Name, h2Name )
379
380 intentResult = checkIntentState( main, intentsId )
381
382 # link up
383 link( main, sw1, sw2, "up" )
384 time.sleep( 5 )
385
386 # Verify flows
387 checkFlowsState( main )
388
389 # Check OnosTopology
390 topoResult = checkTopology( main, expectedLink )
391
392 # Ping hosts
393 pingResult = pingResult and pingHost( main, h1Name, h2Name )
394
395 # Remove intents
396 for intent in intentsId:
397 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
398
399 print main.CLIs[ 0 ].intents()
400 stepResult = pingResult and linkDownResult and linkUpResult \
401 and intentResult
402
403 return stepResult
404
kelvin-onlabe5239e52015-05-13 14:46:45 -0700405def link( main, sw1, sw2, option):
406
407 # link down
408 main.log.info( itemName + ": Bring link " + option + "between " +
409 sw1 + " and " + sw2 )
410 main.Mininet1.link( end1=sw1, end2=sw2, option=option )
411
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700412def pingAllHost( main, hosts ):
413 # Ping all host in the hosts list variable
414 import itertools
415
416 main.log.info( itemName + ": Ping host list - " + hosts )
417 hostCombination = itertools.permutation( hosts, 2 )
418 pingResult = main.TRUE
419 for hostPair in hostCombination:
420 pingResult = pingResult and main.Mininet.pingHost(
421 src=hostPair[ 0 ],
422 target=hostPair[ 1 ] )
423 return pingResult
424
kelvin-onlabe5239e52015-05-13 14:46:45 -0700425def pingHost( main, h1Name, h2Name ):
426
427 # Ping hosts
428 main.log.info( itemName + ": Ping " + h1Name + " and " +
429 h2Name )
430 pingResult1 = main.Mininet1.pingHost( src=h1Name , target=h2Name )
431 if not pingResult1:
432 main.log.info( itemName + ": " + h1Name + " cannot ping "
433 + h2Name )
434 pingResult2 = main.Mininet1.pingHost( src=h2Name , target=h1Name )
435 if not pingResult2:
436 main.log.info( itemName + ": " + h2Name + " cannot ping "
437 + h1Name )
438 pingResult = pingResult1 and pingResult2
439 if pingResult:
440 main.log.info( itemName + ": Successfully pinged " +
441 "both hosts" )
442 else:
443 main.log.info( itemName + ": Failed to ping " +
444 "both hosts" )
445 return pingResult
446
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700447def getHostsData( main ):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700448 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700449 Use fwd app and pingall to discover all the hosts
kelvin-onlabe5239e52015-05-13 14:46:45 -0700450 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700451 """
452 hosts json format:
453 """
454 import json
455 activateResult = main.TRUE
456 appCheck = main.TRUE
457 main.log.info( "Activating reactive forwarding app " )
458 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
459
460 for i in range( main.numCtrls ):
461 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
462
463 if appCheck != main.TRUE:
464 main.log.warn( main.CLIs[ 0 ].apps() )
465 main.log.warn( main.CLIs[ 0 ].appIDs() )
466
467 pingResult = main.Mininet1.pingall()
468 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
469 hosts = main.Mininet1.getHosts()
470 for host in hosts:
471 main.hostsData[ host ] = {}
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700472 main.hostsData[ host ][ 'mac' ] = \
473 main.Mininet1.getMacAddress( host ).upper()
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700474 for hostj in hostsJson:
475 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
476 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
477 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
478 main.hostsData[ host ][ 'location' ] = \
479 hostj[ 'location' ][ 'elementId' ] + '/' + \
480 hostj[ 'location' ][ 'port' ]
481 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
482
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700483 main.log.info( "Deactivating reactive forwarding app " )
484 activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700485 print main.hostsData
486 return pingResult
kelvin-onlabe5239e52015-05-13 14:46:45 -0700487
488def checkTopology( main, expectedLink ):
489 statusResult = main.TRUE
490 # Check onos topology
491 main.log.info( itemName + ": Checking ONOS topology " )
492
493 for i in range( main.numCtrls ):
494 topologyResult = main.CLIs[ i ].topology()
495 statusResult = main.ONOSbench.checkStatus( topologyResult,
496 main.numSwitch,
497 expectedLink )\
498 and statusResult
499 if not statusResult:
500 main.log.info( itemName + ": Topology mismatch" )
501 else:
502 main.log.info( itemName + ": Topology match" )
503 return statusResult
504
505def checkIntentState( main, intentsId ):
506
507 main.log.info( itemName + ": Check host intents state" )
508 for i in range( main.numCtrls ):
509 intentResult = main.CLIs[ i ].checkIntentState( intentsId=intentsId )
510 if not intentResult:
511 main.log.info( itemName + ": Check host intents state again")
512 for i in range( main.numCtrls ):
513 intentResult = main.CLIs[ i ].checkIntentState(
514 intentsId=intentsId )
515 return intentResult
516
517def checkFlowsState( main ):
518
519 main.log.info( itemName + ": Check flows state" )
520 checkFlowsResult = main.CLIs[ 0 ].checkFlowsState()
521 return checkFlowsResult
522
523def printMsg( main, h1Name, h2Name ):
524 main.log.info("PINGING HOST INSIDE printMSG")
525 pingHost( main, itemName, h1Name, h2Name )
526 print 'lala'
527