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