blob: 3b5a5c7734440c4fc5407904ca1b3a7c9bd194d2 [file] [log] [blame]
kaouthera3f13ca22015-05-05 15:01:41 -07001
adminbae64d82013-08-01 10:50:15 -07002#!/usr/bin/env python
Jon Hall7eb38402015-01-08 17:19:54 -08003"""
adminbae64d82013-08-01 10:50:15 -07004Created on 26-Oct-2012
5
Jon Hallbe6dfc42015-01-12 17:37:25 -08006author: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07007
8
Jon Hall7eb38402015-01-08 17:19:54 -08009TestON is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 2 of the License, or
12( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070013
Jon Hall7eb38402015-01-08 17:19:54 -080014TestON is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
adminbae64d82013-08-01 10:50:15 -070018
Jon Hall7eb38402015-01-08 17:19:54 -080019You should have received a copy of the GNU General Public License
20along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070021
22
Jon Hallbe6dfc42015-01-12 17:37:25 -080023MininetCliDriver is the basic driver which will handle the Mininet functions
24
Jon Hall272a4db2015-01-12 17:43:48 -080025Some functions rely on STS module. To install this,
26 git clone https://github.com/jhall11/sts.git
27
Jon Hallbe6dfc42015-01-12 17:37:25 -080028Some functions rely on a modified version of Mininet. These functions
29should all be noted in the comments. To get this MN version run these commands
30from within your Mininet folder:
Jon Hall272a4db2015-01-12 17:43:48 -080031 git remote add jhall11 https://github.com/jhall11/mininet.git
Jon Hallbe6dfc42015-01-12 17:37:25 -080032 git fetch jhall11
Jon Hall272a4db2015-01-12 17:43:48 -080033 git checkout -b dynamic_topo remotes/jhall11/dynamic_topo
Jon Hallbe6dfc42015-01-12 17:37:25 -080034 git pull
35
Jon Hall272a4db2015-01-12 17:43:48 -080036
37 Note that you may need to run 'sudo make develop' if your mnexec.c file
Jon Hallbe6dfc42015-01-12 17:37:25 -080038changed when switching branches."""
adminbae64d82013-08-01 10:50:15 -070039import pexpect
adminbae64d82013-08-01 10:50:15 -070040import re
41import sys
Jon Hall7eb38402015-01-08 17:19:54 -080042sys.path.append( "../" )
Jon Hall1ccf82c2014-10-15 14:55:16 -040043from math import pow
adminbae64d82013-08-01 10:50:15 -070044from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070045
Jon Hall7eb38402015-01-08 17:19:54 -080046
kelvin-onlab50907142015-04-01 13:37:45 -070047class MininetCliDriver( Emulator ):
Jon Hall7eb38402015-01-08 17:19:54 -080048
49 """
50 MininetCliDriver is the basic driver which will handle
51 the Mininet functions"""
52 def __init__( self ):
53 super( Emulator, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070054 self.handle = self
Jon Hallefbd9792015-03-05 16:11:36 -080055 self.name = None
Jon Hall7eb38402015-01-08 17:19:54 -080056 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070057 self.flag = 0
58
Jon Hall7eb38402015-01-08 17:19:54 -080059 def connect( self, **connectargs ):
60 """
61 Here the main is the TestON instance after creating
62 all the log handles."""
kelvin-onlaba1484582015-02-02 15:46:20 -080063 try:
64 for key in connectargs:
65 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080066
kelvin-onlaba1484582015-02-02 15:46:20 -080067 self.name = self.options[ 'name' ]
68 self.handle = super(
kelvin-onlab50907142015-04-01 13:37:45 -070069 MininetCliDriver,
kelvin-onlaba1484582015-02-02 15:46:20 -080070 self ).connect(
71 user_name=self.user_name,
72 ip_address=self.ip_address,
73 port=None,
74 pwd=self.pwd )
Jon Hallfbc828e2015-01-06 17:30:19 -080075
kelvin-onlaba1484582015-02-02 15:46:20 -080076 if self.handle:
Jon Hallefbd9792015-03-05 16:11:36 -080077 main.log.info( "Connection successful to the host " +
78 self.user_name +
79 "@" +
80 self.ip_address )
kelvin-onlaba1484582015-02-02 15:46:20 -080081 return main.TRUE
82 else:
83 main.log.error( "Connection failed to the host " +
Jon Hallefbd9792015-03-05 16:11:36 -080084 self.user_name +
85 "@" +
86 self.ip_address )
Jon Hallfebb1c72015-03-05 13:30:09 -080087 main.log.error( "Failed to connect to the Mininet CLI" )
kelvin-onlaba1484582015-02-02 15:46:20 -080088 return main.FALSE
89 except pexpect.EOF:
90 main.log.error( self.name + ": EOF exception found" )
91 main.log.error( self.name + ": " + self.handle.before )
92 main.cleanup()
93 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080094 except Exception:
95 main.log.exception( self.name + ": Uncaught exception!" )
kelvin-onlaba1484582015-02-02 15:46:20 -080096 main.cleanup()
97 main.exit()
98
Jon Hallefbd9792015-03-05 16:11:36 -080099 def startNet( self, topoFile='', args='', timeout=120 ):
kelvin-onlab00ac67b2015-02-04 09:52:02 -0800100 """
101 Starts Mininet accepts a topology(.py) file and/or an optional
Jon Hallefbd9792015-03-05 16:11:36 -0800102 argument ,to start the mininet, as a parameter.
Jon Hall21270ac2015-02-16 17:59:55 -0800103 Returns main.TRUE if the mininet starts successfully and
104 main.FALSE otherwise
kelvin-onlab00ac67b2015-02-04 09:52:02 -0800105 """
Jon Hall7eb38402015-01-08 17:19:54 -0800106 if self.handle:
Jon Hall689d8e42015-04-03 13:59:24 -0700107 # make sure old networks are cleaned up
108 main.log.info( self.name +
109 ": Clearing any residual state or processes" )
Jon Hall7eb38402015-01-08 17:19:54 -0800110 self.handle.sendline( "sudo mn -c" )
111 i = self.handle.expect( [ 'password\sfor\s',
112 'Cleanup\scomplete',
113 pexpect.EOF,
114 pexpect.TIMEOUT ],
kelvin-onlaba1484582015-02-02 15:46:20 -0800115 timeout )
Jon Hall7eb38402015-01-08 17:19:54 -0800116 if i == 0:
Jon Hall689d8e42015-04-03 13:59:24 -0700117 # Sudo asking for password
Jon Hall7eb38402015-01-08 17:19:54 -0800118 main.log.info( self.name + ": Sending sudo password" )
119 self.handle.sendline( self.pwd )
Jon Hallefbd9792015-03-05 16:11:36 -0800120 i = self.handle.expect( [ '%s:' % self.user,
Jon Hall7eb38402015-01-08 17:19:54 -0800121 '\$',
122 pexpect.EOF,
123 pexpect.TIMEOUT ],
kelvin-onlaba1484582015-02-02 15:46:20 -0800124 timeout )
Jon Hall7eb38402015-01-08 17:19:54 -0800125 if i == 1:
126 main.log.info( self.name + ": Clean" )
127 elif i == 2:
128 main.log.error( self.name + ": Connection terminated" )
129 elif i == 3: # timeout
Jon Hall689d8e42015-04-03 13:59:24 -0700130 main.log.error( self.name + ": Something while cleaning " +
131 "Mininet took too long... " )
132 # Craft the string to start mininet
133 cmdString = "sudo "
134 if topoFile is None or topoFile == '': # If no file is given
135 main.log.info( self.name + ": building fresh Mininet" )
136 cmdString += "mn "
137 if args is None or args == '':
138 # If no args given, use args from .topo file
139 args = self.options[ 'arg1' ] +\
140 " " + self.options[ 'arg2' ] +\
141 " --mac --controller " +\
142 self.options[ 'controller' ] + " " +\
143 self.options[ 'arg3' ]
144 else: # else only use given args
145 pass
146 # TODO: allow use of topo args and method args?
147 else: # Use given topology file
148 main.log.info( "Starting Mininet from topo file " + topoFile )
149 cmdString += topoFile + " "
Jon Hallefbd9792015-03-05 16:11:36 -0800150 if args is None:
kelvin-onlaba1484582015-02-02 15:46:20 -0800151 args = ''
Jon Hall689d8e42015-04-03 13:59:24 -0700152 # TODO: allow use of args from .topo file?
153 cmdString += args
154 # Send the command and check if network started
155 self.handle.sendline( "" )
156 self.handle.expect( '\$' )
157 main.log.info( "Sending '" + cmdString + "' to " + self.name )
158 self.handle.sendline( cmdString )
159 while True:
Jon Hall7eb38402015-01-08 17:19:54 -0800160 i = self.handle.expect( [ 'mininet>',
Jon Hall689d8e42015-04-03 13:59:24 -0700161 'Exception',
162 '\*\*\*',
Jon Hallefbd9792015-03-05 16:11:36 -0800163 pexpect.EOF,
164 pexpect.TIMEOUT ],
Jon Hall689d8e42015-04-03 13:59:24 -0700165 timeout )
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800166 if i == 0:
Jon Hall689d8e42015-04-03 13:59:24 -0700167 main.log.info( self.name + ": Mininet built" )
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800168 return main.TRUE
kelvin-onlabec228b82015-02-09 15:45:55 -0800169 elif i == 1:
Jon Hall689d8e42015-04-03 13:59:24 -0700170 response = str( self.handle.before +
171 self.handle.after )
172 self.handle.expect( '\$' )
173 response += str( self.handle.before +
174 self.handle.after )
175 main.log.error(
176 self.name +
177 ": Launching Mininet failed: " + response )
178 return main.FALSE
179 elif i == 2:
180 self.handle.expect( [ "\n",
181 pexpect.EOF,
182 pexpect.TIMEOUT ],
183 timeout )
184 main.log.info( self.handle.before )
185 elif i == 3:
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800186 main.log.error( self.name + ": Connection timeout" )
187 return main.FALSE
Jon Hall689d8e42015-04-03 13:59:24 -0700188 elif i == 4: # timeout
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800189 main.log.error(
190 self.name +
191 ": Something took too long... " )
192 return main.FALSE
Jon Hall689d8e42015-04-03 13:59:24 -0700193 # Why did we hit this part?
194 main.log.error( "startNet did not return correctly" )
195 return main.FASLE
Jon Hall7eb38402015-01-08 17:19:54 -0800196 else: # if no handle
Jon Hall689d8e42015-04-03 13:59:24 -0700197 main.log.error( self.name + ": Connection failed to the host " +
198 self.user_name + "@" + self.ip_address )
Jon Hall7eb38402015-01-08 17:19:54 -0800199 main.log.error( self.name + ": Failed to connect to the Mininet" )
adminbae64d82013-08-01 10:50:15 -0700200 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800201
kelvin-onlabfccaafa2015-01-20 13:50:44 -0800202 def numSwitchesNlinks( self, topoType, depth, fanout ):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400203 if topoType == 'tree':
Jon Hall7eb38402015-01-08 17:19:54 -0800204 # In tree topology, if fanout arg is not given, by default it is 2
205 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400206 fanout = 2
207 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500208 count = 0
Jon Hall7eb38402015-01-08 17:19:54 -0800209 while( k <= depth - 1 ):
210 count = count + pow( fanout, k )
211 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800212 numSwitches = count
Jon Hall7eb38402015-01-08 17:19:54 -0800213 while( k <= depth - 2 ):
214 # depth-2 gives you only core links and not considering
215 # edge links as seen by ONOS. If all the links including
216 # edge links are required, do depth-1
217 count = count + pow( fanout, k )
218 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 numLinks = count * fanout
Jon Hall7eb38402015-01-08 17:19:54 -0800220 # print "num_switches for %s(%d,%d) = %d and links=%d" %(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 # topoType,depth,fanout,numSwitches,numLinks )
Jon Hallfbc828e2015-01-06 17:30:19 -0800222
Jon Hall7eb38402015-01-08 17:19:54 -0800223 elif topoType == 'linear':
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 # In linear topology, if fanout or numHostsPerSw is not given,
Jon Hall7eb38402015-01-08 17:19:54 -0800225 # by default it is 1
226 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400227 fanout = 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 numSwitches = depth
229 numHostsPerSw = fanout
230 totalNumHosts = numSwitches * numHostsPerSw
231 numLinks = totalNumHosts + ( numSwitches - 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800232 print "num_switches for %s(%d,%d) = %d and links=%d" %\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 ( topoType, depth, fanout, numSwitches, numLinks )
Jon Hallefbd9792015-03-05 16:11:36 -0800234 topoDict = { "num_switches": int( numSwitches ),
235 "num_corelinks": int( numLinks ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400236 return topoDict
237
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 def calculateSwAndLinks( self ):
Jon Hall689d8e42015-04-03 13:59:24 -0700239 """
240 Calculate the number of switches and links in a topo."""
241 # TODO: combine this function and numSwitchesNlinks
242 argList = self.options[ 'arg1' ].split( "," )
243 topoArgList = argList[ 0 ].split( " " )
244 argList = map( int, argList[ 1: ] )
245 topoArgList = topoArgList[ 1: ] + argList
246
247 topoDict = self.numSwitchesNlinks( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400248 return topoDict
249
kelvin-onlabc44f0192015-04-02 22:08:41 -0700250 def pingall( self, timeout=300, shortCircuit=False, acceptableFailed=0):
Jon Hall7eb38402015-01-08 17:19:54 -0800251 """
252 Verifies the reachability of the hosts using pingall command.
253 Optional parameter timeout allows you to specify how long to
254 wait for pingall to complete
kelvin-onlabfbcd82f2015-04-02 12:06:00 -0700255 Optional:
Jon Hall390696c2015-05-05 17:13:41 -0700256 timeout(seconds) - How long to wait before breaking the pingall
kelvin-onlabfbcd82f2015-04-02 12:06:00 -0700257 shortCircuit - Break the pingall based on the number of failed hosts
kelvin-onlabc44f0192015-04-02 22:08:41 -0700258 ping
259 acceptableFailed - Set the number of acceptable failed pings for the
260 function to still return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800261 Returns:
262 main.TRUE if pingall completes with no pings dropped
Jon Hall390696c2015-05-05 17:13:41 -0700263 otherwise main.FALSE
264 """
265 import time
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700266 try:
Jon Hallfb760a02015-04-13 15:35:03 -0700267 timeout = int( timeout )
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700268 if self.handle:
269 main.log.info(
270 self.name +
271 ": Checking reachabilty to the hosts using pingall" )
272 response = ""
273 failedPings = 0
274 returnValue = main.TRUE
275 self.handle.sendline( "pingall" )
Jon Hall390696c2015-05-05 17:13:41 -0700276 startTime = time.time()
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700277 while True:
278 i = self.handle.expect( [ "mininet>","X",
279 pexpect.EOF,
280 pexpect.TIMEOUT ],
281 timeout )
282 if i == 0:
283 main.log.info( self.name + ": pingall finished")
284 response += self.handle.before
285 break
286 elif i == 1:
287 response += self.handle.before + self.handle.after
288 failedPings = failedPings + 1
kelvin-onlabd26a3742015-04-06 15:31:16 -0700289 if failedPings > acceptableFailed:
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700290 returnValue = main.FALSE
291 if shortCircuit:
292 main.log.error( self.name +
293 ": Aborting pingall - "
294 + str( failedPings ) +
295 " pings failed" )
296 break
Jon Hall390696c2015-05-05 17:13:41 -0700297 if ( time.time() - startTime ) > timeout:
298 returnValue = main.FALSE
299 main.log.error( self.name +
300 ": Aborting pingall - " +
301 "Function took too long " )
302 break
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700303 elif i == 2:
304 main.log.error( self.name +
305 ": EOF exception found" )
306 main.log.error( self.name + ": " +
307 self.handle.before )
308 main.cleanup()
309 main.exit()
310 elif i == 3:
311 response += self.handle.before
312 main.log.error( self.name +
313 ": TIMEOUT exception found" )
314 main.log.error( self.name +
315 ": " +
316 str( response ) )
317 # NOTE: Send ctrl-c to make sure pingall is done
318 self.handle.sendline( "\x03" )
319 self.handle.expect( "Interrupt" )
320 self.handle.expect( "mininet>" )
321 break
322 pattern = "Results\:"
323 main.log.info( "Pingall output: " + str( response ) )
324 if re.search( pattern, response ):
325 main.log.info( self.name + ": Pingall finished with "
326 + str( failedPings ) + " failed pings" )
327 return returnValue
328 else:
kelvin-onlabc44f0192015-04-02 22:08:41 -0700329 # NOTE: Send ctrl-c to make sure pingall is done
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700330 self.handle.sendline( "\x03" )
kelvin-onlabc44f0192015-04-02 22:08:41 -0700331 self.handle.expect( "Interrupt" )
kelvin-onlabc44f0192015-04-02 22:08:41 -0700332 self.handle.expect( "mininet>" )
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700333 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700334 else:
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700335 main.log.error( self.name + ": Connection failed to the host" )
336 main.cleanup()
337 main.exit()
338 except pexpect.TIMEOUT:
339 if response:
340 main.log.info( "Pingall output: " + str( response ) )
341 main.log.error( self.name + ": pexpect.TIMEOUT found" )
342 return main.FALSE
343 except pexpect.EOF:
344 main.log.error( self.name + ": EOF exception found" )
345 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500346 main.cleanup()
347 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700348
Jon Hall7eb38402015-01-08 17:19:54 -0800349 def fpingHost( self, **pingParams ):
350 """
351 Uses the fping package for faster pinging...
352 *requires fping to be installed on machine running mininet"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800353 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800354 command = args[ "SRC" ] + \
355 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
356 self.handle.sendline( command )
357 self.handle.expect(
358 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
359 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
360 response = self.handle.before
361 if re.search( ":\s-", response ):
362 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700363 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800364 elif re.search( ":\s\d{1,2}\.\d\d", response ):
365 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700366 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800367 main.log.info( self.name + ": Install fping on mininet machine... " )
368 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700369 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800370
Jon Hall7eb38402015-01-08 17:19:54 -0800371 def pingHost( self, **pingParams ):
372 """
373 Ping from one mininet host to another
374 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800375 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800376 command = args[ "SRC" ] + " ping " + \
377 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700378 try:
Jon Hall61282e32015-03-19 11:34:11 -0700379 main.log.info( "Sending: " + command )
Jon Hall7eb38402015-01-08 17:19:54 -0800380 self.handle.sendline( command )
381 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700382 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800383 main.log.error(
384 self.name +
385 ": timeout when waiting for response from mininet" )
386 main.log.error( "response: " + str( self.handle.before ) )
387 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700388 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800389 main.log.error(
390 self.name +
391 ": timeout when waiting for response from mininet" )
392 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700393 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800394 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800395 main.log.error( self.name + ": EOF exception found" )
396 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700397 main.cleanup()
398 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800399 main.log.info( self.name + ": Ping Response: " + response )
400 if re.search( ',\s0\%\spacket\sloss', response ):
401 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800402 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700403 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800404 else:
405 main.log.error(
406 self.name +
407 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700409 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800410
Jon Hall7eb38402015-01-08 17:19:54 -0800411 def checkIP( self, host ):
412 """
413 Verifies the host's ip configured or not."""
414 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700415 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800416 response = self.execute(
417 cmd=host +
418 " ifconfig",
419 prompt="mininet>",
420 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800421 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800422 main.log.error( self.name + ": EOF exception found" )
423 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700424 main.cleanup()
425 main.exit()
adminbae64d82013-08-01 10:50:15 -0700426
Jon Hall7eb38402015-01-08 17:19:54 -0800427 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800428 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
429 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
430 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
431 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
432 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800433 # pattern = "inet addr:10.0.0.6"
434 if re.search( pattern, response ):
435 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700436 return main.TRUE
437 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800438 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700439 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800440 else:
441 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800442
Jon Hall7eb38402015-01-08 17:19:54 -0800443 def verifySSH( self, **connectargs ):
Jon Hallefbd9792015-03-05 16:11:36 -0800444 # FIXME: Who uses this and what is the purpose? seems very specific
Jon Hall6094a362014-04-11 14:46:56 -0700445 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800446 response = self.execute(
447 cmd="h1 /usr/sbin/sshd -D&",
448 prompt="mininet>",
449 timeout=10 )
450 response = self.execute(
451 cmd="h4 /usr/sbin/sshd -D&",
452 prompt="mininet>",
453 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700454 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800455 vars( self )[ key ] = connectargs[ key ]
456 response = self.execute(
457 cmd="xterm h1 h4 ",
458 prompt="mininet>",
459 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800460 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800461 main.log.error( self.name + ": EOF exception found" )
462 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700463 main.cleanup()
464 main.exit()
adminbae64d82013-08-01 10:50:15 -0700465 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800466 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700467 if self.flag == 0:
468 self.flag = 1
469 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800470 else:
adminbae64d82013-08-01 10:50:15 -0700471 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800472
kelvin-onlaba1484582015-02-02 15:46:20 -0800473 def moveHost( self, host, oldSw, newSw, ):
474 """
475 Moves a host from one switch to another on the fly
476 Note: The intf between host and oldSw when detached
477 using detach(), will still show up in the 'net'
478 cmd, because switch.detach() doesn't affect switch.intfs[]
479 (which is correct behavior since the interfaces
480 haven't moved).
481 """
482 if self.handle:
483 try:
484 # Bring link between oldSw-host down
Jon Hallefbd9792015-03-05 16:11:36 -0800485 cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\
486 "'," + "'down')"
kelvin-onlaba1484582015-02-02 15:46:20 -0800487 print "cmd1= ", cmd
Jon Hallefbd9792015-03-05 16:11:36 -0800488 response = self.execute( cmd=cmd,
489 prompt="mininet>",
490 timeout=10 )
kelvin-onlaba1484582015-02-02 15:46:20 -0800491
492 # Determine hostintf and Oldswitchintf
493 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800494 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800495 print "cmd2= ", cmd
496 self.handle.sendline( cmd )
497 self.handle.expect( "mininet>" )
498
shahshreya73537862015-02-11 15:15:24 -0800499 # Determine ip and mac address of the host-oldSw interface
kelvin-onlaba1484582015-02-02 15:46:20 -0800500 cmd = "px ipaddr = hintf.IP()"
501 print "cmd3= ", cmd
502 self.handle.sendline( cmd )
503 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800504
505 cmd = "px macaddr = hintf.MAC()"
506 print "cmd3= ", cmd
507 self.handle.sendline( cmd )
508 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800509
510 # Detach interface between oldSw-host
511 cmd = "px " + oldSw + ".detach( sintf )"
512 print "cmd4= ", cmd
513 self.handle.sendline( cmd )
514 self.handle.expect( "mininet>" )
515
516 # Add link between host-newSw
517 cmd = "py net.addLink(" + host + "," + newSw + ")"
518 print "cmd5= ", cmd
519 self.handle.sendline( cmd )
520 self.handle.expect( "mininet>" )
521
522 # Determine hostintf and Newswitchintf
523 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800524 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800525 print "cmd6= ", cmd
526 self.handle.sendline( cmd )
527 self.handle.expect( "mininet>" )
528
529 # Attach interface between newSw-host
530 cmd = "px " + newSw + ".attach( sintf )"
531 print "cmd3= ", cmd
532 self.handle.sendline( cmd )
533 self.handle.expect( "mininet>" )
534
535 # Set ipaddress of the host-newSw interface
536 cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)"
537 print "cmd7 = ", cmd
538 self.handle.sendline( cmd )
539 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800540
541 # Set macaddress of the host-newSw interface
542 cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)"
543 print "cmd8 = ", cmd
544 self.handle.sendline( cmd )
545 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800546
547 cmd = "net"
shahshreya73537862015-02-11 15:15:24 -0800548 print "cmd9 = ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800549 self.handle.sendline( cmd )
550 self.handle.expect( "mininet>" )
551 print "output = ", self.handle.before
552
553 # Determine ipaddress of the host-newSw interface
shahshreya73537862015-02-11 15:15:24 -0800554 cmd = host + " ifconfig"
555 print "cmd10= ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800556 self.handle.sendline( cmd )
557 self.handle.expect( "mininet>" )
558 print "ifconfig o/p = ", self.handle.before
559
560 return main.TRUE
561 except pexpect.EOF:
562 main.log.error( self.name + ": EOF exception found" )
563 main.log.error( self.name + ": " + self.handle.before )
564 return main.FALSE
565
Jon Hall7eb38402015-01-08 17:19:54 -0800566 def changeIP( self, host, intf, newIP, newNetmask ):
567 """
568 Changes the ip address of a host on the fly
569 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800570 if self.handle:
571 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800572 cmd = host + " ifconfig " + intf + " " + \
573 newIP + " " + 'netmask' + " " + newNetmask
574 self.handle.sendline( cmd )
575 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800576 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800577 main.log.info( "response = " + response )
578 main.log.info(
579 "Ip of host " +
580 host +
581 " changed to new IP " +
582 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800583 return main.TRUE
584 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800585 main.log.error( self.name + ": EOF exception found" )
586 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800587 return main.FALSE
588
Jon Hall7eb38402015-01-08 17:19:54 -0800589 def changeDefaultGateway( self, host, newGW ):
590 """
591 Changes the default gateway of a host
592 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800593 if self.handle:
594 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800595 cmd = host + " route add default gw " + newGW
596 self.handle.sendline( cmd )
597 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800598 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800599 main.log.info( "response = " + response )
600 main.log.info(
601 "Default gateway of host " +
602 host +
603 " changed to " +
604 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800605 return main.TRUE
606 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800607 main.log.error( self.name + ": EOF exception found" )
608 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800609 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800610
Jon Hall7eb38402015-01-08 17:19:54 -0800611 def addStaticMACAddress( self, host, GW, macaddr ):
612 """
Jon Hallefbd9792015-03-05 16:11:36 -0800613 Changes the mac address of a gateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800614 if self.handle:
615 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800616 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
617 cmd = host + " arp -s " + GW + " " + macaddr
618 self.handle.sendline( cmd )
619 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800620 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800621 main.log.info( "response = " + response )
622 main.log.info(
Jon Hallefbd9792015-03-05 16:11:36 -0800623 "Mac address of gateway " +
Jon Hall7eb38402015-01-08 17:19:54 -0800624 GW +
625 " changed to " +
626 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800627 return main.TRUE
628 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800629 main.log.error( self.name + ": EOF exception found" )
630 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800631 return main.FALSE
632
Jon Hall7eb38402015-01-08 17:19:54 -0800633 def verifyStaticGWandMAC( self, host ):
634 """
635 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800636 if self.handle:
637 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800638 # h1 arp -an
639 cmd = host + " arp -an "
640 self.handle.sendline( cmd )
641 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800642 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800643 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800644 return main.TRUE
645 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800646 main.log.error( self.name + ": EOF exception found" )
647 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800648 return main.FALSE
649
Jon Hall7eb38402015-01-08 17:19:54 -0800650 def getMacAddress( self, host ):
651 """
652 Verifies the host's ip configured or not."""
653 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700654 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800655 response = self.execute(
656 cmd=host +
657 " ifconfig",
658 prompt="mininet>",
659 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800660 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800661 main.log.error( self.name + ": EOF exception found" )
662 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700663 main.cleanup()
664 main.exit()
adminbae64d82013-08-01 10:50:15 -0700665
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700666 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800667 macAddressSearch = re.search( pattern, response, re.I )
668 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800669 main.log.info(
670 self.name +
671 ": Mac-Address of Host " +
672 host +
673 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 macAddress )
675 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700676 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800677 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700678
Jon Hall7eb38402015-01-08 17:19:54 -0800679 def getInterfaceMACAddress( self, host, interface ):
680 """
681 Return the IP address of the interface on the given host"""
682 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700683 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800684 response = self.execute( cmd=host + " ifconfig " + interface,
685 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800686 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800687 main.log.error( self.name + ": EOF exception found" )
688 main.log.error( self.name + ": " + self.handle.before )
689 main.cleanup()
690 main.exit()
691
692 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800693 macAddressSearch = re.search( pattern, response, re.I )
694 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800695 main.log.info( "No mac address found in %s" % response )
696 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800698 main.log.info(
699 "Mac-Address of " +
700 host +
701 ":" +
702 interface +
703 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800704 macAddress )
705 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800706 else:
707 main.log.error( "Connection failed to the host" )
708
709 def getIPAddress( self, host ):
710 """
711 Verifies the host's ip configured or not."""
712 if self.handle:
713 try:
714 response = self.execute(
715 cmd=host +
716 " ifconfig",
717 prompt="mininet>",
718 timeout=10 )
719 except pexpect.EOF:
720 main.log.error( self.name + ": EOF exception found" )
721 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700722 main.cleanup()
723 main.exit()
adminbae64d82013-08-01 10:50:15 -0700724
725 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800726 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800727 main.log.info(
728 self.name +
729 ": IP-Address of Host " +
730 host +
731 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800732 ipAddressSearch.group( 1 ) )
733 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800734 else:
735 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800736
Jon Hall7eb38402015-01-08 17:19:54 -0800737 def getSwitchDPID( self, switch ):
738 """
739 return the datapath ID of the switch"""
740 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700741 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700742 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800743 response = self.execute(
744 cmd=cmd,
745 prompt="mininet>",
746 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800747 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800748 main.log.error( self.name + ": EOF exception found" )
749 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700750 main.cleanup()
751 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800752 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800753 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700754 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800755 main.log.info(
756 "Couldn't find DPID for switch %s, found: %s" %
757 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700758 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800759 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700760 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800761 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700762
Jon Hall7eb38402015-01-08 17:19:54 -0800763 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700764 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800765 self.handle.sendline( "" )
766 self.expect( "mininet>" )
767 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700768 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800769 response = self.execute(
770 cmd=cmd,
771 prompt="mininet>",
772 timeout=10 )
773 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700774 response = self.handle.before
775 return response
776 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800777 main.log.error( self.name + ": EOF exception found" )
778 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700779 main.cleanup()
780 main.exit()
781
Jon Hall7eb38402015-01-08 17:19:54 -0800782 def getInterfaces( self, node ):
783 """
784 return information dict about interfaces connected to the node"""
785 if self.handle:
786 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800787 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700788 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700789 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800790 response = self.execute(
791 cmd=cmd,
792 prompt="mininet>",
793 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800794 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800795 main.log.error( self.name + ": EOF exception found" )
796 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700797 main.cleanup()
798 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700799 return response
800 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800801 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700802
Jon Hall7eb38402015-01-08 17:19:54 -0800803 def dump( self ):
804 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700805 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800806 response = self.execute(
807 cmd='dump',
808 prompt='mininet>',
809 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800810 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800811 main.log.error( self.name + ": EOF exception found" )
812 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700813 main.cleanup()
814 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700815 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800816
Jon Hall7eb38402015-01-08 17:19:54 -0800817 def intfs( self ):
818 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700819 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800820 response = self.execute(
821 cmd='intfs',
822 prompt='mininet>',
823 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800824 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800825 main.log.error( self.name + ": EOF exception found" )
826 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700827 main.cleanup()
828 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700829 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800830
Jon Hall7eb38402015-01-08 17:19:54 -0800831 def net( self ):
832 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700833 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800834 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800835 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800836 main.log.error( self.name + ": EOF exception found" )
837 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700838 main.cleanup()
839 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700840 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800841
842 def iperf( self, host1, host2 ):
843 main.log.info(
844 self.name +
845 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700846 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800847 cmd1 = 'iperf ' + host1 + " " + host2
848 self.handle.sendline( cmd1 )
849 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800850 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800851 if re.search( 'Results:', response ):
Jon Hallefbd9792015-03-05 16:11:36 -0800852 main.log.info( self.name + ": iperf test successful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800853 return main.TRUE
854 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800855 main.log.error( self.name + ": iperf test failed" )
856 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800857 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800858 main.log.error( self.name + ": EOF exception found" )
859 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800860 main.cleanup()
861 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800862
Jon Hall7eb38402015-01-08 17:19:54 -0800863 def iperfudp( self ):
864 main.log.info(
865 self.name +
866 ": Simple iperf TCP test between two " +
867 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700868 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800869 response = self.execute(
870 cmd='iperfudp',
871 prompt='mininet>',
872 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800873 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800874 main.log.error( self.name + ": EOF exception found" )
875 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700876 main.cleanup()
877 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700878 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800879
Jon Hall7eb38402015-01-08 17:19:54 -0800880 def nodes( self ):
881 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700882 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800883 response = self.execute(
884 cmd='nodes',
885 prompt='mininet>',
886 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800887 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800888 main.log.error( self.name + ": EOF exception found" )
889 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700890 main.cleanup()
891 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700892 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800893
Jon Hall7eb38402015-01-08 17:19:54 -0800894 def pingpair( self ):
895 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700896 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800897 response = self.execute(
898 cmd='pingpair',
899 prompt='mininet>',
900 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800901 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800902 main.log.error( self.name + ": EOF exception found" )
903 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700904 main.cleanup()
905 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800906
Jon Hall7eb38402015-01-08 17:19:54 -0800907 if re.search( ',\s0\%\spacket\sloss', response ):
908 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800909 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700910 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800911 else:
912 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700914 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800915
Jon Hall7eb38402015-01-08 17:19:54 -0800916 def link( self, **linkargs ):
917 """
918 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800919 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800920 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
921 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
922 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
923 main.log.info(
924 "Bring link between '" +
925 end1 +
926 "' and '" +
927 end2 +
928 "' '" +
929 option +
930 "'" )
931 command = "link " + \
932 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700933 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800934 self.handle.sendline( command )
935 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800936 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800937 main.log.error( self.name + ": EOF exception found" )
938 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700939 main.cleanup()
940 main.exit()
adminbae64d82013-08-01 10:50:15 -0700941 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800942
Jon Hall7eb38402015-01-08 17:19:54 -0800943 def yank( self, **yankargs ):
944 """
945 yank a mininet switch interface to a host"""
946 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800947 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800948 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
949 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
950 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700951 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800952 response = self.execute(
953 cmd=command,
954 prompt="mininet>",
955 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800956 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800957 main.log.error( self.name + ": EOF exception found" )
958 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700959 main.cleanup()
960 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700961 return main.TRUE
962
Jon Hall7eb38402015-01-08 17:19:54 -0800963 def plug( self, **plugargs ):
964 """
965 plug the yanked mininet switch interface to a switch"""
966 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800967 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800968 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
969 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
970 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700971 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800972 response = self.execute(
973 cmd=command,
974 prompt="mininet>",
975 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800976 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800977 main.log.error( self.name + ": EOF exception found" )
978 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700979 main.cleanup()
980 main.exit()
adminbae64d82013-08-01 10:50:15 -0700981 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800982
Jon Hall7eb38402015-01-08 17:19:54 -0800983 def dpctl( self, **dpctlargs ):
984 """
985 Run dpctl command on all switches."""
986 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800987 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800988 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
989 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
990 command = "dpctl " + cmd + " " + str( cmdargs )
991 try:
992 response = self.execute(
993 cmd=command,
994 prompt="mininet>",
995 timeout=10 )
996 except pexpect.EOF:
997 main.log.error( self.name + ": EOF exception found" )
998 main.log.error( self.name + ": " + self.handle.before )
999 main.cleanup()
1000 main.exit()
1001 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001002
kelvin-onlabd3b64892015-01-20 13:26:24 -08001003 def getVersion( self ):
Jon Hallff6b4b22015-02-23 09:25:15 -08001004 #FIXME: What uses this? This should be refactored to get
1005 # version from MN and not some other file
kelvin-onlabd3b64892015-01-20 13:26:24 -08001006 fileInput = path + '/lib/Mininet/INSTALL'
1007 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -07001008 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -08001010 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -07001011 if result:
Jon Hall7eb38402015-01-08 17:19:54 -08001012 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -05001013 return version
adminbae64d82013-08-01 10:50:15 -07001014
kelvin-onlabd3b64892015-01-20 13:26:24 -08001015 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001016 """
Jon Hallec3c21e2014-11-10 22:22:37 -05001017 Parameters:
1018 sw: The name of an OVS switch. Example "s1"
1019 Return:
Jon Hall7eb38402015-01-08 17:19:54 -08001020 The output of the command from the mininet cli
1021 or main.FALSE on timeout"""
1022 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001023 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001024 response = self.execute(
1025 cmd=command,
1026 prompt="mininet>",
1027 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001028 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -05001029 return response
admin2a9548d2014-06-17 14:08:07 -07001030 else:
1031 return main.FALSE
1032 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001033 main.log.error( self.name + ": EOF exception found" )
1034 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001035 main.cleanup()
1036 main.exit()
adminbae64d82013-08-01 10:50:15 -07001037
kelvin-onlabd3b64892015-01-20 13:26:24 -08001038 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001039 """
1040 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001041 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001042 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -07001043
1044 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -08001045 for j in range( count ):
1046 argstring = argstring + ",IP" + \
1047 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001048 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -07001049
Jon Hall7eb38402015-01-08 17:19:54 -08001050 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1051 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001052 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -08001053 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -08001054
Jon Hall7eb38402015-01-08 17:19:54 -08001055 command = "sh ovs-vsctl set-controller s" + \
1056 str( sw ) + " " + ptcpB + " "
1057 for j in range( count ):
1058 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001059 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -08001060 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
1061 ip = args[
1062 "IP" +
1063 str( i ) ] if args[
1064 "IP" +
1065 str( i ) ] is not None else ""
1066 port = args[
1067 "PORT" +
1068 str( i ) ] if args[
1069 "PORT" +
1070 str( i ) ] is not None else ""
1071 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001072 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -07001073 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -07001074 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001075 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -07001076 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001077 main.log.error( self.name + ": EOF exception found" )
1078 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001079 main.cleanup()
1080 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001081 except Exception:
1082 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall6094a362014-04-11 14:46:56 -07001083 main.cleanup()
1084 main.exit()
adminbae64d82013-08-01 10:50:15 -07001085
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001087 """
1088 Removes the controller target from sw"""
1089 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -07001090 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001091 response = self.execute(
1092 cmd=command,
1093 prompt="mininet>",
1094 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001095 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001096 main.log.error( self.name + ": EOF exception found" )
1097 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -07001098 main.cleanup()
1099 main.exit()
1100 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001101 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -07001102
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001104 """
Jon Hallb1290e82014-11-18 16:17:48 -05001105 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001106 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001107 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001108 NOTE: cannot currently specify what type of switch
1109 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001110 sw = name of the new switch as a string
1111 optional keywords:
Jon Hallb1290e82014-11-18 16:17:48 -05001112 dpid = "dpid"
Jon Hallefbd9792015-03-05 16:11:36 -08001113 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001114 """
1115 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001116 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -05001117 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001118 response = self.execute(
1119 cmd=command,
1120 prompt="mininet>",
1121 timeout=10 )
1122 if re.search( "already exists!", response ):
1123 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001124 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001125 elif re.search( "Error", response ):
1126 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001127 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001128 elif re.search( "usage:", response ):
1129 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001130 return main.FALSE
1131 else:
1132 return main.TRUE
1133 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001134 main.log.error( self.name + ": EOF exception found" )
kaouthera3f13ca22015-05-05 15:01:41 -07001135 main.log.error(self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001136 main.cleanup()
1137 main.exit()
1138
kelvin-onlabd3b64892015-01-20 13:26:24 -08001139 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001140 """
Jon Hallbe6dfc42015-01-12 17:37:25 -08001141 delete a switch from the mininet topology
1142 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001143 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -08001144 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001145 sw = name of the switch as a string
1146 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001147 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -05001148 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001149 response = self.execute(
1150 cmd=command,
1151 prompt="mininet>",
1152 timeout=10 )
1153 if re.search( "no switch named", response ):
1154 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001155 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001156 elif re.search( "Error", response ):
1157 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001158 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001159 elif re.search( "usage:", response ):
1160 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001161 return main.FALSE
1162 else:
1163 return main.TRUE
1164 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001165 main.log.error( self.name + ": EOF exception found" )
1166 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001167 main.cleanup()
1168 main.exit()
1169
kelvin-onlabd3b64892015-01-20 13:26:24 -08001170 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001171 """
1172 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001173 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001174 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001175 NOTE: cannot currently specify what type of link
1176 required params:
1177 node1 = the string node name of the first endpoint of the link
1178 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001179 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001180 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001181 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001182 response = self.execute(
1183 cmd=command,
1184 prompt="mininet>",
1185 timeout=10 )
1186 if re.search( "doesnt exist!", response ):
1187 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001188 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001189 elif re.search( "Error", response ):
1190 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001191 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001192 elif re.search( "usage:", response ):
1193 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001194 return main.FALSE
1195 else:
1196 return main.TRUE
1197 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001198 main.log.error( self.name + ": EOF exception found" )
1199 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001200 main.cleanup()
1201 main.exit()
1202
kelvin-onlabd3b64892015-01-20 13:26:24 -08001203 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001204 """
1205 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001206 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001207 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001208 required params:
1209 node1 = the string node name of the first endpoint of the link
1210 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001211 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001212 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001213 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001214 response = self.execute(
1215 cmd=command,
1216 prompt="mininet>",
1217 timeout=10 )
1218 if re.search( "no node named", response ):
1219 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001220 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001221 elif re.search( "Error", response ):
1222 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001223 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001224 elif re.search( "usage:", response ):
1225 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001226 return main.FALSE
1227 else:
1228 return main.TRUE
1229 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001230 main.log.error( self.name + ": EOF exception found" )
1231 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001232 main.cleanup()
1233 main.exit()
1234
kelvin-onlabd3b64892015-01-20 13:26:24 -08001235 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001236 """
Jon Hallb1290e82014-11-18 16:17:48 -05001237 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001238 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001239 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001240 NOTE: cannot currently specify what type of host
1241 required params:
1242 hostname = the string hostname
1243 optional key-value params
1244 switch = "switch name"
Jon Hallefbd9792015-03-05 16:11:36 -08001245 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001246 """
1247 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001248 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001249 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001250 response = self.execute(
1251 cmd=command,
1252 prompt="mininet>",
1253 timeout=10 )
1254 if re.search( "already exists!", response ):
1255 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001256 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001257 elif re.search( "doesnt exists!", response ):
1258 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001259 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001260 elif re.search( "Error", response ):
1261 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001262 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001263 elif re.search( "usage:", response ):
1264 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001265 return main.FALSE
1266 else:
1267 return main.TRUE
1268 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001269 main.log.error( self.name + ": EOF exception found" )
1270 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001271 main.cleanup()
1272 main.exit()
1273
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001275 """
1276 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001277 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001278 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001279 NOTE: this uses a custom mn function
1280 required params:
1281 hostname = the string hostname
Jon Hallefbd9792015-03-05 16:11:36 -08001282 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001283 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001284 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001285 response = self.execute(
1286 cmd=command,
1287 prompt="mininet>",
1288 timeout=10 )
1289 if re.search( "no host named", response ):
1290 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001291 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001292 elif re.search( "Error", response ):
1293 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001294 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001295 elif re.search( "usage:", response ):
1296 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001297 return main.FALSE
1298 else:
1299 return main.TRUE
1300 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001301 main.log.error( self.name + ": EOF exception found" )
1302 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001303 main.cleanup()
1304 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001305
Jon Hall7eb38402015-01-08 17:19:54 -08001306 def disconnect( self ):
kelvin-onlaba1484582015-02-02 15:46:20 -08001307 """
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001308 Called at the end of the test to stop the mininet and
1309 disconnect the handle.
kelvin-onlaba1484582015-02-02 15:46:20 -08001310 """
1311 self.handle.sendline('')
Jon Halld61331b2015-02-17 16:35:47 -08001312 i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ],
Jon Hallefbd9792015-03-05 16:11:36 -08001313 timeout=2)
Jon Hall390696c2015-05-05 17:13:41 -07001314 response = main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001315 if i == 0:
Jon Hall390696c2015-05-05 17:13:41 -07001316 response = self.stopNet()
Jon Halld61331b2015-02-17 16:35:47 -08001317 elif i == 1:
1318 return main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001319 # print "Disconnecting Mininet"
1320 if self.handle:
1321 self.handle.sendline( "exit" )
1322 self.handle.expect( "exit" )
1323 self.handle.expect( "(.*)" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001324 else:
1325 main.log.error( "Connection failed to the host" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001326 return response
1327
Hari Krishnab35c6d02015-03-18 11:13:51 -07001328 def stopNet( self, fileName = "", timeout=5):
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001329 """
Jon Hall21270ac2015-02-16 17:59:55 -08001330 Stops mininet.
Jon Hallefbd9792015-03-05 16:11:36 -08001331 Returns main.TRUE if the mininet successfully stops and
Jon Hall21270ac2015-02-16 17:59:55 -08001332 main.FALSE if the pexpect handle does not exist.
1333
Jon Halld61331b2015-02-17 16:35:47 -08001334 Will cleanup and exit the test if mininet fails to stop
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001335 """
Jon Hall21270ac2015-02-16 17:59:55 -08001336
Jon Halld61331b2015-02-17 16:35:47 -08001337 main.log.info( self.name + ": Stopping mininet..." )
adminbae64d82013-08-01 10:50:15 -07001338 response = ''
1339 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001340 try:
kelvin-onlab26bc17f2015-02-06 14:08:59 -08001341 self.handle.sendline("")
kelvin-onlab56a3f462015-02-06 14:04:43 -08001342 i = self.handle.expect( [ 'mininet>',
1343 '\$',
1344 pexpect.EOF,
1345 pexpect.TIMEOUT ],
1346 timeout )
1347 if i == 0:
1348 main.log.info( "Exiting mininet..." )
1349
Jon Hall7eb38402015-01-08 17:19:54 -08001350 response = self.execute(
1351 cmd="exit",
1352 prompt="(.*)",
1353 timeout=120 )
Jon Halld61331b2015-02-17 16:35:47 -08001354 main.log.info( self.name + ": Stopped")
Jon Hall7eb38402015-01-08 17:19:54 -08001355 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001356 response = main.TRUE
Hari Krishnab35c6d02015-03-18 11:13:51 -07001357
kelvin-onlab56a3f462015-02-06 14:04:43 -08001358 if i == 1:
1359 main.log.info( " Mininet trying to exit while not " +
1360 "in the mininet prompt" )
1361 elif i == 2:
1362 main.log.error( "Something went wrong exiting mininet" )
1363 elif i == 3: # timeout
1364 main.log.error( "Something went wrong exiting mininet " +
1365 "TIMEOUT" )
1366
Hari Krishnab35c6d02015-03-18 11:13:51 -07001367 if fileName:
1368 self.handle.sendline("")
1369 self.handle.expect('\$')
1370 self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`")
Jon Hallfbc828e2015-01-06 17:30:19 -08001371 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001372 main.log.error( self.name + ": EOF exception found" )
1373 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001374 main.cleanup()
1375 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001376 else:
1377 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001378 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001379 return response
1380
kelvin-onlab65782a82015-05-07 14:12:13 -07001381 def arping( self, host="", ip="10.128.20.211" ):
1382 """
1383 Description:
1384 Sends arp message from mininet host for hosts discovery
1385 Required:
1386 host - hosts name
1387 Optional:
1388 ip - ip address that does not exist in the network so there would
1389 be no reply.
1390 """
1391 cmd = " py " + host + ".cmd(\"arping -c 1 " + ip + "\")"
admin07529932013-11-22 14:58:28 -08001392 try:
kelvin-onlab65782a82015-05-07 14:12:13 -07001393 main.log.warn( "Sending: " + cmd )
1394 self.handle.sendline( cmd )
1395 response = self.handle.before
1396 self.handle.sendline( "" )
1397 self.handle.expect( "mininet>" )
admin07529932013-11-22 14:58:28 -08001398 return main.TRUE
kelvin-onlab65782a82015-05-07 14:12:13 -07001399
1400 except pexpect.EOF:
1401 main.log.error( self.name + ": EOF exception found" )
1402 main.log.error( self.name + ": " + self.handle.before )
1403 main.cleanup()
1404 main.exit()
admin07529932013-11-22 14:58:28 -08001405
Jon Hall7eb38402015-01-08 17:19:54 -08001406 def decToHex( self, num ):
1407 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001408
Jon Hall7eb38402015-01-08 17:19:54 -08001409 def getSwitchFlowCount( self, switch ):
1410 """
1411 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001412 if self.handle:
1413 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1414 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001415 response = self.execute(
1416 cmd=cmd,
1417 prompt="mininet>",
1418 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001419 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001420 main.log.error( self.name + ": EOF exception found" )
1421 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001422 main.cleanup()
1423 main.exit()
1424 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001425 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001426 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001427 main.log.info(
1428 "Couldn't find flows on switch %s, found: %s" %
1429 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001430 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001431 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001432 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001433 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001434
kelvin-onlabd3b64892015-01-20 13:26:24 -08001435 def checkFlows( self, sw, dumpFormat=None ):
1436 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001437 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001438 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001439 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001440 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001441 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001442 response = self.execute(
1443 cmd=command,
1444 prompt="mininet>",
1445 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001446 return response
1447 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001448 main.log.error( self.name + ": EOF exception found" )
1449 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001450 main.cleanup()
1451 main.exit()
admin2a9548d2014-06-17 14:08:07 -07001452
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001454 """
Jon Hallefbd9792015-03-05 16:11:36 -08001455 Runs tpdump on an interface and saves the file
Jon Hall7eb38402015-01-08 17:19:54 -08001456 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001457 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001458 self.handle.sendline( "" )
1459 self.handle.expect( "mininet>" )
1460 self.handle.sendline(
1461 "sh sudo tcpdump -n -i " +
1462 intf +
1463 " " +
1464 port +
1465 " -w " +
1466 filename.strip() +
1467 " &" )
1468 self.handle.sendline( "" )
1469 i = self.handle.expect( [ 'No\ssuch\device',
1470 'listening\son',
1471 pexpect.TIMEOUT,
1472 "mininet>" ],
1473 timeout=10 )
1474 main.log.warn( self.handle.before + self.handle.after )
1475 self.handle.sendline( "" )
1476 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001477 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001478 main.log.error(
1479 self.name +
1480 ": tcpdump - No such device exists. " +
1481 "tcpdump attempted on: " +
1482 intf )
admin2a9548d2014-06-17 14:08:07 -07001483 return main.FALSE
1484 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001485 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001486 return main.TRUE
1487 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001488 main.log.error(
1489 self.name +
1490 ": tcpdump command timed out! Check interface name," +
1491 " given interface was: " +
1492 intf )
admin2a9548d2014-06-17 14:08:07 -07001493 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001494 elif i == 3:
1495 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001496 return main.TRUE
1497 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001498 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001499 return main.FALSE
1500 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001501 main.log.error( self.name + ": EOF exception found" )
1502 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001503 main.cleanup()
1504 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001505 except Exception:
1506 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001507 main.cleanup()
1508 main.exit()
1509
kelvin-onlabd3b64892015-01-20 13:26:24 -08001510 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -08001511 """
1512 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -07001513 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001514 self.handle.sendline( "sh sudo pkill tcpdump" )
1515 self.handle.expect( "mininet>" )
1516 self.handle.sendline( "" )
1517 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001518 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001519 main.log.error( self.name + ": EOF exception found" )
1520 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001521 main.cleanup()
1522 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001523 except Exception:
1524 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001525 main.cleanup()
1526 main.exit()
1527
kelvin-onlabd3b64892015-01-20 13:26:24 -08001528 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001529 """
1530 Compare mn and onos switches
1531 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001533
Jon Hall7eb38402015-01-08 17:19:54 -08001534 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001535 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001536 output = { "switches": [] }
1537 # iterate through the MN topology and pull out switches and and port
1538 # info
1539 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001540 ports = []
1541 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001542 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001543 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001544 'name': port.name } )
1545 output[ 'switches' ].append( {
1546 "name": switch.name,
1547 "dpid": str( switch.dpid ).zfill( 16 ),
1548 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001549
Jon Hall7eb38402015-01-08 17:19:54 -08001550 # print "mn"
1551 # print json.dumps( output,
Jon Hallff6b4b22015-02-23 09:25:15 -08001552 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001553 # indent=4,
1554 # separators=( ',', ': ' ) )
1555 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001556 # print json.dumps( switchesJson,
Jon Hallff6b4b22015-02-23 09:25:15 -08001557 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001558 # indent=4,
1559 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001560
1561 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001562 mnDPIDs = []
1563 for switch in output[ 'switches' ]:
1564 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001565 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001566 # print "List of Mininet switch DPID's"
1567 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001569 main.log.error(
1570 self.name +
1571 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001572 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001573 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001574 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001575 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001576 if switch[ 'available' ]:
1577 onosDPIDs.append(
1578 switch[ 'id' ].replace(
1579 ":",
1580 '' ).replace(
1581 "of",
1582 '' ).lower() )
1583 # else:
1584 # print "Switch is unavailable:"
1585 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001586 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001587 # print "List of ONOS switch DPID's"
1588 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001589
Jon Hall7eb38402015-01-08 17:19:54 -08001590 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001591 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001592 main.log.report( "Switches in MN but not in ONOS:" )
1593 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1594 main.log.report( str( list1 ) )
1595 main.log.report( "Switches in ONOS but not in MN:" )
1596 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001597 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001598 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001599 switchResults = main.TRUE
1600 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001601
kelvin-onlabd3b64892015-01-20 13:26:24 -08001602 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001603 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001604 Compare mn and onos ports
1605 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001606 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001607
Jon Hallfbc828e2015-01-06 17:30:19 -08001608 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001609 1. This uses the sts TestONTopology object
1610 2. numpy - "sudo pip install numpy"
1611
Jon Hall7eb38402015-01-08 17:19:54 -08001612 """
1613 # FIXME: this does not look for extra ports in ONOS, only checks that
1614 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001615 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001616 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001617 output = { "switches": [] }
1618 # iterate through the MN topology and pull out switches and and port
1619 # info
1620 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001621 ports = []
1622 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001623 # print port.hw_addr.toStr( separator='' )
Jon Hallefbd9792015-03-05 16:11:36 -08001624 tmpPort = { 'of_port': port.port_no,
1625 'mac': str( port.hw_addr ).replace( '\'', '' ),
1626 'name': port.name,
1627 'enabled': port.enabled }
Jon Hall39f29df2014-11-04 19:30:21 -05001628
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 ports.append( tmpPort )
Jon Hallefbd9792015-03-05 16:11:36 -08001630 tmpSwitch = { 'name': switch.name,
1631 'dpid': str( switch.dpid ).zfill( 16 ),
1632 'ports': ports }
Jon Hall39f29df2014-11-04 19:30:21 -05001633
kelvin-onlabd3b64892015-01-20 13:26:24 -08001634 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001635
Jon Hall7eb38402015-01-08 17:19:54 -08001636 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001637 for mnSwitch in output[ 'switches' ]:
1638 mnPorts = []
1639 onosPorts = []
1640 switchResult = main.TRUE
1641 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001642 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001643 mnPorts.append( port[ 'of_port' ] )
1644 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001645 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001646 # print onosSwitch
1647 if onosSwitch[ 'device' ][ 'available' ]:
1648 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001649 ':',
1650 '' ).replace(
1651 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 '' ) == mnSwitch[ 'dpid' ]:
1653 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001654 if port[ 'isEnabled' ]:
1655 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001656 # onosPorts.append( 'local' )
1657 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001658 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001659 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001660 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001661 mnPorts.sort( key=float )
1662 onosPorts.sort( key=float )
1663 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1664 # print "\tmn_ports[] = ", mnPorts
1665 # print "\tonos_ports[] = ", onosPorts
1666 mnPortsLog = mnPorts
1667 onosPortsLog = onosPorts
1668 mnPorts = [ x for x in mnPorts ]
1669 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001670
Jon Hall7eb38402015-01-08 17:19:54 -08001671 # TODO: handle other reserved port numbers besides LOCAL
1672 # NOTE: Reserved ports
1673 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1674 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001675 for mnPort in mnPortsLog:
1676 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001677 # don't set results to true here as this is just one of
1678 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001679 mnPorts.remove( mnPort )
1680 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001681 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001682 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001683 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 if 65534 in mnPorts:
1685 mnPorts.remove( 65534 )
1686 if long( uint64( -2 ) ) in onosPorts:
1687 onosPorts.remove( long( uint64( -2 ) ) )
1688 if len( mnPorts ): # the ports of this switch don't match
1689 switchResult = main.FALSE
1690 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1691 if len( onosPorts ): # the ports of this switch don't match
1692 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001693 main.log.warn(
1694 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 str( onosPorts ) )
1696 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001697 main.log.report(
1698 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001699 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1700 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1701 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1702 portsResults = portsResults and switchResult
1703 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001704
kelvin-onlabd3b64892015-01-20 13:26:24 -08001705 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001706 """
1707 Compare mn and onos links
1708 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001709 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001710
Jon Hall7eb38402015-01-08 17:19:54 -08001711 This uses the sts TestONTopology object"""
1712 # FIXME: this does not look for extra links in ONOS, only checks that
Jon Hallefbd9792015-03-05 16:11:36 -08001713 # ONOS has what is in MN
Jon Hall7eb38402015-01-08 17:19:54 -08001714 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001715 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001716 # iterate through the MN topology and pull out switches and and port
1717 # info
1718 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001719 # print "Iterating though switches as seen by Mininet"
1720 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001721 ports = []
1722 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001723 # print port.hw_addr.toStr( separator='' )
1724 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001725 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001726 'name': port.name } )
1727 output[ 'switches' ].append( {
1728 "name": switch.name,
1729 "dpid": str( switch.dpid ).zfill( 16 ),
1730 "ports": ports } )
1731 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001732
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001734 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001735 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 if 2 * len( mnLinks ) == len( onos ):
1737 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001738 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001740 main.log.report(
Jon Hall328ddca2015-01-28 15:57:15 -08001741 "Mininet has " + str( len( mnLinks ) ) +
1742 " bidirectional links and ONOS has " +
1743 str( len( onos ) ) + " unidirectional links" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001744
Jon Hall7eb38402015-01-08 17:19:54 -08001745 # iterate through MN links and check if an ONOS link exists in
1746 # both directions
1747 # NOTE: Will currently only show mn links as down if they are
1748 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001749 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001750 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001751 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001752 # print "Link: %s" % link
1753 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001754 node1 = None
1755 port1 = None
1756 node2 = None
1757 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001758 firstDir = main.FALSE
1759 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001760 for switch in output[ 'switches' ]:
1761 # print "Switch: %s" % switch[ 'name' ]
1762 if switch[ 'name' ] == link.node1.name:
1763 node1 = switch[ 'dpid' ]
1764 for port in switch[ 'ports' ]:
1765 if str( port[ 'name' ] ) == str( link.port1 ):
1766 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001767 if node1 is not None and node2 is not None:
1768 break
Jon Hall7eb38402015-01-08 17:19:54 -08001769 if switch[ 'name' ] == link.node2.name:
1770 node2 = switch[ 'dpid' ]
1771 for port in switch[ 'ports' ]:
1772 if str( port[ 'name' ] ) == str( link.port2 ):
1773 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001774 if node1 is not None and node2 is not None:
1775 break
1776
kelvin-onlabd3b64892015-01-20 13:26:24 -08001777 for onosLink in onos:
1778 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001779 ":",
1780 '' ).replace(
1781 "of",
1782 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001783 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001784 ":",
1785 '' ).replace(
1786 "of",
1787 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001788 onosPort1 = onosLink[ 'src' ][ 'port' ]
1789 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001790
Jon Hall72cf1dc2014-10-20 21:04:50 -04001791 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001792 if str( onosNode1 ) == str( node1 ) and str(
1793 onosNode2 ) == str( node2 ):
1794 if int( onosPort1 ) == int( port1 ) and int(
1795 onosPort2 ) == int( port2 ):
1796 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001797 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001798 main.log.warn(
1799 'The port numbers do not match for ' +
1800 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001801 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001802 'link %s/%s -> %s/%s' %
1803 ( node1,
1804 port1,
1805 node2,
1806 port2 ) +
1807 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001808 ( onosNode1,
1809 onosPort1,
1810 onosNode2,
1811 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001812
1813 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001814 elif ( str( onosNode1 ) == str( node2 ) and
1815 str( onosNode2 ) == str( node1 ) ):
1816 if ( int( onosPort1 ) == int( port2 )
1817 and int( onosPort2 ) == int( port1 ) ):
1818 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001819 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001820 main.log.warn(
1821 'The port numbers do not match for ' +
1822 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001823 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001824 'link %s/%s -> %s/%s' %
1825 ( node2,
1826 port2,
1827 node1,
1828 port1 ) +
1829 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 ( onosNode2,
1831 onosPort2,
1832 onosNode1,
1833 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001834 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001835 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001836 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001837 main.log.report(
1838 'ONOS does not have the link %s/%s -> %s/%s' %
1839 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001840 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001841 main.log.report(
1842 'ONOS does not have the link %s/%s -> %s/%s' %
1843 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001844 linkResults = linkResults and firstDir and secondDir
1845 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001846
Jon Hallff6b4b22015-02-23 09:25:15 -08001847 def compareHosts( self, topo, hostsJson ):
1848 """
1849 Compare mn and onos Hosts.
1850 Since Mininet hosts are quiet, ONOS will only know of them when they
1851 speak. For this reason, we will only check that the hosts in ONOS
1852 stores are in Mininet, and not vice versa.
1853 topo: sts TestONTopology object
1854 hostsJson: parsed json object from the onos hosts api
1855
1856 This uses the sts TestONTopology object"""
1857 import json
1858 hostResults = main.TRUE
1859 hosts = []
1860 # iterate through the MN topology and pull out hosts
1861 for mnHost in topo.graph.hosts:
1862 interfaces = []
1863 for intf in mnHost.interfaces:
1864 interfaces.append( {
1865 "name": intf.name, # str
1866 "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs
1867 # hw_addr is of type EthAddr, Not JSON serializable
1868 "hw_addr": str( intf.hw_addr ) } )
1869 hosts.append( {
1870 "name": mnHost.name, # str
1871 "interfaces": interfaces } ) # list
1872 for onosHost in hostsJson:
1873 onosMAC = onosHost[ 'mac' ].lower()
1874 match = False
1875 for mnHost in hosts:
1876 for mnIntf in mnHost[ 'interfaces' ]:
1877 if onosMAC == mnIntf[ 'hw_addr' ].lower() :
1878 match = True
1879 for ip in mnIntf[ 'ips' ]:
1880 if ip in onosHost[ 'ips' ]:
1881 pass # all is well
1882 else:
1883 # misssing ip
1884 main.log.error( "ONOS host " + onosHost[ 'id' ]
1885 + " has a different IP than " +
1886 "the Mininet host." )
1887 output = json.dumps(
1888 onosHost,
1889 sort_keys=True,
1890 indent=4,
1891 separators=( ',', ': ' ) )
1892 main.log.info( output )
1893 hostResults = main.FALSE
1894 if not match:
1895 hostResults = main.FALSE
1896 main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " +
1897 "corresponding Mininet host." )
1898 output = json.dumps( onosHost,
1899 sort_keys=True,
1900 indent=4,
1901 separators=( ',', ': ' ) )
1902 main.log.info( output )
Jon Hallff6b4b22015-02-23 09:25:15 -08001903 return hostResults
1904
kelvin-onlabd3b64892015-01-20 13:26:24 -08001905 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001906 """
1907 Returns a list of all hosts
1908 Don't ask questions just use it"""
1909 self.handle.sendline( "" )
1910 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001911
Jon Hall7eb38402015-01-08 17:19:54 -08001912 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1913 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001914
kelvin-onlabd3b64892015-01-20 13:26:24 -08001915 handlePy = self.handle.before
1916 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1917 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001918
Jon Hall7eb38402015-01-08 17:19:54 -08001919 self.handle.sendline( "" )
1920 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001921
kelvin-onlabd3b64892015-01-20 13:26:24 -08001922 hostStr = handlePy.replace( "]", "" )
1923 hostStr = hostStr.replace( "'", "" )
1924 hostStr = hostStr.replace( "[", "" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -07001925 hostStr = hostStr.replace( " ", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001926 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001927
kelvin-onlabd3b64892015-01-20 13:26:24 -08001928 return hostList
adminbae64d82013-08-01 10:50:15 -07001929
Jon Hall7eb38402015-01-08 17:19:54 -08001930 def update( self ):
1931 """
1932 updates the port address and status information for
1933 each port in mn"""
1934 # TODO: Add error checking. currently the mininet command has no output
Jon Hallefbd9792015-03-05 16:11:36 -08001935 main.log.info( "Updating MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001936 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001937 self.handle.sendline( "" )
1938 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001939
Jon Hall7eb38402015-01-08 17:19:54 -08001940 self.handle.sendline( "update" )
1941 self.handle.expect( "update" )
1942 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001943
Jon Hall7eb38402015-01-08 17:19:54 -08001944 self.handle.sendline( "" )
1945 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001946
Jon Hallb1290e82014-11-18 16:17:48 -05001947 return main.TRUE
1948 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001949 main.log.error( self.name + ": EOF exception found" )
1950 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001951 main.cleanup()
1952 main.exit()
1953
kaouthera3f13ca22015-05-05 15:01:41 -07001954 def assignVLAN( self, host, intf, vlan):
1955 """
1956 Add vlan tag to a host.
1957 Dependencies:
1958 This class depends on the "vlan" package
1959 $ sudo apt-get install vlan
1960 Configuration:
1961 Load the 8021q module into the kernel
1962 $sudo modprobe 8021q
1963
1964 To make this setup permanent:
1965 $ sudo su -c 'echo "8021q" >> /etc/modules'
1966 """
1967 if self.handle:
1968 try:
1969 # get the ip address of the host
1970 main.log.info("Get the ip address of the host")
1971 ipaddr = self.getIPAddress(host)
1972 print repr(ipaddr)
1973
1974 # remove IP from interface intf
1975 # Ex: h1 ifconfig h1-eth0 inet 0
1976 main.log.info("Remove IP from interface ")
1977 cmd2 = host + " ifconfig " + intf + " " + " inet 0 "
1978 self.handle.sendline( cmd2 )
1979 self.handle.expect( "mininet>" )
1980 response = self.handle.before
1981 main.log.info ( "====> %s ", response)
1982
1983
1984 # create VLAN interface
1985 # Ex: h1 vconfig add h1-eth0 100
1986 main.log.info("Create Vlan")
1987 cmd3 = host + " vconfig add " + intf + " " + vlan
1988 self.handle.sendline( cmd3 )
1989 self.handle.expect( "mininet>" )
1990 response = self.handle.before
1991 main.log.info( "====> %s ", response )
1992
1993 # assign the host's IP to the VLAN interface
1994 # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1
1995 main.log.info("Assign the host IP to the vlan interface")
1996 vintf = intf + "." + vlan
1997 cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr
1998 self.handle.sendline( cmd4 )
1999 self.handle.expect( "mininet>" )
2000 response = self.handle.before
2001 main.log.info ( "====> %s ", response)
2002
2003
2004 return main.TRUE
2005 except pexpect.EOF:
2006 main.log.error( self.name + ": EOF exception found" )
2007 main.log.error( self.name + ": " + self.handle.before )
2008 return main.FALSE
2009
adminbae64d82013-08-01 10:50:15 -07002010if __name__ != "__main__":
2011 import sys
kelvin-onlab50907142015-04-01 13:37:45 -07002012 sys.modules[ __name__ ] = MininetCliDriver()
kaouthera3f13ca22015-05-05 15:01:41 -07002013
2014