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 | 7eb3840 | 2015-01-08 17:19:54 -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 | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 22 | MininetCliDriver is the basic driver which will handle the Mininet functions""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 23 | import traceback |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 27 | sys.path.append( "../" ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 28 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 31 | |
| 32 | class MininetCliDriver( Emulator ): |
| 33 | |
| 34 | """ |
| 35 | MininetCliDriver is the basic driver which will handle |
| 36 | the Mininet functions""" |
| 37 | def __init__( self ): |
| 38 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | self.handle = self |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 40 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | self.flag = 0 |
| 42 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 43 | def connect( self, **connectargs ): |
| 44 | """ |
| 45 | Here the main is the TestON instance after creating |
| 46 | all the log handles.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 48 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 49 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 50 | self.name = self.options[ 'name' ] |
| 51 | self.handle = super( |
| 52 | MininetCliDriver, |
| 53 | self ).connect( |
| 54 | user_name=self.user_name, |
| 55 | ip_address=self.ip_address, |
| 56 | port=None, |
| 57 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 58 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | self.ssh_handle = self.handle |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 60 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 61 | if self.handle: |
| 62 | main.log.info( |
| 63 | self.name + |
| 64 | ": Clearing any residual state or processes" ) |
| 65 | self.handle.sendline( "sudo mn -c" ) |
| 66 | i = self.handle.expect( [ 'password\sfor\s', |
| 67 | 'Cleanup\scomplete', |
| 68 | pexpect.EOF, |
| 69 | pexpect.TIMEOUT ], |
| 70 | 120 ) |
| 71 | if i == 0: |
| 72 | main.log.info( self.name + ": Sending sudo password" ) |
| 73 | self.handle.sendline( self.pwd ) |
| 74 | i = self.handle.expect( [ '%s:' % ( self.user ), |
| 75 | '\$', |
| 76 | pexpect.EOF, |
| 77 | pexpect.TIMEOUT ], |
| 78 | 120 ) |
| 79 | if i == 1: |
| 80 | main.log.info( self.name + ": Clean" ) |
| 81 | elif i == 2: |
| 82 | main.log.error( self.name + ": Connection terminated" ) |
| 83 | elif i == 3: # timeout |
| 84 | main.log.error( |
| 85 | self.name + |
| 86 | ": Something while cleaning MN took too long... " ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 87 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 88 | main.log.info( self.name + ": building fresh mininet" ) |
| 89 | # for reactive/PARP enabled tests |
| 90 | cmdString = "sudo mn " + self.options[ 'arg1' ] +\ |
| 91 | " " + self.options[ 'arg2' ] +\ |
| 92 | " --mac --controller " +\ |
| 93 | self.options[ 'controller' ] + " " +\ |
| 94 | self.options[ 'arg3' ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 95 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 96 | argList = self.options[ 'arg1' ].split( "," ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 97 | global topoArgList |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 98 | topoArgList = argList[ 0 ].split( " " ) |
| 99 | argList = map( int, argList[ 1: ] ) |
| 100 | topoArgList = topoArgList[ 1: ] + argList |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 101 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 102 | self.handle.sendline( cmdString ) |
| 103 | self.handle.expect( [ "sudo mn", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 104 | while True: |
| 105 | i = self.handle.expect( [ 'mininet>', |
| 106 | '\*\*\*', |
| 107 | 'Exception', |
| 108 | pexpect.EOF, |
| 109 | pexpect.TIMEOUT ], |
| 110 | 300 ) |
| 111 | if i == 0: |
| 112 | main.log.info( self.name + ": mininet built" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 114 | if i == 1: |
| 115 | self.handle.expect( |
| 116 | [ "\n", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 117 | main.log.info( self.handle.before ) |
| 118 | elif i == 2: |
| 119 | main.log.error( |
| 120 | self.name + |
| 121 | ": Launching mininet failed..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 123 | elif i == 3: |
| 124 | main.log.error( self.name + ": Connection timeout" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 125 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 126 | elif i == 4: # timeout |
| 127 | main.log.error( |
| 128 | self.name + |
| 129 | ": Something took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 132 | else: # if no handle |
| 133 | main.log.error( |
| 134 | self.name + |
| 135 | ": Connection failed to the host " + |
| 136 | self.user_name + |
| 137 | "@" + |
| 138 | self.ip_address ) |
| 139 | main.log.error( self.name + ": Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 140 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 141 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 142 | def num_switches_n_links( self, topoType, depth, fanout ): |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 143 | if topoType == 'tree': |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 144 | # In tree topology, if fanout arg is not given, by default it is 2 |
| 145 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 146 | fanout = 2 |
| 147 | k = 0 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 148 | count = 0 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 149 | while( k <= depth - 1 ): |
| 150 | count = count + pow( fanout, k ) |
| 151 | k = k + 1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 152 | num_switches = count |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 153 | while( k <= depth - 2 ): |
| 154 | # depth-2 gives you only core links and not considering |
| 155 | # edge links as seen by ONOS. If all the links including |
| 156 | # edge links are required, do depth-1 |
| 157 | count = count + pow( fanout, k ) |
| 158 | k = k + 1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 159 | num_links = count * fanout |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 160 | # print "num_switches for %s(%d,%d) = %d and links=%d" %( |
| 161 | # topoType,depth,fanout,num_switches,num_links ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 162 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 163 | elif topoType == 'linear': |
| 164 | # In linear topology, if fanout or num_hosts_per_sw is not given, |
| 165 | # by default it is 1 |
| 166 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 167 | fanout = 1 |
| 168 | num_switches = depth |
| 169 | num_hosts_per_sw = fanout |
| 170 | total_num_hosts = num_switches * num_hosts_per_sw |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 171 | num_links = total_num_hosts + ( num_switches - 1 ) |
| 172 | print "num_switches for %s(%d,%d) = %d and links=%d" %\ |
| 173 | ( topoType, depth, fanout, num_switches, num_links ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 174 | topoDict = {} |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 175 | topoDict = { |
| 176 | "num_switches": int( num_switches ), |
| 177 | "num_corelinks": int( num_links ) } |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 178 | return topoDict |
| 179 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 180 | def calculate_sw_and_links( self ): |
| 181 | topoDict = self.num_switches_n_links( *topoArgList ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 182 | return topoDict |
| 183 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 184 | def pingall( self, timeout=300 ): |
| 185 | """ |
| 186 | Verifies the reachability of the hosts using pingall command. |
| 187 | Optional parameter timeout allows you to specify how long to |
| 188 | wait for pingall to complete |
| 189 | Returns: |
| 190 | main.TRUE if pingall completes with no pings dropped |
| 191 | otherwise main.FALSE""" |
| 192 | if self.handle: |
| 193 | main.log.info( |
| 194 | self.name + |
| 195 | ": Checking reachabilty to the hosts using pingall" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 196 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 197 | response = self.execute( |
| 198 | cmd="pingall", |
| 199 | prompt="mininet>", |
| 200 | timeout=int( timeout ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 201 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 202 | main.log.error( self.name + ": EOF exception found" ) |
| 203 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 204 | main.cleanup() |
| 205 | main.exit() |
| 206 | except pexpect.TIMEOUT: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 207 | # We may not want to kill the test if pexpect times out |
| 208 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 209 | main.log.error( self.name + |
| 210 | ": " + |
| 211 | str( self.handle.before ) ) |
| 212 | # NOTE: mininet's pingall rounds, so we will check the number of |
| 213 | # passed and number of failed |
| 214 | pattern = "Results\:\s0\%\sdropped\s\(" +\ |
| 215 | "(?P<passed>[\d]+)/(?P=passed)" |
| 216 | if re.search( pattern, response ): |
| 217 | main.log.info( self.name + ": All hosts are reachable" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 218 | return main.TRUE |
| 219 | else: |
andrew@onlab.us | 35ad446 | 2015-01-14 09:44:04 -0800 | [diff] [blame^] | 220 | main.log.error( self.name + ": Unable to reach all the hosts" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 221 | main.log.info( "Pingall ouput: " + str( response ) ) |
| 222 | # NOTE: Send ctrl-c to make sure pingall is done |
| 223 | self.handle.send( "\x03" ) |
| 224 | self.handle.expect( "Interrupt" ) |
| 225 | self.handle.expect( "mininet>" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 226 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 227 | else: |
| 228 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 229 | main.cleanup() |
| 230 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 231 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 232 | def fpingHost( self, **pingParams ): |
| 233 | """ |
| 234 | Uses the fping package for faster pinging... |
| 235 | *requires fping to be installed on machine running mininet""" |
| 236 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 237 | command = args[ "SRC" ] + \ |
| 238 | " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ] |
| 239 | self.handle.sendline( command ) |
| 240 | self.handle.expect( |
| 241 | [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] ) |
| 242 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 243 | response = self.handle.before |
| 244 | if re.search( ":\s-", response ): |
| 245 | main.log.info( self.name + ": Ping fail" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 246 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 247 | elif re.search( ":\s\d{1,2}\.\d\d", response ): |
| 248 | main.log.info( self.name + ": Ping good!" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 249 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 250 | main.log.info( self.name + ": Install fping on mininet machine... " ) |
| 251 | main.log.info( self.name + ": \n---\n" + response ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 252 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 253 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 254 | def pingHost( self, **pingParams ): |
| 255 | """ |
| 256 | Ping from one mininet host to another |
| 257 | Currently the only supported Params: SRC and TARGET""" |
| 258 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 259 | command = args[ "SRC" ] + " ping " + \ |
| 260 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 261 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 262 | main.log.warn( "Sending: " + command ) |
| 263 | self.handle.sendline( command ) |
| 264 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 265 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 266 | main.log.error( |
| 267 | self.name + |
| 268 | ": timeout when waiting for response from mininet" ) |
| 269 | main.log.error( "response: " + str( self.handle.before ) ) |
| 270 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 271 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 272 | main.log.error( |
| 273 | self.name + |
| 274 | ": timeout when waiting for response from mininet" ) |
| 275 | main.log.error( "response: " + str( self.handle.before ) ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 276 | response = self.handle.before |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 277 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 278 | main.log.error( self.name + ": EOF exception found" ) |
| 279 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 280 | main.cleanup() |
| 281 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 282 | main.log.info( self.name + ": Ping Response: " + response ) |
| 283 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 284 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 285 | main.last_result = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 287 | else: |
| 288 | main.log.error( |
| 289 | self.name + |
| 290 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 291 | main.last_result = main.FALSE |
| 292 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 293 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 294 | def checkIP( self, host ): |
| 295 | """ |
| 296 | Verifies the host's ip configured or not.""" |
| 297 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 298 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 299 | response = self.execute( |
| 300 | cmd=host + |
| 301 | " ifconfig", |
| 302 | prompt="mininet>", |
| 303 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 304 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 305 | main.log.error( self.name + ": EOF exception found" ) |
| 306 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 307 | main.cleanup() |
| 308 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 309 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 310 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\ |
| 311 | "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\ |
| 312 | "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\ |
| 313 | "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\ |
| 314 | "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\ |
| 315 | "[0-9]|25[0-5]|[0-9]{1,2})" |
| 316 | # pattern = "inet addr:10.0.0.6" |
| 317 | if re.search( pattern, response ): |
| 318 | main.log.info( self.name + ": Host Ip configured properly" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 319 | return main.TRUE |
| 320 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 321 | main.log.error( self.name + ": Host IP not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 322 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 323 | else: |
| 324 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 325 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 326 | def verifySSH( self, **connectargs ): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 327 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 328 | response = self.execute( |
| 329 | cmd="h1 /usr/sbin/sshd -D&", |
| 330 | prompt="mininet>", |
| 331 | timeout=10 ) |
| 332 | response = self.execute( |
| 333 | cmd="h4 /usr/sbin/sshd -D&", |
| 334 | prompt="mininet>", |
| 335 | timeout=10 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 336 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 337 | vars( self )[ key ] = connectargs[ key ] |
| 338 | response = self.execute( |
| 339 | cmd="xterm h1 h4 ", |
| 340 | prompt="mininet>", |
| 341 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 342 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 343 | main.log.error( self.name + ": EOF exception found" ) |
| 344 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 345 | main.cleanup() |
| 346 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 347 | import time |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 348 | time.sleep( 20 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 349 | if self.flag == 0: |
| 350 | self.flag = 1 |
| 351 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 352 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 353 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 354 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 355 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 356 | """ |
| 357 | Changes the ip address of a host on the fly |
| 358 | 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] | 359 | if self.handle: |
| 360 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 361 | cmd = host + " ifconfig " + intf + " " + \ |
| 362 | newIP + " " + 'netmask' + " " + newNetmask |
| 363 | self.handle.sendline( cmd ) |
| 364 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 365 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 366 | main.log.info( "response = " + response ) |
| 367 | main.log.info( |
| 368 | "Ip of host " + |
| 369 | host + |
| 370 | " changed to new IP " + |
| 371 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 372 | return main.TRUE |
| 373 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 374 | main.log.error( self.name + ": EOF exception found" ) |
| 375 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 376 | return main.FALSE |
| 377 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 378 | def changeDefaultGateway( self, host, newGW ): |
| 379 | """ |
| 380 | Changes the default gateway of a host |
| 381 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 382 | if self.handle: |
| 383 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 384 | cmd = host + " route add default gw " + newGW |
| 385 | self.handle.sendline( cmd ) |
| 386 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 387 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 388 | main.log.info( "response = " + response ) |
| 389 | main.log.info( |
| 390 | "Default gateway of host " + |
| 391 | host + |
| 392 | " changed to " + |
| 393 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 394 | return main.TRUE |
| 395 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 396 | main.log.error( self.name + ": EOF exception found" ) |
| 397 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 398 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 399 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 400 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 401 | """ |
| 402 | Changes the mac address of a geateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 403 | if self.handle: |
| 404 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 405 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 406 | cmd = host + " arp -s " + GW + " " + macaddr |
| 407 | self.handle.sendline( cmd ) |
| 408 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 409 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 410 | main.log.info( "response = " + response ) |
| 411 | main.log.info( |
| 412 | "Mac adrress of gateway " + |
| 413 | GW + |
| 414 | " changed to " + |
| 415 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 416 | return main.TRUE |
| 417 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 418 | main.log.error( self.name + ": EOF exception found" ) |
| 419 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 420 | return main.FALSE |
| 421 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 422 | def verifyStaticGWandMAC( self, host ): |
| 423 | """ |
| 424 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 425 | if self.handle: |
| 426 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 427 | # h1 arp -an |
| 428 | cmd = host + " arp -an " |
| 429 | self.handle.sendline( cmd ) |
| 430 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 431 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 432 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 433 | return main.TRUE |
| 434 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 435 | main.log.error( self.name + ": EOF exception found" ) |
| 436 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 437 | return main.FALSE |
| 438 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 439 | def getMacAddress( self, host ): |
| 440 | """ |
| 441 | Verifies the host's ip configured or not.""" |
| 442 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 443 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 444 | response = self.execute( |
| 445 | cmd=host + |
| 446 | " ifconfig", |
| 447 | prompt="mininet>", |
| 448 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 449 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 450 | main.log.error( self.name + ": EOF exception found" ) |
| 451 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 452 | main.cleanup() |
| 453 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 454 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 455 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 456 | mac_address_search = re.search( pattern, response, re.I ) |
| 457 | mac_address = mac_address_search.group().split( " " )[ 1 ] |
| 458 | main.log.info( |
| 459 | self.name + |
| 460 | ": Mac-Address of Host " + |
| 461 | host + |
| 462 | " is " + |
| 463 | mac_address ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 464 | return mac_address |
| 465 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 466 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 467 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 468 | def getInterfaceMACAddress( self, host, interface ): |
| 469 | """ |
| 470 | Return the IP address of the interface on the given host""" |
| 471 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 472 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 473 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 474 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 475 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 476 | main.log.error( self.name + ": EOF exception found" ) |
| 477 | main.log.error( self.name + ": " + self.handle.before ) |
| 478 | main.cleanup() |
| 479 | main.exit() |
| 480 | |
| 481 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 482 | mac_address_search = re.search( pattern, response, re.I ) |
| 483 | if mac_address_search is None: |
| 484 | main.log.info( "No mac address found in %s" % response ) |
| 485 | return main.FALSE |
| 486 | mac_address = mac_address_search.group().split( " " )[ 1 ] |
| 487 | main.log.info( |
| 488 | "Mac-Address of " + |
| 489 | host + |
| 490 | ":" + |
| 491 | interface + |
| 492 | " is " + |
| 493 | mac_address ) |
| 494 | return mac_address |
| 495 | else: |
| 496 | main.log.error( "Connection failed to the host" ) |
| 497 | |
| 498 | def getIPAddress( self, host ): |
| 499 | """ |
| 500 | Verifies the host's ip configured or not.""" |
| 501 | if self.handle: |
| 502 | try: |
| 503 | response = self.execute( |
| 504 | cmd=host + |
| 505 | " ifconfig", |
| 506 | prompt="mininet>", |
| 507 | timeout=10 ) |
| 508 | except pexpect.EOF: |
| 509 | main.log.error( self.name + ": EOF exception found" ) |
| 510 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 511 | main.cleanup() |
| 512 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 513 | |
| 514 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 515 | ip_address_search = re.search( pattern, response ) |
| 516 | main.log.info( |
| 517 | self.name + |
| 518 | ": IP-Address of Host " + |
| 519 | host + |
| 520 | " is " + |
| 521 | ip_address_search.group( 1 ) ) |
| 522 | return ip_address_search.group( 1 ) |
| 523 | else: |
| 524 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 525 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 526 | def getSwitchDPID( self, switch ): |
| 527 | """ |
| 528 | return the datapath ID of the switch""" |
| 529 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 530 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 531 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 532 | response = self.execute( |
| 533 | cmd=cmd, |
| 534 | prompt="mininet>", |
| 535 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 536 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 537 | main.log.error( self.name + ": EOF exception found" ) |
| 538 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 539 | main.cleanup() |
| 540 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 541 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 542 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 543 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 544 | main.log.info( |
| 545 | "Couldn't find DPID for switch %s, found: %s" % |
| 546 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 547 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 548 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 549 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 550 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 551 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 552 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 553 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 554 | self.handle.sendline( "" ) |
| 555 | self.expect( "mininet>" ) |
| 556 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 557 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 558 | response = self.execute( |
| 559 | cmd=cmd, |
| 560 | prompt="mininet>", |
| 561 | timeout=10 ) |
| 562 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 563 | response = self.handle.before |
| 564 | return response |
| 565 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 566 | main.log.error( self.name + ": EOF exception found" ) |
| 567 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 568 | main.cleanup() |
| 569 | main.exit() |
| 570 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 571 | def getInterfaces( self, node ): |
| 572 | """ |
| 573 | return information dict about interfaces connected to the node""" |
| 574 | if self.handle: |
| 575 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
| 576 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 577 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 578 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 579 | response = self.execute( |
| 580 | cmd=cmd, |
| 581 | prompt="mininet>", |
| 582 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 583 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 584 | main.log.error( self.name + ": EOF exception found" ) |
| 585 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 586 | main.cleanup() |
| 587 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 588 | return response |
| 589 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 590 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 591 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 592 | def dump( self ): |
| 593 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 594 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 595 | response = self.execute( |
| 596 | cmd='dump', |
| 597 | prompt='mininet>', |
| 598 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 599 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 600 | main.log.error( self.name + ": EOF exception found" ) |
| 601 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 602 | main.cleanup() |
| 603 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 604 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 605 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 606 | def intfs( self ): |
| 607 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 608 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 609 | response = self.execute( |
| 610 | cmd='intfs', |
| 611 | prompt='mininet>', |
| 612 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 613 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 614 | main.log.error( self.name + ": EOF exception found" ) |
| 615 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 616 | main.cleanup() |
| 617 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 618 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 619 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 620 | def net( self ): |
| 621 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 622 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 623 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 624 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 625 | main.log.error( self.name + ": EOF exception found" ) |
| 626 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 627 | main.cleanup() |
| 628 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 629 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 630 | |
| 631 | def iperf( self, host1, host2 ): |
| 632 | main.log.info( |
| 633 | self.name + |
| 634 | ": Simple iperf TCP test between two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 635 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 636 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 637 | self.handle.sendline( cmd1 ) |
| 638 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 639 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 640 | if re.search( 'Results:', response ): |
| 641 | main.log.info( self.name + ": iperf test succssful" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 642 | return main.TRUE |
| 643 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 644 | main.log.error( self.name + ": iperf test failed" ) |
| 645 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 646 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 647 | main.log.error( self.name + ": EOF exception found" ) |
| 648 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 649 | main.cleanup() |
| 650 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 651 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 652 | def iperfudp( self ): |
| 653 | main.log.info( |
| 654 | self.name + |
| 655 | ": Simple iperf TCP test between two " + |
| 656 | "(optionally specified) hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 657 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 658 | response = self.execute( |
| 659 | cmd='iperfudp', |
| 660 | prompt='mininet>', |
| 661 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 662 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 663 | main.log.error( self.name + ": EOF exception found" ) |
| 664 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 665 | main.cleanup() |
| 666 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 667 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 668 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 669 | def nodes( self ): |
| 670 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 671 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 672 | response = self.execute( |
| 673 | cmd='nodes', |
| 674 | prompt='mininet>', |
| 675 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 676 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 677 | main.log.error( self.name + ": EOF exception found" ) |
| 678 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 679 | main.cleanup() |
| 680 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 681 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 682 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 683 | def pingpair( self ): |
| 684 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 685 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 686 | response = self.execute( |
| 687 | cmd='pingpair', |
| 688 | prompt='mininet>', |
| 689 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 690 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 691 | main.log.error( self.name + ": EOF exception found" ) |
| 692 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 693 | main.cleanup() |
| 694 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 695 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 696 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 697 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 698 | main.last_result = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 699 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 700 | else: |
| 701 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 702 | main.last_result = main.FALSE |
| 703 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 704 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 705 | def link( self, **linkargs ): |
| 706 | """ |
| 707 | Bring link( s ) between two nodes up or down""" |
| 708 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
| 709 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 710 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 711 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 712 | main.log.info( |
| 713 | "Bring link between '" + |
| 714 | end1 + |
| 715 | "' and '" + |
| 716 | end2 + |
| 717 | "' '" + |
| 718 | option + |
| 719 | "'" ) |
| 720 | command = "link " + \ |
| 721 | str( end1 ) + " " + str( end2 ) + " " + str( option ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 722 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 723 | self.handle.sendline( command ) |
| 724 | self.handle.expect( "mininet>" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 725 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 726 | main.log.error( self.name + ": EOF exception found" ) |
| 727 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 728 | main.cleanup() |
| 729 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 730 | return main.TRUE |
| 731 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 732 | def yank( self, **yankargs ): |
| 733 | """ |
| 734 | yank a mininet switch interface to a host""" |
| 735 | main.log.info( 'Yank the switch interface attached to a host' ) |
| 736 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
| 737 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 738 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 739 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 740 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 741 | response = self.execute( |
| 742 | cmd=command, |
| 743 | prompt="mininet>", |
| 744 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 745 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 746 | main.log.error( self.name + ": EOF exception found" ) |
| 747 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 748 | main.cleanup() |
| 749 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 750 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 751 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 752 | def plug( self, **plugargs ): |
| 753 | """ |
| 754 | plug the yanked mininet switch interface to a switch""" |
| 755 | main.log.info( 'Plug the switch interface attached to a switch' ) |
| 756 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
| 757 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 758 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 759 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 760 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 761 | response = self.execute( |
| 762 | cmd=command, |
| 763 | prompt="mininet>", |
| 764 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 765 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 766 | main.log.error( self.name + ": EOF exception found" ) |
| 767 | main.log.error( self.name + ": " + self.handle.before ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 768 | main.cleanup() |
| 769 | main.exit() |
| 770 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 771 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 772 | def dpctl( self, **dpctlargs ): |
| 773 | """ |
| 774 | Run dpctl command on all switches.""" |
| 775 | main.log.info( 'Run dpctl command on all switches' ) |
| 776 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
| 777 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 778 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 779 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 780 | try: |
| 781 | response = self.execute( |
| 782 | cmd=command, |
| 783 | prompt="mininet>", |
| 784 | timeout=10 ) |
| 785 | except pexpect.EOF: |
| 786 | main.log.error( self.name + ": EOF exception found" ) |
| 787 | main.log.error( self.name + ": " + self.handle.before ) |
| 788 | main.cleanup() |
| 789 | main.exit() |
| 790 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 791 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 792 | def get_version( self ): |
| 793 | file_input = path + '/lib/Mininet/INSTALL' |
| 794 | version = super( Mininet, self ).get_version() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 795 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 796 | for line in open( file_input, 'r' ).readlines(): |
| 797 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 798 | if result: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 799 | version = result.group( 0 ) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 800 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 801 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 802 | def get_sw_controller( self, sw ): |
| 803 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 804 | Parameters: |
| 805 | sw: The name of an OVS switch. Example "s1" |
| 806 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 807 | The output of the command from the mininet cli |
| 808 | or main.FALSE on timeout""" |
| 809 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 810 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 811 | response = self.execute( |
| 812 | cmd=command, |
| 813 | prompt="mininet>", |
| 814 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 815 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 816 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 817 | else: |
| 818 | return main.FALSE |
| 819 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 820 | main.log.error( self.name + ": EOF exception found" ) |
| 821 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 822 | main.cleanup() |
| 823 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 824 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 825 | def assign_sw_controller( self, **kwargs ): |
| 826 | """ |
| 827 | count is only needed if there is more than 1 controller""" |
| 828 | args = utilities.parse_args( [ "COUNT" ], **kwargs ) |
| 829 | count = args[ "COUNT" ] if args != {} else 1 |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 830 | |
| 831 | argstring = "SW" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 832 | for j in range( count ): |
| 833 | argstring = argstring + ",IP" + \ |
| 834 | str( j + 1 ) + ",PORT" + str( j + 1 ) |
| 835 | args = utilities.parse_args( argstring.split( "," ), **kwargs ) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 836 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 837 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 838 | ptcpA = int( args[ "PORT1" ] ) + \ |
| 839 | int( sw ) if args[ "PORT1" ] is not None else "" |
| 840 | ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else "" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 841 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 842 | command = "sh ovs-vsctl set-controller s" + \ |
| 843 | str( sw ) + " " + ptcpB + " " |
| 844 | for j in range( count ): |
| 845 | i = j + 1 |
| 846 | args = utilities.parse_args( |
| 847 | [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs ) |
| 848 | ip = args[ |
| 849 | "IP" + |
| 850 | str( i ) ] if args[ |
| 851 | "IP" + |
| 852 | str( i ) ] is not None else "" |
| 853 | port = args[ |
| 854 | "PORT" + |
| 855 | str( i ) ] if args[ |
| 856 | "PORT" + |
| 857 | str( i ) ] is not None else "" |
| 858 | tcp = "tcp:" + str( ip ) + ":" + str( port ) + \ |
| 859 | " " if ip != "" else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 860 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 861 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 862 | self.execute( cmd=command, prompt="mininet>", timeout=5 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 863 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 864 | main.log.error( self.name + ": EOF exception found" ) |
| 865 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 866 | main.cleanup() |
| 867 | main.exit() |
| 868 | except: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 869 | main.log.info( self.name + ":" * 50 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 870 | main.log.error( traceback.print_exc() ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 871 | main.log.info(":" * 50 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 872 | main.cleanup() |
| 873 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 874 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 875 | def delete_sw_controller( self, sw ): |
| 876 | """ |
| 877 | Removes the controller target from sw""" |
| 878 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 879 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 880 | response = self.execute( |
| 881 | cmd=command, |
| 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 | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 887 | main.cleanup() |
| 888 | main.exit() |
| 889 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 890 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 891 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 892 | def add_switch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 893 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 894 | adds a switch to the mininet topology |
| 895 | NOTE: this uses a custom mn function |
| 896 | NOTE: cannot currently specify what type of switch |
| 897 | required params: |
| 898 | switchname = name of the new switch as a string |
| 899 | optional keyvalues: |
| 900 | dpid = "dpid" |
| 901 | returns: main.FASLE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 902 | """ |
| 903 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 904 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 905 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 906 | response = self.execute( |
| 907 | cmd=command, |
| 908 | prompt="mininet>", |
| 909 | timeout=10 ) |
| 910 | if re.search( "already exists!", response ): |
| 911 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 912 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 913 | elif re.search( "Error", response ): |
| 914 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 915 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 916 | elif re.search( "usage:", response ): |
| 917 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 918 | return main.FALSE |
| 919 | else: |
| 920 | return main.TRUE |
| 921 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 922 | main.log.error( self.name + ": EOF exception found" ) |
| 923 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 924 | main.cleanup() |
| 925 | main.exit() |
| 926 | |
| 927 | def del_switch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 928 | """ |
| 929 | delete a switch from the mininet topology |
| 930 | NOTE: this uses a custom mn function |
| 931 | required params: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 932 | switchname = name of the switch as a string |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 933 | returns: main.FASLE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 934 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 935 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 936 | response = self.execute( |
| 937 | cmd=command, |
| 938 | prompt="mininet>", |
| 939 | timeout=10 ) |
| 940 | if re.search( "no switch named", response ): |
| 941 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 942 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 943 | elif re.search( "Error", response ): |
| 944 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 945 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 946 | elif re.search( "usage:", response ): |
| 947 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 948 | return main.FALSE |
| 949 | else: |
| 950 | return main.TRUE |
| 951 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 952 | main.log.error( self.name + ": EOF exception found" ) |
| 953 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 954 | main.cleanup() |
| 955 | main.exit() |
| 956 | |
| 957 | def add_link( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 958 | """ |
| 959 | add a link to the mininet topology |
| 960 | NOTE: this uses a custom mn function |
| 961 | NOTE: cannot currently specify what type of link |
| 962 | required params: |
| 963 | node1 = the string node name of the first endpoint of the link |
| 964 | node2 = the string node name of the second endpoint of the link |
| 965 | returns: main.FASLE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 966 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 967 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 968 | response = self.execute( |
| 969 | cmd=command, |
| 970 | prompt="mininet>", |
| 971 | timeout=10 ) |
| 972 | if re.search( "doesnt exist!", response ): |
| 973 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 974 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 975 | elif re.search( "Error", response ): |
| 976 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 977 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 978 | elif re.search( "usage:", response ): |
| 979 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 980 | return main.FALSE |
| 981 | else: |
| 982 | return main.TRUE |
| 983 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 984 | main.log.error( self.name + ": EOF exception found" ) |
| 985 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 986 | main.cleanup() |
| 987 | main.exit() |
| 988 | |
| 989 | def del_link( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 990 | """ |
| 991 | delete a link from the mininet topology |
| 992 | NOTE: this uses a custom mn function |
| 993 | required params: |
| 994 | node1 = the string node name of the first endpoint of the link |
| 995 | node2 = the string node name of the second endpoint of the link |
| 996 | returns: main.FASLE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 997 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 998 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 999 | response = self.execute( |
| 1000 | cmd=command, |
| 1001 | prompt="mininet>", |
| 1002 | timeout=10 ) |
| 1003 | if re.search( "no node named", response ): |
| 1004 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1005 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1006 | elif re.search( "Error", response ): |
| 1007 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1008 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1009 | elif re.search( "usage:", response ): |
| 1010 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1011 | return main.FALSE |
| 1012 | else: |
| 1013 | return main.TRUE |
| 1014 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1015 | main.log.error( self.name + ": EOF exception found" ) |
| 1016 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1017 | main.cleanup() |
| 1018 | main.exit() |
| 1019 | |
| 1020 | def add_host( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1021 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1022 | Add a host to the mininet topology |
| 1023 | NOTE: this uses a custom mn function |
| 1024 | NOTE: cannot currently specify what type of host |
| 1025 | required params: |
| 1026 | hostname = the string hostname |
| 1027 | optional key-value params |
| 1028 | switch = "switch name" |
| 1029 | returns: main.FASLE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1030 | """ |
| 1031 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1032 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1033 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1034 | response = self.execute( |
| 1035 | cmd=command, |
| 1036 | prompt="mininet>", |
| 1037 | timeout=10 ) |
| 1038 | if re.search( "already exists!", response ): |
| 1039 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1040 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1041 | elif re.search( "doesnt exists!", response ): |
| 1042 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1043 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1044 | elif re.search( "Error", response ): |
| 1045 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1046 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1047 | elif re.search( "usage:", response ): |
| 1048 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1049 | return main.FALSE |
| 1050 | else: |
| 1051 | return main.TRUE |
| 1052 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1053 | main.log.error( self.name + ": EOF exception found" ) |
| 1054 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1055 | main.cleanup() |
| 1056 | main.exit() |
| 1057 | |
| 1058 | def del_host( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1059 | """ |
| 1060 | delete a host from the mininet topology |
| 1061 | NOTE: this uses a custom mn function |
| 1062 | required params: |
| 1063 | hostname = the string hostname |
| 1064 | returns: main.FASLE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1065 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1066 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1067 | response = self.execute( |
| 1068 | cmd=command, |
| 1069 | prompt="mininet>", |
| 1070 | timeout=10 ) |
| 1071 | if re.search( "no host named", response ): |
| 1072 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1073 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1074 | elif re.search( "Error", response ): |
| 1075 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1076 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1077 | elif re.search( "usage:", response ): |
| 1078 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1079 | return main.FALSE |
| 1080 | else: |
| 1081 | return main.TRUE |
| 1082 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1083 | main.log.error( self.name + ": EOF exception found" ) |
| 1084 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1085 | main.cleanup() |
| 1086 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1087 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1088 | def disconnect( self ): |
| 1089 | main.log.info( self.name + ": Disconnecting mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1090 | response = '' |
| 1091 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1092 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1093 | response = self.execute( |
| 1094 | cmd="exit", |
| 1095 | prompt="(.*)", |
| 1096 | timeout=120 ) |
| 1097 | response = self.execute( |
| 1098 | cmd="exit", |
| 1099 | prompt="(.*)", |
| 1100 | timeout=120 ) |
| 1101 | self.handle.sendline( "sudo mn -c" ) |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 1102 | response = main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1103 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1104 | main.log.error( self.name + ": EOF exception found" ) |
| 1105 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1106 | main.cleanup() |
| 1107 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1108 | else: |
| 1109 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1110 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1111 | return response |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1112 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1113 | def arping( self, src, dest, destmac ): |
| 1114 | self.handle.sendline( '' ) |
| 1115 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1116 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1117 | self.handle.sendline( src + ' arping ' + dest ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1118 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1119 | self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] ) |
| 1120 | main.log.info( self.name + ": ARP successful" ) |
| 1121 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1122 | return main.TRUE |
| 1123 | except: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1124 | main.log.warn( self.name + ": ARP FAILURE" ) |
| 1125 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1126 | return main.FALSE |
| 1127 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1128 | def decToHex( self, num ): |
| 1129 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1130 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1131 | def getSwitchFlowCount( self, switch ): |
| 1132 | """ |
| 1133 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1134 | if self.handle: |
| 1135 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 1136 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1137 | response = self.execute( |
| 1138 | cmd=cmd, |
| 1139 | prompt="mininet>", |
| 1140 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1141 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1142 | main.log.error( self.name + ": EOF exception found" ) |
| 1143 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1144 | main.cleanup() |
| 1145 | main.exit() |
| 1146 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1147 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1148 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1149 | main.log.info( |
| 1150 | "Couldn't find flows on switch %s, found: %s" % |
| 1151 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1152 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1153 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1154 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1155 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1156 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1157 | def check_flows( self, sw, dump_format=None ): |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 1158 | if dump_format: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1159 | command = "sh ovs-ofctl -F " + \ |
| 1160 | dump_format + " dump-flows " + str( sw ) |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 1161 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1162 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1163 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1164 | response = self.execute( |
| 1165 | cmd=command, |
| 1166 | prompt="mininet>", |
| 1167 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1168 | return response |
| 1169 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1170 | main.log.error( self.name + ": EOF exception found" ) |
| 1171 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1172 | main.cleanup() |
| 1173 | main.exit() |
| 1174 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1175 | main.log.info( response ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1176 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1177 | def start_tcpdump( self, filename, intf="eth0", port="port 6633" ): |
| 1178 | """ |
| 1179 | Runs tpdump on an intferface and saves the file |
| 1180 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1181 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1182 | self.handle.sendline( "" ) |
| 1183 | self.handle.expect( "mininet>" ) |
| 1184 | self.handle.sendline( |
| 1185 | "sh sudo tcpdump -n -i " + |
| 1186 | intf + |
| 1187 | " " + |
| 1188 | port + |
| 1189 | " -w " + |
| 1190 | filename.strip() + |
| 1191 | " &" ) |
| 1192 | self.handle.sendline( "" ) |
| 1193 | i = self.handle.expect( [ 'No\ssuch\device', |
| 1194 | 'listening\son', |
| 1195 | pexpect.TIMEOUT, |
| 1196 | "mininet>" ], |
| 1197 | timeout=10 ) |
| 1198 | main.log.warn( self.handle.before + self.handle.after ) |
| 1199 | self.handle.sendline( "" ) |
| 1200 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1201 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1202 | main.log.error( |
| 1203 | self.name + |
| 1204 | ": tcpdump - No such device exists. " + |
| 1205 | "tcpdump attempted on: " + |
| 1206 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1207 | return main.FALSE |
| 1208 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1209 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1210 | return main.TRUE |
| 1211 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1212 | main.log.error( |
| 1213 | self.name + |
| 1214 | ": tcpdump command timed out! Check interface name," + |
| 1215 | " given interface was: " + |
| 1216 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1217 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1218 | elif i == 3: |
| 1219 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1220 | return main.TRUE |
| 1221 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1222 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1223 | return main.FALSE |
| 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 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1227 | main.cleanup() |
| 1228 | main.exit() |
| 1229 | except: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1230 | main.log.info( self.name + ":" * 50 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1231 | main.log.error( traceback.print_exc() ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1232 | main.log.info(":" * 50 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1233 | main.cleanup() |
| 1234 | main.exit() |
| 1235 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1236 | def stop_tcpdump( self ): |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1237 | "pkills tcpdump" |
| 1238 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1239 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 1240 | self.handle.expect( "mininet>" ) |
| 1241 | self.handle.sendline( "" ) |
| 1242 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1243 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1244 | main.log.error( self.name + ": EOF exception found" ) |
| 1245 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1246 | main.cleanup() |
| 1247 | main.exit() |
| 1248 | except: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1249 | main.log.info( self.name + ":" * 50 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1250 | main.log.error( traceback.print_exc() ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1251 | main.log.info(":" * 50 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1252 | main.cleanup() |
| 1253 | main.exit() |
| 1254 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1255 | def compare_switches( self, topo, switches_json ): |
| 1256 | """ |
| 1257 | Compare mn and onos switches |
| 1258 | topo: sts TestONTopology object |
| 1259 | switches_json: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1260 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1261 | This uses the sts TestONTopology object""" |
| 1262 | # main.log.debug( "Switches_json string: ", switches_json ) |
| 1263 | output = { "switches": [] } |
| 1264 | # iterate through the MN topology and pull out switches and and port |
| 1265 | # info |
| 1266 | for switch in topo.graph.switches: |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1267 | ports = [] |
| 1268 | for port in switch.ports.values(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1269 | ports.append( { 'of_port': port.port_no, |
| 1270 | 'mac': str( port.hw_addr ).replace( '\'', |
| 1271 | ''), |
| 1272 | 'name': port.name } ) |
| 1273 | output[ 'switches' ].append( { |
| 1274 | "name": switch.name, |
| 1275 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1276 | "ports": ports } ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1277 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1278 | # print "mn" |
| 1279 | # print json.dumps( output, |
| 1280 | # sort_keys=True, |
| 1281 | # indent=4, |
| 1282 | # separators=( ',', ': ' ) ) |
| 1283 | # print "onos" |
| 1284 | # print json.dumps( switches_json, |
| 1285 | # sort_keys=True, |
| 1286 | # indent=4, |
| 1287 | # separators=( ',', ': ' ) ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1288 | |
| 1289 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1290 | mnDPIDs = [] |
| 1291 | for switch in output[ 'switches' ]: |
| 1292 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1293 | mnDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1294 | # print "List of Mininet switch DPID's" |
| 1295 | # print mnDPIDs |
| 1296 | if switches_json == "": # if rest call fails |
| 1297 | main.log.error( |
| 1298 | self.name + |
| 1299 | ".compare_switches(): Empty JSON object given from ONOS" ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1300 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1301 | onos = switches_json |
| 1302 | onosDPIDs = [] |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1303 | for switch in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1304 | if switch[ 'available' ]: |
| 1305 | onosDPIDs.append( |
| 1306 | switch[ 'id' ].replace( |
| 1307 | ":", |
| 1308 | '' ).replace( |
| 1309 | "of", |
| 1310 | '' ).lower() ) |
| 1311 | # else: |
| 1312 | # print "Switch is unavailable:" |
| 1313 | # print switch |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1314 | onosDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1315 | # print "List of ONOS switch DPID's" |
| 1316 | # print onosDPIDs |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1317 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1318 | if mnDPIDs != onosDPIDs: |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1319 | switch_results = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1320 | main.log.report( "Switches in MN but not in ONOS:" ) |
| 1321 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
| 1322 | main.log.report( str( list1 ) ) |
| 1323 | main.log.report( "Switches in ONOS but not in MN:" ) |
| 1324 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
| 1325 | main.log.report(str( list2 ) ) |
| 1326 | else: # list of dpid's match in onos and mn |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1327 | switch_results = main.TRUE |
| 1328 | return switch_results |
| 1329 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1330 | def compare_ports( self, topo, ports_json ): |
| 1331 | """ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1332 | Compare mn and onos ports |
| 1333 | topo: sts TestONTopology object |
| 1334 | ports_json: parsed json object from the onos ports api |
| 1335 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1336 | Dependencies: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1337 | 1. This uses the sts TestONTopology object |
| 1338 | 2. numpy - "sudo pip install numpy" |
| 1339 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1340 | """ |
| 1341 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 1342 | # ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1343 | from numpy import uint64 |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1344 | ports_results = main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1345 | output = { "switches": [] } |
| 1346 | # iterate through the MN topology and pull out switches and and port |
| 1347 | # info |
| 1348 | for switch in topo.graph.switches: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1349 | ports = [] |
| 1350 | for port in switch.ports.values(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1351 | # print port.hw_addr.toStr( separator='' ) |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1352 | tmp_port = {} |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1353 | tmp_port[ 'of_port' ] = port.port_no |
| 1354 | tmp_port[ 'mac' ] = str( port.hw_addr ).replace( '\'', '' ) |
| 1355 | tmp_port[ 'name' ] = port.name |
| 1356 | tmp_port[ 'enabled' ] = port.enabled |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1357 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1358 | ports.append( tmp_port ) |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1359 | tmp_switch = {} |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1360 | tmp_switch[ 'name' ] = switch.name |
| 1361 | tmp_switch[ 'dpid' ] = str( switch.dpid ).zfill( 16 ) |
| 1362 | tmp_switch[ 'ports' ] = ports |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1363 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1364 | output[ 'switches' ].append( tmp_switch ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1365 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1366 | # PORTS |
| 1367 | for mn_switch in output[ 'switches' ]: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1368 | mn_ports = [] |
| 1369 | onos_ports = [] |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1370 | switch_result = main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1371 | for port in mn_switch[ 'ports' ]: |
| 1372 | if port[ 'enabled' ]: |
| 1373 | mn_ports.append( port[ 'of_port' ] ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1374 | for onos_switch in ports_json: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1375 | # print "Iterating through a new switch as seen by ONOS" |
| 1376 | # print onos_switch |
| 1377 | if onos_switch[ 'device' ][ 'available' ]: |
| 1378 | if onos_switch[ 'device' ][ 'id' ].replace( |
| 1379 | ':', |
| 1380 | '' ).replace( |
| 1381 | "of", |
| 1382 | '' ) == mn_switch[ 'dpid' ]: |
| 1383 | for port in onos_switch[ 'ports' ]: |
| 1384 | if port[ 'isEnabled' ]: |
| 1385 | if port[ 'port' ] == 'local': |
| 1386 | # onos_ports.append( 'local' ) |
| 1387 | onos_ports.append( long( uint64( -2 ) ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1388 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1389 | onos_ports.append( int( port[ 'port' ] ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1390 | break |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1391 | mn_ports.sort( key=float ) |
| 1392 | onos_ports.sort( key=float ) |
| 1393 | # print "\nPorts for Switch %s:" % ( mn_switch[ 'name' ] ) |
| 1394 | # print "\tmn_ports[] = ", mn_ports |
| 1395 | # print "\tonos_ports[] = ", onos_ports |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1396 | mn_ports_log = mn_ports |
| 1397 | onos_ports_log = onos_ports |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1398 | mn_ports = [ x for x in mn_ports ] |
| 1399 | onos_ports = [ x for x in onos_ports ] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1400 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1401 | # TODO: handle other reserved port numbers besides LOCAL |
| 1402 | # NOTE: Reserved ports |
| 1403 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 1404 | # long( uint64( -2 ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1405 | for mn_port in mn_ports_log: |
| 1406 | if mn_port in onos_ports: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1407 | # don't set results to true here as this is just one of |
| 1408 | # many checks and it might override a failure |
| 1409 | mn_ports.remove( mn_port ) |
| 1410 | onos_ports.remove( mn_port ) |
| 1411 | # NOTE: OVS reports this as down since there is no link |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1412 | # So ignoring these for now |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1413 | # TODO: Come up with a better way of handling these |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1414 | if 65534 in mn_ports: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1415 | mn_ports.remove( 65534 ) |
| 1416 | if long( uint64( -2 ) ) in onos_ports: |
| 1417 | onos_ports.remove( long( uint64( -2 ) ) ) |
| 1418 | if len( mn_ports ): # the ports of this switch don't match |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1419 | switch_result = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1420 | main.log.warn( "Ports in MN but not ONOS: " + str( mn_ports ) ) |
| 1421 | if len( onos_ports ): # the ports of this switch don't match |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1422 | switch_result = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1423 | main.log.warn( |
| 1424 | "Ports in ONOS but not MN: " + |
| 1425 | str( onos_ports ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1426 | if switch_result == main.FALSE: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1427 | main.log.report( |
| 1428 | "The list of ports for switch %s(%s) does not match:" % |
| 1429 | ( mn_switch[ 'name' ], mn_switch[ 'dpid' ] ) ) |
| 1430 | main.log.warn( "mn_ports[] = " + str( mn_ports_log ) ) |
| 1431 | main.log.warn( "onos_ports[] = " + str( onos_ports_log ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1432 | ports_results = ports_results and switch_result |
| 1433 | return ports_results |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1434 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1435 | def compare_links( self, topo, links_json ): |
| 1436 | """ |
| 1437 | Compare mn and onos links |
| 1438 | topo: sts TestONTopology object |
| 1439 | links_json: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1440 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1441 | This uses the sts TestONTopology object""" |
| 1442 | # FIXME: this does not look for extra links in ONOS, only checks that |
| 1443 | # ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1444 | link_results = main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1445 | output = { "switches": [] } |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1446 | onos = links_json |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1447 | # iterate through the MN topology and pull out switches and and port |
| 1448 | # info |
| 1449 | for switch in topo.graph.switches: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1450 | # print "Iterating though switches as seen by Mininet" |
| 1451 | # print switch |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1452 | ports = [] |
| 1453 | for port in switch.ports.values(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1454 | # print port.hw_addr.toStr( separator='' ) |
| 1455 | ports.append( { 'of_port': port.port_no, |
| 1456 | 'mac': str( port.hw_addr ).replace( '\'', |
| 1457 | ''), |
| 1458 | 'name': port.name } ) |
| 1459 | output[ 'switches' ].append( { |
| 1460 | "name": switch.name, |
| 1461 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1462 | "ports": ports } ) |
| 1463 | # LINKS |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1464 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1465 | mn_links = [ |
| 1466 | link for link in topo.patch_panel.network_links if ( |
| 1467 | link.port1.enabled and link.port2.enabled ) ] |
| 1468 | if 2 * len( mn_links ) == len( onos ): |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1469 | link_results = main.TRUE |
| 1470 | else: |
| 1471 | link_results = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1472 | main.log.report( |
| 1473 | "Mininet has %i bidirectional links and " + |
| 1474 | "ONOS has %i unidirectional links" % |
| 1475 | ( len( mn_links ), len( onos ) ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1476 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1477 | # iterate through MN links and check if an ONOS link exists in |
| 1478 | # both directions |
| 1479 | # NOTE: Will currently only show mn links as down if they are |
| 1480 | # cut through STS. We can either do everything through STS or |
| 1481 | # wait for up_network_links and down_network_links to be |
| 1482 | # fully implemented. |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1483 | for link in mn_links: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1484 | # print "Link: %s" % link |
| 1485 | # TODO: Find a more efficient search method |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1486 | node1 = None |
| 1487 | port1 = None |
| 1488 | node2 = None |
| 1489 | port2 = None |
| 1490 | first_dir = main.FALSE |
| 1491 | second_dir = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1492 | for switch in output[ 'switches' ]: |
| 1493 | # print "Switch: %s" % switch[ 'name' ] |
| 1494 | if switch[ 'name' ] == link.node1.name: |
| 1495 | node1 = switch[ 'dpid' ] |
| 1496 | for port in switch[ 'ports' ]: |
| 1497 | if str( port[ 'name' ] ) == str( link.port1 ): |
| 1498 | port1 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1499 | if node1 is not None and node2 is not None: |
| 1500 | break |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1501 | if switch[ 'name' ] == link.node2.name: |
| 1502 | node2 = switch[ 'dpid' ] |
| 1503 | for port in switch[ 'ports' ]: |
| 1504 | if str( port[ 'name' ] ) == str( link.port2 ): |
| 1505 | port2 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1506 | if node1 is not None and node2 is not None: |
| 1507 | break |
| 1508 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1509 | for onos_link in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1510 | onos_node1 = onos_link[ 'src' ][ 'device' ].replace( |
| 1511 | ":", |
| 1512 | '' ).replace( |
| 1513 | "of", |
| 1514 | '' ) |
| 1515 | onos_node2 = onos_link[ 'dst' ][ 'device' ].replace( |
| 1516 | ":", |
| 1517 | '' ).replace( |
| 1518 | "of", |
| 1519 | '' ) |
| 1520 | onos_port1 = onos_link[ 'src' ][ 'port' ] |
| 1521 | onos_port2 = onos_link[ 'dst' ][ 'port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1522 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1523 | # check onos link from node1 to node2 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1524 | if str( onos_node1 ) == str( node1 ) and str( |
| 1525 | onos_node2 ) == str( node2 ): |
| 1526 | if int( onos_port1 ) == int( port1 ) and int( |
| 1527 | onos_port2 ) == int( port2 ): |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1528 | first_dir = main.TRUE |
| 1529 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1530 | main.log.warn( |
| 1531 | 'The port numbers do not match for ' + |
| 1532 | str( link ) + |
| 1533 | ' between ONOS and MN. When cheking ONOS for ' + |
| 1534 | 'link %s/%s -> %s/%s' % |
| 1535 | ( node1, |
| 1536 | port1, |
| 1537 | node2, |
| 1538 | port2 ) + |
| 1539 | ' ONOS has the values %s/%s -> %s/%s' % |
| 1540 | ( onos_node1, |
| 1541 | onos_port1, |
| 1542 | onos_node2, |
| 1543 | onos_port2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1544 | |
| 1545 | # check onos link from node2 to node1 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1546 | elif ( str( onos_node1 ) == str( node2 ) and |
| 1547 | str( onos_node2 ) == str( node1 ) ): |
| 1548 | if ( int( onos_port1 ) == int( port2 ) |
| 1549 | and int( onos_port2 ) == int( port1 ) ): |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1550 | second_dir = main.TRUE |
| 1551 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1552 | main.log.warn( |
| 1553 | 'The port numbers do not match for ' + |
| 1554 | str( link ) + |
| 1555 | ' between ONOS and MN. When cheking ONOS for ' + |
| 1556 | 'link %s/%s -> %s/%s' % |
| 1557 | ( node2, |
| 1558 | port2, |
| 1559 | node1, |
| 1560 | port1 ) + |
| 1561 | ' ONOS has the values %s/%s -> %s/%s' % |
| 1562 | ( onos_node2, |
| 1563 | onos_port2, |
| 1564 | onos_node1, |
| 1565 | onos_port1 ) ) |
| 1566 | else: # this is not the link you're looking for |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1567 | pass |
| 1568 | if not first_dir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1569 | main.log.report( |
| 1570 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1571 | ( node1, port1, node2, port2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1572 | if not second_dir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1573 | main.log.report( |
| 1574 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1575 | ( node2, port2, node1, port1 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1576 | link_results = link_results and first_dir and second_dir |
Jon Hall | 62df924 | 2014-10-22 12:20:17 -0400 | [diff] [blame] | 1577 | return link_results |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1578 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1579 | def get_hosts( self ): |
| 1580 | """ |
| 1581 | Returns a list of all hosts |
| 1582 | Don't ask questions just use it""" |
| 1583 | self.handle.sendline( "" ) |
| 1584 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1585 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1586 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 1587 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1588 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1589 | handle_py = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1590 | handle_py = handle_py.split( "]\r\n", 1 )[ 1 ] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1591 | handle_py = handle_py.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1592 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1593 | self.handle.sendline( "" ) |
| 1594 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1595 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1596 | host_str = handle_py.replace( "]", "" ) |
| 1597 | host_str = host_str.replace( "'", "" ) |
| 1598 | host_str = host_str.replace( "[", "" ) |
| 1599 | host_list = host_str.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1600 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1601 | return host_list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1602 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1603 | def update( self ): |
| 1604 | """ |
| 1605 | updates the port address and status information for |
| 1606 | each port in mn""" |
| 1607 | # TODO: Add error checking. currently the mininet command has no output |
| 1608 | main.log.info( "Updateing MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1609 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1610 | self.handle.sendline( "" ) |
| 1611 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1612 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1613 | self.handle.sendline( "update" ) |
| 1614 | self.handle.expect( "update" ) |
| 1615 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1616 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1617 | self.handle.sendline( "" ) |
| 1618 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1619 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1620 | return main.TRUE |
| 1621 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1622 | main.log.error( self.name + ": EOF exception found" ) |
| 1623 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1624 | main.cleanup() |
| 1625 | main.exit() |
| 1626 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1627 | if __name__ != "__main__": |
| 1628 | import sys |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1629 | sys.modules[ __name__ ] = MininetCliDriver() |