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