blob: 6bbbb84a0b9704450964596cb239da875538ca86 [file] [log] [blame]
kelvin-onlab44147802015-07-27 17:57:31 -07001"""
2 Wrapper functions for FuncIntent
3 This functions include Onosclidriver and Mininetclidriver driver functions
4 Author: kelvin@onlab.us
5"""
6import time
7import copy
8import json
9
10def __init__( self ):
11 self.default = ''
12
13def hostIntent( main,
14 name,
15 host1,
16 host2,
17 onosNode=0,
18 host1Id="",
19 host2Id="",
20 mac1="",
21 mac2="",
22 vlan1="-1",
23 vlan2="-1",
24 sw1="",
25 sw2="",
26 expectedLink=0 ):
27 """
28 Description:
29 Verify add-host-intent
30 Steps:
31 - Discover hosts
32 - Add host intents
33 - Check intents
34 - Verify flows
35 - Ping hosts
36 - Reroute
37 - Link down
38 - Verify flows
39 - Check topology
40 - Ping hosts
41 - Link up
42 - Verify flows
43 - Check topology
44 - Ping hosts
45 - Remove intents
46 Required:
47 name - Type of host intent to add eg. IPV4 | VLAN | Dualstack
48 host1 - Name of first host
49 host2 - Name of second host
50 Optional:
51 onosNode - ONOS node to install the intents in main.CLIs[ ]
52 0 by default so that it will always use the first
53 ONOS node
54 host1Id - ONOS id of the first host eg. 00:00:00:00:00:01/-1
55 host2Id - ONOS id of the second host
56 mac1 - Mac address of first host
57 mac2 - Mac address of the second host
58 vlan1 - Vlan tag of first host, defaults to -1
59 vlan2 - Vlan tag of second host, defaults to -1
60 sw1 - First switch to bring down & up for rerouting purpose
61 sw2 - Second switch to bring down & up for rerouting purpose
62 expectedLink - Expected link when the switches are down, it should
63 be two links lower than the links before the two
64 switches are down
65 """
66
67 # Assert variables
68 assert main, "There is no main variable"
69 assert name, "variable name is empty"
70 assert host1 and host2, "You must specify hosts"
71
72 global itemName
73 itemName = name
74 h1Id = host1Id
75 h2Id = host2Id
76 h1Mac = mac1
77 h2Mac = mac2
78 vlan1 = vlan1
79 vlan2 = vlan2
80 hostNames = [ host1 , host2 ]
81 intentsId = []
82 stepResult = main.TRUE
83 pingResult = main.TRUE
84 intentResult = main.TRUE
85 removeIntentResult = main.TRUE
86 flowResult = main.TRUE
87 topoResult = main.TRUE
88 linkDownResult = main.TRUE
89 linkUpResult = main.TRUE
90 onosNode = int( onosNode )
91
92 if main.hostsData:
93 if not h1Mac:
94 h1Mac = main.hostsData[ host1 ][ 'mac' ]
95 if not h2Mac:
96 h2Mac = main.hostsData[ host2 ][ 'mac' ]
97 if main.hostsData[ host1 ][ 'vlan' ] != '-1':
98 vlan1 = main.hostsData[ host1 ][ 'vlan' ]
99 if main.hostsData[ host2 ][ 'vlan' ] != '-1':
100 vlan2 = main.hostsData[ host2 ][ 'vlan' ]
101 if not h1Id:
102 h1Id = main.hostsData[ host1 ][ 'id' ]
103 if not h2Id:
104 h2Id = main.hostsData[ host2 ][ 'id' ]
105
106 assert h1Id and h2Id, "You must specify host IDs"
107 if not ( h1Id and h2Id ):
108 main.log.info( "There are no host IDs" )
109 return main.FALSE
110
111 # Discover hosts using arping
112 if not main.hostsData:
113 main.log.info( itemName + ": Discover host using arping" )
114 main.Mininet1.arping( host=host1 )
115 main.Mininet1.arping( host=host2 )
116 h1Id = main.CLIs[ 0 ].getHost( mac=h1Mac )
117 h2Id = main.CLIs[ 0 ].getHost( mac=h2Mac )
118
119 # Check flows count in each node
120 checkFlowsCount( main )
121
122 # Adding host intents
123 main.log.info( itemName + ": Adding host intents" )
124
125 intent1 = main.CLIs[ onosNode ].addHostIntent( hostIdOne=h1Id,
126 hostIdTwo=h2Id )
127
128 time.sleep( main.hostIntentSleep )
129 intentsId = main.CLIs[ 0 ].getIntentsId()
130 print intentsId
131
132 # Check intents state
133 time.sleep( main.checkIntentSleep )
134 intentResult = checkIntentState( main, intentsId )
135 checkFlowsCount( main )
136
137 # Check intents state again if first check fails...
138 if not intentResult:
139 intentResult = checkIntentState( main, intentsId )
140
141 # Check flows count in each node
142 checkFlowsCount( main )
143 # Verify flows
144 checkFlowsState( main )
145
146 # Ping hosts
147 firstPingResult = pingallHosts( main, hostNames )
148 if not firstPingResult:
149 main.log.debug( "First ping failed, there must be" +
150 " something wrong with ONOS performance" )
151
152 # Ping hosts again...
153 pingResult = pingResult and pingallHosts( main, hostNames )
154
155 # Test rerouting if these variables exist
156 if sw1 and sw2 and expectedLink:
157 # link down
158 linkDownResult = link( main, sw1, sw2, "down" )
159 intentResult = intentResult and checkIntentState( main, intentsId )
160
161 # Check flows count in each node
162 checkFlowsCount( main )
163 # Verify flows
164 checkFlowsState( main )
165
166 # Check OnosTopology
167 topoResult = checkTopology( main, expectedLink )
168
169 # Ping hosts
170 pingResult = pingResult and pingallHosts( main, hostNames )
171
172 intentResult = checkIntentState( main, intentsId )
173
174 # Checks ONOS state in link down
175 if linkDownResult and topoResult and pingResult and intentResult:
176 main.log.info( itemName + ": Successfully brought link down" )
177 else:
178 main.log.error( itemName + ": Failed to bring link down" )
179
180 # link up
181 linkUpResult = link( main, sw1, sw2, "up" )
182 time.sleep( main.rerouteSleep )
183
184 # Check flows count in each node
185 checkFlowsCount( main )
186 # Verify flows
187 checkFlowsState( main )
188
189 # Check OnosTopology
190 topoResult = checkTopology( main, main.numLinks )
191
192 # Ping hosts
193 pingResult = pingResult and pingallHosts( main, hostNames )
194
195 intentResult = checkIntentState( main, intentsId )
196
197 # Checks ONOS state in link up
198 if linkUpResult and topoResult and pingResult and intentResult:
199 main.log.info( itemName + ": Successfully brought link back up" )
200 else:
201 main.log.error( itemName + ": Failed to bring link back up" )
202
203 # Remove all intents
204 removeIntentResult = removeAllIntents( main )
205
206 stepResult = pingResult and linkDownResult and linkUpResult \
207 and intentResult and removeIntentResult
208
209 return stepResult
210
211def pointIntent( main,
212 name,
213 host1,
214 host2,
215 onosNode=0,
216 deviceId1="",
217 deviceId2="",
218 port1="",
219 port2="",
220 ethType="",
221 mac1="",
222 mac2="",
223 bandwidth="",
224 lambdaAlloc=False,
225 ipProto="",
226 ip1="",
227 ip2="",
228 tcp1="",
229 tcp2="",
230 sw1="",
231 sw2="",
232 expectedLink=0 ):
233
234 """
235 Description:
236 Verify add-point-intent
237 Steps:
238 - Get device ids | ports
239 - Add point intents
240 - Check intents
241 - Verify flows
242 - Ping hosts
243 - Reroute
244 - Link down
245 - Verify flows
246 - Check topology
247 - Ping hosts
248 - Link up
249 - Verify flows
250 - Check topology
251 - Ping hosts
252 - Remove intents
253 Required:
254 name - Type of point intent to add eg. IPV4 | VLAN | Dualstack
255 host1 - Name of first host
256 host2 - Name of second host
257 Optional:
258 onosNode - ONOS node to install the intents in main.CLIs[ ]
259 0 by default so that it will always use the first
260 ONOS node
261 deviceId1 - ONOS device id of the first switch, the same as the
262 location of the first host eg. of:0000000000000001/1,
263 located at device 1 port 1
264 deviceId2 - ONOS device id of the second switch
265 port1 - The port number where the first host is attached
266 port2 - The port number where the second host is attached
267 ethType - Ethernet type eg. IPV4, IPV6
268 mac1 - Mac address of first host
269 mac2 - Mac address of the second host
270 bandwidth - Bandwidth capacity
271 lambdaAlloc - Allocate lambda, defaults to False
272 ipProto - IP protocol
273 ip1 - IP address of first host
274 ip2 - IP address of second host
275 tcp1 - TCP port of first host
276 tcp2 - TCP port of second host
277 sw1 - First switch to bring down & up for rerouting purpose
278 sw2 - Second switch to bring down & up for rerouting purpose
279 expectedLink - Expected link when the switches are down, it should
280 be two links lower than the links before the two
281 switches are down
282 """
283
284 assert main, "There is no main variable"
285 assert name, "variable name is empty"
286 assert host1 and host2, "You must specify hosts"
287
288 global itemName
289 itemName = name
290 host1 = host1
291 host2 = host2
292 hostNames = [ host1, host2 ]
293 intentsId = []
294
295 pingResult = main.TRUE
296 intentResult = main.TRUE
297 removeIntentResult = main.TRUE
298 flowResult = main.TRUE
299 topoResult = main.TRUE
300 linkDownResult = main.TRUE
301 linkUpResult = main.TRUE
302 onosNode = int( onosNode )
303
304 # Adding bidirectional point intents
305 main.log.info( itemName + ": Adding point intents" )
306 intent1 = main.CLIs[ onosNode ].addPointIntent( ingressDevice=deviceId1,
307 egressDevice=deviceId2,
308 ingressPort=port1,
309 egressPort=port2,
310 ethType=ethType,
311 ethSrc=mac1,
312 ethDst=mac2,
313 bandwidth=bandwidth,
314 lambdaAlloc=lambdaAlloc,
315 ipProto=ipProto,
316 ipSrc=ip1,
317 ipDst=ip2,
318 tcpSrc=tcp1,
319 tcpDst=tcp2 )
320
321 intent2 = main.CLIs[ onosNode ].addPointIntent( ingressDevice=deviceId2,
322 egressDevice=deviceId1,
323 ingressPort=port2,
324 egressPort=port1,
325 ethType=ethType,
326 ethSrc=mac2,
327 ethDst=mac1,
328 bandwidth=bandwidth,
329 lambdaAlloc=lambdaAlloc,
330 ipProto=ipProto,
331 ipSrc=ip2,
332 ipDst=ip1,
333 tcpSrc=tcp2,
334 tcpDst=tcp1 )
335 intentsId = main.CLIs[ 0 ].getIntentsId()
336 print intentsId
337
338 # Check intents state
339 time.sleep( main.checkIntentSleep )
340 intentResult = checkIntentState( main, intentsId )
341 # Check flows count in each node
342 checkFlowsCount( main )
343
344 # Check intents state again if first check fails...
345 if not intentResult:
346 intentResult = checkIntentState( main, intentsId )
347
348 # Check flows count in each node
349 checkFlowsCount( main )
350 # Verify flows
351 checkFlowsState( main )
352
353 # Ping hosts
354 firstPingResult = pingallHosts( main, hostNames )
355 if not firstPingResult:
356 main.log.debug( "First ping failed, there must be" +
357 " something wrong with ONOS performance" )
358
359 # Ping hosts again...
360 pingResult = pingResult and pingallHosts( main, hostNames )
361
362 # Test rerouting if these variables exist
363 if sw1 and sw2 and expectedLink:
364 # link down
365 linkDownResult = link( main, sw1, sw2, "down" )
366 intentResult = intentResult and checkIntentState( main, intentsId )
367
368 # Check flows count in each node
369 checkFlowsCount( main )
370 # Verify flows
371 checkFlowsState( main )
372
373 # Check OnosTopology
374 topoResult = checkTopology( main, expectedLink )
375
376 # Ping hosts
377 pingResult = pingResult and pingallHosts( main, hostNames )
378
379 intentResult = checkIntentState( main, intentsId )
380
381 # Checks ONOS state in link down
382 if linkDownResult and topoResult and pingResult and intentResult:
383 main.log.info( itemName + ": Successfully brought link down" )
384 else:
385 main.log.error( itemName + ": Failed to bring link down" )
386
387 # link up
388 linkUpResult = link( main, sw1, sw2, "up" )
389 time.sleep( main.rerouteSleep )
390
391 # Check flows count in each node
392 checkFlowsCount( main )
393 # Verify flows
394 checkFlowsState( main )
395
396 # Check OnosTopology
397 topoResult = checkTopology( main, main.numLinks )
398
399 # Ping hosts
400 pingResult = pingResult and pingallHosts( main, hostNames )
401
402 intentResult = checkIntentState( main, intentsId )
403
404 # Checks ONOS state in link up
405 if linkUpResult and topoResult and pingResult and intentResult:
406 main.log.info( itemName + ": Successfully brought link back up" )
407 else:
408 main.log.error( itemName + ": Failed to bring link back up" )
409
410 # Remove all intents
411 removeIntentResult = removeAllIntents( main )
412
413 stepResult = pingResult and linkDownResult and linkUpResult \
414 and intentResult and removeIntentResult
415
416 return stepResult
417
418def singleToMultiIntent( main,
419 name,
420 hostNames,
421 onosNode=0,
422 devices="",
423 ports=None,
424 ethType="",
425 macs=None,
426 bandwidth="",
427 lambdaAlloc=False,
428 ipProto="",
429 ipAddresses="",
430 tcp="",
431 sw1="",
432 sw2="",
433 expectedLink=0 ):
434 """
435 Verify Single to Multi Point intents
436 NOTE:If main.hostsData is not defined, variables data should be passed
437 in the same order index wise. All devices in the list should have the
438 same format, either all the devices have its port or it doesn't.
439 eg. hostName = [ 'h1', 'h2' ,.. ]
440 devices = [ 'of:0000000000000001', 'of:0000000000000002', ...]
441 ports = [ '1', '1', ..]
442 ...
443 Description:
444 Verify add-single-to-multi-intent iterates through the list of given
445 host | devices and add intents
446 Steps:
447 - Get device ids | ports
448 - Add single to multi point intents
449 - Check intents
450 - Verify flows
451 - Ping hosts
452 - Reroute
453 - Link down
454 - Verify flows
455 - Check topology
456 - Ping hosts
457 - Link up
458 - Verify flows
459 - Check topology
460 - Ping hosts
461 - Remove intents
462 Required:
463 name - Type of point intent to add eg. IPV4 | VLAN | Dualstack
464 hostNames - List of host names
465 Optional:
466 onosNode - ONOS node to install the intents in main.CLIs[ ]
467 0 by default so that it will always use the first
468 ONOS node
469 devices - List of device ids in the same order as the hosts
470 in hostNames
471 ports - List of port numbers in the same order as the device in
472 devices
473 ethType - Ethernet type eg. IPV4, IPV6
474 macs - List of hosts mac address in the same order as the hosts in
475 hostNames
476 bandwidth - Bandwidth capacity
477 lambdaAlloc - Allocate lambda, defaults to False
478 ipProto - IP protocol
479 ipAddresses - IP addresses of host in the same order as the hosts in
480 hostNames
481 tcp - TCP ports in the same order as the hosts in hostNames
482 sw1 - First switch to bring down & up for rerouting purpose
483 sw2 - Second switch to bring down & up for rerouting purpose
484 expectedLink - Expected link when the switches are down, it should
485 be two links lower than the links before the two
486 switches are down
487 """
488
489 assert main, "There is no main variable"
490 assert hostNames, "You must specify hosts"
491 assert devices or main.hostsData, "You must specify devices"
492
493 global itemName
494 itemName = name
495 tempHostsData = {}
496 intentsId = []
497 onosNode = int( onosNode )
498
499 macsDict = {}
500 ipDict = {}
501 if hostNames and devices:
502 if len( hostNames ) != len( devices ):
503 main.log.debug( "hosts and devices does not have the same length" )
504 #print "len hostNames = ", len( hostNames )
505 #print "len devices = ", len( devices )
506 return main.FALSE
507 if ports:
508 if len( ports ) != len( devices ):
509 main.log.error( "Ports and devices does " +
510 "not have the same length" )
511 #print "len devices = ", len( devices )
512 #print "len ports = ", len( ports )
513 return main.FALSE
514 else:
515 main.log.info( "Device Ports are not specified" )
516 if macs:
517 for i in range( len( devices ) ):
518 macsDict[ devices[ i ] ] = macs[ i ]
519
520 elif hostNames and not devices and main.hostsData:
521 devices = []
522 main.log.info( "singleToMultiIntent function is using main.hostsData" )
523 for host in hostNames:
524 devices.append( main.hostsData.get( host ).get( 'location' ) )
525 macsDict[ main.hostsData.get( host ).get( 'location' ) ] = \
526 main.hostsData.get( host ).get( 'mac' )
527 ipDict[ main.hostsData.get( host ).get( 'location' ) ] = \
528 main.hostsData.get( host ).get( 'ipAddresses' )
529 #print main.hostsData
530
531 #print 'host names = ', hostNames
532 #print 'devices = ', devices
533 #print "macsDict = ", macsDict
534
535 pingResult = main.TRUE
536 intentResult = main.TRUE
537 removeIntentResult = main.TRUE
538 flowResult = main.TRUE
539 topoResult = main.TRUE
540 linkDownResult = main.TRUE
541 linkUpResult = main.TRUE
542
543 devicesCopy = copy.copy( devices )
544 if ports:
545 portsCopy = copy.copy( ports )
546 main.log.info( itemName + ": Adding single point to multi point intents" )
547
548 # Check flows count in each node
549 checkFlowsCount( main )
550
551 # Adding bidirectional point intents
552 for i in range( len( devices ) ):
553 ingressDevice = devicesCopy[ i ]
554 egressDeviceList = copy.copy( devicesCopy )
555 egressDeviceList.remove( ingressDevice )
556 if ports:
557 portIngress = portsCopy[ i ]
558 portEgressList = copy.copy( portsCopy )
559 del portEgressList[ i ]
560 else:
561 portIngress = ""
562 portEgressList = None
563 if not macsDict:
564 srcMac = ""
565 else:
566 srcMac = macsDict[ ingressDevice ]
567 if srcMac == None:
568 main.log.debug( "There is no MAC in device - " + ingressDevice )
569 srcMac = ""
570
571 intentsId.append(
572 main.CLIs[ onosNode ].addSinglepointToMultipointIntent(
573 ingressDevice=ingressDevice,
574 egressDeviceList=egressDeviceList,
575 portIngress=portIngress,
576 portEgressList=portEgressList,
577 ethType=ethType,
578 ethSrc=srcMac,
579 bandwidth=bandwidth,
580 lambdaAlloc=lambdaAlloc,
581 ipProto=ipProto,
582 ipSrc="",
583 ipDst="",
584 tcpSrc="",
585 tcpDst="" ) )
586
587 # Wait some time for the flow to go through when using multi instance
588 pingResult = pingallHosts( main, hostNames )
589
590 # Check intents state
591 time.sleep( main.checkIntentSleep )
592 intentResult = checkIntentState( main, intentsId )
593
594 # Check intents state again if first check fails...
595 if not intentResult:
596 intentResult = checkIntentState( main, intentsId )
597
598 # Check flows count in each node
599 checkFlowsCount( main )
600 # Verify flows
601 checkFlowsState( main )
602
603 pingResult = pingResult and pingallHosts( main, hostNames )
604
605 # Test rerouting if these variables exist
606 if sw1 and sw2 and expectedLink:
607 # link down
608 linkDownResult = link( main, sw1, sw2, "down" )
609 intentResult = intentResult and checkIntentState( main, intentsId )
610
611 # Check flows count in each node
612 checkFlowsCount( main )
613 # Verify flows
614 checkFlowsState( main )
615
616 # Check OnosTopology
617 topoResult = checkTopology( main, expectedLink )
618
619 # Ping hosts
620 pingResult = pingResult and pingallHosts( main, hostNames )
621
622 intentResult = checkIntentState( main, intentsId )
623
624 # Checks ONOS state in link down
625 if linkDownResult and topoResult and pingResult and intentResult:
626 main.log.info( itemName + ": Successfully brought link down" )
627 else:
628 main.log.error( itemName + ": Failed to bring link down" )
629
630 # link up
631 linkUpResult = link( main, sw1, sw2, "up" )
632 time.sleep( main.rerouteSleep )
633
634 # Check flows count in each node
635 checkFlowsCount( main )
636 # Verify flows
637 checkFlowsState( main )
638
639 # Check OnosTopology
640 topoResult = checkTopology( main, main.numLinks )
641
642 # Ping hosts
643 pingResult = pingResult and pingallHosts( main, hostNames )
644
645 intentResult = checkIntentState( main, intentsId )
646
647 # Checks ONOS state in link up
648 if linkUpResult and topoResult and pingResult and intentResult:
649 main.log.info( itemName + ": Successfully brought link back up" )
650 else:
651 main.log.error( itemName + ": Failed to bring link back up" )
652
653 # Remove all intents
654 removeIntentResult = removeAllIntents( main, intentsId )
655
656 stepResult = pingResult and linkDownResult and linkUpResult \
657 and intentResult and removeIntentResult
658
659 return stepResult
660
661def multiToSingleIntent( main,
662 name,
663 hostNames,
664 onosNode=0,
665 devices="",
666 ports=None,
667 ethType="",
668 macs=None,
669 bandwidth="",
670 lambdaAlloc=False,
671 ipProto="",
672 ipAddresses="",
673 tcp="",
674 sw1="",
675 sw2="",
676 expectedLink=0 ):
677 """
678 Verify Single to Multi Point intents
679 NOTE:If main.hostsData is not defined, variables data should be passed in the
680 same order index wise. All devices in the list should have the same
681 format, either all the devices have its port or it doesn't.
682 eg. hostName = [ 'h1', 'h2' ,.. ]
683 devices = [ 'of:0000000000000001', 'of:0000000000000002', ...]
684 ports = [ '1', '1', ..]
685 ...
686 Description:
687 Verify add-multi-to-single-intent
688 Steps:
689 - Get device ids | ports
690 - Add multi to single point intents
691 - Check intents
692 - Verify flows
693 - Ping hosts
694 - Reroute
695 - Link down
696 - Verify flows
697 - Check topology
698 - Ping hosts
699 - Link up
700 - Verify flows
701 - Check topology
702 - Ping hosts
703 - Remove intents
704 Required:
705 name - Type of point intent to add eg. IPV4 | VLAN | Dualstack
706 hostNames - List of host names
707 Optional:
708 onosNode - ONOS node to install the intents in main.CLIs[ ]
709 0 by default so that it will always use the first
710 ONOS node
711 devices - List of device ids in the same order as the hosts
712 in hostNames
713 ports - List of port numbers in the same order as the device in
714 devices
715 ethType - Ethernet type eg. IPV4, IPV6
716 macs - List of hosts mac address in the same order as the hosts in
717 hostNames
718 bandwidth - Bandwidth capacity
719 lambdaAlloc - Allocate lambda, defaults to False
720 ipProto - IP protocol
721 ipAddresses - IP addresses of host in the same order as the hosts in
722 hostNames
723 tcp - TCP ports in the same order as the hosts in hostNames
724 sw1 - First switch to bring down & up for rerouting purpose
725 sw2 - Second switch to bring down & up for rerouting purpose
726 expectedLink - Expected link when the switches are down, it should
727 be two links lower than the links before the two
728 switches are down
729 """
730
731 assert main, "There is no main variable"
732 assert hostNames, "You must specify hosts"
733 assert devices or main.hostsData, "You must specify devices"
734
735 global itemName
736 itemName = name
737 tempHostsData = {}
738 intentsId = []
739 onosNode = int( onosNode )
740
741 macsDict = {}
742 ipDict = {}
743 if hostNames and devices:
744 if len( hostNames ) != len( devices ):
745 main.log.debug( "hosts and devices does not have the same length" )
746 #print "len hostNames = ", len( hostNames )
747 #print "len devices = ", len( devices )
748 return main.FALSE
749 if ports:
750 if len( ports ) != len( devices ):
751 main.log.error( "Ports and devices does " +
752 "not have the same length" )
753 #print "len devices = ", len( devices )
754 #print "len ports = ", len( ports )
755 return main.FALSE
756 else:
757 main.log.info( "Device Ports are not specified" )
758 if macs:
759 for i in range( len( devices ) ):
760 macsDict[ devices[ i ] ] = macs[ i ]
761 elif hostNames and not devices and main.hostsData:
762 devices = []
763 main.log.info( "multiToSingleIntent function is using main.hostsData" )
764 for host in hostNames:
765 devices.append( main.hostsData.get( host ).get( 'location' ) )
766 macsDict[ main.hostsData.get( host ).get( 'location' ) ] = \
767 main.hostsData.get( host ).get( 'mac' )
768 ipDict[ main.hostsData.get( host ).get( 'location' ) ] = \
769 main.hostsData.get( host ).get( 'ipAddresses' )
770 #print main.hostsData
771
772 #print 'host names = ', hostNames
773 #print 'devices = ', devices
774 #print "macsDict = ", macsDict
775
776 pingResult = main.TRUE
777 intentResult = main.TRUE
778 removeIntentResult = main.TRUE
779 flowResult = main.TRUE
780 topoResult = main.TRUE
781 linkDownResult = main.TRUE
782 linkUpResult = main.TRUE
783
784 devicesCopy = copy.copy( devices )
785 if ports:
786 portsCopy = copy.copy( ports )
787 main.log.info( itemName + ": Adding multi point to single point intents" )
788
789 # Check flows count in each node
790 checkFlowsCount( main )
791
792 # Adding bidirectional point intents
793 for i in range( len( devices ) ):
794 egressDevice = devicesCopy[ i ]
795 ingressDeviceList = copy.copy( devicesCopy )
796 ingressDeviceList.remove( egressDevice )
797 if ports:
798 portEgress = portsCopy[ i ]
799 portIngressList = copy.copy( portsCopy )
800 del portIngressList[ i ]
801 else:
802 portEgress = ""
803 portIngressList = None
804 if not macsDict:
805 dstMac = ""
806 else:
807 dstMac = macsDict[ egressDevice ]
808 if dstMac == None:
809 main.log.debug( "There is no MAC in device - " + egressDevice )
810 dstMac = ""
811
812 intentsId.append(
813 main.CLIs[ onosNode ].addMultipointToSinglepointIntent(
814 ingressDeviceList=ingressDeviceList,
815 egressDevice=egressDevice,
816 portIngressList=portIngressList,
817 portEgress=portEgress,
818 ethType=ethType,
819 ethDst=dstMac,
820 bandwidth=bandwidth,
821 lambdaAlloc=lambdaAlloc,
822 ipProto=ipProto,
823 ipSrc="",
824 ipDst="",
825 tcpSrc="",
826 tcpDst="" ) )
827
828 pingResult = pingallHosts( main, hostNames )
829
830 # Check intents state
831 time.sleep( main.checkIntentSleep )
832 intentResult = checkIntentState( main, intentsId )
833
834 # Check intents state again if first check fails...
835 if not intentResult:
836 intentResult = checkIntentState( main, intentsId )
837
838 # Check flows count in each node
839 checkFlowsCount( main )
840 # Verify flows
841 checkFlowsState( main )
842
843 # Ping hosts
844 pingResult = pingResult and pingallHosts( main, hostNames )
845 # Ping hosts again...
846 pingResult = pingResult and pingallHosts( main, hostNames )
847
848 # Test rerouting if these variables exist
849 if sw1 and sw2 and expectedLink:
850 # link down
851 linkDownResult = link( main, sw1, sw2, "down" )
852 intentResult = intentResult and checkIntentState( main, intentsId )
853
854 # Check flows count in each node
855 checkFlowsCount( main )
856 # Verify flows
857 checkFlowsState( main )
858
859 # Check OnosTopology
860 topoResult = checkTopology( main, expectedLink )
861
862 # Ping hosts
863 pingResult = pingResult and pingallHosts( main, hostNames )
864
865 intentResult = checkIntentState( main, intentsId )
866
867 # Checks ONOS state in link down
868 if linkDownResult and topoResult and pingResult and intentResult:
869 main.log.info( itemName + ": Successfully brought link down" )
870 else:
871 main.log.error( itemName + ": Failed to bring link down" )
872
873 # link up
874 linkUpResult = link( main, sw1, sw2, "up" )
875 time.sleep( main.rerouteSleep )
876
877 # Check flows count in each node
878 checkFlowsCount( main )
879 # Verify flows
880 checkFlowsState( main )
881
882 # Check OnosTopology
883 topoResult = checkTopology( main, main.numLinks )
884
885 # Ping hosts
886 pingResult = pingResult and pingallHosts( main, hostNames )
887
888 intentResult = checkIntentState( main, intentsId )
889
890 # Checks ONOS state in link up
891 if linkUpResult and topoResult and pingResult and intentResult:
892 main.log.info( itemName + ": Successfully brought link back up" )
893 else:
894 main.log.error( itemName + ": Failed to bring link back up" )
895
896 # Remove all intents
897 removeIntentResult = removeAllIntents( main, intentsId )
898
899 stepResult = pingResult and linkDownResult and linkUpResult \
900 and intentResult and removeIntentResult
901
902 return stepResult
903
904def pingallHosts( main, hostList, pingType="ipv4" ):
905 # Ping all host in the hosts list variable
906 print "Pinging : ", hostList
907 pingResult = main.TRUE
908 pingResult = main.Mininet1.pingallHosts( hostList, pingType )
909 return pingResult
910
911def getHostsData( main ):
912 """
913 Use fwd app and pingall to discover all the hosts
914 """
915
916 activateResult = main.TRUE
917 appCheck = main.TRUE
918 getDataResult = main.TRUE
919 main.log.info( "Activating reactive forwarding app " )
920 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
921 if not activateResult:
922 main.log.error( "Something went wrong installing fwd app" )
923 time.sleep( main.fwdSleep )
924 pingResult = main.Mininet1.pingall( timeout = 600 )
925 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
926 hosts = main.Mininet1.getHosts().keys()
927 # TODO: Make better use of new getHosts function
928 for host in hosts:
929 main.hostsData[ host ] = {}
930 main.hostsData[ host ][ 'mac' ] = \
931 main.Mininet1.getMacAddress( host ).upper()
932 for hostj in hostsJson:
933 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
934 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
935 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
936 main.hostsData[ host ][ 'location' ] = \
937 hostj[ 'location' ][ 'elementId' ] + '/' + \
938 hostj[ 'location' ][ 'port' ]
939 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
940
941 main.log.info( "Deactivating reactive forwarding app " )
942 deactivateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
943 if activateResult and deactivateResult and main.hostsData:
944 main.log.info( "Successfully used fwd app to discover hosts " )
945 getDataResult = main.TRUE
946 else:
947 main.log.info( "Failed to use fwd app to discover hosts " )
948 getDataResult = main.FALSE
949
950 print main.hostsData
951
952 return getDataResult
953
954def checkTopology( main, expectedLink ):
955 statusResult = main.TRUE
956 # Check onos topology
957 main.log.info( itemName + ": Checking ONOS topology " )
958
959 for i in range( main.numCtrls ):
960 topologyResult = main.CLIs[ i ].topology()
961 statusResult = main.ONOSbench.checkStatus( topologyResult,
962 main.numSwitch,
963 expectedLink )\
964 and statusResult
965 if not statusResult:
966 main.log.error( itemName + ": Topology mismatch" )
967 else:
968 main.log.info( itemName + ": Topology match" )
969 return statusResult
970
971def checkIntentState( main, intentsId ):
972 """
973 This function will check intent state to make sure all the intents
974 are in INSTALLED state
975 """
976
977 intentResult = main.TRUE
978 results = []
979
980 main.log.info( itemName + ": Checking intents state" )
981 # First check of intents
982 for i in range( main.numCtrls ):
983 tempResult = main.CLIs[ i ].checkIntentState( intentsId=intentsId )
984 results.append( tempResult )
985
986 expectedState = [ 'INSTALLED', 'INSTALLING' ]
987
988 if all( result == main.TRUE for result in results ):
989 main.log.info( itemName + ": Intents are installed correctly" )
990 else:
991 # Wait for at least 5 second before checking the intents again
992 time.sleep( 5 )
993 results = []
994 # Second check of intents since some of the intents may be in
995 # INSTALLING state, they should be in INSTALLED at this time
996 for i in range( main.numCtrls ):
997 tempResult = main.CLIs[ i ].checkIntentState(
998 intentsId=intentsId )
999 results.append( tempResult )
1000 if all( result == main.TRUE for result in results ):
1001 main.log.info( itemName + ": Intents are installed correctly" )
1002 else:
1003 main.log.error( itemName + ": Intents are NOT installed correctly" )
1004 intentResult = main.FALSE
1005
1006 return intentResult
1007
1008def checkFlowsState( main ):
1009
1010 main.log.info( itemName + ": Check flows state" )
1011 checkFlowsResult = main.CLIs[ 0 ].checkFlowsState()
1012 return checkFlowsResult
1013
1014def link( main, sw1, sw2, option):
1015
1016 # link down
1017 main.log.info( itemName + ": Bring link " + option + "between " +
1018 sw1 + " and " + sw2 )
1019 linkResult = main.Mininet1.link( end1=sw1, end2=sw2, option=option )
1020 return linkResult
1021
1022def removeAllIntents( main ):
1023 """
1024 Remove all intents in the intentsId
1025 """
1026
1027 onosSummary = []
1028 removeIntentResult = main.TRUE
1029 # Remove intents
1030 removeIntentResult = main.CLIs[ 0 ].removeAllIntents( )
1031
1032 if removeIntentResult:
1033 main.log.info( itemName + ": There are no more intents remaining, " +
1034 "successfully removed all the intents." )
1035
1036 return removeIntentResult
1037
1038def checkFlowsCount( main ):
1039 """
1040 Check flows count in each node
1041 """
1042
1043 flowsCount = []
1044 main.log.info( itemName + ": Checking flows count in each ONOS node" )
1045 for i in range( main.numCtrls ):
1046 flowsCount.append( len( json.loads( main.CLIs[ i ].flows() ) ) )
1047
1048 if flowsCount:
1049 if all( flows==flowsCount[ 0 ] for flows in flowsCount ):
1050 main.log.info( itemName + ": There are " + str( flowsCount[ 0 ] ) +
1051 " flows in all ONOS node" )
1052 else:
1053 for i in range( main.numCtrls ):
1054 main.log.debug( itemName + ": ONOS node " + str( i + 1 ) +
1055 " has " + flowsCount[ i ] + " flows" )
1056 else:
1057 main.log.error( "Checking flows count failed, check summary command" )
1058 return main.FALSE
1059
1060 return main.TRUE
1061