Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 2 | Copyright 2016 Open Networking Foundation ( ONF ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 11 | ( at your option ) any later version. |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 21 | import time |
| 22 | import re |
| 23 | import imp |
| 24 | import json |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 25 | |
| 26 | |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 27 | class Topology: |
| 28 | |
| 29 | def __init__( self ): |
| 30 | self.default = '' |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 31 | |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 32 | """ |
| 33 | These functions can be used for topology comparisons |
| 34 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 35 | def getAll( self, function, needRetry=False, kwargs={}, inJson=False ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 36 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 37 | Description: |
| 38 | get all devices/links/hosts/ports of the onosCli |
| 39 | Required: |
| 40 | * function - name of the function |
| 41 | * needRetry - it will retry if this is true. |
| 42 | * kwargs - kwargs of the function |
| 43 | * inJson - True if want it in Json form |
| 44 | Returns: |
| 45 | Returns the list of the result. |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 46 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 47 | returnList = [] |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 48 | threads = [] |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 49 | for ctrl in main.Cluster.active(): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 50 | func = getattr( ctrl.CLI, function ) |
| 51 | t = main.Thread( target=utilities.retry if needRetry else func, |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 52 | name=function + "-" + str( ctrl ), |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 53 | args=[ func, [ None ] ] if needRetry else [], |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 54 | kwargs=kwargs ) |
| 55 | threads.append( t ) |
| 56 | t.start() |
| 57 | |
| 58 | for t in threads: |
| 59 | t.join() |
| 60 | if inJson: |
| 61 | try: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 62 | returnList.append( json.loads( t.result ) ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 63 | except ( ValueError, TypeError ): |
| 64 | main.log.exception( "Error parsing hosts results" ) |
| 65 | main.log.error( repr( t.result ) ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 66 | returnList.append( None ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 67 | else: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 68 | returnList.append( t.result ) |
| 69 | return returnList |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 70 | |
| 71 | def compareDevicePort( self, Mininet, controller, mnSwitches, devices, ports ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 72 | """ |
| 73 | Description: |
| 74 | compares the devices and port of the onos to the mininet. |
| 75 | Required: |
| 76 | * Mininet - mininet driver to use |
| 77 | * controller - controller position of the devices |
| 78 | * mnSwitches - switches of mininet |
| 79 | * devices - devices of the onos |
| 80 | * ports - ports of the onos |
| 81 | Returns: |
| 82 | Returns main.TRUE if the results are matching else |
| 83 | Returns main.FALSE |
| 84 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 85 | if devices[ controller ] and ports[ controller ] and \ |
| 86 | "Error" not in devices[ controller ] and \ |
| 87 | "Error" not in ports[ controller ]: |
| 88 | try: |
| 89 | currentDevicesResult = Mininet.compareSwitches( |
| 90 | mnSwitches, |
| 91 | json.loads( devices[ controller ] ), |
| 92 | json.loads( ports[ controller ] ) ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 93 | except ( TypeError, ValueError ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 94 | main.log.error( |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 95 | "Could not load json: {0} or {1}".format( str( devices[ controller ] ), |
| 96 | str( ports[ controller ] ) ) ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 97 | currentDevicesResult = main.FALSE |
| 98 | else: |
| 99 | currentDevicesResult = main.FALSE |
| 100 | return currentDevicesResult |
| 101 | |
| 102 | def compareBase( self, compareElem, controller, compareF, compareArg ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 103 | """ |
| 104 | Description: |
| 105 | compares the links/hosts of the onos to the mininet. |
| 106 | Required: |
| 107 | * compareElem - list of links/hosts of the onos |
| 108 | * controller - controller position of the devices |
| 109 | * compareF - function of the mininet that will compare the |
| 110 | results |
| 111 | * compareArg - arg of the compareF. |
| 112 | Returns: |
| 113 | Returns main.TRUE if the results are matching else |
| 114 | Returns main.FALSE |
| 115 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 116 | if compareElem[ controller ] and "Error" not in compareElem[ controller ]: |
| 117 | try: |
| 118 | if isinstance( compareArg, list ): |
| 119 | compareArg.append( json.loads( compareElem[ controller ] ) ) |
| 120 | else: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 121 | compareArg = [ compareArg, json.loads( compareElem[ controller ] ) ] |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 122 | |
| 123 | currentCompareResult = compareF( *compareArg ) |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 124 | except( TypeError, ValueError ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 125 | main.log.error( |
| 126 | "Could not load json: {0} or {1}".format( str( compareElem[ controller ] ) ) ) |
| 127 | currentCompareResult = main.FALSE |
| 128 | else: |
| 129 | currentCompareResult = main.FALSE |
| 130 | |
| 131 | return currentCompareResult |
| 132 | |
| 133 | def compareTopos( self, Mininet, attempts=1 ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 134 | """ |
| 135 | Description: |
| 136 | compares the links and hosts and switches of the onos to the mininet. |
| 137 | Required: |
| 138 | * Mininet - Mininet driver to use. |
| 139 | * attempts - number of attempts to compare in case |
| 140 | the result is different after a certain time. |
| 141 | Returns: |
| 142 | Returns main.TRUE if the results are matching else |
| 143 | Returns main.FALSE |
| 144 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 145 | main.case( "Compare ONOS Topology view to Mininet topology" ) |
| 146 | main.caseExplanation = "Compare topology elements between Mininet" +\ |
| 147 | " and ONOS" |
| 148 | main.log.info( "Gathering topology information from Mininet" ) |
| 149 | devicesResults = main.FALSE # Overall Boolean for device correctness |
| 150 | linksResults = main.FALSE # Overall Boolean for link correctness |
| 151 | hostsResults = main.FALSE # Overall Boolean for host correctness |
| 152 | deviceFails = [] # Nodes where devices are incorrect |
| 153 | linkFails = [] # Nodes where links are incorrect |
| 154 | hostFails = [] # Nodes where hosts are incorrect |
| 155 | |
| 156 | mnSwitches = Mininet.getSwitches() |
| 157 | mnLinks = Mininet.getLinks() |
| 158 | mnHosts = Mininet.getHosts() |
| 159 | |
| 160 | main.step( "Comparing Mininet topology to ONOS topology" ) |
| 161 | |
| 162 | while ( attempts >= 0 ) and\ |
| 163 | ( not devicesResults or not linksResults or not hostsResults ): |
| 164 | main.log.info( "Sleeping {} seconds".format( 2 ) ) |
| 165 | time.sleep( 2 ) |
| 166 | if not devicesResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 167 | devices = self.getAll( "devices", False ) |
| 168 | ports = self.getAll( "ports", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 169 | devicesResults = main.TRUE |
| 170 | deviceFails = [] # Reset for each failed attempt |
| 171 | if not linksResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 172 | links = self.getAll( "links", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 173 | linksResults = main.TRUE |
| 174 | linkFails = [] # Reset for each failed attempt |
| 175 | if not hostsResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 176 | hosts = self.getAll( "hosts", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 177 | hostsResults = main.TRUE |
| 178 | hostFails = [] # Reset for each failed attempt |
| 179 | |
| 180 | # Check for matching topology on each node |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 181 | for controller in main.Cluster.getRunningPos(): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 182 | controllerStr = str( controller + 1 ) # ONOS node number |
| 183 | # Compare Devices |
| 184 | currentDevicesResult = self.compareDevicePort( Mininet, controller, |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 185 | mnSwitches, |
| 186 | devices, ports ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 187 | if not currentDevicesResult: |
| 188 | deviceFails.append( controllerStr ) |
| 189 | devicesResults = devicesResults and currentDevicesResult |
| 190 | # Compare Links |
| 191 | currentLinksResult = self.compareBase( links, controller, |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 192 | Mininet.compareLinks, |
| 193 | [ mnSwitches, mnLinks ] ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 194 | if not currentLinksResult: |
| 195 | linkFails.append( controllerStr ) |
| 196 | linksResults = linksResults and currentLinksResult |
| 197 | # Compare Hosts |
| 198 | currentHostsResult = self.compareBase( hosts, controller, |
| 199 | Mininet.compareHosts, |
| 200 | mnHosts ) |
| 201 | if not currentHostsResult: |
| 202 | hostFails.append( controllerStr ) |
| 203 | hostsResults = hostsResults and currentHostsResult |
| 204 | # Decrement Attempts Remaining |
| 205 | attempts -= 1 |
| 206 | |
| 207 | utilities.assert_equals( expect=[], |
| 208 | actual=deviceFails, |
| 209 | onpass="ONOS correctly discovered all devices", |
| 210 | onfail="ONOS incorrectly discovered devices on nodes: " + |
| 211 | str( deviceFails ) ) |
| 212 | utilities.assert_equals( expect=[], |
| 213 | actual=linkFails, |
| 214 | onpass="ONOS correctly discovered all links", |
| 215 | onfail="ONOS incorrectly discovered links on nodes: " + |
| 216 | str( linkFails ) ) |
| 217 | utilities.assert_equals( expect=[], |
| 218 | actual=hostFails, |
| 219 | onpass="ONOS correctly discovered all hosts", |
| 220 | onfail="ONOS incorrectly discovered hosts on nodes: " + |
| 221 | str( hostFails ) ) |
| 222 | topoResults = hostsResults and linksResults and devicesResults |
| 223 | utilities.assert_equals( expect=main.TRUE, |
| 224 | actual=topoResults, |
| 225 | onpass="ONOS correctly discovered the topology", |
| 226 | onfail="ONOS incorrectly discovered the topology" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 227 | return topoResults |