blob: 820d9ce4ac3a59025a841fac64d2545376397c1b [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
Jon Hall7eb38402015-01-08 17:19:54 -08001381 def arping( self, src, dest, destmac ):
1382 self.handle.sendline( '' )
1383 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001384
Jon Hall7eb38402015-01-08 17:19:54 -08001385 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001386 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001387 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1388 main.log.info( self.name + ": ARP successful" )
1389 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001390 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001391 except Exception:
Jon Hall7eb38402015-01-08 17:19:54 -08001392 main.log.warn( self.name + ": ARP FAILURE" )
1393 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001394 return main.FALSE
1395
Jon Hall7eb38402015-01-08 17:19:54 -08001396 def decToHex( self, num ):
1397 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001398
Jon Hall7eb38402015-01-08 17:19:54 -08001399 def getSwitchFlowCount( self, switch ):
1400 """
1401 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001402 if self.handle:
1403 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1404 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001405 response = self.execute(
1406 cmd=cmd,
1407 prompt="mininet>",
1408 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001409 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001410 main.log.error( self.name + ": EOF exception found" )
1411 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001412 main.cleanup()
1413 main.exit()
1414 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001415 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001416 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001417 main.log.info(
1418 "Couldn't find flows on switch %s, found: %s" %
1419 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001420 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001421 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001422 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001423 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001424
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 def checkFlows( self, sw, dumpFormat=None ):
1426 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001427 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001428 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001429 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001430 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001431 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001432 response = self.execute(
1433 cmd=command,
1434 prompt="mininet>",
1435 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001436 return response
1437 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001438 main.log.error( self.name + ": EOF exception found" )
1439 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001440 main.cleanup()
1441 main.exit()
admin2a9548d2014-06-17 14:08:07 -07001442
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001444 """
Jon Hallefbd9792015-03-05 16:11:36 -08001445 Runs tpdump on an interface and saves the file
Jon Hall7eb38402015-01-08 17:19:54 -08001446 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001447 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001448 self.handle.sendline( "" )
1449 self.handle.expect( "mininet>" )
1450 self.handle.sendline(
1451 "sh sudo tcpdump -n -i " +
1452 intf +
1453 " " +
1454 port +
1455 " -w " +
1456 filename.strip() +
1457 " &" )
1458 self.handle.sendline( "" )
1459 i = self.handle.expect( [ 'No\ssuch\device',
1460 'listening\son',
1461 pexpect.TIMEOUT,
1462 "mininet>" ],
1463 timeout=10 )
1464 main.log.warn( self.handle.before + self.handle.after )
1465 self.handle.sendline( "" )
1466 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001467 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001468 main.log.error(
1469 self.name +
1470 ": tcpdump - No such device exists. " +
1471 "tcpdump attempted on: " +
1472 intf )
admin2a9548d2014-06-17 14:08:07 -07001473 return main.FALSE
1474 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001475 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001476 return main.TRUE
1477 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001478 main.log.error(
1479 self.name +
1480 ": tcpdump command timed out! Check interface name," +
1481 " given interface was: " +
1482 intf )
admin2a9548d2014-06-17 14:08:07 -07001483 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001484 elif i == 3:
1485 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001486 return main.TRUE
1487 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001488 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001489 return main.FALSE
1490 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001491 main.log.error( self.name + ": EOF exception found" )
1492 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001493 main.cleanup()
1494 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001495 except Exception:
1496 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001497 main.cleanup()
1498 main.exit()
1499
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -08001501 """
1502 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -07001503 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001504 self.handle.sendline( "sh sudo pkill tcpdump" )
1505 self.handle.expect( "mininet>" )
1506 self.handle.sendline( "" )
1507 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001508 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001509 main.log.error( self.name + ": EOF exception found" )
1510 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001511 main.cleanup()
1512 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001513 except Exception:
1514 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001515 main.cleanup()
1516 main.exit()
1517
kelvin-onlabd3b64892015-01-20 13:26:24 -08001518 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001519 """
1520 Compare mn and onos switches
1521 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001522 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001523
Jon Hall7eb38402015-01-08 17:19:54 -08001524 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001525 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001526 output = { "switches": [] }
1527 # iterate through the MN topology and pull out switches and and port
1528 # info
1529 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001530 ports = []
1531 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001532 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001533 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001534 'name': port.name } )
1535 output[ 'switches' ].append( {
1536 "name": switch.name,
1537 "dpid": str( switch.dpid ).zfill( 16 ),
1538 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001539
Jon Hall7eb38402015-01-08 17:19:54 -08001540 # print "mn"
1541 # print json.dumps( output,
Jon Hallff6b4b22015-02-23 09:25:15 -08001542 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001543 # indent=4,
1544 # separators=( ',', ': ' ) )
1545 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001546 # print json.dumps( switchesJson,
Jon Hallff6b4b22015-02-23 09:25:15 -08001547 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001548 # indent=4,
1549 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001550
1551 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001552 mnDPIDs = []
1553 for switch in output[ 'switches' ]:
1554 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001555 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001556 # print "List of Mininet switch DPID's"
1557 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001559 main.log.error(
1560 self.name +
1561 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001562 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001563 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001564 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001565 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001566 if switch[ 'available' ]:
1567 onosDPIDs.append(
1568 switch[ 'id' ].replace(
1569 ":",
1570 '' ).replace(
1571 "of",
1572 '' ).lower() )
1573 # else:
1574 # print "Switch is unavailable:"
1575 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001576 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001577 # print "List of ONOS switch DPID's"
1578 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001579
Jon Hall7eb38402015-01-08 17:19:54 -08001580 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001581 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001582 main.log.report( "Switches in MN but not in ONOS:" )
1583 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1584 main.log.report( str( list1 ) )
1585 main.log.report( "Switches in ONOS but not in MN:" )
1586 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001587 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001588 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001589 switchResults = main.TRUE
1590 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001591
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001593 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001594 Compare mn and onos ports
1595 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001596 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001597
Jon Hallfbc828e2015-01-06 17:30:19 -08001598 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001599 1. This uses the sts TestONTopology object
1600 2. numpy - "sudo pip install numpy"
1601
Jon Hall7eb38402015-01-08 17:19:54 -08001602 """
1603 # FIXME: this does not look for extra ports in ONOS, only checks that
1604 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001605 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001606 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001607 output = { "switches": [] }
1608 # iterate through the MN topology and pull out switches and and port
1609 # info
1610 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001611 ports = []
1612 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001613 # print port.hw_addr.toStr( separator='' )
Jon Hallefbd9792015-03-05 16:11:36 -08001614 tmpPort = { 'of_port': port.port_no,
1615 'mac': str( port.hw_addr ).replace( '\'', '' ),
1616 'name': port.name,
1617 'enabled': port.enabled }
Jon Hall39f29df2014-11-04 19:30:21 -05001618
kelvin-onlabd3b64892015-01-20 13:26:24 -08001619 ports.append( tmpPort )
Jon Hallefbd9792015-03-05 16:11:36 -08001620 tmpSwitch = { 'name': switch.name,
1621 'dpid': str( switch.dpid ).zfill( 16 ),
1622 'ports': ports }
Jon Hall39f29df2014-11-04 19:30:21 -05001623
kelvin-onlabd3b64892015-01-20 13:26:24 -08001624 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001625
Jon Hall7eb38402015-01-08 17:19:54 -08001626 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001627 for mnSwitch in output[ 'switches' ]:
1628 mnPorts = []
1629 onosPorts = []
1630 switchResult = main.TRUE
1631 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001632 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001633 mnPorts.append( port[ 'of_port' ] )
1634 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001635 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001636 # print onosSwitch
1637 if onosSwitch[ 'device' ][ 'available' ]:
1638 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001639 ':',
1640 '' ).replace(
1641 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001642 '' ) == mnSwitch[ 'dpid' ]:
1643 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001644 if port[ 'isEnabled' ]:
1645 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001646 # onosPorts.append( 'local' )
1647 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001648 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001650 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001651 mnPorts.sort( key=float )
1652 onosPorts.sort( key=float )
1653 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1654 # print "\tmn_ports[] = ", mnPorts
1655 # print "\tonos_ports[] = ", onosPorts
1656 mnPortsLog = mnPorts
1657 onosPortsLog = onosPorts
1658 mnPorts = [ x for x in mnPorts ]
1659 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001660
Jon Hall7eb38402015-01-08 17:19:54 -08001661 # TODO: handle other reserved port numbers besides LOCAL
1662 # NOTE: Reserved ports
1663 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1664 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001665 for mnPort in mnPortsLog:
1666 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001667 # don't set results to true here as this is just one of
1668 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 mnPorts.remove( mnPort )
1670 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001671 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001672 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001673 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001674 if 65534 in mnPorts:
1675 mnPorts.remove( 65534 )
1676 if long( uint64( -2 ) ) in onosPorts:
1677 onosPorts.remove( long( uint64( -2 ) ) )
1678 if len( mnPorts ): # the ports of this switch don't match
1679 switchResult = main.FALSE
1680 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1681 if len( onosPorts ): # the ports of this switch don't match
1682 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001683 main.log.warn(
1684 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001685 str( onosPorts ) )
1686 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001687 main.log.report(
1688 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001689 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1690 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1691 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1692 portsResults = portsResults and switchResult
1693 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001694
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001696 """
1697 Compare mn and onos links
1698 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001699 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001700
Jon Hall7eb38402015-01-08 17:19:54 -08001701 This uses the sts TestONTopology object"""
1702 # FIXME: this does not look for extra links in ONOS, only checks that
Jon Hallefbd9792015-03-05 16:11:36 -08001703 # ONOS has what is in MN
Jon Hall7eb38402015-01-08 17:19:54 -08001704 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001705 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001706 # iterate through the MN topology and pull out switches and and port
1707 # info
1708 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001709 # print "Iterating though switches as seen by Mininet"
1710 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001711 ports = []
1712 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001713 # print port.hw_addr.toStr( separator='' )
1714 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001715 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001716 'name': port.name } )
1717 output[ 'switches' ].append( {
1718 "name": switch.name,
1719 "dpid": str( switch.dpid ).zfill( 16 ),
1720 "ports": ports } )
1721 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001722
kelvin-onlabd3b64892015-01-20 13:26:24 -08001723 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001724 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001725 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001726 if 2 * len( mnLinks ) == len( onos ):
1727 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001728 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001729 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001730 main.log.report(
Jon Hall328ddca2015-01-28 15:57:15 -08001731 "Mininet has " + str( len( mnLinks ) ) +
1732 " bidirectional links and ONOS has " +
1733 str( len( onos ) ) + " unidirectional links" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001734
Jon Hall7eb38402015-01-08 17:19:54 -08001735 # iterate through MN links and check if an ONOS link exists in
1736 # both directions
1737 # NOTE: Will currently only show mn links as down if they are
1738 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001740 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001742 # print "Link: %s" % link
1743 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001744 node1 = None
1745 port1 = None
1746 node2 = None
1747 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 firstDir = main.FALSE
1749 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001750 for switch in output[ 'switches' ]:
1751 # print "Switch: %s" % switch[ 'name' ]
1752 if switch[ 'name' ] == link.node1.name:
1753 node1 = switch[ 'dpid' ]
1754 for port in switch[ 'ports' ]:
1755 if str( port[ 'name' ] ) == str( link.port1 ):
1756 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001757 if node1 is not None and node2 is not None:
1758 break
Jon Hall7eb38402015-01-08 17:19:54 -08001759 if switch[ 'name' ] == link.node2.name:
1760 node2 = switch[ 'dpid' ]
1761 for port in switch[ 'ports' ]:
1762 if str( port[ 'name' ] ) == str( link.port2 ):
1763 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001764 if node1 is not None and node2 is not None:
1765 break
1766
kelvin-onlabd3b64892015-01-20 13:26:24 -08001767 for onosLink in onos:
1768 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001769 ":",
1770 '' ).replace(
1771 "of",
1772 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001773 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001774 ":",
1775 '' ).replace(
1776 "of",
1777 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001778 onosPort1 = onosLink[ 'src' ][ 'port' ]
1779 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001780
Jon Hall72cf1dc2014-10-20 21:04:50 -04001781 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001782 if str( onosNode1 ) == str( node1 ) and str(
1783 onosNode2 ) == str( node2 ):
1784 if int( onosPort1 ) == int( port1 ) and int(
1785 onosPort2 ) == int( port2 ):
1786 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001787 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001788 main.log.warn(
1789 'The port numbers do not match for ' +
1790 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001791 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001792 'link %s/%s -> %s/%s' %
1793 ( node1,
1794 port1,
1795 node2,
1796 port2 ) +
1797 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001798 ( onosNode1,
1799 onosPort1,
1800 onosNode2,
1801 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001802
1803 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001804 elif ( str( onosNode1 ) == str( node2 ) and
1805 str( onosNode2 ) == str( node1 ) ):
1806 if ( int( onosPort1 ) == int( port2 )
1807 and int( onosPort2 ) == int( port1 ) ):
1808 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001809 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001810 main.log.warn(
1811 'The port numbers do not match for ' +
1812 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001813 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001814 'link %s/%s -> %s/%s' %
1815 ( node2,
1816 port2,
1817 node1,
1818 port1 ) +
1819 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001820 ( onosNode2,
1821 onosPort2,
1822 onosNode1,
1823 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001824 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001825 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001826 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001827 main.log.report(
1828 'ONOS does not have the link %s/%s -> %s/%s' %
1829 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001831 main.log.report(
1832 'ONOS does not have the link %s/%s -> %s/%s' %
1833 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001834 linkResults = linkResults and firstDir and secondDir
1835 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001836
Jon Hallff6b4b22015-02-23 09:25:15 -08001837 def compareHosts( self, topo, hostsJson ):
1838 """
1839 Compare mn and onos Hosts.
1840 Since Mininet hosts are quiet, ONOS will only know of them when they
1841 speak. For this reason, we will only check that the hosts in ONOS
1842 stores are in Mininet, and not vice versa.
1843 topo: sts TestONTopology object
1844 hostsJson: parsed json object from the onos hosts api
1845
1846 This uses the sts TestONTopology object"""
1847 import json
1848 hostResults = main.TRUE
1849 hosts = []
1850 # iterate through the MN topology and pull out hosts
1851 for mnHost in topo.graph.hosts:
1852 interfaces = []
1853 for intf in mnHost.interfaces:
1854 interfaces.append( {
1855 "name": intf.name, # str
1856 "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs
1857 # hw_addr is of type EthAddr, Not JSON serializable
1858 "hw_addr": str( intf.hw_addr ) } )
1859 hosts.append( {
1860 "name": mnHost.name, # str
1861 "interfaces": interfaces } ) # list
1862 for onosHost in hostsJson:
1863 onosMAC = onosHost[ 'mac' ].lower()
1864 match = False
1865 for mnHost in hosts:
1866 for mnIntf in mnHost[ 'interfaces' ]:
1867 if onosMAC == mnIntf[ 'hw_addr' ].lower() :
1868 match = True
1869 for ip in mnIntf[ 'ips' ]:
1870 if ip in onosHost[ 'ips' ]:
1871 pass # all is well
1872 else:
1873 # misssing ip
1874 main.log.error( "ONOS host " + onosHost[ 'id' ]
1875 + " has a different IP than " +
1876 "the Mininet host." )
1877 output = json.dumps(
1878 onosHost,
1879 sort_keys=True,
1880 indent=4,
1881 separators=( ',', ': ' ) )
1882 main.log.info( output )
1883 hostResults = main.FALSE
1884 if not match:
1885 hostResults = main.FALSE
1886 main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " +
1887 "corresponding Mininet host." )
1888 output = json.dumps( onosHost,
1889 sort_keys=True,
1890 indent=4,
1891 separators=( ',', ': ' ) )
1892 main.log.info( output )
Jon Hallff6b4b22015-02-23 09:25:15 -08001893 return hostResults
1894
kelvin-onlabd3b64892015-01-20 13:26:24 -08001895 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001896 """
1897 Returns a list of all hosts
1898 Don't ask questions just use it"""
1899 self.handle.sendline( "" )
1900 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001901
Jon Hall7eb38402015-01-08 17:19:54 -08001902 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1903 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001904
kelvin-onlabd3b64892015-01-20 13:26:24 -08001905 handlePy = self.handle.before
1906 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1907 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001908
Jon Hall7eb38402015-01-08 17:19:54 -08001909 self.handle.sendline( "" )
1910 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001911
kelvin-onlabd3b64892015-01-20 13:26:24 -08001912 hostStr = handlePy.replace( "]", "" )
1913 hostStr = hostStr.replace( "'", "" )
1914 hostStr = hostStr.replace( "[", "" )
1915 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001916
kelvin-onlabd3b64892015-01-20 13:26:24 -08001917 return hostList
adminbae64d82013-08-01 10:50:15 -07001918
Jon Hall7eb38402015-01-08 17:19:54 -08001919 def update( self ):
1920 """
1921 updates the port address and status information for
1922 each port in mn"""
1923 # TODO: Add error checking. currently the mininet command has no output
Jon Hallefbd9792015-03-05 16:11:36 -08001924 main.log.info( "Updating MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001925 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001926 self.handle.sendline( "" )
1927 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001928
Jon Hall7eb38402015-01-08 17:19:54 -08001929 self.handle.sendline( "update" )
1930 self.handle.expect( "update" )
1931 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001932
Jon Hall7eb38402015-01-08 17:19:54 -08001933 self.handle.sendline( "" )
1934 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001935
Jon Hallb1290e82014-11-18 16:17:48 -05001936 return main.TRUE
1937 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001938 main.log.error( self.name + ": EOF exception found" )
1939 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001940 main.cleanup()
1941 main.exit()
1942
kaouthera3f13ca22015-05-05 15:01:41 -07001943 def assignVLAN( self, host, intf, vlan):
1944 """
1945 Add vlan tag to a host.
1946 Dependencies:
1947 This class depends on the "vlan" package
1948 $ sudo apt-get install vlan
1949 Configuration:
1950 Load the 8021q module into the kernel
1951 $sudo modprobe 8021q
1952
1953 To make this setup permanent:
1954 $ sudo su -c 'echo "8021q" >> /etc/modules'
1955 """
1956 if self.handle:
1957 try:
1958 # get the ip address of the host
1959 main.log.info("Get the ip address of the host")
1960 ipaddr = self.getIPAddress(host)
1961 print repr(ipaddr)
1962
1963 # remove IP from interface intf
1964 # Ex: h1 ifconfig h1-eth0 inet 0
1965 main.log.info("Remove IP from interface ")
1966 cmd2 = host + " ifconfig " + intf + " " + " inet 0 "
1967 self.handle.sendline( cmd2 )
1968 self.handle.expect( "mininet>" )
1969 response = self.handle.before
1970 main.log.info ( "====> %s ", response)
1971
1972
1973 # create VLAN interface
1974 # Ex: h1 vconfig add h1-eth0 100
1975 main.log.info("Create Vlan")
1976 cmd3 = host + " vconfig add " + intf + " " + vlan
1977 self.handle.sendline( cmd3 )
1978 self.handle.expect( "mininet>" )
1979 response = self.handle.before
1980 main.log.info( "====> %s ", response )
1981
1982 # assign the host's IP to the VLAN interface
1983 # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1
1984 main.log.info("Assign the host IP to the vlan interface")
1985 vintf = intf + "." + vlan
1986 cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr
1987 self.handle.sendline( cmd4 )
1988 self.handle.expect( "mininet>" )
1989 response = self.handle.before
1990 main.log.info ( "====> %s ", response)
1991
1992
1993 return main.TRUE
1994 except pexpect.EOF:
1995 main.log.error( self.name + ": EOF exception found" )
1996 main.log.error( self.name + ": " + self.handle.before )
1997 return main.FALSE
1998
adminbae64d82013-08-01 10:50:15 -07001999if __name__ != "__main__":
2000 import sys
kelvin-onlab50907142015-04-01 13:37:45 -07002001 sys.modules[ __name__ ] = MininetCliDriver()
kaouthera3f13ca22015-05-05 15:01:41 -07002002
2003