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