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