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 | """ |
| 13 | Description: |
| 14 | This function combines different wrapper functions in this module |
| 15 | to simulate a topology test |
| 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 | |
| 28 | Returns: |
| 29 | Returns main.TRUE if the test is successful, main.FALSE otherwise |
| 30 | """ |
| 31 | testTopoResult = main.TRUE |
| 32 | compareTopoResult = main.TRUE |
| 33 | topoObjectResult = main.TRUE |
| 34 | |
| 35 | # Starts topology |
| 36 | startResult = startNewTopology( main, topoFile, args, mnCmd, clean ) |
| 37 | |
| 38 | # Create topology object using sts |
| 39 | topoObjectResult = createTopoObject( main ) |
| 40 | |
| 41 | # Compare Topology |
| 42 | compareTopoResult = compareTopo( main ) |
| 43 | |
| 44 | # This function activates fwd app then does pingall as well as store |
| 45 | # hosts data in a variable main.hostsData |
| 46 | getHostsResult = getHostsData( main ) |
| 47 | |
| 48 | testTopoResult = startResult and topoObjectResult and \ |
| 49 | compareTopoResult and getHostsResult |
| 50 | |
| 51 | return testTopoResult |
| 52 | |
| 53 | def startNewTopology( main, topoFile='', args='', mnCmd='', clean=True ): |
| 54 | """ |
| 55 | Description: |
| 56 | This wrapper function starts new topology |
| 57 | Optional: |
| 58 | clean - Stops current topology and execute mn -c basically triggers |
| 59 | stopNet in mininetclidrivers |
| 60 | Return: |
| 61 | Returns main.TRUE if topology is successfully created by mininet, |
| 62 | main.FALSE otherwise |
| 63 | NOTE: |
| 64 | Assumes Mininet1 is the name of the handler |
| 65 | """ |
| 66 | assert main, "There is no main variable" |
| 67 | assert main.Mininet1, "Mininet 1 is not created" |
| 68 | result = main.TRUE |
| 69 | |
| 70 | main.log.info( main.topoName + ": Starting new Mininet topology" ) |
| 71 | |
| 72 | # log which method is being used |
| 73 | if topoFile: |
| 74 | main.log.info( main.topoName + ": Starting topology with " + |
| 75 | topoFile + "topology file" ) |
| 76 | elif not topoFile and not mnCmd: |
| 77 | main.log.info( main.topoName + ": Starting topology using" + |
| 78 | " the topo file" ) |
| 79 | elif topoFile and mnCmd: |
| 80 | main.log.error( main.topoName + ": You can only use one " + |
| 81 | "method to start a topology" ) |
| 82 | elif mnCmd: |
| 83 | main.log.info( main.topoName + ": Starting topology with '" + |
| 84 | mnCmd + "' Mininet command" ) |
| 85 | |
| 86 | if clean: |
| 87 | main.Mininet1.stopNet() |
| 88 | time.sleep( 30 ) |
| 89 | else: |
| 90 | main.log.info( main.topoName + ": Did not stop Mininet topology" ) |
| 91 | |
| 92 | result = main.Mininet1.startNet( topoFile=topoFile, |
| 93 | args=args, |
| 94 | mnCmd=mnCmd ) |
| 95 | |
| 96 | return result |
| 97 | |
| 98 | def createTopoObject( main ): |
| 99 | """ |
| 100 | Creates topology object using sts module |
| 101 | """ |
| 102 | from sts.topology.teston_topology import TestONTopology |
| 103 | global MNTopo |
| 104 | try: |
| 105 | ctrls = [] |
| 106 | main.log.info( main.topoName + ": Creating topology object" + |
| 107 | " from mininet" ) |
| 108 | for node in main.nodes: |
| 109 | temp = ( node, node.name, node.ip_address, 6633 ) |
| 110 | ctrls.append( temp ) |
| 111 | MNTopo = TestONTopology( main.Mininet1, ctrls ) |
| 112 | except Exception: |
| 113 | objResult = main.FALSE |
| 114 | else: |
| 115 | objResult = main.TRUE |
| 116 | |
| 117 | return objResult |
| 118 | |
| 119 | def compareTopo( main ): |
| 120 | """ |
| 121 | Compare topology( devices, links, ports, hosts ) between ONOS and |
| 122 | mininet using sts |
| 123 | """ |
| 124 | assert MNTopo, "There is no MNTopo object" |
| 125 | devices = [] |
| 126 | links = [] |
| 127 | ports = [] |
| 128 | hosts = [] |
| 129 | switchResult = [] |
| 130 | linksResult = [] |
| 131 | portsResult = [] |
| 132 | hostsResult = [] |
| 133 | compareTopoResult = main.TRUE |
| 134 | |
| 135 | for i in range( main.numCtrls ): |
| 136 | devices.append( json.loads( main.CLIs[ i ].devices() ) ) |
| 137 | links.append( json.loads( main.CLIs[ i ].links() ) ) |
| 138 | ports.append( json.loads( main.CLIs[ i ].ports() ) ) |
| 139 | hosts.append( json.loads( main.CLIs[ i ].hosts() ) ) |
| 140 | |
| 141 | # Comparing switches |
| 142 | main.log.info( main.topoName + ": Comparing switches in each ONOS nodes" + |
| 143 | " with Mininet" ) |
| 144 | for i in range( main.numCtrls ): |
| 145 | tempResult = main.Mininet1.compareSwitches( MNTopo, devices[ i ] ) |
| 146 | switchResult.append( tempResult ) |
| 147 | if tempResult == main.FALSE: |
| 148 | main.log.error( main.topoName + ": ONOS-" + str( i ) + |
| 149 | " switch view is incorrect " ) |
| 150 | |
| 151 | if all( result == main.TRUE for result in switchResult ): |
| 152 | main.log.info( main.topoName + ": Switch view in all ONOS nodes "+ |
| 153 | "are correct " ) |
| 154 | else: |
| 155 | compareTopoResult = main.FALSE |
| 156 | |
| 157 | # Comparing ports |
| 158 | main.log.info( main.topoName + ": Comparing ports in each ONOS nodes" + |
| 159 | " with Mininet" ) |
| 160 | for i in range( main.numCtrls ): |
| 161 | tempResult = main.Mininet1.comparePorts( MNTopo, ports[ i ] ) |
| 162 | portsResult.append( tempResult ) |
| 163 | if tempResult == main.FALSE: |
| 164 | main.log.error( main.topoName + ": ONOS-" + str( i ) + |
| 165 | " ports view are incorrect " ) |
| 166 | |
| 167 | if all( result == main.TRUE for result in portsResult ): |
| 168 | main.log.info( main.topoName + ": Ports view in all ONOS nodes "+ |
| 169 | "are correct " ) |
| 170 | else: |
| 171 | compareTopoResult = main.FALSE |
| 172 | |
| 173 | # Comparing links |
| 174 | main.log.info( main.topoName + ": Comparing links in each ONOS nodes" + |
| 175 | " with Mininet" ) |
| 176 | for i in range( main.numCtrls ): |
| 177 | tempResult = main.Mininet1.compareLinks( MNTopo, links[ i ] ) |
| 178 | linksResult.append( tempResult ) |
| 179 | if tempResult == main.FALSE: |
| 180 | main.log.error( main.topoName + ": ONOS-" + str( i ) + |
| 181 | " links view are incorrect " ) |
| 182 | |
| 183 | if all( result == main.TRUE for result in linksResult ): |
| 184 | main.log.info( main.topoName + ": Links view in all ONOS nodes "+ |
| 185 | "are correct " ) |
| 186 | else: |
| 187 | compareTopoResult = main.FALSE |
| 188 | |
| 189 | # Comparing hosts |
| 190 | main.log.info( main.topoName + ": Comparing hosts in each ONOS nodes" + |
| 191 | " with Mininet" ) |
| 192 | for i in range( main.numCtrls ): |
| 193 | tempResult = main.Mininet1.compareHosts( MNTopo, hosts[ i ] ) |
| 194 | hostsResult.append( tempResult ) |
| 195 | if tempResult == main.FALSE: |
| 196 | main.log.error( main.topoName + ": ONOS-" + str( i ) + |
| 197 | " hosts view are incorrect " ) |
| 198 | |
| 199 | if all( result == main.TRUE for result in hostsResult ): |
| 200 | main.log.info( main.topoName + ": Hosts view in all ONOS nodes "+ |
| 201 | "are correct " ) |
| 202 | else: |
| 203 | compareTopoResult = main.FALSE |
| 204 | |
| 205 | return compareTopoResult |
| 206 | |
| 207 | def getHostsData( main ): |
| 208 | """ |
| 209 | Use fwd app and pingall to discover all the hosts |
| 210 | """ |
| 211 | activateResult = main.TRUE |
| 212 | appCheck = main.TRUE |
| 213 | getDataResult = main.TRUE |
| 214 | main.log.info( main.topoName + ": Activating reactive forwarding app " ) |
| 215 | activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" ) |
| 216 | if main.hostsData: |
| 217 | main.hostsData = {} |
| 218 | for i in range( main.numCtrls ): |
| 219 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 220 | if appCheck != main.TRUE: |
| 221 | main.log.warn( main.CLIs[ i ].apps() ) |
| 222 | main.log.warn( main.CLIs[ i ].appIDs() ) |
| 223 | |
| 224 | pingResult = main.Mininet1.pingall( timeout=900 ) |
| 225 | hostsJson = json.loads( main.CLIs[ 0 ].hosts() ) |
| 226 | hosts = main.Mininet1.getHosts() |
| 227 | for host in hosts: |
| 228 | main.hostsData[ host ] = {} |
| 229 | main.hostsData[ host ][ 'mac' ] = \ |
| 230 | main.Mininet1.getMacAddress( host ).upper() |
| 231 | for hostj in hostsJson: |
| 232 | if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]: |
| 233 | main.hostsData[ host ][ 'id' ] = hostj[ 'id' ] |
| 234 | main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ] |
| 235 | main.hostsData[ host ][ 'location' ] = \ |
| 236 | hostj[ 'location' ][ 'elementId' ] + '/' + \ |
| 237 | hostj[ 'location' ][ 'port' ] |
| 238 | main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ] |
| 239 | |
| 240 | if activateResult and main.hostsData: |
| 241 | main.log.info( main.topoName + ": Successfully used fwd app" + |
| 242 | " to discover hosts " ) |
| 243 | getDataResult = main.TRUE |
| 244 | else: |
| 245 | main.log.info( main.topoName + ": Failed to use fwd app" + |
| 246 | " to discover hosts " ) |
| 247 | getDataResult = main.FALSE |
| 248 | |
| 249 | # This data can be use later for intents |
| 250 | print main.hostsData |
| 251 | |
| 252 | return getDataResult |