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