blob: 6a31a608fae0d987fd2303abee6be387a29140c0 [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
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400371 def pingallHosts( self, hostList, pingType='ipv4' ):
372 """
373 Ping all specified hosts with a specific ping type
374
375 Acceptable pingTypes:
376 - 'ipv4'
377 - 'ipv6'
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400378
379 Acceptable hostList:
380 - ['h1','h2','h3','h4']
381
382 Returns main.TRUE if all hosts specified can reach
383 each other
384
385 Returns main.FALSE if one or more of hosts specified
386 cannot reach each other"""
387
388 if pingType == "ipv4":
389 cmd = " ping -c 1 -i 1 -W 8 "
390 elif pingType == "ipv6":
391 cmd = " ping6 -c 1 -i 1 -W 8 "
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400392 else:
393 main.log.warn( "Invalid pingType specified" )
394 return
395
396 try:
397 main.log.info( "Testing reachability between specified hosts" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400398
399 isReachable = main.TRUE
400
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400401 for host in hostList:
402 listIndex = hostList.index(host)
403 # List of hosts to ping other than itself
404 pingList = hostList[:listIndex] + hostList[(listIndex+1):]
405
406 for temp in pingList:
407 # Current host pings all other hosts specified
408 pingCmd = str(host) + cmd + str(temp)
409 self.handle.sendline( pingCmd )
410 i = self.handle.expect( [ pingCmd, pexpect.TIMEOUT ] )
411 j = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
412 response = self.handle.before
413 if re.search( ',\s0\%\spacket\sloss', response ):
414 main.log.info( str(host) + " -> " + str(temp) )
415 else:
416 main.log.info( str(host) + " -> X ("+str(temp)+") "
417 " Destination Unreachable" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400418 # One of the host to host pair is unreachable
419 isReachable = main.FALSE
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400420
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400421 return isReachable
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400422
423 except pexpect.EOF:
424 main.log.error( self.name + ": EOF exception found" )
425 main.log.error( self.name + ": " + self.handle.before )
426 main.cleanup()
427 main.exit()
428
Jon Hall7eb38402015-01-08 17:19:54 -0800429 def pingHost( self, **pingParams ):
430 """
431 Ping from one mininet host to another
432 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800433 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800434 command = args[ "SRC" ] + " ping " + \
435 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700436 try:
Jon Hall61282e32015-03-19 11:34:11 -0700437 main.log.info( "Sending: " + command )
Jon Hall7eb38402015-01-08 17:19:54 -0800438 self.handle.sendline( command )
439 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700440 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800441 main.log.error(
442 self.name +
443 ": timeout when waiting for response from mininet" )
444 main.log.error( "response: " + str( self.handle.before ) )
445 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700446 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800447 main.log.error(
448 self.name +
449 ": timeout when waiting for response from mininet" )
450 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700451 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800452 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800453 main.log.error( self.name + ": EOF exception found" )
454 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700455 main.cleanup()
456 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800457 main.log.info( self.name + ": Ping Response: " + response )
458 if re.search( ',\s0\%\spacket\sloss', response ):
459 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800460 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700461 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800462 else:
463 main.log.error(
464 self.name +
465 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800466 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700467 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800468
Jon Hall7eb38402015-01-08 17:19:54 -0800469 def checkIP( self, host ):
470 """
471 Verifies the host's ip configured or not."""
472 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700473 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800474 response = self.execute(
475 cmd=host +
476 " ifconfig",
477 prompt="mininet>",
478 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800479 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800480 main.log.error( self.name + ": EOF exception found" )
481 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700482 main.cleanup()
483 main.exit()
adminbae64d82013-08-01 10:50:15 -0700484
Jon Hall7eb38402015-01-08 17:19:54 -0800485 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800486 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
487 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
488 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
489 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
490 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800491 # pattern = "inet addr:10.0.0.6"
492 if re.search( pattern, response ):
493 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700494 return main.TRUE
495 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800496 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700497 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800498 else:
499 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800500
Jon Hall7eb38402015-01-08 17:19:54 -0800501 def verifySSH( self, **connectargs ):
Jon Hallefbd9792015-03-05 16:11:36 -0800502 # FIXME: Who uses this and what is the purpose? seems very specific
Jon Hall6094a362014-04-11 14:46:56 -0700503 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800504 response = self.execute(
505 cmd="h1 /usr/sbin/sshd -D&",
506 prompt="mininet>",
507 timeout=10 )
508 response = self.execute(
509 cmd="h4 /usr/sbin/sshd -D&",
510 prompt="mininet>",
511 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700512 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800513 vars( self )[ key ] = connectargs[ key ]
514 response = self.execute(
515 cmd="xterm h1 h4 ",
516 prompt="mininet>",
517 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800518 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800519 main.log.error( self.name + ": EOF exception found" )
520 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700521 main.cleanup()
522 main.exit()
adminbae64d82013-08-01 10:50:15 -0700523 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800524 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700525 if self.flag == 0:
526 self.flag = 1
527 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800528 else:
adminbae64d82013-08-01 10:50:15 -0700529 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800530
kelvin-onlaba1484582015-02-02 15:46:20 -0800531 def moveHost( self, host, oldSw, newSw, ):
532 """
533 Moves a host from one switch to another on the fly
534 Note: The intf between host and oldSw when detached
535 using detach(), will still show up in the 'net'
536 cmd, because switch.detach() doesn't affect switch.intfs[]
537 (which is correct behavior since the interfaces
538 haven't moved).
539 """
540 if self.handle:
541 try:
542 # Bring link between oldSw-host down
Jon Hallefbd9792015-03-05 16:11:36 -0800543 cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\
544 "'," + "'down')"
kelvin-onlaba1484582015-02-02 15:46:20 -0800545 print "cmd1= ", cmd
Jon Hallefbd9792015-03-05 16:11:36 -0800546 response = self.execute( cmd=cmd,
547 prompt="mininet>",
548 timeout=10 )
kelvin-onlaba1484582015-02-02 15:46:20 -0800549
550 # Determine hostintf and Oldswitchintf
551 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800552 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800553 print "cmd2= ", cmd
554 self.handle.sendline( cmd )
555 self.handle.expect( "mininet>" )
556
shahshreya73537862015-02-11 15:15:24 -0800557 # Determine ip and mac address of the host-oldSw interface
kelvin-onlaba1484582015-02-02 15:46:20 -0800558 cmd = "px ipaddr = hintf.IP()"
559 print "cmd3= ", cmd
560 self.handle.sendline( cmd )
561 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800562
563 cmd = "px macaddr = hintf.MAC()"
564 print "cmd3= ", cmd
565 self.handle.sendline( cmd )
566 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800567
568 # Detach interface between oldSw-host
569 cmd = "px " + oldSw + ".detach( sintf )"
570 print "cmd4= ", cmd
571 self.handle.sendline( cmd )
572 self.handle.expect( "mininet>" )
573
574 # Add link between host-newSw
575 cmd = "py net.addLink(" + host + "," + newSw + ")"
576 print "cmd5= ", cmd
577 self.handle.sendline( cmd )
578 self.handle.expect( "mininet>" )
579
580 # Determine hostintf and Newswitchintf
581 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800582 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800583 print "cmd6= ", cmd
584 self.handle.sendline( cmd )
585 self.handle.expect( "mininet>" )
586
587 # Attach interface between newSw-host
588 cmd = "px " + newSw + ".attach( sintf )"
589 print "cmd3= ", cmd
590 self.handle.sendline( cmd )
591 self.handle.expect( "mininet>" )
592
593 # Set ipaddress of the host-newSw interface
594 cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)"
595 print "cmd7 = ", cmd
596 self.handle.sendline( cmd )
597 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800598
599 # Set macaddress of the host-newSw interface
600 cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)"
601 print "cmd8 = ", cmd
602 self.handle.sendline( cmd )
603 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800604
605 cmd = "net"
shahshreya73537862015-02-11 15:15:24 -0800606 print "cmd9 = ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800607 self.handle.sendline( cmd )
608 self.handle.expect( "mininet>" )
609 print "output = ", self.handle.before
610
611 # Determine ipaddress of the host-newSw interface
shahshreya73537862015-02-11 15:15:24 -0800612 cmd = host + " ifconfig"
613 print "cmd10= ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800614 self.handle.sendline( cmd )
615 self.handle.expect( "mininet>" )
616 print "ifconfig o/p = ", self.handle.before
617
618 return main.TRUE
619 except pexpect.EOF:
620 main.log.error( self.name + ": EOF exception found" )
621 main.log.error( self.name + ": " + self.handle.before )
622 return main.FALSE
623
Jon Hall7eb38402015-01-08 17:19:54 -0800624 def changeIP( self, host, intf, newIP, newNetmask ):
625 """
626 Changes the ip address of a host on the fly
627 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800628 if self.handle:
629 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800630 cmd = host + " ifconfig " + intf + " " + \
631 newIP + " " + 'netmask' + " " + newNetmask
632 self.handle.sendline( cmd )
633 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800634 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800635 main.log.info( "response = " + response )
636 main.log.info(
637 "Ip of host " +
638 host +
639 " changed to new IP " +
640 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800641 return main.TRUE
642 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800645 return main.FALSE
646
Jon Hall7eb38402015-01-08 17:19:54 -0800647 def changeDefaultGateway( self, host, newGW ):
648 """
649 Changes the default gateway of a host
650 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800651 if self.handle:
652 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800653 cmd = host + " route add default gw " + newGW
654 self.handle.sendline( cmd )
655 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800656 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800657 main.log.info( "response = " + response )
658 main.log.info(
659 "Default gateway of host " +
660 host +
661 " changed to " +
662 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800663 return main.TRUE
664 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800665 main.log.error( self.name + ": EOF exception found" )
666 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800667 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800668
Jon Hall7eb38402015-01-08 17:19:54 -0800669 def addStaticMACAddress( self, host, GW, macaddr ):
670 """
Jon Hallefbd9792015-03-05 16:11:36 -0800671 Changes the mac address of a gateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800672 if self.handle:
673 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800674 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
675 cmd = host + " arp -s " + GW + " " + macaddr
676 self.handle.sendline( cmd )
677 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800678 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800679 main.log.info( "response = " + response )
680 main.log.info(
Jon Hallefbd9792015-03-05 16:11:36 -0800681 "Mac address of gateway " +
Jon Hall7eb38402015-01-08 17:19:54 -0800682 GW +
683 " changed to " +
684 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800685 return main.TRUE
686 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 )
shahshreyad0c80432014-12-04 16:56:05 -0800689 return main.FALSE
690
Jon Hall7eb38402015-01-08 17:19:54 -0800691 def verifyStaticGWandMAC( self, host ):
692 """
693 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800694 if self.handle:
695 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800696 # h1 arp -an
697 cmd = host + " arp -an "
698 self.handle.sendline( cmd )
699 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800700 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800701 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800702 return main.TRUE
703 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800704 main.log.error( self.name + ": EOF exception found" )
705 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800706 return main.FALSE
707
Jon Hall7eb38402015-01-08 17:19:54 -0800708 def getMacAddress( self, host ):
709 """
710 Verifies the host's ip configured or not."""
711 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700712 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800713 response = self.execute(
714 cmd=host +
715 " ifconfig",
716 prompt="mininet>",
717 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800718 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800719 main.log.error( self.name + ": EOF exception found" )
720 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700721 main.cleanup()
722 main.exit()
adminbae64d82013-08-01 10:50:15 -0700723
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700724 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800725 macAddressSearch = re.search( pattern, response, re.I )
726 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800727 main.log.info(
728 self.name +
729 ": Mac-Address of Host " +
730 host +
731 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800732 macAddress )
733 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700734 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800735 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700736
Jon Hall7eb38402015-01-08 17:19:54 -0800737 def getInterfaceMACAddress( self, host, interface ):
738 """
739 Return the IP address of the interface on the given host"""
740 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700741 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800742 response = self.execute( cmd=host + " ifconfig " + interface,
743 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800744 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800745 main.log.error( self.name + ": EOF exception found" )
746 main.log.error( self.name + ": " + self.handle.before )
747 main.cleanup()
748 main.exit()
749
750 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 macAddressSearch = re.search( pattern, response, re.I )
752 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800753 main.log.info( "No mac address found in %s" % response )
754 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800755 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800756 main.log.info(
757 "Mac-Address of " +
758 host +
759 ":" +
760 interface +
761 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800762 macAddress )
763 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800764 else:
765 main.log.error( "Connection failed to the host" )
766
767 def getIPAddress( self, host ):
768 """
769 Verifies the host's ip configured or not."""
770 if self.handle:
771 try:
772 response = self.execute(
773 cmd=host +
774 " ifconfig",
775 prompt="mininet>",
776 timeout=10 )
777 except pexpect.EOF:
778 main.log.error( self.name + ": EOF exception found" )
779 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700780 main.cleanup()
781 main.exit()
adminbae64d82013-08-01 10:50:15 -0700782
783 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800784 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800785 main.log.info(
786 self.name +
787 ": IP-Address of Host " +
788 host +
789 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800790 ipAddressSearch.group( 1 ) )
791 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800792 else:
793 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800794
Jon Hall7eb38402015-01-08 17:19:54 -0800795 def getSwitchDPID( self, switch ):
796 """
797 return the datapath ID of the switch"""
798 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700799 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700800 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800801 response = self.execute(
802 cmd=cmd,
803 prompt="mininet>",
804 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800805 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800806 main.log.error( self.name + ": EOF exception found" )
807 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700808 main.cleanup()
809 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800810 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800811 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700812 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800813 main.log.info(
814 "Couldn't find DPID for switch %s, found: %s" %
815 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700816 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800817 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700818 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800819 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700820
Jon Hall7eb38402015-01-08 17:19:54 -0800821 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700822 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800823 self.handle.sendline( "" )
824 self.expect( "mininet>" )
825 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700826 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800827 response = self.execute(
828 cmd=cmd,
829 prompt="mininet>",
830 timeout=10 )
831 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700832 response = self.handle.before
833 return response
834 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800835 main.log.error( self.name + ": EOF exception found" )
836 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700837 main.cleanup()
838 main.exit()
839
Jon Hall7eb38402015-01-08 17:19:54 -0800840 def getInterfaces( self, node ):
841 """
842 return information dict about interfaces connected to the node"""
843 if self.handle:
844 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800845 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700846 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700847 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800848 response = self.execute(
849 cmd=cmd,
850 prompt="mininet>",
851 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800852 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800853 main.log.error( self.name + ": EOF exception found" )
854 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700855 main.cleanup()
856 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700857 return response
858 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800859 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700860
Jon Hall7eb38402015-01-08 17:19:54 -0800861 def dump( self ):
862 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700863 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800864 response = self.execute(
865 cmd='dump',
866 prompt='mininet>',
867 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800868 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800869 main.log.error( self.name + ": EOF exception found" )
870 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700871 main.cleanup()
872 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700873 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800874
Jon Hall7eb38402015-01-08 17:19:54 -0800875 def intfs( self ):
876 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700877 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800878 response = self.execute(
879 cmd='intfs',
880 prompt='mininet>',
881 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800882 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800883 main.log.error( self.name + ": EOF exception found" )
884 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700885 main.cleanup()
886 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700887 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800888
Jon Hall7eb38402015-01-08 17:19:54 -0800889 def net( self ):
890 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700891 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800892 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800893 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800894 main.log.error( self.name + ": EOF exception found" )
895 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700896 main.cleanup()
897 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700898 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800899
900 def iperf( self, host1, host2 ):
901 main.log.info(
902 self.name +
903 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700904 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800905 cmd1 = 'iperf ' + host1 + " " + host2
906 self.handle.sendline( cmd1 )
907 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800908 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800909 if re.search( 'Results:', response ):
Jon Hallefbd9792015-03-05 16:11:36 -0800910 main.log.info( self.name + ": iperf test successful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800911 return main.TRUE
912 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800913 main.log.error( self.name + ": iperf test failed" )
914 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800915 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800916 main.log.error( self.name + ": EOF exception found" )
917 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800918 main.cleanup()
919 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800920
Jon Hall7eb38402015-01-08 17:19:54 -0800921 def iperfudp( self ):
922 main.log.info(
923 self.name +
924 ": Simple iperf TCP test between two " +
925 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700926 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800927 response = self.execute(
928 cmd='iperfudp',
929 prompt='mininet>',
930 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800931 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800932 main.log.error( self.name + ": EOF exception found" )
933 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700934 main.cleanup()
935 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700936 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800937
Jon Hall7eb38402015-01-08 17:19:54 -0800938 def nodes( self ):
939 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700940 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800941 response = self.execute(
942 cmd='nodes',
943 prompt='mininet>',
944 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800945 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800946 main.log.error( self.name + ": EOF exception found" )
947 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700948 main.cleanup()
949 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700950 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800951
Jon Hall7eb38402015-01-08 17:19:54 -0800952 def pingpair( self ):
953 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700954 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800955 response = self.execute(
956 cmd='pingpair',
957 prompt='mininet>',
958 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800959 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800960 main.log.error( self.name + ": EOF exception found" )
961 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700962 main.cleanup()
963 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800964
Jon Hall7eb38402015-01-08 17:19:54 -0800965 if re.search( ',\s0\%\spacket\sloss', response ):
966 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700968 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800969 else:
970 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800971 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700972 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800973
Jon Hall7eb38402015-01-08 17:19:54 -0800974 def link( self, **linkargs ):
975 """
976 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800977 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800978 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
979 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
980 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
981 main.log.info(
982 "Bring link between '" +
983 end1 +
984 "' and '" +
985 end2 +
986 "' '" +
987 option +
988 "'" )
989 command = "link " + \
990 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700991 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800992 self.handle.sendline( command )
993 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800994 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800995 main.log.error( self.name + ": EOF exception found" )
996 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700997 main.cleanup()
998 main.exit()
adminbae64d82013-08-01 10:50:15 -0700999 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001000
Jon Hall7eb38402015-01-08 17:19:54 -08001001 def yank( self, **yankargs ):
1002 """
1003 yank a mininet switch interface to a host"""
1004 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001005 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001006 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1007 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1008 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001009 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001010 response = self.execute(
1011 cmd=command,
1012 prompt="mininet>",
1013 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001014 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001015 main.log.error( self.name + ": EOF exception found" )
1016 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001017 main.cleanup()
1018 main.exit()
adminaeedddd2013-08-02 15:14:15 -07001019 return main.TRUE
1020
Jon Hall7eb38402015-01-08 17:19:54 -08001021 def plug( self, **plugargs ):
1022 """
1023 plug the yanked mininet switch interface to a switch"""
1024 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001025 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001026 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1027 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1028 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001029 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001030 response = self.execute(
1031 cmd=command,
1032 prompt="mininet>",
1033 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001034 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001035 main.log.error( self.name + ": EOF exception found" )
1036 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001037 main.cleanup()
1038 main.exit()
adminbae64d82013-08-01 10:50:15 -07001039 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001040
Jon Hall7eb38402015-01-08 17:19:54 -08001041 def dpctl( self, **dpctlargs ):
1042 """
1043 Run dpctl command on all switches."""
1044 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001045 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001046 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
1047 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
1048 command = "dpctl " + cmd + " " + str( cmdargs )
1049 try:
1050 response = self.execute(
1051 cmd=command,
1052 prompt="mininet>",
1053 timeout=10 )
1054 except pexpect.EOF:
1055 main.log.error( self.name + ": EOF exception found" )
1056 main.log.error( self.name + ": " + self.handle.before )
1057 main.cleanup()
1058 main.exit()
1059 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001060
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 def getVersion( self ):
Jon Hallff6b4b22015-02-23 09:25:15 -08001062 #FIXME: What uses this? This should be refactored to get
1063 # version from MN and not some other file
kelvin-onlabd3b64892015-01-20 13:26:24 -08001064 fileInput = path + '/lib/Mininet/INSTALL'
1065 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -07001066 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -08001067 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -08001068 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -07001069 if result:
Jon Hall7eb38402015-01-08 17:19:54 -08001070 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -05001071 return version
adminbae64d82013-08-01 10:50:15 -07001072
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001074 """
Jon Hallec3c21e2014-11-10 22:22:37 -05001075 Parameters:
1076 sw: The name of an OVS switch. Example "s1"
1077 Return:
Jon Hall7eb38402015-01-08 17:19:54 -08001078 The output of the command from the mininet cli
1079 or main.FALSE on timeout"""
1080 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001081 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001082 response = self.execute(
1083 cmd=command,
1084 prompt="mininet>",
1085 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001086 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -05001087 return response
admin2a9548d2014-06-17 14:08:07 -07001088 else:
1089 return main.FALSE
1090 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001091 main.log.error( self.name + ": EOF exception found" )
1092 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001093 main.cleanup()
1094 main.exit()
adminbae64d82013-08-01 10:50:15 -07001095
kelvin-onlabd3b64892015-01-20 13:26:24 -08001096 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001097 """
1098 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001099 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001100 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -07001101
1102 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -08001103 for j in range( count ):
1104 argstring = argstring + ",IP" + \
1105 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001106 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -07001107
Jon Hall7eb38402015-01-08 17:19:54 -08001108 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1109 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001110 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -08001111 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -08001112
Jon Hall7eb38402015-01-08 17:19:54 -08001113 command = "sh ovs-vsctl set-controller s" + \
1114 str( sw ) + " " + ptcpB + " "
1115 for j in range( count ):
1116 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001117 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -08001118 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
1119 ip = args[
1120 "IP" +
1121 str( i ) ] if args[
1122 "IP" +
1123 str( i ) ] is not None else ""
1124 port = args[
1125 "PORT" +
1126 str( i ) ] if args[
1127 "PORT" +
1128 str( i ) ] is not None else ""
1129 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001130 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -07001131 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -07001132 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001133 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -07001134 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001135 main.log.error( self.name + ": EOF exception found" )
1136 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001137 main.cleanup()
1138 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001139 except Exception:
1140 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall6094a362014-04-11 14:46:56 -07001141 main.cleanup()
1142 main.exit()
adminbae64d82013-08-01 10:50:15 -07001143
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001145 """
1146 Removes the controller target from sw"""
1147 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -07001148 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001149 response = self.execute(
1150 cmd=command,
1151 prompt="mininet>",
1152 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001153 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001154 main.log.error( self.name + ": EOF exception found" )
1155 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -07001156 main.cleanup()
1157 main.exit()
1158 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001159 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -07001160
kelvin-onlabd3b64892015-01-20 13:26:24 -08001161 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001162 """
Jon Hallb1290e82014-11-18 16:17:48 -05001163 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001164 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001165 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001166 NOTE: cannot currently specify what type of switch
1167 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001168 sw = name of the new switch as a string
1169 optional keywords:
Jon Hallb1290e82014-11-18 16:17:48 -05001170 dpid = "dpid"
Jon Hallefbd9792015-03-05 16:11:36 -08001171 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001172 """
1173 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001174 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -05001175 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001176 response = self.execute(
1177 cmd=command,
1178 prompt="mininet>",
1179 timeout=10 )
1180 if re.search( "already exists!", response ):
1181 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001182 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001183 elif re.search( "Error", response ):
1184 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001185 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001186 elif re.search( "usage:", response ):
1187 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001188 return main.FALSE
1189 else:
1190 return main.TRUE
1191 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001192 main.log.error( self.name + ": EOF exception found" )
kaouthera3f13ca22015-05-05 15:01:41 -07001193 main.log.error(self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001194 main.cleanup()
1195 main.exit()
1196
kelvin-onlabd3b64892015-01-20 13:26:24 -08001197 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001198 """
Jon Hallbe6dfc42015-01-12 17:37:25 -08001199 delete a switch from the mininet topology
1200 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001201 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -08001202 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001203 sw = name of the switch as a string
1204 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001205 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -05001206 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001207 response = self.execute(
1208 cmd=command,
1209 prompt="mininet>",
1210 timeout=10 )
1211 if re.search( "no switch named", response ):
1212 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001213 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001214 elif re.search( "Error", response ):
1215 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001216 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001217 elif re.search( "usage:", response ):
1218 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001219 return main.FALSE
1220 else:
1221 return main.TRUE
1222 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001223 main.log.error( self.name + ": EOF exception found" )
1224 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001225 main.cleanup()
1226 main.exit()
1227
kelvin-onlabd3b64892015-01-20 13:26:24 -08001228 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001229 """
1230 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001231 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001232 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001233 NOTE: cannot currently specify what type of link
1234 required params:
1235 node1 = the string node name of the first endpoint of the link
1236 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001237 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001238 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001239 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001240 response = self.execute(
1241 cmd=command,
1242 prompt="mininet>",
1243 timeout=10 )
1244 if re.search( "doesnt exist!", response ):
1245 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001246 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001247 elif re.search( "Error", response ):
1248 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001249 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001250 elif re.search( "usage:", response ):
1251 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001252 return main.FALSE
1253 else:
1254 return main.TRUE
1255 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001256 main.log.error( self.name + ": EOF exception found" )
1257 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001258 main.cleanup()
1259 main.exit()
1260
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001262 """
1263 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001264 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001265 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001266 required params:
1267 node1 = the string node name of the first endpoint of the link
1268 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001269 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001270 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001271 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001272 response = self.execute(
1273 cmd=command,
1274 prompt="mininet>",
1275 timeout=10 )
1276 if re.search( "no node named", response ):
1277 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001278 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001279 elif re.search( "Error", response ):
1280 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001281 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001282 elif re.search( "usage:", response ):
1283 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001284 return main.FALSE
1285 else:
1286 return main.TRUE
1287 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001288 main.log.error( self.name + ": EOF exception found" )
1289 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001290 main.cleanup()
1291 main.exit()
1292
kelvin-onlabd3b64892015-01-20 13:26:24 -08001293 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001294 """
Jon Hallb1290e82014-11-18 16:17:48 -05001295 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001296 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001297 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001298 NOTE: cannot currently specify what type of host
1299 required params:
1300 hostname = the string hostname
1301 optional key-value params
1302 switch = "switch name"
Jon Hallefbd9792015-03-05 16:11:36 -08001303 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001304 """
1305 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001306 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001307 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001308 response = self.execute(
1309 cmd=command,
1310 prompt="mininet>",
1311 timeout=10 )
1312 if re.search( "already exists!", response ):
1313 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001314 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001315 elif re.search( "doesnt exists!", response ):
1316 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001317 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001318 elif re.search( "Error", response ):
1319 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001320 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001321 elif re.search( "usage:", response ):
1322 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001323 return main.FALSE
1324 else:
1325 return main.TRUE
1326 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001327 main.log.error( self.name + ": EOF exception found" )
1328 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001329 main.cleanup()
1330 main.exit()
1331
kelvin-onlabd3b64892015-01-20 13:26:24 -08001332 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001333 """
1334 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001335 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001336 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001337 NOTE: this uses a custom mn function
1338 required params:
1339 hostname = the string hostname
Jon Hallefbd9792015-03-05 16:11:36 -08001340 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001341 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001342 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001343 response = self.execute(
1344 cmd=command,
1345 prompt="mininet>",
1346 timeout=10 )
1347 if re.search( "no host named", response ):
1348 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001349 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001350 elif re.search( "Error", response ):
1351 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001352 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001353 elif re.search( "usage:", response ):
1354 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001355 return main.FALSE
1356 else:
1357 return main.TRUE
1358 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001359 main.log.error( self.name + ": EOF exception found" )
1360 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001361 main.cleanup()
1362 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001363
Jon Hall7eb38402015-01-08 17:19:54 -08001364 def disconnect( self ):
kelvin-onlaba1484582015-02-02 15:46:20 -08001365 """
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001366 Called at the end of the test to stop the mininet and
1367 disconnect the handle.
kelvin-onlaba1484582015-02-02 15:46:20 -08001368 """
1369 self.handle.sendline('')
Jon Halld61331b2015-02-17 16:35:47 -08001370 i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ],
Jon Hallefbd9792015-03-05 16:11:36 -08001371 timeout=2)
Jon Hall390696c2015-05-05 17:13:41 -07001372 response = main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001373 if i == 0:
Jon Hall390696c2015-05-05 17:13:41 -07001374 response = self.stopNet()
Jon Halld61331b2015-02-17 16:35:47 -08001375 elif i == 1:
1376 return main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001377 # print "Disconnecting Mininet"
1378 if self.handle:
1379 self.handle.sendline( "exit" )
1380 self.handle.expect( "exit" )
1381 self.handle.expect( "(.*)" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001382 else:
1383 main.log.error( "Connection failed to the host" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001384 return response
1385
Hari Krishnab35c6d02015-03-18 11:13:51 -07001386 def stopNet( self, fileName = "", timeout=5):
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001387 """
Jon Hall21270ac2015-02-16 17:59:55 -08001388 Stops mininet.
Jon Hallefbd9792015-03-05 16:11:36 -08001389 Returns main.TRUE if the mininet successfully stops and
Jon Hall21270ac2015-02-16 17:59:55 -08001390 main.FALSE if the pexpect handle does not exist.
1391
Jon Halld61331b2015-02-17 16:35:47 -08001392 Will cleanup and exit the test if mininet fails to stop
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001393 """
Jon Hall21270ac2015-02-16 17:59:55 -08001394
Jon Halld61331b2015-02-17 16:35:47 -08001395 main.log.info( self.name + ": Stopping mininet..." )
adminbae64d82013-08-01 10:50:15 -07001396 response = ''
1397 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001398 try:
kelvin-onlab26bc17f2015-02-06 14:08:59 -08001399 self.handle.sendline("")
kelvin-onlab56a3f462015-02-06 14:04:43 -08001400 i = self.handle.expect( [ 'mininet>',
1401 '\$',
1402 pexpect.EOF,
1403 pexpect.TIMEOUT ],
1404 timeout )
1405 if i == 0:
1406 main.log.info( "Exiting mininet..." )
1407
Jon Hall7eb38402015-01-08 17:19:54 -08001408 response = self.execute(
1409 cmd="exit",
1410 prompt="(.*)",
1411 timeout=120 )
Jon Halld61331b2015-02-17 16:35:47 -08001412 main.log.info( self.name + ": Stopped")
Jon Hall7eb38402015-01-08 17:19:54 -08001413 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001414 response = main.TRUE
Hari Krishnab35c6d02015-03-18 11:13:51 -07001415
kelvin-onlab56a3f462015-02-06 14:04:43 -08001416 if i == 1:
1417 main.log.info( " Mininet trying to exit while not " +
1418 "in the mininet prompt" )
1419 elif i == 2:
1420 main.log.error( "Something went wrong exiting mininet" )
1421 elif i == 3: # timeout
1422 main.log.error( "Something went wrong exiting mininet " +
1423 "TIMEOUT" )
1424
Hari Krishnab35c6d02015-03-18 11:13:51 -07001425 if fileName:
1426 self.handle.sendline("")
1427 self.handle.expect('\$')
1428 self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`")
Jon Hallfbc828e2015-01-06 17:30:19 -08001429 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001430 main.log.error( self.name + ": EOF exception found" )
1431 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001432 main.cleanup()
1433 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001434 else:
1435 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001436 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001437 return response
1438
kelvin-onlabf0594d72015-05-19 17:25:12 -07001439 def arping( self, host="", ip="10.128.20.211", ethDevice="" ):
kelvin-onlab65782a82015-05-07 14:12:13 -07001440 """
1441 Description:
1442 Sends arp message from mininet host for hosts discovery
1443 Required:
1444 host - hosts name
1445 Optional:
1446 ip - ip address that does not exist in the network so there would
1447 be no reply.
1448 """
kelvin-onlabf0594d72015-05-19 17:25:12 -07001449 if ethDevice:
1450 ethDevice = '-I ' + ethDevice + ' '
1451 cmd = " py " + host + ".cmd(\"arping -c 1 " + ethDevice + ip + "\")"
admin07529932013-11-22 14:58:28 -08001452 try:
kelvin-onlab65782a82015-05-07 14:12:13 -07001453 main.log.warn( "Sending: " + cmd )
1454 self.handle.sendline( cmd )
1455 response = self.handle.before
1456 self.handle.sendline( "" )
1457 self.handle.expect( "mininet>" )
admin07529932013-11-22 14:58:28 -08001458 return main.TRUE
kelvin-onlab65782a82015-05-07 14:12:13 -07001459
1460 except pexpect.EOF:
1461 main.log.error( self.name + ": EOF exception found" )
1462 main.log.error( self.name + ": " + self.handle.before )
1463 main.cleanup()
1464 main.exit()
admin07529932013-11-22 14:58:28 -08001465
Jon Hall7eb38402015-01-08 17:19:54 -08001466 def decToHex( self, num ):
1467 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001468
Jon Hall7eb38402015-01-08 17:19:54 -08001469 def getSwitchFlowCount( self, switch ):
1470 """
1471 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001472 if self.handle:
1473 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1474 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001475 response = self.execute(
1476 cmd=cmd,
1477 prompt="mininet>",
1478 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001479 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001480 main.log.error( self.name + ": EOF exception found" )
1481 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001482 main.cleanup()
1483 main.exit()
1484 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001485 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001486 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001487 main.log.info(
1488 "Couldn't find flows on switch %s, found: %s" %
1489 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001490 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001491 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001492 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001493 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001494
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 def checkFlows( self, sw, dumpFormat=None ):
1496 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001497 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001498 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001499 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001500 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001501 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001502 response = self.execute(
1503 cmd=command,
1504 prompt="mininet>",
1505 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001506 return response
1507 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001508 main.log.error( self.name + ": EOF exception found" )
1509 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001510 main.cleanup()
1511 main.exit()
admin2a9548d2014-06-17 14:08:07 -07001512
kelvin-onlabd3b64892015-01-20 13:26:24 -08001513 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001514 """
Jon Hallefbd9792015-03-05 16:11:36 -08001515 Runs tpdump on an interface and saves the file
Jon Hall7eb38402015-01-08 17:19:54 -08001516 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001517 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001518 self.handle.sendline( "" )
1519 self.handle.expect( "mininet>" )
1520 self.handle.sendline(
1521 "sh sudo tcpdump -n -i " +
1522 intf +
1523 " " +
1524 port +
1525 " -w " +
1526 filename.strip() +
1527 " &" )
1528 self.handle.sendline( "" )
1529 i = self.handle.expect( [ 'No\ssuch\device',
1530 'listening\son',
1531 pexpect.TIMEOUT,
1532 "mininet>" ],
1533 timeout=10 )
1534 main.log.warn( self.handle.before + self.handle.after )
1535 self.handle.sendline( "" )
1536 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001537 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001538 main.log.error(
1539 self.name +
1540 ": tcpdump - No such device exists. " +
1541 "tcpdump attempted on: " +
1542 intf )
admin2a9548d2014-06-17 14:08:07 -07001543 return main.FALSE
1544 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001545 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001546 return main.TRUE
1547 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001548 main.log.error(
1549 self.name +
1550 ": tcpdump command timed out! Check interface name," +
1551 " given interface was: " +
1552 intf )
admin2a9548d2014-06-17 14:08:07 -07001553 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001554 elif i == 3:
1555 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001556 return main.TRUE
1557 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001558 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001559 return main.FALSE
1560 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001561 main.log.error( self.name + ": EOF exception found" )
1562 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001563 main.cleanup()
1564 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001565 except Exception:
1566 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001567 main.cleanup()
1568 main.exit()
1569
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -08001571 """
1572 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -07001573 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001574 self.handle.sendline( "sh sudo pkill tcpdump" )
1575 self.handle.expect( "mininet>" )
1576 self.handle.sendline( "" )
1577 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001578 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001579 main.log.error( self.name + ": EOF exception found" )
1580 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001581 main.cleanup()
1582 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001583 except Exception:
1584 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001585 main.cleanup()
1586 main.exit()
1587
kelvin-onlabd3b64892015-01-20 13:26:24 -08001588 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001589 """
1590 Compare mn and onos switches
1591 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001593
Jon Hall7eb38402015-01-08 17:19:54 -08001594 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001595 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001596 output = { "switches": [] }
1597 # iterate through the MN topology and pull out switches and and port
1598 # info
1599 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001600 ports = []
1601 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001602 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001603 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001604 'name': port.name } )
1605 output[ 'switches' ].append( {
1606 "name": switch.name,
1607 "dpid": str( switch.dpid ).zfill( 16 ),
1608 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001609
Jon Hall7eb38402015-01-08 17:19:54 -08001610 # print "mn"
1611 # print json.dumps( output,
Jon Hallff6b4b22015-02-23 09:25:15 -08001612 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001613 # indent=4,
1614 # separators=( ',', ': ' ) )
1615 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001616 # print json.dumps( switchesJson,
Jon Hallff6b4b22015-02-23 09:25:15 -08001617 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001618 # indent=4,
1619 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001620
1621 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001622 mnDPIDs = []
1623 for switch in output[ 'switches' ]:
1624 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001625 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001626 # print "List of Mininet switch DPID's"
1627 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001628 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001629 main.log.error(
1630 self.name +
Jon Hallfeff3082015-05-19 10:23:26 -07001631 ".compareSwitches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001632 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001633 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001634 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001635 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001636 if switch[ 'available' ]:
1637 onosDPIDs.append(
1638 switch[ 'id' ].replace(
1639 ":",
1640 '' ).replace(
1641 "of",
1642 '' ).lower() )
1643 # else:
1644 # print "Switch is unavailable:"
1645 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001646 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001647 # print "List of ONOS switch DPID's"
1648 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001649
Jon Hall7eb38402015-01-08 17:19:54 -08001650 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001651 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001652 main.log.report( "Switches in MN but not in ONOS:" )
1653 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1654 main.log.report( str( list1 ) )
1655 main.log.report( "Switches in ONOS but not in MN:" )
1656 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001657 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001658 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001659 switchResults = main.TRUE
1660 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001661
kelvin-onlabd3b64892015-01-20 13:26:24 -08001662 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001663 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001664 Compare mn and onos ports
1665 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001666 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001667
Jon Hallfbc828e2015-01-06 17:30:19 -08001668 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001669 1. This uses the sts TestONTopology object
1670 2. numpy - "sudo pip install numpy"
1671
Jon Hall7eb38402015-01-08 17:19:54 -08001672 """
1673 # FIXME: this does not look for extra ports in ONOS, only checks that
1674 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001675 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001677 output = { "switches": [] }
1678 # iterate through the MN topology and pull out switches and and port
1679 # info
1680 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001681 ports = []
1682 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001683 # print port.hw_addr.toStr( separator='' )
Jon Hallefbd9792015-03-05 16:11:36 -08001684 tmpPort = { 'of_port': port.port_no,
1685 'mac': str( port.hw_addr ).replace( '\'', '' ),
1686 'name': port.name,
1687 'enabled': port.enabled }
Jon Hall39f29df2014-11-04 19:30:21 -05001688
kelvin-onlabd3b64892015-01-20 13:26:24 -08001689 ports.append( tmpPort )
Jon Hallefbd9792015-03-05 16:11:36 -08001690 tmpSwitch = { 'name': switch.name,
1691 'dpid': str( switch.dpid ).zfill( 16 ),
1692 'ports': ports }
Jon Hall39f29df2014-11-04 19:30:21 -05001693
kelvin-onlabd3b64892015-01-20 13:26:24 -08001694 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001695
Jon Hall7eb38402015-01-08 17:19:54 -08001696 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001697 for mnSwitch in output[ 'switches' ]:
1698 mnPorts = []
1699 onosPorts = []
1700 switchResult = main.TRUE
1701 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001702 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001703 mnPorts.append( port[ 'of_port' ] )
1704 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001705 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 # print onosSwitch
1707 if onosSwitch[ 'device' ][ 'available' ]:
1708 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001709 ':',
1710 '' ).replace(
1711 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 '' ) == mnSwitch[ 'dpid' ]:
1713 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001714 if port[ 'isEnabled' ]:
1715 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001716 # onosPorts.append( 'local' )
1717 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001718 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001719 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001720 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001721 mnPorts.sort( key=float )
1722 onosPorts.sort( key=float )
1723 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1724 # print "\tmn_ports[] = ", mnPorts
1725 # print "\tonos_ports[] = ", onosPorts
1726 mnPortsLog = mnPorts
1727 onosPortsLog = onosPorts
1728 mnPorts = [ x for x in mnPorts ]
1729 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001730
Jon Hall7eb38402015-01-08 17:19:54 -08001731 # TODO: handle other reserved port numbers besides LOCAL
1732 # NOTE: Reserved ports
1733 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1734 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001735 for mnPort in mnPortsLog:
1736 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001737 # don't set results to true here as this is just one of
1738 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 mnPorts.remove( mnPort )
1740 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001741 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001742 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001743 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001744 if 65534 in mnPorts:
1745 mnPorts.remove( 65534 )
1746 if long( uint64( -2 ) ) in onosPorts:
1747 onosPorts.remove( long( uint64( -2 ) ) )
1748 if len( mnPorts ): # the ports of this switch don't match
1749 switchResult = main.FALSE
1750 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1751 if len( onosPorts ): # the ports of this switch don't match
1752 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001753 main.log.warn(
1754 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 str( onosPorts ) )
1756 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001757 main.log.report(
1758 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1760 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1761 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1762 portsResults = portsResults and switchResult
1763 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001764
kelvin-onlabd3b64892015-01-20 13:26:24 -08001765 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001766 """
1767 Compare mn and onos links
1768 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001769 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001770
Jon Hall7eb38402015-01-08 17:19:54 -08001771 This uses the sts TestONTopology object"""
1772 # FIXME: this does not look for extra links in ONOS, only checks that
Jon Hallefbd9792015-03-05 16:11:36 -08001773 # ONOS has what is in MN
Jon Hall7eb38402015-01-08 17:19:54 -08001774 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001775 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001776 # iterate through the MN topology and pull out switches and and port
1777 # info
1778 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001779 # print "Iterating though switches as seen by Mininet"
1780 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001781 ports = []
1782 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001783 # print port.hw_addr.toStr( separator='' )
1784 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001785 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001786 'name': port.name } )
1787 output[ 'switches' ].append( {
1788 "name": switch.name,
1789 "dpid": str( switch.dpid ).zfill( 16 ),
1790 "ports": ports } )
1791 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001792
kelvin-onlabd3b64892015-01-20 13:26:24 -08001793 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001794 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001795 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001796 if 2 * len( mnLinks ) == len( onos ):
1797 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001798 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001799 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001800 main.log.report(
Jon Hall328ddca2015-01-28 15:57:15 -08001801 "Mininet has " + str( len( mnLinks ) ) +
1802 " bidirectional links and ONOS has " +
1803 str( len( onos ) ) + " unidirectional links" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001804
Jon Hall7eb38402015-01-08 17:19:54 -08001805 # iterate through MN links and check if an ONOS link exists in
1806 # both directions
1807 # NOTE: Will currently only show mn links as down if they are
1808 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001809 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001810 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001811 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001812 # print "Link: %s" % link
1813 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001814 node1 = None
1815 port1 = None
1816 node2 = None
1817 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001818 firstDir = main.FALSE
1819 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001820 for switch in output[ 'switches' ]:
1821 # print "Switch: %s" % switch[ 'name' ]
1822 if switch[ 'name' ] == link.node1.name:
1823 node1 = switch[ 'dpid' ]
1824 for port in switch[ 'ports' ]:
1825 if str( port[ 'name' ] ) == str( link.port1 ):
1826 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001827 if node1 is not None and node2 is not None:
1828 break
Jon Hall7eb38402015-01-08 17:19:54 -08001829 if switch[ 'name' ] == link.node2.name:
1830 node2 = switch[ 'dpid' ]
1831 for port in switch[ 'ports' ]:
1832 if str( port[ 'name' ] ) == str( link.port2 ):
1833 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001834 if node1 is not None and node2 is not None:
1835 break
1836
kelvin-onlabd3b64892015-01-20 13:26:24 -08001837 for onosLink in onos:
1838 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001839 ":",
1840 '' ).replace(
1841 "of",
1842 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001843 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001844 ":",
1845 '' ).replace(
1846 "of",
1847 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001848 onosPort1 = onosLink[ 'src' ][ 'port' ]
1849 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001850
Jon Hall72cf1dc2014-10-20 21:04:50 -04001851 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001852 if str( onosNode1 ) == str( node1 ) and str(
1853 onosNode2 ) == str( node2 ):
1854 if int( onosPort1 ) == int( port1 ) and int(
1855 onosPort2 ) == int( port2 ):
1856 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001857 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001858 main.log.warn(
1859 'The port numbers do not match for ' +
1860 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001861 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001862 'link %s/%s -> %s/%s' %
1863 ( node1,
1864 port1,
1865 node2,
1866 port2 ) +
1867 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001868 ( onosNode1,
1869 onosPort1,
1870 onosNode2,
1871 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001872
1873 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001874 elif ( str( onosNode1 ) == str( node2 ) and
1875 str( onosNode2 ) == str( node1 ) ):
1876 if ( int( onosPort1 ) == int( port2 )
1877 and int( onosPort2 ) == int( port1 ) ):
1878 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001879 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001880 main.log.warn(
1881 'The port numbers do not match for ' +
1882 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001883 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001884 'link %s/%s -> %s/%s' %
1885 ( node2,
1886 port2,
1887 node1,
1888 port1 ) +
1889 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001890 ( onosNode2,
1891 onosPort2,
1892 onosNode1,
1893 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001894 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001895 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001896 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001897 main.log.report(
1898 'ONOS does not have the link %s/%s -> %s/%s' %
1899 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001900 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001901 main.log.report(
1902 'ONOS does not have the link %s/%s -> %s/%s' %
1903 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001904 linkResults = linkResults and firstDir and secondDir
1905 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001906
Jon Hallff6b4b22015-02-23 09:25:15 -08001907 def compareHosts( self, topo, hostsJson ):
1908 """
1909 Compare mn and onos Hosts.
1910 Since Mininet hosts are quiet, ONOS will only know of them when they
1911 speak. For this reason, we will only check that the hosts in ONOS
1912 stores are in Mininet, and not vice versa.
1913 topo: sts TestONTopology object
1914 hostsJson: parsed json object from the onos hosts api
1915
1916 This uses the sts TestONTopology object"""
1917 import json
1918 hostResults = main.TRUE
1919 hosts = []
1920 # iterate through the MN topology and pull out hosts
1921 for mnHost in topo.graph.hosts:
1922 interfaces = []
1923 for intf in mnHost.interfaces:
1924 interfaces.append( {
1925 "name": intf.name, # str
1926 "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs
1927 # hw_addr is of type EthAddr, Not JSON serializable
1928 "hw_addr": str( intf.hw_addr ) } )
1929 hosts.append( {
1930 "name": mnHost.name, # str
1931 "interfaces": interfaces } ) # list
1932 for onosHost in hostsJson:
1933 onosMAC = onosHost[ 'mac' ].lower()
1934 match = False
1935 for mnHost in hosts:
1936 for mnIntf in mnHost[ 'interfaces' ]:
1937 if onosMAC == mnIntf[ 'hw_addr' ].lower() :
1938 match = True
1939 for ip in mnIntf[ 'ips' ]:
Jon Hallfeff3082015-05-19 10:23:26 -07001940 if ip in onosHost[ 'ipAddresses' ]:
Jon Hallff6b4b22015-02-23 09:25:15 -08001941 pass # all is well
1942 else:
1943 # misssing ip
1944 main.log.error( "ONOS host " + onosHost[ 'id' ]
1945 + " has a different IP than " +
1946 "the Mininet host." )
1947 output = json.dumps(
1948 onosHost,
1949 sort_keys=True,
1950 indent=4,
1951 separators=( ',', ': ' ) )
1952 main.log.info( output )
1953 hostResults = main.FALSE
1954 if not match:
1955 hostResults = main.FALSE
1956 main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " +
1957 "corresponding Mininet host." )
1958 output = json.dumps( onosHost,
1959 sort_keys=True,
1960 indent=4,
1961 separators=( ',', ': ' ) )
1962 main.log.info( output )
Jon Hallff6b4b22015-02-23 09:25:15 -08001963 return hostResults
1964
kelvin-onlabd3b64892015-01-20 13:26:24 -08001965 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001966 """
1967 Returns a list of all hosts
1968 Don't ask questions just use it"""
1969 self.handle.sendline( "" )
1970 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001971
Jon Hall7eb38402015-01-08 17:19:54 -08001972 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1973 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001974
kelvin-onlabd3b64892015-01-20 13:26:24 -08001975 handlePy = self.handle.before
1976 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1977 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001978
Jon Hall7eb38402015-01-08 17:19:54 -08001979 self.handle.sendline( "" )
1980 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001981
kelvin-onlabd3b64892015-01-20 13:26:24 -08001982 hostStr = handlePy.replace( "]", "" )
1983 hostStr = hostStr.replace( "'", "" )
1984 hostStr = hostStr.replace( "[", "" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -07001985 hostStr = hostStr.replace( " ", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001986 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001987
kelvin-onlabd3b64892015-01-20 13:26:24 -08001988 return hostList
adminbae64d82013-08-01 10:50:15 -07001989
Jon Hall7eb38402015-01-08 17:19:54 -08001990 def update( self ):
1991 """
1992 updates the port address and status information for
1993 each port in mn"""
1994 # TODO: Add error checking. currently the mininet command has no output
Jon Hallefbd9792015-03-05 16:11:36 -08001995 main.log.info( "Updating MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001996 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001997 self.handle.sendline( "" )
1998 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001999
Jon Hall7eb38402015-01-08 17:19:54 -08002000 self.handle.sendline( "update" )
2001 self.handle.expect( "update" )
2002 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05002003
Jon Hall7eb38402015-01-08 17:19:54 -08002004 self.handle.sendline( "" )
2005 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05002006
Jon Hallb1290e82014-11-18 16:17:48 -05002007 return main.TRUE
2008 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08002009 main.log.error( self.name + ": EOF exception found" )
2010 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05002011 main.cleanup()
2012 main.exit()
2013
kaouthera3f13ca22015-05-05 15:01:41 -07002014 def assignVLAN( self, host, intf, vlan):
2015 """
2016 Add vlan tag to a host.
2017 Dependencies:
2018 This class depends on the "vlan" package
2019 $ sudo apt-get install vlan
2020 Configuration:
2021 Load the 8021q module into the kernel
2022 $sudo modprobe 8021q
2023
2024 To make this setup permanent:
2025 $ sudo su -c 'echo "8021q" >> /etc/modules'
2026 """
2027 if self.handle:
2028 try:
2029 # get the ip address of the host
2030 main.log.info("Get the ip address of the host")
2031 ipaddr = self.getIPAddress(host)
2032 print repr(ipaddr)
2033
2034 # remove IP from interface intf
2035 # Ex: h1 ifconfig h1-eth0 inet 0
2036 main.log.info("Remove IP from interface ")
2037 cmd2 = host + " ifconfig " + intf + " " + " inet 0 "
2038 self.handle.sendline( cmd2 )
2039 self.handle.expect( "mininet>" )
2040 response = self.handle.before
2041 main.log.info ( "====> %s ", response)
2042
2043
2044 # create VLAN interface
2045 # Ex: h1 vconfig add h1-eth0 100
2046 main.log.info("Create Vlan")
2047 cmd3 = host + " vconfig add " + intf + " " + vlan
2048 self.handle.sendline( cmd3 )
2049 self.handle.expect( "mininet>" )
2050 response = self.handle.before
2051 main.log.info( "====> %s ", response )
2052
2053 # assign the host's IP to the VLAN interface
2054 # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1
2055 main.log.info("Assign the host IP to the vlan interface")
2056 vintf = intf + "." + vlan
2057 cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr
2058 self.handle.sendline( cmd4 )
2059 self.handle.expect( "mininet>" )
2060 response = self.handle.before
2061 main.log.info ( "====> %s ", response)
2062
2063
2064 return main.TRUE
2065 except pexpect.EOF:
2066 main.log.error( self.name + ": EOF exception found" )
2067 main.log.error( self.name + ": " + self.handle.before )
2068 return main.FALSE
2069
adminbae64d82013-08-01 10:50:15 -07002070if __name__ != "__main__":
2071 import sys
kelvin-onlab50907142015-04-01 13:37:45 -07002072 sys.modules[ __name__ ] = MininetCliDriver()
kaouthera3f13ca22015-05-05 15:01:41 -07002073
2074