kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 1 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2 | #!/usr/bin/env python |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 4 | Created on 26-Oct-2012 |
| 5 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 6 | author: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 7 | |
| 8 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
| 12 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 13 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 18 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 19 | You should have received a copy of the GNU General Public License |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 21 | |
| 22 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 23 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 24 | |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 25 | Some functions rely on STS module. To install this, |
| 26 | git clone https://github.com/jhall11/sts.git |
| 27 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 28 | Some functions rely on a modified version of Mininet. These functions |
| 29 | should all be noted in the comments. To get this MN version run these commands |
| 30 | from within your Mininet folder: |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 31 | git remote add jhall11 https://github.com/jhall11/mininet.git |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 32 | git fetch jhall11 |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 33 | git checkout -b dynamic_topo remotes/jhall11/dynamic_topo |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 34 | git pull |
| 35 | |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 36 | |
| 37 | Note that you may need to run 'sudo make develop' if your mnexec.c file |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 38 | changed when switching branches.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | import re |
| 41 | import sys |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 42 | sys.path.append( "../" ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 43 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 45 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 46 | |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 47 | class MininetCliDriver( Emulator ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 48 | |
| 49 | """ |
| 50 | MininetCliDriver is the basic driver which will handle |
| 51 | the Mininet functions""" |
| 52 | def __init__( self ): |
| 53 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | self.handle = self |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 55 | self.name = None |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 56 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | self.flag = 0 |
| 58 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 59 | def connect( self, **connectargs ): |
| 60 | """ |
| 61 | Here the main is the TestON instance after creating |
| 62 | all the log handles.""" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 63 | try: |
| 64 | for key in connectargs: |
| 65 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 66 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 67 | self.name = self.options[ 'name' ] |
| 68 | self.handle = super( |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 69 | MininetCliDriver, |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 70 | self ).connect( |
| 71 | user_name=self.user_name, |
| 72 | ip_address=self.ip_address, |
| 73 | port=None, |
| 74 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 75 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 76 | if self.handle: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 77 | main.log.info( "Connection successful to the host " + |
| 78 | self.user_name + |
| 79 | "@" + |
| 80 | self.ip_address ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 81 | return main.TRUE |
| 82 | else: |
| 83 | main.log.error( "Connection failed to the host " + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 84 | self.user_name + |
| 85 | "@" + |
| 86 | self.ip_address ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 87 | main.log.error( "Failed to connect to the Mininet CLI" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 88 | return main.FALSE |
| 89 | except pexpect.EOF: |
| 90 | main.log.error( self.name + ": EOF exception found" ) |
| 91 | main.log.error( self.name + ": " + self.handle.before ) |
| 92 | main.cleanup() |
| 93 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 94 | except Exception: |
| 95 | main.log.exception( self.name + ": Uncaught exception!" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 96 | main.cleanup() |
| 97 | main.exit() |
| 98 | |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 99 | def startNet( self, topoFile='', args='', mnCmd='', timeout=120 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 100 | """ |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 101 | Description: |
| 102 | Starts Mininet accepts a topology(.py) file and/or an optional |
| 103 | argument, to start the mininet, as a parameter. |
| 104 | Can also send regular mininet command to load up desired topology. |
| 105 | Eg. Pass in a string 'sudo mn --topo=tree,3,3' to mnCmd |
| 106 | Options: |
| 107 | topoFile = file path for topology file (.py) |
| 108 | args = extra option added when starting the topology from the file |
| 109 | mnCmd = Mininet command use to start topology |
| 110 | Returns: |
| 111 | main.TRUE if the mininet starts successfully, main.FALSE |
| 112 | otherwise |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 114 | if self.handle: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 115 | # make sure old networks are cleaned up |
| 116 | main.log.info( self.name + |
| 117 | ": Clearing any residual state or processes" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 118 | self.handle.sendline( "sudo mn -c" ) |
| 119 | i = self.handle.expect( [ 'password\sfor\s', |
| 120 | 'Cleanup\scomplete', |
| 121 | pexpect.EOF, |
| 122 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 123 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 124 | if i == 0: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 125 | # Sudo asking for password |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 126 | main.log.info( self.name + ": Sending sudo password" ) |
| 127 | self.handle.sendline( self.pwd ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 128 | i = self.handle.expect( [ '%s:' % self.user, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 129 | '\$', |
| 130 | pexpect.EOF, |
| 131 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 132 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 133 | if i == 1: |
| 134 | main.log.info( self.name + ": Clean" ) |
| 135 | elif i == 2: |
| 136 | main.log.error( self.name + ": Connection terminated" ) |
| 137 | elif i == 3: # timeout |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 138 | main.log.error( self.name + ": Something while cleaning " + |
| 139 | "Mininet took too long... " ) |
| 140 | # Craft the string to start mininet |
| 141 | cmdString = "sudo " |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 142 | if not mnCmd: |
| 143 | if topoFile is None or topoFile == '': # If no file is given |
| 144 | main.log.info( self.name + ": building fresh Mininet" ) |
| 145 | cmdString += "mn " |
| 146 | if args is None or args == '': |
| 147 | # If no args given, use args from .topo file |
| 148 | args = self.options[ 'arg1' ] +\ |
| 149 | " " + self.options[ 'arg2' ] +\ |
| 150 | " --mac --controller " +\ |
| 151 | self.options[ 'controller' ] + " " +\ |
| 152 | self.options[ 'arg3' ] |
| 153 | else: # else only use given args |
| 154 | pass |
| 155 | # TODO: allow use of topo args and method args? |
| 156 | else: # Use given topology file |
| 157 | main.log.info( "Starting Mininet from topo file " + topoFile ) |
| 158 | cmdString += topoFile + " " |
| 159 | if args is None: |
| 160 | args = '' |
| 161 | # TODO: allow use of args from .topo file? |
| 162 | cmdString += args |
| 163 | else: |
| 164 | main.log.info( "Starting Mininet topology using '" + mnCmd + |
| 165 | "' command" ) |
| 166 | cmdString += mnCmd |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 167 | # Send the command and check if network started |
| 168 | self.handle.sendline( "" ) |
| 169 | self.handle.expect( '\$' ) |
| 170 | main.log.info( "Sending '" + cmdString + "' to " + self.name ) |
| 171 | self.handle.sendline( cmdString ) |
| 172 | while True: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 173 | i = self.handle.expect( [ 'mininet>', |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 174 | 'Exception', |
| 175 | '\*\*\*', |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 176 | pexpect.EOF, |
| 177 | pexpect.TIMEOUT ], |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 178 | timeout ) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 179 | if i == 0: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 180 | main.log.info( self.name + ": Mininet built" ) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 181 | return main.TRUE |
kelvin-onlab | ec228b8 | 2015-02-09 15:45:55 -0800 | [diff] [blame] | 182 | elif i == 1: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 183 | response = str( self.handle.before + |
| 184 | self.handle.after ) |
| 185 | self.handle.expect( '\$' ) |
| 186 | response += str( self.handle.before + |
| 187 | self.handle.after ) |
| 188 | main.log.error( |
| 189 | self.name + |
| 190 | ": Launching Mininet failed: " + response ) |
| 191 | return main.FALSE |
| 192 | elif i == 2: |
| 193 | self.handle.expect( [ "\n", |
| 194 | pexpect.EOF, |
| 195 | pexpect.TIMEOUT ], |
| 196 | timeout ) |
| 197 | main.log.info( self.handle.before ) |
| 198 | elif i == 3: |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 199 | main.log.error( self.name + ": Connection timeout" ) |
| 200 | return main.FALSE |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 201 | elif i == 4: # timeout |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 202 | main.log.error( |
| 203 | self.name + |
| 204 | ": Something took too long... " ) |
| 205 | return main.FALSE |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 206 | # Why did we hit this part? |
| 207 | main.log.error( "startNet did not return correctly" ) |
| 208 | return main.FASLE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 209 | else: # if no handle |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 210 | main.log.error( self.name + ": Connection failed to the host " + |
| 211 | self.user_name + "@" + self.ip_address ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 212 | main.log.error( self.name + ": Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 214 | |
kelvin-onlab | fccaafa | 2015-01-20 13:50:44 -0800 | [diff] [blame] | 215 | def numSwitchesNlinks( self, topoType, depth, fanout ): |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 216 | if topoType == 'tree': |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 217 | # In tree topology, if fanout arg is not given, by default it is 2 |
| 218 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 219 | fanout = 2 |
| 220 | k = 0 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 221 | count = 0 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 222 | while( k <= depth - 1 ): |
| 223 | count = count + pow( fanout, k ) |
| 224 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 225 | numSwitches = count |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 226 | while( k <= depth - 2 ): |
| 227 | # depth-2 gives you only core links and not considering |
| 228 | # edge links as seen by ONOS. If all the links including |
| 229 | # edge links are required, do depth-1 |
| 230 | count = count + pow( fanout, k ) |
| 231 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 232 | numLinks = count * fanout |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 233 | # print "num_switches for %s(%d,%d) = %d and links=%d" %( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 234 | # topoType,depth,fanout,numSwitches,numLinks ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 235 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 236 | elif topoType == 'linear': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 237 | # In linear topology, if fanout or numHostsPerSw is not given, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 238 | # by default it is 1 |
| 239 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 240 | fanout = 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | numSwitches = depth |
| 242 | numHostsPerSw = fanout |
| 243 | totalNumHosts = numSwitches * numHostsPerSw |
| 244 | numLinks = totalNumHosts + ( numSwitches - 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 245 | print "num_switches for %s(%d,%d) = %d and links=%d" %\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 246 | ( topoType, depth, fanout, numSwitches, numLinks ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 247 | topoDict = { "num_switches": int( numSwitches ), |
| 248 | "num_corelinks": int( numLinks ) } |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 249 | return topoDict |
| 250 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 251 | def calculateSwAndLinks( self ): |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 252 | """ |
| 253 | Calculate the number of switches and links in a topo.""" |
| 254 | # TODO: combine this function and numSwitchesNlinks |
| 255 | argList = self.options[ 'arg1' ].split( "," ) |
| 256 | topoArgList = argList[ 0 ].split( " " ) |
| 257 | argList = map( int, argList[ 1: ] ) |
| 258 | topoArgList = topoArgList[ 1: ] + argList |
| 259 | |
| 260 | topoDict = self.numSwitchesNlinks( *topoArgList ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 261 | return topoDict |
| 262 | |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 263 | def pingall( self, timeout=300, shortCircuit=False, acceptableFailed=0): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 264 | """ |
| 265 | Verifies the reachability of the hosts using pingall command. |
| 266 | Optional parameter timeout allows you to specify how long to |
| 267 | wait for pingall to complete |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 268 | Optional: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 269 | timeout(seconds) - How long to wait before breaking the pingall |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 270 | shortCircuit - Break the pingall based on the number of failed hosts |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 271 | ping |
| 272 | acceptableFailed - Set the number of acceptable failed pings for the |
| 273 | function to still return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 274 | Returns: |
| 275 | main.TRUE if pingall completes with no pings dropped |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 276 | otherwise main.FALSE |
| 277 | """ |
| 278 | import time |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 279 | try: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 280 | timeout = int( timeout ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 281 | if self.handle: |
| 282 | main.log.info( |
| 283 | self.name + |
| 284 | ": Checking reachabilty to the hosts using pingall" ) |
| 285 | response = "" |
| 286 | failedPings = 0 |
| 287 | returnValue = main.TRUE |
| 288 | self.handle.sendline( "pingall" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 289 | startTime = time.time() |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 290 | while True: |
| 291 | i = self.handle.expect( [ "mininet>","X", |
| 292 | pexpect.EOF, |
| 293 | pexpect.TIMEOUT ], |
| 294 | timeout ) |
| 295 | if i == 0: |
| 296 | main.log.info( self.name + ": pingall finished") |
| 297 | response += self.handle.before |
| 298 | break |
| 299 | elif i == 1: |
| 300 | response += self.handle.before + self.handle.after |
| 301 | failedPings = failedPings + 1 |
kelvin-onlab | d26a374 | 2015-04-06 15:31:16 -0700 | [diff] [blame] | 302 | if failedPings > acceptableFailed: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 303 | returnValue = main.FALSE |
| 304 | if shortCircuit: |
| 305 | main.log.error( self.name + |
| 306 | ": Aborting pingall - " |
| 307 | + str( failedPings ) + |
| 308 | " pings failed" ) |
| 309 | break |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 310 | if ( time.time() - startTime ) > timeout: |
| 311 | returnValue = main.FALSE |
| 312 | main.log.error( self.name + |
| 313 | ": Aborting pingall - " + |
| 314 | "Function took too long " ) |
| 315 | break |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 316 | elif i == 2: |
| 317 | main.log.error( self.name + |
| 318 | ": EOF exception found" ) |
| 319 | main.log.error( self.name + ": " + |
| 320 | self.handle.before ) |
| 321 | main.cleanup() |
| 322 | main.exit() |
| 323 | elif i == 3: |
| 324 | response += self.handle.before |
| 325 | main.log.error( self.name + |
| 326 | ": TIMEOUT exception found" ) |
| 327 | main.log.error( self.name + |
| 328 | ": " + |
| 329 | str( response ) ) |
| 330 | # NOTE: Send ctrl-c to make sure pingall is done |
| 331 | self.handle.sendline( "\x03" ) |
| 332 | self.handle.expect( "Interrupt" ) |
| 333 | self.handle.expect( "mininet>" ) |
| 334 | break |
| 335 | pattern = "Results\:" |
| 336 | main.log.info( "Pingall output: " + str( response ) ) |
| 337 | if re.search( pattern, response ): |
| 338 | main.log.info( self.name + ": Pingall finished with " |
| 339 | + str( failedPings ) + " failed pings" ) |
| 340 | return returnValue |
| 341 | else: |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 342 | # NOTE: Send ctrl-c to make sure pingall is done |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 343 | self.handle.sendline( "\x03" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 344 | self.handle.expect( "Interrupt" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 345 | self.handle.expect( "mininet>" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 346 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 347 | else: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 348 | main.log.error( self.name + ": Connection failed to the host" ) |
| 349 | main.cleanup() |
| 350 | main.exit() |
| 351 | except pexpect.TIMEOUT: |
| 352 | if response: |
| 353 | main.log.info( "Pingall output: " + str( response ) ) |
| 354 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 355 | return main.FALSE |
| 356 | except pexpect.EOF: |
| 357 | main.log.error( self.name + ": EOF exception found" ) |
| 358 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 359 | main.cleanup() |
| 360 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 361 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 362 | def fpingHost( self, **pingParams ): |
| 363 | """ |
| 364 | Uses the fping package for faster pinging... |
| 365 | *requires fping to be installed on machine running mininet""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 366 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 367 | command = args[ "SRC" ] + \ |
| 368 | " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ] |
| 369 | self.handle.sendline( command ) |
| 370 | self.handle.expect( |
| 371 | [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] ) |
| 372 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 373 | response = self.handle.before |
| 374 | if re.search( ":\s-", response ): |
| 375 | main.log.info( self.name + ": Ping fail" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 376 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 377 | elif re.search( ":\s\d{1,2}\.\d\d", response ): |
| 378 | main.log.info( self.name + ": Ping good!" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 379 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 380 | main.log.info( self.name + ": Install fping on mininet machine... " ) |
| 381 | main.log.info( self.name + ": \n---\n" + response ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 382 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 383 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 384 | def pingallHosts( self, hostList, pingType='ipv4' ): |
| 385 | """ |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 386 | Ping all specified hosts with a specific ping type |
| 387 | |
| 388 | Acceptable pingTypes: |
| 389 | - 'ipv4' |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 390 | - 'ipv6' |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 391 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 392 | Acceptable hostList: |
| 393 | - ['h1','h2','h3','h4'] |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 394 | |
| 395 | Returns main.TRUE if all hosts specified can reach |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 396 | each other |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 397 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 398 | Returns main.FALSE if one or more of hosts specified |
| 399 | cannot reach each other""" |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 400 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 401 | if pingType == "ipv4": |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 402 | cmd = " ping -c 1 -i 1 -W 8 " |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 403 | elif pingType == "ipv6": |
| 404 | cmd = " ping6 -c 1 -i 1 -W 8 " |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 405 | else: |
| 406 | main.log.warn( "Invalid pingType specified" ) |
| 407 | return |
| 408 | |
| 409 | try: |
| 410 | main.log.info( "Testing reachability between specified hosts" ) |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 411 | |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 412 | isReachable = main.TRUE |
| 413 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 414 | for host in hostList: |
| 415 | listIndex = hostList.index(host) |
| 416 | # List of hosts to ping other than itself |
| 417 | pingList = hostList[:listIndex] + hostList[(listIndex+1):] |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 418 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 419 | for temp in pingList: |
| 420 | # Current host pings all other hosts specified |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 421 | pingCmd = str(host) + cmd + str(temp) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 422 | self.handle.sendline( pingCmd ) |
| 423 | i = self.handle.expect( [ pingCmd, pexpect.TIMEOUT ] ) |
| 424 | j = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
| 425 | response = self.handle.before |
| 426 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 427 | main.log.info( str(host) + " -> " + str(temp) ) |
| 428 | else: |
| 429 | main.log.info( str(host) + " -> X ("+str(temp)+") " |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 430 | " Destination Unreachable" ) |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 431 | # One of the host to host pair is unreachable |
| 432 | isReachable = main.FALSE |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 433 | |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 434 | return isReachable |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 435 | |
| 436 | except pexpect.EOF: |
| 437 | main.log.error( self.name + ": EOF exception found" ) |
| 438 | main.log.error( self.name + ": " + self.handle.before ) |
| 439 | main.cleanup() |
| 440 | main.exit() |
| 441 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 442 | def pingHost( self, **pingParams ): |
| 443 | """ |
| 444 | Ping from one mininet host to another |
| 445 | Currently the only supported Params: SRC and TARGET""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 446 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 447 | command = args[ "SRC" ] + " ping " + \ |
| 448 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 449 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 450 | main.log.info( "Sending: " + command ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 451 | self.handle.sendline( command ) |
| 452 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 453 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 454 | main.log.error( |
| 455 | self.name + |
| 456 | ": timeout when waiting for response from mininet" ) |
| 457 | main.log.error( "response: " + str( self.handle.before ) ) |
| 458 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 459 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 460 | main.log.error( |
| 461 | self.name + |
| 462 | ": timeout when waiting for response from mininet" ) |
| 463 | main.log.error( "response: " + str( self.handle.before ) ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 464 | response = self.handle.before |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 465 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 466 | main.log.error( self.name + ": EOF exception found" ) |
| 467 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 468 | main.cleanup() |
| 469 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 470 | main.log.info( self.name + ": Ping Response: " + response ) |
| 471 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 472 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 473 | main.lastResult = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 474 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 475 | else: |
| 476 | main.log.error( |
| 477 | self.name + |
| 478 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 479 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 480 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 481 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 482 | def checkIP( self, host ): |
| 483 | """ |
| 484 | Verifies the host's ip configured or not.""" |
| 485 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 486 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 487 | response = self.execute( |
| 488 | cmd=host + |
| 489 | " ifconfig", |
| 490 | prompt="mininet>", |
| 491 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 492 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 493 | main.log.error( self.name + ": EOF exception found" ) |
| 494 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 495 | main.cleanup() |
| 496 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 497 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 498 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 499 | "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\ |
| 500 | "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\ |
| 501 | "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\ |
| 502 | "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\ |
| 503 | "[0-9]|25[0-5]|[0-9]{1,2})" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 504 | # pattern = "inet addr:10.0.0.6" |
| 505 | if re.search( pattern, response ): |
| 506 | main.log.info( self.name + ": Host Ip configured properly" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 507 | return main.TRUE |
| 508 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 509 | main.log.error( self.name + ": Host IP not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 510 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 511 | else: |
| 512 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 513 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 514 | def verifySSH( self, **connectargs ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 515 | # FIXME: Who uses this and what is the purpose? seems very specific |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 516 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 517 | response = self.execute( |
| 518 | cmd="h1 /usr/sbin/sshd -D&", |
| 519 | prompt="mininet>", |
| 520 | timeout=10 ) |
| 521 | response = self.execute( |
| 522 | cmd="h4 /usr/sbin/sshd -D&", |
| 523 | prompt="mininet>", |
| 524 | timeout=10 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 525 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 526 | vars( self )[ key ] = connectargs[ key ] |
| 527 | response = self.execute( |
| 528 | cmd="xterm h1 h4 ", |
| 529 | prompt="mininet>", |
| 530 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 531 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 532 | main.log.error( self.name + ": EOF exception found" ) |
| 533 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 534 | main.cleanup() |
| 535 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 536 | import time |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 537 | time.sleep( 20 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 538 | if self.flag == 0: |
| 539 | self.flag = 1 |
| 540 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 541 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 542 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 543 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 544 | def moveHost( self, host, oldSw, newSw, ): |
| 545 | """ |
| 546 | Moves a host from one switch to another on the fly |
| 547 | Note: The intf between host and oldSw when detached |
| 548 | using detach(), will still show up in the 'net' |
| 549 | cmd, because switch.detach() doesn't affect switch.intfs[] |
| 550 | (which is correct behavior since the interfaces |
| 551 | haven't moved). |
| 552 | """ |
| 553 | if self.handle: |
| 554 | try: |
| 555 | # Bring link between oldSw-host down |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 556 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\ |
| 557 | "'," + "'down')" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 558 | print "cmd1= ", cmd |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 559 | response = self.execute( cmd=cmd, |
| 560 | prompt="mininet>", |
| 561 | timeout=10 ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 562 | |
| 563 | # Determine hostintf and Oldswitchintf |
| 564 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 565 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 566 | print "cmd2= ", cmd |
| 567 | self.handle.sendline( cmd ) |
| 568 | self.handle.expect( "mininet>" ) |
| 569 | |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 570 | # Determine ip and mac address of the host-oldSw interface |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 571 | cmd = "px ipaddr = hintf.IP()" |
| 572 | print "cmd3= ", cmd |
| 573 | self.handle.sendline( cmd ) |
| 574 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 575 | |
| 576 | cmd = "px macaddr = hintf.MAC()" |
| 577 | print "cmd3= ", cmd |
| 578 | self.handle.sendline( cmd ) |
| 579 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 580 | |
| 581 | # Detach interface between oldSw-host |
| 582 | cmd = "px " + oldSw + ".detach( sintf )" |
| 583 | print "cmd4= ", cmd |
| 584 | self.handle.sendline( cmd ) |
| 585 | self.handle.expect( "mininet>" ) |
| 586 | |
| 587 | # Add link between host-newSw |
| 588 | cmd = "py net.addLink(" + host + "," + newSw + ")" |
| 589 | print "cmd5= ", cmd |
| 590 | self.handle.sendline( cmd ) |
| 591 | self.handle.expect( "mininet>" ) |
| 592 | |
| 593 | # Determine hostintf and Newswitchintf |
| 594 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 595 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 596 | print "cmd6= ", cmd |
| 597 | self.handle.sendline( cmd ) |
| 598 | self.handle.expect( "mininet>" ) |
| 599 | |
| 600 | # Attach interface between newSw-host |
| 601 | cmd = "px " + newSw + ".attach( sintf )" |
| 602 | print "cmd3= ", cmd |
| 603 | self.handle.sendline( cmd ) |
| 604 | self.handle.expect( "mininet>" ) |
| 605 | |
| 606 | # Set ipaddress of the host-newSw interface |
| 607 | cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)" |
| 608 | print "cmd7 = ", cmd |
| 609 | self.handle.sendline( cmd ) |
| 610 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 611 | |
| 612 | # Set macaddress of the host-newSw interface |
| 613 | cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)" |
| 614 | print "cmd8 = ", cmd |
| 615 | self.handle.sendline( cmd ) |
| 616 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 617 | |
| 618 | cmd = "net" |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 619 | print "cmd9 = ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 620 | self.handle.sendline( cmd ) |
| 621 | self.handle.expect( "mininet>" ) |
| 622 | print "output = ", self.handle.before |
| 623 | |
| 624 | # Determine ipaddress of the host-newSw interface |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 625 | cmd = host + " ifconfig" |
| 626 | print "cmd10= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 627 | self.handle.sendline( cmd ) |
| 628 | self.handle.expect( "mininet>" ) |
| 629 | print "ifconfig o/p = ", self.handle.before |
| 630 | |
| 631 | return main.TRUE |
| 632 | except pexpect.EOF: |
| 633 | main.log.error( self.name + ": EOF exception found" ) |
| 634 | main.log.error( self.name + ": " + self.handle.before ) |
| 635 | return main.FALSE |
| 636 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 637 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 638 | """ |
| 639 | Changes the ip address of a host on the fly |
| 640 | Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 641 | if self.handle: |
| 642 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 643 | cmd = host + " ifconfig " + intf + " " + \ |
| 644 | newIP + " " + 'netmask' + " " + newNetmask |
| 645 | self.handle.sendline( cmd ) |
| 646 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 647 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 648 | main.log.info( "response = " + response ) |
| 649 | main.log.info( |
| 650 | "Ip of host " + |
| 651 | host + |
| 652 | " changed to new IP " + |
| 653 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 654 | return main.TRUE |
| 655 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 656 | main.log.error( self.name + ": EOF exception found" ) |
| 657 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 658 | return main.FALSE |
| 659 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 660 | def changeDefaultGateway( self, host, newGW ): |
| 661 | """ |
| 662 | Changes the default gateway of a host |
| 663 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 664 | if self.handle: |
| 665 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 666 | cmd = host + " route add default gw " + newGW |
| 667 | self.handle.sendline( cmd ) |
| 668 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 669 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 670 | main.log.info( "response = " + response ) |
| 671 | main.log.info( |
| 672 | "Default gateway of host " + |
| 673 | host + |
| 674 | " changed to " + |
| 675 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 676 | return main.TRUE |
| 677 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 678 | main.log.error( self.name + ": EOF exception found" ) |
| 679 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 680 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 681 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 682 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 683 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 684 | Changes the mac address of a gateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 685 | if self.handle: |
| 686 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 687 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 688 | cmd = host + " arp -s " + GW + " " + macaddr |
| 689 | self.handle.sendline( cmd ) |
| 690 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 691 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 692 | main.log.info( "response = " + response ) |
| 693 | main.log.info( |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 694 | "Mac address of gateway " + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 695 | GW + |
| 696 | " changed to " + |
| 697 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 698 | return main.TRUE |
| 699 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 700 | main.log.error( self.name + ": EOF exception found" ) |
| 701 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 702 | return main.FALSE |
| 703 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 704 | def verifyStaticGWandMAC( self, host ): |
| 705 | """ |
| 706 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 707 | if self.handle: |
| 708 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 709 | # h1 arp -an |
| 710 | cmd = host + " arp -an " |
| 711 | self.handle.sendline( cmd ) |
| 712 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 713 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 714 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 715 | return main.TRUE |
| 716 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 717 | main.log.error( self.name + ": EOF exception found" ) |
| 718 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 719 | return main.FALSE |
| 720 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 721 | def getMacAddress( self, host ): |
| 722 | """ |
| 723 | Verifies the host's ip configured or not.""" |
| 724 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 725 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 726 | response = self.execute( |
| 727 | cmd=host + |
| 728 | " ifconfig", |
| 729 | prompt="mininet>", |
| 730 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 731 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 732 | main.log.error( self.name + ": EOF exception found" ) |
| 733 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 734 | main.cleanup() |
| 735 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 736 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 737 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | macAddressSearch = re.search( pattern, response, re.I ) |
| 739 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 740 | main.log.info( |
| 741 | self.name + |
| 742 | ": Mac-Address of Host " + |
| 743 | host + |
| 744 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 745 | macAddress ) |
| 746 | return macAddress |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 747 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 748 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 749 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 750 | def getInterfaceMACAddress( self, host, interface ): |
| 751 | """ |
| 752 | Return the IP address of the interface on the given host""" |
| 753 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 754 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 755 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 756 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 757 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 758 | main.log.error( self.name + ": EOF exception found" ) |
| 759 | main.log.error( self.name + ": " + self.handle.before ) |
| 760 | main.cleanup() |
| 761 | main.exit() |
| 762 | |
| 763 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 764 | macAddressSearch = re.search( pattern, response, re.I ) |
| 765 | if macAddressSearch is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 766 | main.log.info( "No mac address found in %s" % response ) |
| 767 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 768 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 769 | main.log.info( |
| 770 | "Mac-Address of " + |
| 771 | host + |
| 772 | ":" + |
| 773 | interface + |
| 774 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 775 | macAddress ) |
| 776 | return macAddress |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 777 | else: |
| 778 | main.log.error( "Connection failed to the host" ) |
| 779 | |
| 780 | def getIPAddress( self, host ): |
| 781 | """ |
| 782 | Verifies the host's ip configured or not.""" |
| 783 | if self.handle: |
| 784 | try: |
| 785 | response = self.execute( |
| 786 | cmd=host + |
| 787 | " ifconfig", |
| 788 | prompt="mininet>", |
| 789 | timeout=10 ) |
| 790 | except pexpect.EOF: |
| 791 | main.log.error( self.name + ": EOF exception found" ) |
| 792 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 793 | main.cleanup() |
| 794 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 795 | |
| 796 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 797 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 798 | main.log.info( |
| 799 | self.name + |
| 800 | ": IP-Address of Host " + |
| 801 | host + |
| 802 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 803 | ipAddressSearch.group( 1 ) ) |
| 804 | return ipAddressSearch.group( 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 805 | else: |
| 806 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 807 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 808 | def getSwitchDPID( self, switch ): |
| 809 | """ |
| 810 | return the datapath ID of the switch""" |
| 811 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 812 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 813 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 814 | response = self.execute( |
| 815 | cmd=cmd, |
| 816 | prompt="mininet>", |
| 817 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 818 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 819 | main.log.error( self.name + ": EOF exception found" ) |
| 820 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 821 | main.cleanup() |
| 822 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 823 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 824 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 825 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 826 | main.log.info( |
| 827 | "Couldn't find DPID for switch %s, found: %s" % |
| 828 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 829 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 830 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 831 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 832 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 833 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 834 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 835 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 836 | self.handle.sendline( "" ) |
| 837 | self.expect( "mininet>" ) |
| 838 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 839 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 840 | response = self.execute( |
| 841 | cmd=cmd, |
| 842 | prompt="mininet>", |
| 843 | timeout=10 ) |
| 844 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 845 | response = self.handle.before |
| 846 | return response |
| 847 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 848 | main.log.error( self.name + ": EOF exception found" ) |
| 849 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 850 | main.cleanup() |
| 851 | main.exit() |
| 852 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 853 | def getInterfaces( self, node ): |
| 854 | """ |
| 855 | return information dict about interfaces connected to the node""" |
| 856 | if self.handle: |
| 857 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 858 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 859 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 860 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 861 | response = self.execute( |
| 862 | cmd=cmd, |
| 863 | prompt="mininet>", |
| 864 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 865 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 866 | main.log.error( self.name + ": EOF exception found" ) |
| 867 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 868 | main.cleanup() |
| 869 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 870 | return response |
| 871 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 872 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 873 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 874 | def dump( self ): |
| 875 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 876 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 877 | response = self.execute( |
| 878 | cmd='dump', |
| 879 | prompt='mininet>', |
| 880 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 881 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 882 | main.log.error( self.name + ": EOF exception found" ) |
| 883 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 884 | main.cleanup() |
| 885 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 886 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 887 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 888 | def intfs( self ): |
| 889 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 890 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 891 | response = self.execute( |
| 892 | cmd='intfs', |
| 893 | prompt='mininet>', |
| 894 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 895 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 896 | main.log.error( self.name + ": EOF exception found" ) |
| 897 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 898 | main.cleanup() |
| 899 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 900 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 901 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 902 | def net( self ): |
| 903 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 904 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 905 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 906 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 907 | main.log.error( self.name + ": EOF exception found" ) |
| 908 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 909 | main.cleanup() |
| 910 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 911 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 912 | |
| 913 | def iperf( self, host1, host2 ): |
| 914 | main.log.info( |
| 915 | self.name + |
| 916 | ": Simple iperf TCP test between two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 917 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 918 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 919 | self.handle.sendline( cmd1 ) |
| 920 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 921 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 922 | if re.search( 'Results:', response ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 923 | main.log.info( self.name + ": iperf test successful" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 924 | return main.TRUE |
| 925 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 926 | main.log.error( self.name + ": iperf test failed" ) |
| 927 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 928 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 929 | main.log.error( self.name + ": EOF exception found" ) |
| 930 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 931 | main.cleanup() |
| 932 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 933 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 934 | def iperfudp( self ): |
| 935 | main.log.info( |
| 936 | self.name + |
| 937 | ": Simple iperf TCP test between two " + |
| 938 | "(optionally specified) hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 939 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 940 | response = self.execute( |
| 941 | cmd='iperfudp', |
| 942 | prompt='mininet>', |
| 943 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 944 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 945 | main.log.error( self.name + ": EOF exception found" ) |
| 946 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 947 | main.cleanup() |
| 948 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 949 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 950 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 951 | def nodes( self ): |
| 952 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 953 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 954 | response = self.execute( |
| 955 | cmd='nodes', |
| 956 | prompt='mininet>', |
| 957 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 958 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 959 | main.log.error( self.name + ": EOF exception found" ) |
| 960 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 961 | main.cleanup() |
| 962 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 963 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 964 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 965 | def pingpair( self ): |
| 966 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 967 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 968 | response = self.execute( |
| 969 | cmd='pingpair', |
| 970 | prompt='mininet>', |
| 971 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 972 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 973 | main.log.error( self.name + ": EOF exception found" ) |
| 974 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 975 | main.cleanup() |
| 976 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 977 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 978 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 979 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 980 | main.lastResult = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 981 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 982 | else: |
| 983 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 984 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 985 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 986 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 987 | def link( self, **linkargs ): |
| 988 | """ |
| 989 | Bring link( s ) between two nodes up or down""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 990 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 991 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 992 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 993 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 994 | main.log.info( |
| 995 | "Bring link between '" + |
| 996 | end1 + |
| 997 | "' and '" + |
| 998 | end2 + |
| 999 | "' '" + |
| 1000 | option + |
| 1001 | "'" ) |
| 1002 | command = "link " + \ |
| 1003 | str( end1 ) + " " + str( end2 ) + " " + str( option ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1004 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1005 | self.handle.sendline( command ) |
| 1006 | self.handle.expect( "mininet>" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1007 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1008 | main.log.error( self.name + ": EOF exception found" ) |
| 1009 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1010 | main.cleanup() |
| 1011 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1012 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1013 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1014 | def yank( self, **yankargs ): |
| 1015 | """ |
| 1016 | yank a mininet switch interface to a host""" |
| 1017 | main.log.info( 'Yank the switch interface attached to a host' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1018 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1019 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1020 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1021 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1022 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1023 | response = self.execute( |
| 1024 | cmd=command, |
| 1025 | prompt="mininet>", |
| 1026 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1027 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1028 | main.log.error( self.name + ": EOF exception found" ) |
| 1029 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1030 | main.cleanup() |
| 1031 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 1032 | return main.TRUE |
| 1033 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1034 | def plug( self, **plugargs ): |
| 1035 | """ |
| 1036 | plug the yanked mininet switch interface to a switch""" |
| 1037 | main.log.info( 'Plug the switch interface attached to a switch' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1038 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1039 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1040 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1041 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1042 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1043 | response = self.execute( |
| 1044 | cmd=command, |
| 1045 | prompt="mininet>", |
| 1046 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1047 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1048 | main.log.error( self.name + ": EOF exception found" ) |
| 1049 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1050 | main.cleanup() |
| 1051 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1052 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1053 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1054 | def dpctl( self, **dpctlargs ): |
| 1055 | """ |
| 1056 | Run dpctl command on all switches.""" |
| 1057 | main.log.info( 'Run dpctl command on all switches' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1058 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1059 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 1060 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 1061 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 1062 | try: |
| 1063 | response = self.execute( |
| 1064 | cmd=command, |
| 1065 | prompt="mininet>", |
| 1066 | timeout=10 ) |
| 1067 | except pexpect.EOF: |
| 1068 | main.log.error( self.name + ": EOF exception found" ) |
| 1069 | main.log.error( self.name + ": " + self.handle.before ) |
| 1070 | main.cleanup() |
| 1071 | main.exit() |
| 1072 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1073 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1074 | def getVersion( self ): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1075 | #FIXME: What uses this? This should be refactored to get |
| 1076 | # version from MN and not some other file |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1077 | fileInput = path + '/lib/Mininet/INSTALL' |
| 1078 | version = super( Mininet, self ).getVersion() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1079 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1080 | for line in open( fileInput, 'r' ).readlines(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1081 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1082 | if result: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1083 | version = result.group( 0 ) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1084 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1085 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1086 | def getSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1087 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1088 | Parameters: |
| 1089 | sw: The name of an OVS switch. Example "s1" |
| 1090 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1091 | The output of the command from the mininet cli |
| 1092 | or main.FALSE on timeout""" |
| 1093 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1094 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1095 | response = self.execute( |
| 1096 | cmd=command, |
| 1097 | prompt="mininet>", |
| 1098 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1099 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1100 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1101 | else: |
| 1102 | return main.FALSE |
| 1103 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1104 | main.log.error( self.name + ": EOF exception found" ) |
| 1105 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1106 | main.cleanup() |
| 1107 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1108 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1109 | def assignSwController( self, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1110 | """ |
| 1111 | count is only needed if there is more than 1 controller""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1112 | args = utilities.parse_args( [ "COUNT" ], **kwargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1113 | count = args[ "COUNT" ] if args != {} else 1 |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1114 | |
| 1115 | argstring = "SW" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1116 | for j in range( count ): |
| 1117 | argstring = argstring + ",IP" + \ |
| 1118 | str( j + 1 ) + ",PORT" + str( j + 1 ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1119 | args = utilities.parse_args( argstring.split( "," ), **kwargs ) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1120 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1121 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1122 | ptcpA = int( args[ "PORT1" ] ) + \ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1123 | int( sw ) if args[ "PORT1" ] is not None else "" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1124 | ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else "" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1125 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1126 | command = "sh ovs-vsctl set-controller s" + \ |
| 1127 | str( sw ) + " " + ptcpB + " " |
| 1128 | for j in range( count ): |
| 1129 | i = j + 1 |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1130 | args = utilities.parse_args( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1131 | [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs ) |
| 1132 | ip = args[ |
| 1133 | "IP" + |
| 1134 | str( i ) ] if args[ |
| 1135 | "IP" + |
| 1136 | str( i ) ] is not None else "" |
| 1137 | port = args[ |
| 1138 | "PORT" + |
| 1139 | str( i ) ] if args[ |
| 1140 | "PORT" + |
| 1141 | str( i ) ] is not None else "" |
| 1142 | tcp = "tcp:" + str( ip ) + ":" + str( port ) + \ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1143 | " " if ip != "" else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1144 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1145 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1146 | self.execute( cmd=command, prompt="mininet>", timeout=5 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1147 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1148 | main.log.error( self.name + ": EOF exception found" ) |
| 1149 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1150 | main.cleanup() |
| 1151 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1152 | except Exception: |
| 1153 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1154 | main.cleanup() |
| 1155 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1156 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1157 | def deleteSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1158 | """ |
| 1159 | Removes the controller target from sw""" |
| 1160 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1161 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1162 | response = self.execute( |
| 1163 | cmd=command, |
| 1164 | prompt="mininet>", |
| 1165 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1166 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1167 | main.log.error( self.name + ": EOF exception found" ) |
| 1168 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1169 | main.cleanup() |
| 1170 | main.exit() |
| 1171 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1172 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1173 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1174 | def addSwitch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1175 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1176 | adds a switch to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1177 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1178 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1179 | NOTE: cannot currently specify what type of switch |
| 1180 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1181 | sw = name of the new switch as a string |
| 1182 | optional keywords: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1183 | dpid = "dpid" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1184 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1185 | """ |
| 1186 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1187 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1188 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1189 | response = self.execute( |
| 1190 | cmd=command, |
| 1191 | prompt="mininet>", |
| 1192 | timeout=10 ) |
| 1193 | if re.search( "already exists!", response ): |
| 1194 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1195 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1196 | elif re.search( "Error", response ): |
| 1197 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1198 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1199 | elif re.search( "usage:", response ): |
| 1200 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1201 | return main.FALSE |
| 1202 | else: |
| 1203 | return main.TRUE |
| 1204 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1205 | main.log.error( self.name + ": EOF exception found" ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 1206 | main.log.error(self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1207 | main.cleanup() |
| 1208 | main.exit() |
| 1209 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1210 | def delSwitch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1211 | """ |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1212 | delete a switch from the mininet topology |
| 1213 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1214 | dynamic_topo branch |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1215 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1216 | sw = name of the switch as a string |
| 1217 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1218 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1219 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1220 | response = self.execute( |
| 1221 | cmd=command, |
| 1222 | prompt="mininet>", |
| 1223 | timeout=10 ) |
| 1224 | if re.search( "no switch named", response ): |
| 1225 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1226 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1227 | elif re.search( "Error", response ): |
| 1228 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1229 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1230 | elif re.search( "usage:", response ): |
| 1231 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1232 | return main.FALSE |
| 1233 | else: |
| 1234 | return main.TRUE |
| 1235 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1236 | main.log.error( self.name + ": EOF exception found" ) |
| 1237 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1238 | main.cleanup() |
| 1239 | main.exit() |
| 1240 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1241 | def addLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1242 | """ |
| 1243 | add a link to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1244 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1245 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1246 | NOTE: cannot currently specify what type of link |
| 1247 | required params: |
| 1248 | node1 = the string node name of the first endpoint of the link |
| 1249 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1250 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1251 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1252 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1253 | response = self.execute( |
| 1254 | cmd=command, |
| 1255 | prompt="mininet>", |
| 1256 | timeout=10 ) |
| 1257 | if re.search( "doesnt exist!", response ): |
| 1258 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1259 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1260 | elif re.search( "Error", response ): |
| 1261 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1262 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1263 | elif re.search( "usage:", response ): |
| 1264 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1265 | return main.FALSE |
| 1266 | else: |
| 1267 | return main.TRUE |
| 1268 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1269 | main.log.error( self.name + ": EOF exception found" ) |
| 1270 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1271 | main.cleanup() |
| 1272 | main.exit() |
| 1273 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1274 | def delLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1275 | """ |
| 1276 | delete a link from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1277 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1278 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1279 | required params: |
| 1280 | node1 = the string node name of the first endpoint of the link |
| 1281 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1282 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1283 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1284 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1285 | response = self.execute( |
| 1286 | cmd=command, |
| 1287 | prompt="mininet>", |
| 1288 | timeout=10 ) |
| 1289 | if re.search( "no node named", response ): |
| 1290 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1291 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1292 | elif re.search( "Error", response ): |
| 1293 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1294 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1295 | elif re.search( "usage:", response ): |
| 1296 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1297 | return main.FALSE |
| 1298 | else: |
| 1299 | return main.TRUE |
| 1300 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1301 | main.log.error( self.name + ": EOF exception found" ) |
| 1302 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1303 | main.cleanup() |
| 1304 | main.exit() |
| 1305 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1306 | def addHost( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1307 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1308 | Add a host to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1309 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1310 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1311 | NOTE: cannot currently specify what type of host |
| 1312 | required params: |
| 1313 | hostname = the string hostname |
| 1314 | optional key-value params |
| 1315 | switch = "switch name" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1316 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1317 | """ |
| 1318 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1319 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1320 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1321 | response = self.execute( |
| 1322 | cmd=command, |
| 1323 | prompt="mininet>", |
| 1324 | timeout=10 ) |
| 1325 | if re.search( "already exists!", response ): |
| 1326 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1327 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1328 | elif re.search( "doesnt exists!", response ): |
| 1329 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1330 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1331 | elif re.search( "Error", response ): |
| 1332 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1333 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1334 | elif re.search( "usage:", response ): |
| 1335 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1336 | return main.FALSE |
| 1337 | else: |
| 1338 | return main.TRUE |
| 1339 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1340 | main.log.error( self.name + ": EOF exception found" ) |
| 1341 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1342 | main.cleanup() |
| 1343 | main.exit() |
| 1344 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1345 | def delHost( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1346 | """ |
| 1347 | delete a host from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1348 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1349 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1350 | NOTE: this uses a custom mn function |
| 1351 | required params: |
| 1352 | hostname = the string hostname |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1353 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1354 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1355 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1356 | response = self.execute( |
| 1357 | cmd=command, |
| 1358 | prompt="mininet>", |
| 1359 | timeout=10 ) |
| 1360 | if re.search( "no host named", response ): |
| 1361 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1362 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1363 | elif re.search( "Error", response ): |
| 1364 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1365 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1366 | elif re.search( "usage:", response ): |
| 1367 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1368 | return main.FALSE |
| 1369 | else: |
| 1370 | return main.TRUE |
| 1371 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1372 | main.log.error( self.name + ": EOF exception found" ) |
| 1373 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1374 | main.cleanup() |
| 1375 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1376 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1377 | def disconnect( self ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1378 | """ |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1379 | Called at the end of the test to stop the mininet and |
| 1380 | disconnect the handle. |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1381 | """ |
| 1382 | self.handle.sendline('') |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1383 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1384 | timeout=2) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1385 | response = main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1386 | if i == 0: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1387 | response = self.stopNet() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1388 | elif i == 1: |
| 1389 | return main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1390 | # print "Disconnecting Mininet" |
| 1391 | if self.handle: |
| 1392 | self.handle.sendline( "exit" ) |
| 1393 | self.handle.expect( "exit" ) |
| 1394 | self.handle.expect( "(.*)" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1395 | else: |
| 1396 | main.log.error( "Connection failed to the host" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1397 | return response |
| 1398 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1399 | def stopNet( self, fileName = "", timeout=5): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1400 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1401 | Stops mininet. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1402 | Returns main.TRUE if the mininet successfully stops and |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1403 | main.FALSE if the pexpect handle does not exist. |
| 1404 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1405 | Will cleanup and exit the test if mininet fails to stop |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1406 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1407 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1408 | main.log.info( self.name + ": Stopping mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1409 | response = '' |
| 1410 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1411 | try: |
kelvin-onlab | 26bc17f | 2015-02-06 14:08:59 -0800 | [diff] [blame] | 1412 | self.handle.sendline("") |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1413 | i = self.handle.expect( [ 'mininet>', |
| 1414 | '\$', |
| 1415 | pexpect.EOF, |
| 1416 | pexpect.TIMEOUT ], |
| 1417 | timeout ) |
| 1418 | if i == 0: |
| 1419 | main.log.info( "Exiting mininet..." ) |
| 1420 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1421 | response = self.execute( |
| 1422 | cmd="exit", |
| 1423 | prompt="(.*)", |
| 1424 | timeout=120 ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1425 | main.log.info( self.name + ": Stopped") |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1426 | self.handle.sendline( "sudo mn -c" ) |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 1427 | response = main.TRUE |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1428 | |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1429 | if i == 1: |
| 1430 | main.log.info( " Mininet trying to exit while not " + |
| 1431 | "in the mininet prompt" ) |
| 1432 | elif i == 2: |
| 1433 | main.log.error( "Something went wrong exiting mininet" ) |
| 1434 | elif i == 3: # timeout |
| 1435 | main.log.error( "Something went wrong exiting mininet " + |
| 1436 | "TIMEOUT" ) |
| 1437 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1438 | if fileName: |
| 1439 | self.handle.sendline("") |
| 1440 | self.handle.expect('\$') |
| 1441 | self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`") |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1442 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1443 | main.log.error( self.name + ": EOF exception found" ) |
| 1444 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1445 | main.cleanup() |
| 1446 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1447 | else: |
| 1448 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1449 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1450 | return response |
| 1451 | |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 1452 | def arping( self, host="", ip="10.128.20.211", ethDevice="" ): |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1453 | """ |
| 1454 | Description: |
| 1455 | Sends arp message from mininet host for hosts discovery |
| 1456 | Required: |
| 1457 | host - hosts name |
| 1458 | Optional: |
| 1459 | ip - ip address that does not exist in the network so there would |
| 1460 | be no reply. |
| 1461 | """ |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 1462 | if ethDevice: |
| 1463 | ethDevice = '-I ' + ethDevice + ' ' |
| 1464 | cmd = " py " + host + ".cmd(\"arping -c 1 " + ethDevice + ip + "\")" |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1465 | try: |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1466 | main.log.warn( "Sending: " + cmd ) |
| 1467 | self.handle.sendline( cmd ) |
| 1468 | response = self.handle.before |
| 1469 | self.handle.sendline( "" ) |
| 1470 | self.handle.expect( "mininet>" ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1471 | return main.TRUE |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1472 | |
| 1473 | except pexpect.EOF: |
| 1474 | main.log.error( self.name + ": EOF exception found" ) |
| 1475 | main.log.error( self.name + ": " + self.handle.before ) |
| 1476 | main.cleanup() |
| 1477 | main.exit() |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1478 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1479 | def decToHex( self, num ): |
| 1480 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1481 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1482 | def getSwitchFlowCount( self, switch ): |
| 1483 | """ |
| 1484 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1485 | if self.handle: |
| 1486 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 1487 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1488 | response = self.execute( |
| 1489 | cmd=cmd, |
| 1490 | prompt="mininet>", |
| 1491 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1492 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1493 | main.log.error( self.name + ": EOF exception found" ) |
| 1494 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1495 | main.cleanup() |
| 1496 | main.exit() |
| 1497 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1498 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1499 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1500 | main.log.info( |
| 1501 | "Couldn't find flows on switch %s, found: %s" % |
| 1502 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1503 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1504 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1505 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1506 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1507 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1508 | def checkFlows( self, sw, dumpFormat=None ): |
| 1509 | if dumpFormat: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1510 | command = "sh ovs-ofctl -F " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1511 | dumpFormat + " dump-flows " + str( sw ) |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 1512 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1513 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1514 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1515 | response = self.execute( |
| 1516 | cmd=command, |
| 1517 | prompt="mininet>", |
| 1518 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1519 | return response |
| 1520 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1521 | main.log.error( self.name + ": EOF exception found" ) |
| 1522 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1523 | main.cleanup() |
| 1524 | main.exit() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1525 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1526 | def startTcpdump( self, filename, intf="eth0", port="port 6633" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1527 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1528 | Runs tpdump on an interface and saves the file |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1529 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1530 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1531 | self.handle.sendline( "" ) |
| 1532 | self.handle.expect( "mininet>" ) |
| 1533 | self.handle.sendline( |
| 1534 | "sh sudo tcpdump -n -i " + |
| 1535 | intf + |
| 1536 | " " + |
| 1537 | port + |
| 1538 | " -w " + |
| 1539 | filename.strip() + |
| 1540 | " &" ) |
| 1541 | self.handle.sendline( "" ) |
| 1542 | i = self.handle.expect( [ 'No\ssuch\device', |
| 1543 | 'listening\son', |
| 1544 | pexpect.TIMEOUT, |
| 1545 | "mininet>" ], |
| 1546 | timeout=10 ) |
| 1547 | main.log.warn( self.handle.before + self.handle.after ) |
| 1548 | self.handle.sendline( "" ) |
| 1549 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1550 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1551 | main.log.error( |
| 1552 | self.name + |
| 1553 | ": tcpdump - No such device exists. " + |
| 1554 | "tcpdump attempted on: " + |
| 1555 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1556 | return main.FALSE |
| 1557 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1558 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1559 | return main.TRUE |
| 1560 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1561 | main.log.error( |
| 1562 | self.name + |
| 1563 | ": tcpdump command timed out! Check interface name," + |
| 1564 | " given interface was: " + |
| 1565 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1566 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1567 | elif i == 3: |
| 1568 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1569 | return main.TRUE |
| 1570 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1571 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1572 | return main.FALSE |
| 1573 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1574 | main.log.error( self.name + ": EOF exception found" ) |
| 1575 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1576 | main.cleanup() |
| 1577 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1578 | except Exception: |
| 1579 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1580 | main.cleanup() |
| 1581 | main.exit() |
| 1582 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1583 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1584 | """ |
| 1585 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1586 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1587 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 1588 | self.handle.expect( "mininet>" ) |
| 1589 | self.handle.sendline( "" ) |
| 1590 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1591 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1592 | main.log.error( self.name + ": EOF exception found" ) |
| 1593 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1594 | main.cleanup() |
| 1595 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1596 | except Exception: |
| 1597 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1598 | main.cleanup() |
| 1599 | main.exit() |
| 1600 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1601 | def compareSwitches( self, topo, switchesJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1602 | """ |
| 1603 | Compare mn and onos switches |
| 1604 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1605 | switchesJson: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1606 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1607 | This uses the sts TestONTopology object""" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1608 | # main.log.debug( "Switches_json string: ", switchesJson ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1609 | output = { "switches": [] } |
| 1610 | # iterate through the MN topology and pull out switches and and port |
| 1611 | # info |
| 1612 | for switch in topo.graph.switches: |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1613 | ports = [] |
| 1614 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1615 | ports.append( { 'of_port': port.port_no, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1616 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1617 | 'name': port.name } ) |
| 1618 | output[ 'switches' ].append( { |
| 1619 | "name": switch.name, |
| 1620 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1621 | "ports": ports } ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1622 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1623 | # print "mn" |
| 1624 | # print json.dumps( output, |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1625 | # sort_keys=True, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1626 | # indent=4, |
| 1627 | # separators=( ',', ': ' ) ) |
| 1628 | # print "onos" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1629 | # print json.dumps( switchesJson, |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1630 | # sort_keys=True, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1631 | # indent=4, |
| 1632 | # separators=( ',', ': ' ) ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1633 | |
| 1634 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1635 | mnDPIDs = [] |
| 1636 | for switch in output[ 'switches' ]: |
| 1637 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1638 | mnDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1639 | # print "List of Mininet switch DPID's" |
| 1640 | # print mnDPIDs |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1641 | if switchesJson == "": # if rest call fails |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1642 | main.log.error( |
| 1643 | self.name + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 1644 | ".compareSwitches(): Empty JSON object given from ONOS" ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1645 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1646 | onos = switchesJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1647 | onosDPIDs = [] |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1648 | for switch in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1649 | if switch[ 'available' ]: |
| 1650 | onosDPIDs.append( |
| 1651 | switch[ 'id' ].replace( |
| 1652 | ":", |
| 1653 | '' ).replace( |
| 1654 | "of", |
| 1655 | '' ).lower() ) |
| 1656 | # else: |
| 1657 | # print "Switch is unavailable:" |
| 1658 | # print switch |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1659 | onosDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1660 | # print "List of ONOS switch DPID's" |
| 1661 | # print onosDPIDs |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1662 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1663 | if mnDPIDs != onosDPIDs: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1664 | switchResults = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1665 | main.log.report( "Switches in MN but not in ONOS:" ) |
| 1666 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
| 1667 | main.log.report( str( list1 ) ) |
| 1668 | main.log.report( "Switches in ONOS but not in MN:" ) |
| 1669 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1670 | main.log.report( str( list2 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1671 | else: # list of dpid's match in onos and mn |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1672 | switchResults = main.TRUE |
| 1673 | return switchResults |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1674 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1675 | def comparePorts( self, topo, portsJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1676 | """ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1677 | Compare mn and onos ports |
| 1678 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1679 | portsJson: parsed json object from the onos ports api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1680 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1681 | Dependencies: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1682 | 1. This uses the sts TestONTopology object |
| 1683 | 2. numpy - "sudo pip install numpy" |
| 1684 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1685 | """ |
| 1686 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 1687 | # ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1688 | from numpy import uint64 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1689 | portsResults = main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1690 | output = { "switches": [] } |
| 1691 | # iterate through the MN topology and pull out switches and and port |
| 1692 | # info |
| 1693 | for switch in topo.graph.switches: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1694 | ports = [] |
| 1695 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1696 | # print port.hw_addr.toStr( separator='' ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1697 | tmpPort = { 'of_port': port.port_no, |
| 1698 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
| 1699 | 'name': port.name, |
| 1700 | 'enabled': port.enabled } |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1701 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1702 | ports.append( tmpPort ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1703 | tmpSwitch = { 'name': switch.name, |
| 1704 | 'dpid': str( switch.dpid ).zfill( 16 ), |
| 1705 | 'ports': ports } |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1706 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1707 | output[ 'switches' ].append( tmpSwitch ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1708 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1709 | # PORTS |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1710 | for mnSwitch in output[ 'switches' ]: |
| 1711 | mnPorts = [] |
| 1712 | onosPorts = [] |
| 1713 | switchResult = main.TRUE |
| 1714 | for port in mnSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1715 | if port[ 'enabled' ]: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1716 | mnPorts.append( port[ 'of_port' ] ) |
| 1717 | for onosSwitch in portsJson: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1718 | # print "Iterating through a new switch as seen by ONOS" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1719 | # print onosSwitch |
| 1720 | if onosSwitch[ 'device' ][ 'available' ]: |
| 1721 | if onosSwitch[ 'device' ][ 'id' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1722 | ':', |
| 1723 | '' ).replace( |
| 1724 | "of", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1725 | '' ) == mnSwitch[ 'dpid' ]: |
| 1726 | for port in onosSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1727 | if port[ 'isEnabled' ]: |
| 1728 | if port[ 'port' ] == 'local': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1729 | # onosPorts.append( 'local' ) |
| 1730 | onosPorts.append( long( uint64( -2 ) ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1731 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1732 | onosPorts.append( int( port[ 'port' ] ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1733 | break |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1734 | mnPorts.sort( key=float ) |
| 1735 | onosPorts.sort( key=float ) |
| 1736 | # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] ) |
| 1737 | # print "\tmn_ports[] = ", mnPorts |
| 1738 | # print "\tonos_ports[] = ", onosPorts |
| 1739 | mnPortsLog = mnPorts |
| 1740 | onosPortsLog = onosPorts |
| 1741 | mnPorts = [ x for x in mnPorts ] |
| 1742 | onosPorts = [ x for x in onosPorts ] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1743 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1744 | # TODO: handle other reserved port numbers besides LOCAL |
| 1745 | # NOTE: Reserved ports |
| 1746 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 1747 | # long( uint64( -2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1748 | for mnPort in mnPortsLog: |
| 1749 | if mnPort in onosPorts: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1750 | # don't set results to true here as this is just one of |
| 1751 | # many checks and it might override a failure |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1752 | mnPorts.remove( mnPort ) |
| 1753 | onosPorts.remove( mnPort ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1754 | # NOTE: OVS reports this as down since there is no link |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1755 | # So ignoring these for now |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1756 | # TODO: Come up with a better way of handling these |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1757 | if 65534 in mnPorts: |
| 1758 | mnPorts.remove( 65534 ) |
| 1759 | if long( uint64( -2 ) ) in onosPorts: |
| 1760 | onosPorts.remove( long( uint64( -2 ) ) ) |
| 1761 | if len( mnPorts ): # the ports of this switch don't match |
| 1762 | switchResult = main.FALSE |
| 1763 | main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) ) |
| 1764 | if len( onosPorts ): # the ports of this switch don't match |
| 1765 | switchResult = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1766 | main.log.warn( |
| 1767 | "Ports in ONOS but not MN: " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1768 | str( onosPorts ) ) |
| 1769 | if switchResult == main.FALSE: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1770 | main.log.report( |
| 1771 | "The list of ports for switch %s(%s) does not match:" % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1772 | ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) ) |
| 1773 | main.log.warn( "mn_ports[] = " + str( mnPortsLog ) ) |
| 1774 | main.log.warn( "onos_ports[] = " + str( onosPortsLog ) ) |
| 1775 | portsResults = portsResults and switchResult |
| 1776 | return portsResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1777 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1778 | def compareLinks( self, topo, linksJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1779 | """ |
| 1780 | Compare mn and onos links |
| 1781 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1782 | linksJson: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1783 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1784 | This uses the sts TestONTopology object""" |
| 1785 | # FIXME: this does not look for extra links in ONOS, only checks that |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1786 | # ONOS has what is in MN |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1787 | output = { "switches": [] } |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1788 | onos = linksJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1789 | # iterate through the MN topology and pull out switches and and port |
| 1790 | # info |
| 1791 | for switch in topo.graph.switches: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1792 | # print "Iterating though switches as seen by Mininet" |
| 1793 | # print switch |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1794 | ports = [] |
| 1795 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1796 | # print port.hw_addr.toStr( separator='' ) |
| 1797 | ports.append( { 'of_port': port.port_no, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1798 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1799 | 'name': port.name } ) |
| 1800 | output[ 'switches' ].append( { |
| 1801 | "name": switch.name, |
| 1802 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1803 | "ports": ports } ) |
| 1804 | # LINKS |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1805 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1806 | mnLinks = [ |
kelvin-onlab | 9592d13 | 2015-01-20 17:18:02 -0800 | [diff] [blame] | 1807 | link for link in topo.patch_panel.network_links if ( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1808 | link.port1.enabled and link.port2.enabled ) ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1809 | if 2 * len( mnLinks ) == len( onos ): |
| 1810 | linkResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1811 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1812 | linkResults = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1813 | main.log.report( |
Jon Hall | 328ddca | 2015-01-28 15:57:15 -0800 | [diff] [blame] | 1814 | "Mininet has " + str( len( mnLinks ) ) + |
| 1815 | " bidirectional links and ONOS has " + |
| 1816 | str( len( onos ) ) + " unidirectional links" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1817 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1818 | # iterate through MN links and check if an ONOS link exists in |
| 1819 | # both directions |
| 1820 | # NOTE: Will currently only show mn links as down if they are |
| 1821 | # cut through STS. We can either do everything through STS or |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1822 | # wait for upNetworkLinks and downNetworkLinks to be |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1823 | # fully implemented. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1824 | for link in mnLinks: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1825 | # print "Link: %s" % link |
| 1826 | # TODO: Find a more efficient search method |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1827 | node1 = None |
| 1828 | port1 = None |
| 1829 | node2 = None |
| 1830 | port2 = None |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1831 | firstDir = main.FALSE |
| 1832 | secondDir = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1833 | for switch in output[ 'switches' ]: |
| 1834 | # print "Switch: %s" % switch[ 'name' ] |
| 1835 | if switch[ 'name' ] == link.node1.name: |
| 1836 | node1 = switch[ 'dpid' ] |
| 1837 | for port in switch[ 'ports' ]: |
| 1838 | if str( port[ 'name' ] ) == str( link.port1 ): |
| 1839 | port1 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1840 | if node1 is not None and node2 is not None: |
| 1841 | break |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1842 | if switch[ 'name' ] == link.node2.name: |
| 1843 | node2 = switch[ 'dpid' ] |
| 1844 | for port in switch[ 'ports' ]: |
| 1845 | if str( port[ 'name' ] ) == str( link.port2 ): |
| 1846 | port2 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1847 | if node1 is not None and node2 is not None: |
| 1848 | break |
| 1849 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1850 | for onosLink in onos: |
| 1851 | onosNode1 = onosLink[ 'src' ][ 'device' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1852 | ":", |
| 1853 | '' ).replace( |
| 1854 | "of", |
| 1855 | '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1856 | onosNode2 = onosLink[ 'dst' ][ 'device' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1857 | ":", |
| 1858 | '' ).replace( |
| 1859 | "of", |
| 1860 | '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1861 | onosPort1 = onosLink[ 'src' ][ 'port' ] |
| 1862 | onosPort2 = onosLink[ 'dst' ][ 'port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1863 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1864 | # check onos link from node1 to node2 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1865 | if str( onosNode1 ) == str( node1 ) and str( |
| 1866 | onosNode2 ) == str( node2 ): |
| 1867 | if int( onosPort1 ) == int( port1 ) and int( |
| 1868 | onosPort2 ) == int( port2 ): |
| 1869 | firstDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1870 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1871 | main.log.warn( |
| 1872 | 'The port numbers do not match for ' + |
| 1873 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1874 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1875 | 'link %s/%s -> %s/%s' % |
| 1876 | ( node1, |
| 1877 | port1, |
| 1878 | node2, |
| 1879 | port2 ) + |
| 1880 | ' ONOS has the values %s/%s -> %s/%s' % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1881 | ( onosNode1, |
| 1882 | onosPort1, |
| 1883 | onosNode2, |
| 1884 | onosPort2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1885 | |
| 1886 | # check onos link from node2 to node1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1887 | elif ( str( onosNode1 ) == str( node2 ) and |
| 1888 | str( onosNode2 ) == str( node1 ) ): |
| 1889 | if ( int( onosPort1 ) == int( port2 ) |
| 1890 | and int( onosPort2 ) == int( port1 ) ): |
| 1891 | secondDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1892 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1893 | main.log.warn( |
| 1894 | 'The port numbers do not match for ' + |
| 1895 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1896 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1897 | 'link %s/%s -> %s/%s' % |
| 1898 | ( node2, |
| 1899 | port2, |
| 1900 | node1, |
| 1901 | port1 ) + |
| 1902 | ' ONOS has the values %s/%s -> %s/%s' % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1903 | ( onosNode2, |
| 1904 | onosPort2, |
| 1905 | onosNode1, |
| 1906 | onosPort1 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1907 | else: # this is not the link you're looking for |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1908 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1909 | if not firstDir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1910 | main.log.report( |
| 1911 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1912 | ( node1, port1, node2, port2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1913 | if not secondDir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1914 | main.log.report( |
| 1915 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1916 | ( node2, port2, node1, port1 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1917 | linkResults = linkResults and firstDir and secondDir |
| 1918 | return linkResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1919 | |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1920 | def compareHosts( self, topo, hostsJson ): |
| 1921 | """ |
| 1922 | Compare mn and onos Hosts. |
| 1923 | Since Mininet hosts are quiet, ONOS will only know of them when they |
| 1924 | speak. For this reason, we will only check that the hosts in ONOS |
| 1925 | stores are in Mininet, and not vice versa. |
| 1926 | topo: sts TestONTopology object |
| 1927 | hostsJson: parsed json object from the onos hosts api |
| 1928 | |
| 1929 | This uses the sts TestONTopology object""" |
| 1930 | import json |
| 1931 | hostResults = main.TRUE |
| 1932 | hosts = [] |
| 1933 | # iterate through the MN topology and pull out hosts |
| 1934 | for mnHost in topo.graph.hosts: |
| 1935 | interfaces = [] |
| 1936 | for intf in mnHost.interfaces: |
| 1937 | interfaces.append( { |
| 1938 | "name": intf.name, # str |
| 1939 | "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs |
| 1940 | # hw_addr is of type EthAddr, Not JSON serializable |
| 1941 | "hw_addr": str( intf.hw_addr ) } ) |
| 1942 | hosts.append( { |
| 1943 | "name": mnHost.name, # str |
| 1944 | "interfaces": interfaces } ) # list |
| 1945 | for onosHost in hostsJson: |
| 1946 | onosMAC = onosHost[ 'mac' ].lower() |
| 1947 | match = False |
| 1948 | for mnHost in hosts: |
| 1949 | for mnIntf in mnHost[ 'interfaces' ]: |
| 1950 | if onosMAC == mnIntf[ 'hw_addr' ].lower() : |
| 1951 | match = True |
| 1952 | for ip in mnIntf[ 'ips' ]: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 1953 | if ip in onosHost[ 'ipAddresses' ]: |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1954 | pass # all is well |
| 1955 | else: |
| 1956 | # misssing ip |
| 1957 | main.log.error( "ONOS host " + onosHost[ 'id' ] |
| 1958 | + " has a different IP than " + |
| 1959 | "the Mininet host." ) |
| 1960 | output = json.dumps( |
| 1961 | onosHost, |
| 1962 | sort_keys=True, |
| 1963 | indent=4, |
| 1964 | separators=( ',', ': ' ) ) |
| 1965 | main.log.info( output ) |
| 1966 | hostResults = main.FALSE |
| 1967 | if not match: |
| 1968 | hostResults = main.FALSE |
| 1969 | main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " + |
| 1970 | "corresponding Mininet host." ) |
| 1971 | output = json.dumps( onosHost, |
| 1972 | sort_keys=True, |
| 1973 | indent=4, |
| 1974 | separators=( ',', ': ' ) ) |
| 1975 | main.log.info( output ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1976 | return hostResults |
| 1977 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1978 | def getHosts( self ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1979 | """ |
| 1980 | Returns a list of all hosts |
| 1981 | Don't ask questions just use it""" |
| 1982 | self.handle.sendline( "" ) |
| 1983 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1984 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1985 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 1986 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1987 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1988 | handlePy = self.handle.before |
| 1989 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 1990 | handlePy = handlePy.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1991 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1992 | self.handle.sendline( "" ) |
| 1993 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1994 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1995 | hostStr = handlePy.replace( "]", "" ) |
| 1996 | hostStr = hostStr.replace( "'", "" ) |
| 1997 | hostStr = hostStr.replace( "[", "" ) |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame] | 1998 | hostStr = hostStr.replace( " ", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1999 | hostList = hostStr.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 2000 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2001 | return hostList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2002 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2003 | def update( self ): |
| 2004 | """ |
| 2005 | updates the port address and status information for |
| 2006 | each port in mn""" |
| 2007 | # TODO: Add error checking. currently the mininet command has no output |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2008 | main.log.info( "Updating MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2009 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2010 | self.handle.sendline( "" ) |
| 2011 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2012 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2013 | self.handle.sendline( "update" ) |
| 2014 | self.handle.expect( "update" ) |
| 2015 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2016 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2017 | self.handle.sendline( "" ) |
| 2018 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2019 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2020 | return main.TRUE |
| 2021 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2022 | main.log.error( self.name + ": EOF exception found" ) |
| 2023 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2024 | main.cleanup() |
| 2025 | main.exit() |
| 2026 | |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2027 | def assignVLAN( self, host, intf, vlan): |
| 2028 | """ |
| 2029 | Add vlan tag to a host. |
| 2030 | Dependencies: |
| 2031 | This class depends on the "vlan" package |
| 2032 | $ sudo apt-get install vlan |
| 2033 | Configuration: |
| 2034 | Load the 8021q module into the kernel |
| 2035 | $sudo modprobe 8021q |
| 2036 | |
| 2037 | To make this setup permanent: |
| 2038 | $ sudo su -c 'echo "8021q" >> /etc/modules' |
| 2039 | """ |
| 2040 | if self.handle: |
| 2041 | try: |
| 2042 | # get the ip address of the host |
| 2043 | main.log.info("Get the ip address of the host") |
| 2044 | ipaddr = self.getIPAddress(host) |
| 2045 | print repr(ipaddr) |
| 2046 | |
| 2047 | # remove IP from interface intf |
| 2048 | # Ex: h1 ifconfig h1-eth0 inet 0 |
| 2049 | main.log.info("Remove IP from interface ") |
| 2050 | cmd2 = host + " ifconfig " + intf + " " + " inet 0 " |
| 2051 | self.handle.sendline( cmd2 ) |
| 2052 | self.handle.expect( "mininet>" ) |
| 2053 | response = self.handle.before |
| 2054 | main.log.info ( "====> %s ", response) |
| 2055 | |
| 2056 | |
| 2057 | # create VLAN interface |
| 2058 | # Ex: h1 vconfig add h1-eth0 100 |
| 2059 | main.log.info("Create Vlan") |
| 2060 | cmd3 = host + " vconfig add " + intf + " " + vlan |
| 2061 | self.handle.sendline( cmd3 ) |
| 2062 | self.handle.expect( "mininet>" ) |
| 2063 | response = self.handle.before |
| 2064 | main.log.info( "====> %s ", response ) |
| 2065 | |
| 2066 | # assign the host's IP to the VLAN interface |
| 2067 | # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1 |
| 2068 | main.log.info("Assign the host IP to the vlan interface") |
| 2069 | vintf = intf + "." + vlan |
| 2070 | cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr |
| 2071 | self.handle.sendline( cmd4 ) |
| 2072 | self.handle.expect( "mininet>" ) |
| 2073 | response = self.handle.before |
| 2074 | main.log.info ( "====> %s ", response) |
| 2075 | |
| 2076 | |
| 2077 | return main.TRUE |
| 2078 | except pexpect.EOF: |
| 2079 | main.log.error( self.name + ": EOF exception found" ) |
| 2080 | main.log.error( self.name + ": " + self.handle.before ) |
| 2081 | return main.FALSE |
| 2082 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2083 | if __name__ != "__main__": |
| 2084 | import sys |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 2085 | sys.modules[ __name__ ] = MininetCliDriver() |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2086 | |
| 2087 | |