kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 1 | """ |
| 2 | Wrapper function for FuncTopo |
| 3 | Includes onosclidriver and mininetclidriver functions |
| 4 | """ |
| 5 | import time |
| 6 | import json |
| 7 | import re |
| 8 | |
| 9 | def __init__( self ): |
| 10 | self.default = '' |
| 11 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 12 | def testTopology( main, topoFile='', args='', mnCmd='', timeout=300, clean=True ): |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 13 | """ |
| 14 | Description: |
| 15 | This function combines different wrapper functions in this module |
| 16 | to simulate a topology test |
| 17 | Test Steps: |
| 18 | - Load topology |
| 19 | - Discover topology |
| 20 | - Compare topology |
| 21 | - pingall |
| 22 | - Bring links down |
| 23 | - Compare topology |
| 24 | - pingall |
| 25 | - Bring links up |
| 26 | - Compare topology |
| 27 | - pingall |
| 28 | Options: |
| 29 | clean: Does sudo mn -c to clean mininet residue |
| 30 | Please read mininetclidriver.py >> startNet( .. ) function for details |
| 31 | Returns: |
| 32 | Returns main.TRUE if the test is successful, main.FALSE otherwise |
| 33 | """ |
| 34 | testTopoResult = main.TRUE |
| 35 | compareTopoResult = main.TRUE |
| 36 | topoObjectResult = main.TRUE |
| 37 | stopResult = main.TRUE |
| 38 | |
| 39 | if clean: |
| 40 | # Cleans minient |
| 41 | stopResult = stopMininet( main ) |
| 42 | |
| 43 | # Restart ONOS to clear hosts test new mininet topology |
| 44 | reinstallOnosResult = reinstallOnos( main ) |
| 45 | |
| 46 | # Starts topology |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 47 | startResult = startNewTopology( main, topoFile, args, mnCmd, timeout=timeout ) |
GlennRC | 1c5df3c | 2015-08-27 16:12:09 -0700 | [diff] [blame] | 48 | # onos needs time to see the links |
| 49 | time.sleep(15) |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 50 | |
| 51 | # Gets list of switches in mininet |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 52 | #assignSwitch( main ) |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 53 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 54 | testTopoResult = startResult and topoObjectResult |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | return testTopoResult |
| 58 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 59 | def startNewTopology( main, topoFile='', args='', mnCmd='', timeout=900 ): |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 60 | """ |
| 61 | Description: |
| 62 | This wrapper function starts new topology |
| 63 | Options: |
| 64 | Please read mininetclidriver.py >> startNet( .. ) function for details |
| 65 | Return: |
| 66 | Returns main.TRUE if topology is successfully created by mininet, |
| 67 | main.FALSE otherwise |
| 68 | NOTE: |
| 69 | Assumes Mininet1 is the name of the handler |
| 70 | """ |
| 71 | assert main, "There is no main variable" |
| 72 | assert main.Mininet1, "Mininet 1 is not created" |
| 73 | result = main.TRUE |
| 74 | |
| 75 | main.log.info( main.topoName + ": Starting new Mininet topology" ) |
| 76 | |
| 77 | # log which method is being used |
| 78 | if topoFile: |
| 79 | main.log.info( main.topoName + ": Starting topology with " + |
| 80 | topoFile + "topology file" ) |
| 81 | elif not topoFile and not mnCmd: |
| 82 | main.log.info( main.topoName + ": Starting topology using" + |
| 83 | " the topo file" ) |
| 84 | elif topoFile and mnCmd: |
| 85 | main.log.error( main.topoName + ": You can only use one " + |
| 86 | "method to start a topology" ) |
| 87 | elif mnCmd: |
| 88 | main.log.info( main.topoName + ": Starting topology with '" + |
| 89 | mnCmd + "' Mininet command" ) |
| 90 | |
| 91 | |
| 92 | result = main.Mininet1.startNet( topoFile=topoFile, |
| 93 | args=args, |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 94 | mnCmd=mnCmd, |
| 95 | timeout=timeout) |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 96 | |
| 97 | return result |
| 98 | |
| 99 | def stopMininet( main ): |
| 100 | """ |
| 101 | Stops current topology and execute mn -c basically triggers |
| 102 | stopNet in mininetclidrivers |
| 103 | |
| 104 | NOTE: Mininet should be running when issuing this command other wise |
| 105 | the this function will cause the test to stop |
| 106 | """ |
| 107 | stopResult = main.TRUE |
| 108 | stopResult = main.Mininet1.stopNet() |
| 109 | time.sleep( 30 ) |
| 110 | if not stopResult: |
| 111 | main.log.info( main.topoName + ": Did not stop Mininet topology" ) |
| 112 | return stopResult |
| 113 | |
| 114 | def compareTopo( main ): |
| 115 | """ |
| 116 | Compare topology( devices, links, ports, hosts ) between ONOS and |
| 117 | mininet using sts |
| 118 | """ |
| 119 | devices = [] |
| 120 | links = [] |
| 121 | ports = [] |
| 122 | hosts = [] |
| 123 | switchResult = [] |
| 124 | linksResult = [] |
| 125 | portsResult = [] |
| 126 | hostsResult = [] |
| 127 | mnSwitches = main.Mininet1.getSwitches() |
| 128 | mnLinks = main.Mininet1.getLinks() |
| 129 | mnHosts = main.Mininet1.getHosts() |
| 130 | compareTopoResult = main.TRUE |
| 131 | |
| 132 | for i in range( main.numCtrls ): |
| 133 | devices.append( json.loads( main.CLIs[ i ].devices() ) ) |
| 134 | links.append( json.loads( main.CLIs[ i ].links() ) ) |
| 135 | ports.append( json.loads( main.CLIs[ i ].ports() ) ) |
| 136 | hosts.append( json.loads( main.CLIs[ i ].hosts() ) ) |
| 137 | |
| 138 | # Comparing switches |
| 139 | main.log.info( main.topoName + ": Comparing switches in each ONOS nodes" + |
| 140 | " with Mininet" ) |
| 141 | for i in range( main.numCtrls ): |
| 142 | tempResult = main.Mininet1.compareSwitches( mnSwitches, |
| 143 | devices[ i ], |
| 144 | ports[ i ] ) |
| 145 | switchResult.append( tempResult ) |
| 146 | if tempResult == main.FALSE: |
| 147 | main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) + |
| 148 | " switch view is incorrect " ) |
| 149 | |
| 150 | if all( result == main.TRUE for result in switchResult ): |
| 151 | main.log.info( main.topoName + ": Switch view in all ONOS nodes "+ |
| 152 | "are correct " ) |
| 153 | else: |
| 154 | compareTopoResult = main.FALSE |
| 155 | |
| 156 | # Comparing links |
| 157 | main.log.info( main.topoName + ": Comparing links in each ONOS nodes" + |
| 158 | " with Mininet" ) |
| 159 | for i in range( main.numCtrls ): |
| 160 | tempResult = main.Mininet1.compareLinks( mnSwitches, |
| 161 | mnLinks, |
| 162 | links[ i ] ) |
| 163 | linksResult.append( tempResult ) |
| 164 | if tempResult == main.FALSE: |
| 165 | main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) + |
| 166 | " links view are incorrect " ) |
| 167 | |
| 168 | if all( result == main.TRUE for result in linksResult ): |
| 169 | main.log.info( main.topoName + ": Links view in all ONOS nodes "+ |
| 170 | "are correct " ) |
| 171 | else: |
| 172 | compareTopoResult = main.FALSE |
| 173 | |
| 174 | # Comparing hosts |
| 175 | main.log.info( main.topoName + ": Comparing hosts in each ONOS nodes" + |
| 176 | " with Mininet" ) |
| 177 | for i in range( main.numCtrls ): |
| 178 | tempResult = main.Mininet1.compareHosts( mnHosts, hosts[ i ] ) |
| 179 | hostsResult.append( tempResult ) |
| 180 | if tempResult == main.FALSE: |
| 181 | main.log.error( main.topoName + ": ONOS-" + str( i + 1 ) + |
| 182 | " hosts view are incorrect " ) |
| 183 | |
| 184 | if all( result == main.TRUE for result in hostsResult ): |
| 185 | main.log.info( main.topoName + ": Hosts view in all ONOS nodes "+ |
| 186 | "are correct " ) |
| 187 | else: |
| 188 | compareTopoResult = main.FALSE |
| 189 | |
| 190 | return compareTopoResult |
| 191 | |
| 192 | def assignSwitch( main ): |
| 193 | """ |
| 194 | Returns switch list using getSwitch in Mininet driver |
| 195 | """ |
| 196 | switchList = [] |
| 197 | assignResult = main.TRUE |
| 198 | switchList = main.Mininet1.getSwitch() |
| 199 | assignResult = main.Mininet1.assignSwController( sw=switchList, |
| 200 | ip=main.ONOSip[ 0 ], |
GlennRC | 632e289 | 2015-10-19 18:58:41 -0700 | [diff] [blame] | 201 | port=6633 ) |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 202 | |
| 203 | for sw in switchList: |
| 204 | response = main.Mininet1.getSwController( sw ) |
| 205 | if re.search( "tcp:" + main.ONOSip[ 0 ], response ): |
| 206 | assignResult = assignResult and main.TRUE |
| 207 | else: |
| 208 | assignResult = main.FALSE |
| 209 | |
| 210 | return switchList |
| 211 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 212 | def connectivity( main, timeout=900, shortCircuit=True, acceptableFailed=20 ): |
| 213 | """ |
| 214 | Use fwd app and pingall to discover all the hosts |
| 215 | """ |
| 216 | activateResult = main.TRUE |
| 217 | appCheck = main.TRUE |
| 218 | getDataResult = main.TRUE |
| 219 | main.log.info( main.topoName + ": Activating reactive forwarding app " ) |
| 220 | activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" ) |
| 221 | |
| 222 | if main.hostsData: |
| 223 | main.hostsData = {} |
| 224 | for i in range( main.numCtrls ): |
| 225 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 226 | if appCheck != main.TRUE: |
| 227 | main.log.warn( main.CLIs[ i ].apps() ) |
| 228 | main.log.warn( main.CLIs[ i ].appIDs() ) |
| 229 | |
| 230 | time.sleep( main.fwdSleep ) |
| 231 | |
| 232 | # Discover hosts using pingall |
| 233 | pingResult = main.Mininet1.pingall( timeout=timeout, |
| 234 | shortCircuit=shortCircuit, |
| 235 | acceptableFailed=acceptableFailed ) |
| 236 | |
| 237 | main.log.info( main.topoName + ": Deactivate reactive forwarding app " ) |
| 238 | activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" ) |
| 239 | for i in range( main.numCtrls ): |
| 240 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 241 | if appCheck != main.TRUE: |
| 242 | main.log.warn( main.CLIs[ i ].apps() ) |
| 243 | main.log.warn( main.CLIs[ i ].appIDs() ) |
| 244 | |
| 245 | return pingResult |
| 246 | |
kelvin-onlab | 1d381fe | 2015-07-14 16:24:56 -0700 | [diff] [blame] | 247 | def getHostsData( main ): |
| 248 | """ |
| 249 | Use fwd app and pingall to discover all the hosts |
| 250 | """ |
| 251 | activateResult = main.TRUE |
| 252 | appCheck = main.TRUE |
| 253 | getDataResult = main.TRUE |
| 254 | main.log.info( main.topoName + ": Activating reactive forwarding app " ) |
| 255 | activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" ) |
| 256 | |
| 257 | if main.hostsData: |
| 258 | main.hostsData = {} |
| 259 | for i in range( main.numCtrls ): |
| 260 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 261 | if appCheck != main.TRUE: |
| 262 | main.log.warn( main.CLIs[ i ].apps() ) |
| 263 | main.log.warn( main.CLIs[ i ].appIDs() ) |
| 264 | |
| 265 | time.sleep( main.fwdSleep ) |
| 266 | # Discover hosts using pingall |
| 267 | pingResult = main.Mininet1.pingall( timeout=900 ) |
| 268 | |
| 269 | hostsJson = json.loads( main.CLIs[ 0 ].hosts() ) |
| 270 | hosts = main.Mininet1.getHosts().keys() |
| 271 | |
| 272 | for host in hosts: |
| 273 | main.hostsData[ host ] = {} |
| 274 | main.hostsData[ host ][ 'mac' ] = \ |
| 275 | main.Mininet1.getMacAddress( host ).upper() |
| 276 | for hostj in hostsJson: |
| 277 | if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]: |
| 278 | main.hostsData[ host ][ 'id' ] = hostj[ 'id' ] |
| 279 | main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ] |
| 280 | main.hostsData[ host ][ 'location' ] = \ |
| 281 | hostj[ 'location' ][ 'elementId' ] + '/' + \ |
| 282 | hostj[ 'location' ][ 'port' ] |
| 283 | main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ] |
| 284 | |
| 285 | if activateResult and main.hostsData: |
| 286 | main.log.info( main.topoName + ": Successfully used fwd app" + |
| 287 | " to discover hosts " ) |
| 288 | getDataResult = main.TRUE |
| 289 | else: |
| 290 | main.log.info( main.topoName + ": Failed to use fwd app" + |
| 291 | " to discover hosts " ) |
| 292 | getDataResult = main.FALSE |
| 293 | |
| 294 | main.log.info( main.topoName + ": Deactivate reactive forwarding app " ) |
| 295 | activateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" ) |
| 296 | for i in range( main.numCtrls ): |
| 297 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 298 | if appCheck != main.TRUE: |
| 299 | main.log.warn( main.CLIs[ i ].apps() ) |
| 300 | main.log.warn( main.CLIs[ i ].appIDs() ) |
| 301 | |
| 302 | # This data can be use later for intents |
| 303 | print main.hostsData |
| 304 | |
| 305 | return getDataResult |
| 306 | |
| 307 | def reinstallOnos( main ): |
| 308 | """ |
| 309 | Description: |
| 310 | Stop and start ONOS that clears hosts,devices etc. in order to test |
| 311 | new mininet topology |
| 312 | Return: |
| 313 | Retruns main.TRUE for a successful restart, main.FALSE otherwise. |
| 314 | """ |
| 315 | uninstallResult = [] |
| 316 | installResult = [] |
| 317 | stopResult = [] |
| 318 | startResult = [] |
| 319 | onosIsUpResult = [] |
| 320 | restartResult = main.TRUE |
| 321 | |
| 322 | main.log.info( main.topoName + ": Uninstall ONOS cluster" ) |
| 323 | for ip in main.ONOSip: |
| 324 | uninstallResult.append( main.ONOSbench.onosUninstall( nodeIp=ip ) ) |
| 325 | |
| 326 | if all( result == main.TRUE for result in uninstallResult ): |
| 327 | main.log.info( main.topoName + ": Successfully uninstall ONOS cluster" ) |
| 328 | else: |
| 329 | restartResult = main.FALSE |
| 330 | main.log.error( main.topoName + ": Failed to uninstall ONOS cluster" ) |
| 331 | |
| 332 | time.sleep( main.startUpSleep ) |
| 333 | |
| 334 | main.log.info( main.topoName + ": Installing ONOS cluster" ) |
| 335 | |
| 336 | for i in range( main.numCtrls ): |
| 337 | installResult.append( main.ONOSbench.onosInstall( |
| 338 | node=main.ONOSip[ i ] ) ) |
| 339 | |
| 340 | if all( result == main.TRUE for result in installResult ): |
| 341 | main.log.info( main.topoName + ": Successfully installed ONOS cluster" ) |
| 342 | else: |
| 343 | restartResult = main.FALSE |
| 344 | main.log.error( main.topoName + ": Failed to install ONOS cluster" ) |
| 345 | |
| 346 | for i in range( main.numCtrls ): |
| 347 | onosIsUpResult.append( main.ONOSbench.isup( main.ONOSip[ i ] ) ) |
| 348 | |
| 349 | if all( result == main.TRUE for result in onosIsUpResult ): |
| 350 | main.log.report( "ONOS instance is up and ready" ) |
| 351 | else: |
| 352 | main.log.report( "ONOS instance may not be up, stop and " + |
| 353 | "start ONOS again " ) |
| 354 | for i in range( main.numCtrls ): |
| 355 | stopResult.append( main.ONOSbench.onosStop( main.ONOSip[ i ] ) ) |
| 356 | |
| 357 | if all( result == main.TRUE for result in stopResult ): |
| 358 | main.log.info( main.topoName + ": Successfully stop ONOS cluster" ) |
| 359 | else: |
| 360 | main.log.error( main.topoName + ": Failed to stop ONOS cluster" ) |
| 361 | |
| 362 | for i in range( main.numCtrls ): |
| 363 | startResult.append( main.ONOSbench.onosStart( main.ONOSip[ i ] ) ) |
| 364 | |
| 365 | if all( result == main.TRUE for result in startResult ): |
| 366 | main.log.info( main.topoName + ": Successfully start ONOS cluster" ) |
| 367 | else: |
| 368 | main.log.error( main.topoName + ": Failed to start ONOS cluster" ) |
| 369 | |
| 370 | main.log.info( main.topoName + ": Starting ONOS CLI" ) |
| 371 | cliResult = [] |
| 372 | for i in range( main.numCtrls ): |
| 373 | cliResult.append( main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] ) ) |
| 374 | |
| 375 | if all( result == main.TRUE for result in cliResult ): |
| 376 | main.log.info( main.topoName + ": Successfully start ONOS cli" ) |
| 377 | else: |
| 378 | main.log.error( main.topoName + ": Failed to start ONOS cli" ) |
| 379 | restartResult = main.FALSE |
| 380 | |
| 381 | |
| 382 | return restartResult |
| 383 | |
| 384 | |
| 385 | |