blob: 8f22fcf905813bb49849f90c83fa730427c4a64f [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
Jon Hall7eb38402015-01-08 17:19:54 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Oct-2012
4
Jon Hallbe6dfc42015-01-12 17:37:25 -08005author: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07006
7
Jon Hall7eb38402015-01-08 17:19:54 -08008TestON is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 2 of the License, or
11( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070012
Jon Hall7eb38402015-01-08 17:19:54 -080013TestON is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
adminbae64d82013-08-01 10:50:15 -070017
Jon Hall7eb38402015-01-08 17:19:54 -080018You should have received a copy of the GNU General Public License
19along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
Jon Hallbe6dfc42015-01-12 17:37:25 -080022MininetCliDriver is the basic driver which will handle the Mininet functions
23
Jon Hall272a4db2015-01-12 17:43:48 -080024Some functions rely on STS module. To install this,
25 git clone https://github.com/jhall11/sts.git
26
Jon Hallbe6dfc42015-01-12 17:37:25 -080027Some functions rely on a modified version of Mininet. These functions
28should all be noted in the comments. To get this MN version run these commands
29from within your Mininet folder:
Jon Hall272a4db2015-01-12 17:43:48 -080030 git remote add jhall11 https://github.com/jhall11/mininet.git
Jon Hallbe6dfc42015-01-12 17:37:25 -080031 git fetch jhall11
Jon Hall272a4db2015-01-12 17:43:48 -080032 git checkout -b dynamic_topo remotes/jhall11/dynamic_topo
Jon Hallbe6dfc42015-01-12 17:37:25 -080033 git pull
34
Jon Hall272a4db2015-01-12 17:43:48 -080035
36 Note that you may need to run 'sudo make develop' if your mnexec.c file
Jon Hallbe6dfc42015-01-12 17:37:25 -080037changed when switching branches."""
adminbae64d82013-08-01 10:50:15 -070038import pexpect
adminbae64d82013-08-01 10:50:15 -070039import re
40import sys
Jon Hall7eb38402015-01-08 17:19:54 -080041sys.path.append( "../" )
Jon Hall1ccf82c2014-10-15 14:55:16 -040042from math import pow
adminbae64d82013-08-01 10:50:15 -070043from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070044
Jon Hall7eb38402015-01-08 17:19:54 -080045
kelvin-onlab50907142015-04-01 13:37:45 -070046class MininetCliDriver( Emulator ):
Jon Hall7eb38402015-01-08 17:19:54 -080047
48 """
49 MininetCliDriver is the basic driver which will handle
50 the Mininet functions"""
51 def __init__( self ):
52 super( Emulator, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070053 self.handle = self
Jon Hallefbd9792015-03-05 16:11:36 -080054 self.name = None
Jon Hall7eb38402015-01-08 17:19:54 -080055 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070056 self.flag = 0
57
Jon Hall7eb38402015-01-08 17:19:54 -080058 def connect( self, **connectargs ):
59 """
60 Here the main is the TestON instance after creating
61 all the log handles."""
kelvin-onlaba1484582015-02-02 15:46:20 -080062 try:
63 for key in connectargs:
64 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080065
kelvin-onlaba1484582015-02-02 15:46:20 -080066 self.name = self.options[ 'name' ]
67 self.handle = super(
kelvin-onlab50907142015-04-01 13:37:45 -070068 MininetCliDriver,
kelvin-onlaba1484582015-02-02 15:46:20 -080069 self ).connect(
70 user_name=self.user_name,
71 ip_address=self.ip_address,
72 port=None,
73 pwd=self.pwd )
Jon Hallfbc828e2015-01-06 17:30:19 -080074
kelvin-onlaba1484582015-02-02 15:46:20 -080075 if self.handle:
Jon Hallefbd9792015-03-05 16:11:36 -080076 main.log.info( "Connection successful to the host " +
77 self.user_name +
78 "@" +
79 self.ip_address )
kelvin-onlaba1484582015-02-02 15:46:20 -080080 return main.TRUE
81 else:
82 main.log.error( "Connection failed to the host " +
Jon Hallefbd9792015-03-05 16:11:36 -080083 self.user_name +
84 "@" +
85 self.ip_address )
Jon Hallfebb1c72015-03-05 13:30:09 -080086 main.log.error( "Failed to connect to the Mininet CLI" )
kelvin-onlaba1484582015-02-02 15:46:20 -080087 return main.FALSE
88 except pexpect.EOF:
89 main.log.error( self.name + ": EOF exception found" )
90 main.log.error( self.name + ": " + self.handle.before )
91 main.cleanup()
92 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080093 except Exception:
94 main.log.exception( self.name + ": Uncaught exception!" )
kelvin-onlaba1484582015-02-02 15:46:20 -080095 main.cleanup()
96 main.exit()
97
Jon Hallefbd9792015-03-05 16:11:36 -080098 def startNet( self, topoFile='', args='', timeout=120 ):
kelvin-onlab00ac67b2015-02-04 09:52:02 -080099 """
100 Starts Mininet accepts a topology(.py) file and/or an optional
Jon Hallefbd9792015-03-05 16:11:36 -0800101 argument ,to start the mininet, as a parameter.
Jon Hall21270ac2015-02-16 17:59:55 -0800102 Returns main.TRUE if the mininet starts successfully and
103 main.FALSE otherwise
kelvin-onlab00ac67b2015-02-04 09:52:02 -0800104 """
Jon Hall7eb38402015-01-08 17:19:54 -0800105 if self.handle:
Jon Hall689d8e42015-04-03 13:59:24 -0700106 # make sure old networks are cleaned up
107 main.log.info( self.name +
108 ": Clearing any residual state or processes" )
Jon Hall7eb38402015-01-08 17:19:54 -0800109 self.handle.sendline( "sudo mn -c" )
110 i = self.handle.expect( [ 'password\sfor\s',
111 'Cleanup\scomplete',
112 pexpect.EOF,
113 pexpect.TIMEOUT ],
kelvin-onlaba1484582015-02-02 15:46:20 -0800114 timeout )
Jon Hall7eb38402015-01-08 17:19:54 -0800115 if i == 0:
Jon Hall689d8e42015-04-03 13:59:24 -0700116 # Sudo asking for password
Jon Hall7eb38402015-01-08 17:19:54 -0800117 main.log.info( self.name + ": Sending sudo password" )
118 self.handle.sendline( self.pwd )
Jon Hallefbd9792015-03-05 16:11:36 -0800119 i = self.handle.expect( [ '%s:' % self.user,
Jon Hall7eb38402015-01-08 17:19:54 -0800120 '\$',
121 pexpect.EOF,
122 pexpect.TIMEOUT ],
kelvin-onlaba1484582015-02-02 15:46:20 -0800123 timeout )
Jon Hall7eb38402015-01-08 17:19:54 -0800124 if i == 1:
125 main.log.info( self.name + ": Clean" )
126 elif i == 2:
127 main.log.error( self.name + ": Connection terminated" )
128 elif i == 3: # timeout
Jon Hall689d8e42015-04-03 13:59:24 -0700129 main.log.error( self.name + ": Something while cleaning " +
130 "Mininet took too long... " )
131 # Craft the string to start mininet
132 cmdString = "sudo "
133 if topoFile is None or topoFile == '': # If no file is given
134 main.log.info( self.name + ": building fresh Mininet" )
135 cmdString += "mn "
136 if args is None or args == '':
137 # If no args given, use args from .topo file
138 args = self.options[ 'arg1' ] +\
139 " " + self.options[ 'arg2' ] +\
140 " --mac --controller " +\
141 self.options[ 'controller' ] + " " +\
142 self.options[ 'arg3' ]
143 else: # else only use given args
144 pass
145 # TODO: allow use of topo args and method args?
146 else: # Use given topology file
147 main.log.info( "Starting Mininet from topo file " + topoFile )
148 cmdString += topoFile + " "
Jon Hallefbd9792015-03-05 16:11:36 -0800149 if args is None:
kelvin-onlaba1484582015-02-02 15:46:20 -0800150 args = ''
Jon Hall689d8e42015-04-03 13:59:24 -0700151 # TODO: allow use of args from .topo file?
152 cmdString += args
153 # Send the command and check if network started
154 self.handle.sendline( "" )
155 self.handle.expect( '\$' )
156 main.log.info( "Sending '" + cmdString + "' to " + self.name )
157 self.handle.sendline( cmdString )
158 while True:
Jon Hall7eb38402015-01-08 17:19:54 -0800159 i = self.handle.expect( [ 'mininet>',
Jon Hall689d8e42015-04-03 13:59:24 -0700160 'Exception',
161 '\*\*\*',
Jon Hallefbd9792015-03-05 16:11:36 -0800162 pexpect.EOF,
163 pexpect.TIMEOUT ],
Jon Hall689d8e42015-04-03 13:59:24 -0700164 timeout )
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800165 if i == 0:
Jon Hall689d8e42015-04-03 13:59:24 -0700166 main.log.info( self.name + ": Mininet built" )
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800167 return main.TRUE
kelvin-onlabec228b82015-02-09 15:45:55 -0800168 elif i == 1:
Jon Hall689d8e42015-04-03 13:59:24 -0700169 response = str( self.handle.before +
170 self.handle.after )
171 self.handle.expect( '\$' )
172 response += str( self.handle.before +
173 self.handle.after )
174 main.log.error(
175 self.name +
176 ": Launching Mininet failed: " + response )
177 return main.FALSE
178 elif i == 2:
179 self.handle.expect( [ "\n",
180 pexpect.EOF,
181 pexpect.TIMEOUT ],
182 timeout )
183 main.log.info( self.handle.before )
184 elif i == 3:
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800185 main.log.error( self.name + ": Connection timeout" )
186 return main.FALSE
Jon Hall689d8e42015-04-03 13:59:24 -0700187 elif i == 4: # timeout
kelvin-onlabef0cc1c2015-02-09 15:20:26 -0800188 main.log.error(
189 self.name +
190 ": Something took too long... " )
191 return main.FALSE
Jon Hall689d8e42015-04-03 13:59:24 -0700192 # Why did we hit this part?
193 main.log.error( "startNet did not return correctly" )
194 return main.FASLE
Jon Hall7eb38402015-01-08 17:19:54 -0800195 else: # if no handle
Jon Hall689d8e42015-04-03 13:59:24 -0700196 main.log.error( self.name + ": Connection failed to the host " +
197 self.user_name + "@" + self.ip_address )
Jon Hall7eb38402015-01-08 17:19:54 -0800198 main.log.error( self.name + ": Failed to connect to the Mininet" )
adminbae64d82013-08-01 10:50:15 -0700199 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800200
kelvin-onlabfccaafa2015-01-20 13:50:44 -0800201 def numSwitchesNlinks( self, topoType, depth, fanout ):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400202 if topoType == 'tree':
Jon Hall7eb38402015-01-08 17:19:54 -0800203 # In tree topology, if fanout arg is not given, by default it is 2
204 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400205 fanout = 2
206 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500207 count = 0
Jon Hall7eb38402015-01-08 17:19:54 -0800208 while( k <= depth - 1 ):
209 count = count + pow( fanout, k )
210 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800211 numSwitches = count
Jon Hall7eb38402015-01-08 17:19:54 -0800212 while( k <= depth - 2 ):
213 # depth-2 gives you only core links and not considering
214 # edge links as seen by ONOS. If all the links including
215 # edge links are required, do depth-1
216 count = count + pow( fanout, k )
217 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800218 numLinks = count * fanout
Jon Hall7eb38402015-01-08 17:19:54 -0800219 # print "num_switches for %s(%d,%d) = %d and links=%d" %(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800220 # topoType,depth,fanout,numSwitches,numLinks )
Jon Hallfbc828e2015-01-06 17:30:19 -0800221
Jon Hall7eb38402015-01-08 17:19:54 -0800222 elif topoType == 'linear':
kelvin-onlabd3b64892015-01-20 13:26:24 -0800223 # In linear topology, if fanout or numHostsPerSw is not given,
Jon Hall7eb38402015-01-08 17:19:54 -0800224 # by default it is 1
225 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400226 fanout = 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800227 numSwitches = depth
228 numHostsPerSw = fanout
229 totalNumHosts = numSwitches * numHostsPerSw
230 numLinks = totalNumHosts + ( numSwitches - 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800231 print "num_switches for %s(%d,%d) = %d and links=%d" %\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800232 ( topoType, depth, fanout, numSwitches, numLinks )
Jon Hallefbd9792015-03-05 16:11:36 -0800233 topoDict = { "num_switches": int( numSwitches ),
234 "num_corelinks": int( numLinks ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400235 return topoDict
236
kelvin-onlabd3b64892015-01-20 13:26:24 -0800237 def calculateSwAndLinks( self ):
Jon Hall689d8e42015-04-03 13:59:24 -0700238 """
239 Calculate the number of switches and links in a topo."""
240 # TODO: combine this function and numSwitchesNlinks
241 argList = self.options[ 'arg1' ].split( "," )
242 topoArgList = argList[ 0 ].split( " " )
243 argList = map( int, argList[ 1: ] )
244 topoArgList = topoArgList[ 1: ] + argList
245
246 topoDict = self.numSwitchesNlinks( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400247 return topoDict
248
kelvin-onlabc44f0192015-04-02 22:08:41 -0700249 def pingall( self, timeout=300, shortCircuit=False, acceptableFailed=0):
Jon Hall7eb38402015-01-08 17:19:54 -0800250 """
251 Verifies the reachability of the hosts using pingall command.
252 Optional parameter timeout allows you to specify how long to
253 wait for pingall to complete
kelvin-onlabfbcd82f2015-04-02 12:06:00 -0700254 Optional:
Jon Hall390696c2015-05-05 17:13:41 -0700255 timeout(seconds) - How long to wait before breaking the pingall
kelvin-onlabfbcd82f2015-04-02 12:06:00 -0700256 shortCircuit - Break the pingall based on the number of failed hosts
kelvin-onlabc44f0192015-04-02 22:08:41 -0700257 ping
258 acceptableFailed - Set the number of acceptable failed pings for the
259 function to still return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800260 Returns:
261 main.TRUE if pingall completes with no pings dropped
Jon Hall390696c2015-05-05 17:13:41 -0700262 otherwise main.FALSE
263 """
264 import time
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700265 try:
Jon Hallfb760a02015-04-13 15:35:03 -0700266 timeout = int( timeout )
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700267 if self.handle:
268 main.log.info(
269 self.name +
270 ": Checking reachabilty to the hosts using pingall" )
271 response = ""
272 failedPings = 0
273 returnValue = main.TRUE
274 self.handle.sendline( "pingall" )
Jon Hall390696c2015-05-05 17:13:41 -0700275 startTime = time.time()
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700276 while True:
277 i = self.handle.expect( [ "mininet>","X",
278 pexpect.EOF,
279 pexpect.TIMEOUT ],
280 timeout )
281 if i == 0:
282 main.log.info( self.name + ": pingall finished")
283 response += self.handle.before
284 break
285 elif i == 1:
286 response += self.handle.before + self.handle.after
287 failedPings = failedPings + 1
kelvin-onlabd26a3742015-04-06 15:31:16 -0700288 if failedPings > acceptableFailed:
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700289 returnValue = main.FALSE
290 if shortCircuit:
291 main.log.error( self.name +
292 ": Aborting pingall - "
293 + str( failedPings ) +
294 " pings failed" )
295 break
Jon Hall390696c2015-05-05 17:13:41 -0700296 if ( time.time() - startTime ) > timeout:
297 returnValue = main.FALSE
298 main.log.error( self.name +
299 ": Aborting pingall - " +
300 "Function took too long " )
301 break
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700302 elif i == 2:
303 main.log.error( self.name +
304 ": EOF exception found" )
305 main.log.error( self.name + ": " +
306 self.handle.before )
307 main.cleanup()
308 main.exit()
309 elif i == 3:
310 response += self.handle.before
311 main.log.error( self.name +
312 ": TIMEOUT exception found" )
313 main.log.error( self.name +
314 ": " +
315 str( response ) )
316 # NOTE: Send ctrl-c to make sure pingall is done
317 self.handle.sendline( "\x03" )
318 self.handle.expect( "Interrupt" )
319 self.handle.expect( "mininet>" )
320 break
321 pattern = "Results\:"
322 main.log.info( "Pingall output: " + str( response ) )
323 if re.search( pattern, response ):
324 main.log.info( self.name + ": Pingall finished with "
325 + str( failedPings ) + " failed pings" )
326 return returnValue
327 else:
kelvin-onlabc44f0192015-04-02 22:08:41 -0700328 # NOTE: Send ctrl-c to make sure pingall is done
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700329 self.handle.sendline( "\x03" )
kelvin-onlabc44f0192015-04-02 22:08:41 -0700330 self.handle.expect( "Interrupt" )
kelvin-onlabc44f0192015-04-02 22:08:41 -0700331 self.handle.expect( "mininet>" )
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700332 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700333 else:
kelvin-onlabd9a8ed32015-04-03 13:55:28 -0700334 main.log.error( self.name + ": Connection failed to the host" )
335 main.cleanup()
336 main.exit()
337 except pexpect.TIMEOUT:
338 if response:
339 main.log.info( "Pingall output: " + str( response ) )
340 main.log.error( self.name + ": pexpect.TIMEOUT found" )
341 return main.FALSE
342 except pexpect.EOF:
343 main.log.error( self.name + ": EOF exception found" )
344 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500345 main.cleanup()
346 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700347
Jon Hall7eb38402015-01-08 17:19:54 -0800348 def fpingHost( self, **pingParams ):
349 """
350 Uses the fping package for faster pinging...
351 *requires fping to be installed on machine running mininet"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800352 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800353 command = args[ "SRC" ] + \
354 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
355 self.handle.sendline( command )
356 self.handle.expect(
357 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
358 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
359 response = self.handle.before
360 if re.search( ":\s-", response ):
361 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700362 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800363 elif re.search( ":\s\d{1,2}\.\d\d", response ):
364 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700365 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800366 main.log.info( self.name + ": Install fping on mininet machine... " )
367 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700368 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800369
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400370 def pingallHosts( self, hostList, pingType='ipv4' ):
371 """
372 Ping all specified hosts with a specific ping type
373
374 Acceptable pingTypes:
375 - 'ipv4'
376 - 'ipv6'
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400377
378 Acceptable hostList:
379 - ['h1','h2','h3','h4']
380
381 Returns main.TRUE if all hosts specified can reach
382 each other
383
384 Returns main.FALSE if one or more of hosts specified
385 cannot reach each other"""
386
387 if pingType == "ipv4":
388 cmd = " ping -c 1 -i 1 -W 8 "
389 elif pingType == "ipv6":
390 cmd = " ping6 -c 1 -i 1 -W 8 "
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400391 else:
392 main.log.warn( "Invalid pingType specified" )
393 return
394
395 try:
396 main.log.info( "Testing reachability between specified hosts" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400397
398 isReachable = main.TRUE
399
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400400 for host in hostList:
401 listIndex = hostList.index(host)
402 # List of hosts to ping other than itself
403 pingList = hostList[:listIndex] + hostList[(listIndex+1):]
404
405 for temp in pingList:
406 # Current host pings all other hosts specified
407 pingCmd = str(host) + cmd + str(temp)
408 self.handle.sendline( pingCmd )
409 i = self.handle.expect( [ pingCmd, pexpect.TIMEOUT ] )
410 j = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
411 response = self.handle.before
412 if re.search( ',\s0\%\spacket\sloss', response ):
413 main.log.info( str(host) + " -> " + str(temp) )
414 else:
415 main.log.info( str(host) + " -> X ("+str(temp)+") "
416 " Destination Unreachable" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400417 # One of the host to host pair is unreachable
418 isReachable = main.FALSE
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400419
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400420 return isReachable
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400421
422 except pexpect.EOF:
423 main.log.error( self.name + ": EOF exception found" )
424 main.log.error( self.name + ": " + self.handle.before )
425 main.cleanup()
426 main.exit()
427
Jon Hall7eb38402015-01-08 17:19:54 -0800428 def pingHost( self, **pingParams ):
429 """
430 Ping from one mininet host to another
431 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800432 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800433 command = args[ "SRC" ] + " ping " + \
434 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700435 try:
Jon Hall61282e32015-03-19 11:34:11 -0700436 main.log.info( "Sending: " + command )
Jon Hall7eb38402015-01-08 17:19:54 -0800437 self.handle.sendline( command )
438 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700439 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800440 main.log.error(
441 self.name +
442 ": timeout when waiting for response from mininet" )
443 main.log.error( "response: " + str( self.handle.before ) )
444 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700445 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800446 main.log.error(
447 self.name +
448 ": timeout when waiting for response from mininet" )
449 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700450 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800451 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800452 main.log.error( self.name + ": EOF exception found" )
453 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700454 main.cleanup()
455 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800456 main.log.info( self.name + ": Ping Response: " + response )
457 if re.search( ',\s0\%\spacket\sloss', response ):
458 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800459 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700460 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800461 else:
462 main.log.error(
463 self.name +
464 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800465 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700466 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800467
Jon Hall7eb38402015-01-08 17:19:54 -0800468 def checkIP( self, host ):
469 """
470 Verifies the host's ip configured or not."""
471 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700472 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800473 response = self.execute(
474 cmd=host +
475 " ifconfig",
476 prompt="mininet>",
477 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800478 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800479 main.log.error( self.name + ": EOF exception found" )
480 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700481 main.cleanup()
482 main.exit()
adminbae64d82013-08-01 10:50:15 -0700483
Jon Hall7eb38402015-01-08 17:19:54 -0800484 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800485 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
486 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
487 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
488 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
489 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800490 # pattern = "inet addr:10.0.0.6"
491 if re.search( pattern, response ):
492 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700493 return main.TRUE
494 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800495 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700496 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800497 else:
498 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800499
Jon Hall7eb38402015-01-08 17:19:54 -0800500 def verifySSH( self, **connectargs ):
Jon Hallefbd9792015-03-05 16:11:36 -0800501 # FIXME: Who uses this and what is the purpose? seems very specific
Jon Hall6094a362014-04-11 14:46:56 -0700502 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800503 response = self.execute(
504 cmd="h1 /usr/sbin/sshd -D&",
505 prompt="mininet>",
506 timeout=10 )
507 response = self.execute(
508 cmd="h4 /usr/sbin/sshd -D&",
509 prompt="mininet>",
510 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700511 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800512 vars( self )[ key ] = connectargs[ key ]
513 response = self.execute(
514 cmd="xterm h1 h4 ",
515 prompt="mininet>",
516 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800517 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800518 main.log.error( self.name + ": EOF exception found" )
519 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700520 main.cleanup()
521 main.exit()
adminbae64d82013-08-01 10:50:15 -0700522 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800523 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700524 if self.flag == 0:
525 self.flag = 1
526 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800527 else:
adminbae64d82013-08-01 10:50:15 -0700528 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800529
kelvin-onlaba1484582015-02-02 15:46:20 -0800530 def moveHost( self, host, oldSw, newSw, ):
531 """
532 Moves a host from one switch to another on the fly
533 Note: The intf between host and oldSw when detached
534 using detach(), will still show up in the 'net'
535 cmd, because switch.detach() doesn't affect switch.intfs[]
536 (which is correct behavior since the interfaces
537 haven't moved).
538 """
539 if self.handle:
540 try:
541 # Bring link between oldSw-host down
Jon Hallefbd9792015-03-05 16:11:36 -0800542 cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\
543 "'," + "'down')"
kelvin-onlaba1484582015-02-02 15:46:20 -0800544 print "cmd1= ", cmd
Jon Hallefbd9792015-03-05 16:11:36 -0800545 response = self.execute( cmd=cmd,
546 prompt="mininet>",
547 timeout=10 )
kelvin-onlaba1484582015-02-02 15:46:20 -0800548
549 # Determine hostintf and Oldswitchintf
550 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800551 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800552 print "cmd2= ", cmd
553 self.handle.sendline( cmd )
554 self.handle.expect( "mininet>" )
555
shahshreya73537862015-02-11 15:15:24 -0800556 # Determine ip and mac address of the host-oldSw interface
kelvin-onlaba1484582015-02-02 15:46:20 -0800557 cmd = "px ipaddr = hintf.IP()"
558 print "cmd3= ", cmd
559 self.handle.sendline( cmd )
560 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800561
562 cmd = "px macaddr = hintf.MAC()"
563 print "cmd3= ", cmd
564 self.handle.sendline( cmd )
565 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800566
567 # Detach interface between oldSw-host
568 cmd = "px " + oldSw + ".detach( sintf )"
569 print "cmd4= ", cmd
570 self.handle.sendline( cmd )
571 self.handle.expect( "mininet>" )
572
573 # Add link between host-newSw
574 cmd = "py net.addLink(" + host + "," + newSw + ")"
575 print "cmd5= ", cmd
576 self.handle.sendline( cmd )
577 self.handle.expect( "mininet>" )
578
579 # Determine hostintf and Newswitchintf
580 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800581 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800582 print "cmd6= ", cmd
583 self.handle.sendline( cmd )
584 self.handle.expect( "mininet>" )
585
586 # Attach interface between newSw-host
587 cmd = "px " + newSw + ".attach( sintf )"
588 print "cmd3= ", cmd
589 self.handle.sendline( cmd )
590 self.handle.expect( "mininet>" )
591
592 # Set ipaddress of the host-newSw interface
593 cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)"
594 print "cmd7 = ", cmd
595 self.handle.sendline( cmd )
596 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800597
598 # Set macaddress of the host-newSw interface
599 cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)"
600 print "cmd8 = ", cmd
601 self.handle.sendline( cmd )
602 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800603
604 cmd = "net"
shahshreya73537862015-02-11 15:15:24 -0800605 print "cmd9 = ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800606 self.handle.sendline( cmd )
607 self.handle.expect( "mininet>" )
608 print "output = ", self.handle.before
609
610 # Determine ipaddress of the host-newSw interface
shahshreya73537862015-02-11 15:15:24 -0800611 cmd = host + " ifconfig"
612 print "cmd10= ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800613 self.handle.sendline( cmd )
614 self.handle.expect( "mininet>" )
615 print "ifconfig o/p = ", self.handle.before
616
617 return main.TRUE
618 except pexpect.EOF:
619 main.log.error( self.name + ": EOF exception found" )
620 main.log.error( self.name + ": " + self.handle.before )
621 return main.FALSE
622
Jon Hall7eb38402015-01-08 17:19:54 -0800623 def changeIP( self, host, intf, newIP, newNetmask ):
624 """
625 Changes the ip address of a host on the fly
626 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800627 if self.handle:
628 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800629 cmd = host + " ifconfig " + intf + " " + \
630 newIP + " " + 'netmask' + " " + newNetmask
631 self.handle.sendline( cmd )
632 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800633 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800634 main.log.info( "response = " + response )
635 main.log.info(
636 "Ip of host " +
637 host +
638 " changed to new IP " +
639 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800640 return main.TRUE
641 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800642 main.log.error( self.name + ": EOF exception found" )
643 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800644 return main.FALSE
645
Jon Hall7eb38402015-01-08 17:19:54 -0800646 def changeDefaultGateway( self, host, newGW ):
647 """
648 Changes the default gateway of a host
649 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800650 if self.handle:
651 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800652 cmd = host + " route add default gw " + newGW
653 self.handle.sendline( cmd )
654 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800655 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800656 main.log.info( "response = " + response )
657 main.log.info(
658 "Default gateway of host " +
659 host +
660 " changed to " +
661 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800662 return main.TRUE
663 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800664 main.log.error( self.name + ": EOF exception found" )
665 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800666 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800667
Jon Hall7eb38402015-01-08 17:19:54 -0800668 def addStaticMACAddress( self, host, GW, macaddr ):
669 """
Jon Hallefbd9792015-03-05 16:11:36 -0800670 Changes the mac address of a gateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800671 if self.handle:
672 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800673 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
674 cmd = host + " arp -s " + GW + " " + macaddr
675 self.handle.sendline( cmd )
676 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800677 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800678 main.log.info( "response = " + response )
679 main.log.info(
Jon Hallefbd9792015-03-05 16:11:36 -0800680 "Mac address of gateway " +
Jon Hall7eb38402015-01-08 17:19:54 -0800681 GW +
682 " changed to " +
683 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800684 return main.TRUE
685 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800686 main.log.error( self.name + ": EOF exception found" )
687 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800688 return main.FALSE
689
Jon Hall7eb38402015-01-08 17:19:54 -0800690 def verifyStaticGWandMAC( self, host ):
691 """
692 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800693 if self.handle:
694 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800695 # h1 arp -an
696 cmd = host + " arp -an "
697 self.handle.sendline( cmd )
698 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800699 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800700 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800701 return main.TRUE
702 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800703 main.log.error( self.name + ": EOF exception found" )
704 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800705 return main.FALSE
706
Jon Hall7eb38402015-01-08 17:19:54 -0800707 def getMacAddress( self, host ):
708 """
709 Verifies the host's ip configured or not."""
710 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700711 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800712 response = self.execute(
713 cmd=host +
714 " ifconfig",
715 prompt="mininet>",
716 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800717 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800718 main.log.error( self.name + ": EOF exception found" )
719 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700720 main.cleanup()
721 main.exit()
adminbae64d82013-08-01 10:50:15 -0700722
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700723 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800724 macAddressSearch = re.search( pattern, response, re.I )
725 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800726 main.log.info(
727 self.name +
728 ": Mac-Address of Host " +
729 host +
730 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 macAddress )
732 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700733 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800734 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700735
Jon Hall7eb38402015-01-08 17:19:54 -0800736 def getInterfaceMACAddress( self, host, interface ):
737 """
738 Return the IP address of the interface on the given host"""
739 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700740 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800741 response = self.execute( cmd=host + " ifconfig " + interface,
742 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800743 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800744 main.log.error( self.name + ": EOF exception found" )
745 main.log.error( self.name + ": " + self.handle.before )
746 main.cleanup()
747 main.exit()
748
749 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800750 macAddressSearch = re.search( pattern, response, re.I )
751 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800752 main.log.info( "No mac address found in %s" % response )
753 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800755 main.log.info(
756 "Mac-Address of " +
757 host +
758 ":" +
759 interface +
760 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800761 macAddress )
762 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800763 else:
764 main.log.error( "Connection failed to the host" )
765
766 def getIPAddress( self, host ):
767 """
768 Verifies the host's ip configured or not."""
769 if self.handle:
770 try:
771 response = self.execute(
772 cmd=host +
773 " ifconfig",
774 prompt="mininet>",
775 timeout=10 )
776 except pexpect.EOF:
777 main.log.error( self.name + ": EOF exception found" )
778 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700779 main.cleanup()
780 main.exit()
adminbae64d82013-08-01 10:50:15 -0700781
782 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800784 main.log.info(
785 self.name +
786 ": IP-Address of Host " +
787 host +
788 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800789 ipAddressSearch.group( 1 ) )
790 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800791 else:
792 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800793
Jon Hall7eb38402015-01-08 17:19:54 -0800794 def getSwitchDPID( self, switch ):
795 """
796 return the datapath ID of the switch"""
797 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700798 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700799 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800800 response = self.execute(
801 cmd=cmd,
802 prompt="mininet>",
803 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800804 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800805 main.log.error( self.name + ": EOF exception found" )
806 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700807 main.cleanup()
808 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800809 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800810 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700811 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800812 main.log.info(
813 "Couldn't find DPID for switch %s, found: %s" %
814 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700815 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800816 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700817 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800818 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700819
Jon Hall7eb38402015-01-08 17:19:54 -0800820 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700821 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800822 self.handle.sendline( "" )
823 self.expect( "mininet>" )
824 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700825 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800826 response = self.execute(
827 cmd=cmd,
828 prompt="mininet>",
829 timeout=10 )
830 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700831 response = self.handle.before
832 return response
833 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800834 main.log.error( self.name + ": EOF exception found" )
835 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700836 main.cleanup()
837 main.exit()
838
Jon Hall7eb38402015-01-08 17:19:54 -0800839 def getInterfaces( self, node ):
840 """
841 return information dict about interfaces connected to the node"""
842 if self.handle:
843 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800844 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700845 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700846 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800847 response = self.execute(
848 cmd=cmd,
849 prompt="mininet>",
850 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800851 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800852 main.log.error( self.name + ": EOF exception found" )
853 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700854 main.cleanup()
855 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700856 return response
857 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800858 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700859
Jon Hall7eb38402015-01-08 17:19:54 -0800860 def dump( self ):
861 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700862 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800863 response = self.execute(
864 cmd='dump',
865 prompt='mininet>',
866 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800867 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800868 main.log.error( self.name + ": EOF exception found" )
869 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700870 main.cleanup()
871 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700872 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800873
Jon Hall7eb38402015-01-08 17:19:54 -0800874 def intfs( self ):
875 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700876 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800877 response = self.execute(
878 cmd='intfs',
879 prompt='mininet>',
880 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800881 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800882 main.log.error( self.name + ": EOF exception found" )
883 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700884 main.cleanup()
885 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700886 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800887
Jon Hall7eb38402015-01-08 17:19:54 -0800888 def net( self ):
889 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700890 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800891 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800892 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800893 main.log.error( self.name + ": EOF exception found" )
894 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700895 main.cleanup()
896 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700897 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800898
899 def iperf( self, host1, host2 ):
900 main.log.info(
901 self.name +
902 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700903 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800904 cmd1 = 'iperf ' + host1 + " " + host2
905 self.handle.sendline( cmd1 )
906 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800907 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800908 if re.search( 'Results:', response ):
Jon Hallefbd9792015-03-05 16:11:36 -0800909 main.log.info( self.name + ": iperf test successful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800910 return main.TRUE
911 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800912 main.log.error( self.name + ": iperf test failed" )
913 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800914 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800915 main.log.error( self.name + ": EOF exception found" )
916 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800917 main.cleanup()
918 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800919
Jon Hall7eb38402015-01-08 17:19:54 -0800920 def iperfudp( self ):
921 main.log.info(
922 self.name +
923 ": Simple iperf TCP test between two " +
924 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700925 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800926 response = self.execute(
927 cmd='iperfudp',
928 prompt='mininet>',
929 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800930 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800931 main.log.error( self.name + ": EOF exception found" )
932 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700933 main.cleanup()
934 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700935 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800936
Jon Hall7eb38402015-01-08 17:19:54 -0800937 def nodes( self ):
938 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700939 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800940 response = self.execute(
941 cmd='nodes',
942 prompt='mininet>',
943 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800944 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800945 main.log.error( self.name + ": EOF exception found" )
946 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700947 main.cleanup()
948 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700949 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800950
Jon Hall7eb38402015-01-08 17:19:54 -0800951 def pingpair( self ):
952 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700953 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800954 response = self.execute(
955 cmd='pingpair',
956 prompt='mininet>',
957 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800958 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800959 main.log.error( self.name + ": EOF exception found" )
960 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700961 main.cleanup()
962 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800963
Jon Hall7eb38402015-01-08 17:19:54 -0800964 if re.search( ',\s0\%\spacket\sloss', response ):
965 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800966 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700967 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800968 else:
969 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800970 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700971 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800972
Jon Hall7eb38402015-01-08 17:19:54 -0800973 def link( self, **linkargs ):
974 """
975 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800976 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800977 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
978 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
979 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
980 main.log.info(
981 "Bring link between '" +
982 end1 +
983 "' and '" +
984 end2 +
985 "' '" +
986 option +
987 "'" )
988 command = "link " + \
989 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700990 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800991 self.handle.sendline( command )
992 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800993 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800994 main.log.error( self.name + ": EOF exception found" )
995 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700996 main.cleanup()
997 main.exit()
adminbae64d82013-08-01 10:50:15 -0700998 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800999
Jon Hall7eb38402015-01-08 17:19:54 -08001000 def yank( self, **yankargs ):
1001 """
1002 yank a mininet switch interface to a host"""
1003 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001004 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001005 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1006 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1007 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001008 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001009 response = self.execute(
1010 cmd=command,
1011 prompt="mininet>",
1012 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001013 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001014 main.log.error( self.name + ": EOF exception found" )
1015 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001016 main.cleanup()
1017 main.exit()
adminaeedddd2013-08-02 15:14:15 -07001018 return main.TRUE
1019
Jon Hall7eb38402015-01-08 17:19:54 -08001020 def plug( self, **plugargs ):
1021 """
1022 plug the yanked mininet switch interface to a switch"""
1023 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001024 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001025 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1026 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1027 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001028 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001029 response = self.execute(
1030 cmd=command,
1031 prompt="mininet>",
1032 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001033 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001034 main.log.error( self.name + ": EOF exception found" )
1035 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001036 main.cleanup()
1037 main.exit()
adminbae64d82013-08-01 10:50:15 -07001038 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001039
Jon Hall7eb38402015-01-08 17:19:54 -08001040 def dpctl( self, **dpctlargs ):
1041 """
1042 Run dpctl command on all switches."""
1043 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001044 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001045 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
1046 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
1047 command = "dpctl " + cmd + " " + str( cmdargs )
1048 try:
1049 response = self.execute(
1050 cmd=command,
1051 prompt="mininet>",
1052 timeout=10 )
1053 except pexpect.EOF:
1054 main.log.error( self.name + ": EOF exception found" )
1055 main.log.error( self.name + ": " + self.handle.before )
1056 main.cleanup()
1057 main.exit()
1058 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001059
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 def getVersion( self ):
Jon Hallff6b4b22015-02-23 09:25:15 -08001061 #FIXME: What uses this? This should be refactored to get
1062 # version from MN and not some other file
kelvin-onlabd3b64892015-01-20 13:26:24 -08001063 fileInput = path + '/lib/Mininet/INSTALL'
1064 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -07001065 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -08001066 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -08001067 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -07001068 if result:
Jon Hall7eb38402015-01-08 17:19:54 -08001069 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -05001070 return version
adminbae64d82013-08-01 10:50:15 -07001071
kelvin-onlabd3b64892015-01-20 13:26:24 -08001072 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001073 """
Jon Hallec3c21e2014-11-10 22:22:37 -05001074 Parameters:
1075 sw: The name of an OVS switch. Example "s1"
1076 Return:
Jon Hall7eb38402015-01-08 17:19:54 -08001077 The output of the command from the mininet cli
1078 or main.FALSE on timeout"""
1079 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001080 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001081 response = self.execute(
1082 cmd=command,
1083 prompt="mininet>",
1084 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001085 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -05001086 return response
admin2a9548d2014-06-17 14:08:07 -07001087 else:
1088 return main.FALSE
1089 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001090 main.log.error( self.name + ": EOF exception found" )
1091 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001092 main.cleanup()
1093 main.exit()
adminbae64d82013-08-01 10:50:15 -07001094
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001096 """
1097 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001098 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001099 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -07001100
1101 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -08001102 for j in range( count ):
1103 argstring = argstring + ",IP" + \
1104 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001105 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -07001106
Jon Hall7eb38402015-01-08 17:19:54 -08001107 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1108 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001109 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -08001110 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -08001111
Jon Hall7eb38402015-01-08 17:19:54 -08001112 command = "sh ovs-vsctl set-controller s" + \
1113 str( sw ) + " " + ptcpB + " "
1114 for j in range( count ):
1115 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001116 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -08001117 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
1118 ip = args[
1119 "IP" +
1120 str( i ) ] if args[
1121 "IP" +
1122 str( i ) ] is not None else ""
1123 port = args[
1124 "PORT" +
1125 str( i ) ] if args[
1126 "PORT" +
1127 str( i ) ] is not None else ""
1128 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001129 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -07001130 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -07001131 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001132 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -07001133 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001134 main.log.error( self.name + ": EOF exception found" )
1135 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001136 main.cleanup()
1137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001138 except Exception:
1139 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall6094a362014-04-11 14:46:56 -07001140 main.cleanup()
1141 main.exit()
adminbae64d82013-08-01 10:50:15 -07001142
kelvin-onlabd3b64892015-01-20 13:26:24 -08001143 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001144 """
1145 Removes the controller target from sw"""
1146 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -07001147 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001148 response = self.execute(
1149 cmd=command,
1150 prompt="mininet>",
1151 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001152 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001153 main.log.error( self.name + ": EOF exception found" )
1154 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -07001155 main.cleanup()
1156 main.exit()
1157 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001158 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -07001159
kelvin-onlabd3b64892015-01-20 13:26:24 -08001160 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001161 """
Jon Hallb1290e82014-11-18 16:17:48 -05001162 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001163 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001164 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001165 NOTE: cannot currently specify what type of switch
1166 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001167 sw = name of the new switch as a string
1168 optional keywords:
Jon Hallb1290e82014-11-18 16:17:48 -05001169 dpid = "dpid"
Jon Hallefbd9792015-03-05 16:11:36 -08001170 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001171 """
1172 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001173 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -05001174 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001175 response = self.execute(
1176 cmd=command,
1177 prompt="mininet>",
1178 timeout=10 )
1179 if re.search( "already exists!", response ):
1180 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001181 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001182 elif re.search( "Error", response ):
1183 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001184 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001185 elif re.search( "usage:", response ):
1186 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001187 return main.FALSE
1188 else:
1189 return main.TRUE
1190 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001191 main.log.error( self.name + ": EOF exception found" )
1192 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001193 main.cleanup()
1194 main.exit()
1195
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001197 """
Jon Hallbe6dfc42015-01-12 17:37:25 -08001198 delete a switch from the mininet topology
1199 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001200 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -08001201 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001202 sw = name of the switch as a string
1203 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001204 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -05001205 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001206 response = self.execute(
1207 cmd=command,
1208 prompt="mininet>",
1209 timeout=10 )
1210 if re.search( "no switch named", response ):
1211 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001212 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001213 elif re.search( "Error", response ):
1214 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001215 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001216 elif re.search( "usage:", response ):
1217 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001218 return main.FALSE
1219 else:
1220 return main.TRUE
1221 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001222 main.log.error( self.name + ": EOF exception found" )
1223 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001224 main.cleanup()
1225 main.exit()
1226
kelvin-onlabd3b64892015-01-20 13:26:24 -08001227 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001228 """
1229 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001230 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001231 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001232 NOTE: cannot currently specify what type of link
1233 required params:
1234 node1 = the string node name of the first endpoint of the link
1235 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001236 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001237 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001238 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001239 response = self.execute(
1240 cmd=command,
1241 prompt="mininet>",
1242 timeout=10 )
1243 if re.search( "doesnt exist!", response ):
1244 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001245 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001246 elif re.search( "Error", response ):
1247 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001248 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001249 elif re.search( "usage:", response ):
1250 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001251 return main.FALSE
1252 else:
1253 return main.TRUE
1254 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001255 main.log.error( self.name + ": EOF exception found" )
1256 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001257 main.cleanup()
1258 main.exit()
1259
kelvin-onlabd3b64892015-01-20 13:26:24 -08001260 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001261 """
1262 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001263 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001264 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001265 required params:
1266 node1 = the string node name of the first endpoint of the link
1267 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001268 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001269 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001270 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001271 response = self.execute(
1272 cmd=command,
1273 prompt="mininet>",
1274 timeout=10 )
1275 if re.search( "no node named", response ):
1276 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001277 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001278 elif re.search( "Error", response ):
1279 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001280 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001281 elif re.search( "usage:", response ):
1282 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001283 return main.FALSE
1284 else:
1285 return main.TRUE
1286 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001287 main.log.error( self.name + ": EOF exception found" )
1288 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001289 main.cleanup()
1290 main.exit()
1291
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001293 """
Jon Hallb1290e82014-11-18 16:17:48 -05001294 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001295 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001296 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001297 NOTE: cannot currently specify what type of host
1298 required params:
1299 hostname = the string hostname
1300 optional key-value params
1301 switch = "switch name"
Jon Hallefbd9792015-03-05 16:11:36 -08001302 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001303 """
1304 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001305 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001306 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001307 response = self.execute(
1308 cmd=command,
1309 prompt="mininet>",
1310 timeout=10 )
1311 if re.search( "already exists!", response ):
1312 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001313 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001314 elif re.search( "doesnt exists!", response ):
1315 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001316 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001317 elif re.search( "Error", response ):
1318 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001319 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001320 elif re.search( "usage:", response ):
1321 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001322 return main.FALSE
1323 else:
1324 return main.TRUE
1325 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001326 main.log.error( self.name + ": EOF exception found" )
1327 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001328 main.cleanup()
1329 main.exit()
1330
kelvin-onlabd3b64892015-01-20 13:26:24 -08001331 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001332 """
1333 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001334 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001335 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001336 NOTE: this uses a custom mn function
1337 required params:
1338 hostname = the string hostname
Jon Hallefbd9792015-03-05 16:11:36 -08001339 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001340 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001341 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001342 response = self.execute(
1343 cmd=command,
1344 prompt="mininet>",
1345 timeout=10 )
1346 if re.search( "no host named", response ):
1347 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001348 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001349 elif re.search( "Error", response ):
1350 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001351 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001352 elif re.search( "usage:", response ):
1353 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001354 return main.FALSE
1355 else:
1356 return main.TRUE
1357 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001358 main.log.error( self.name + ": EOF exception found" )
1359 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001360 main.cleanup()
1361 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001362
Jon Hall7eb38402015-01-08 17:19:54 -08001363 def disconnect( self ):
kelvin-onlaba1484582015-02-02 15:46:20 -08001364 """
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001365 Called at the end of the test to stop the mininet and
1366 disconnect the handle.
kelvin-onlaba1484582015-02-02 15:46:20 -08001367 """
1368 self.handle.sendline('')
Jon Halld61331b2015-02-17 16:35:47 -08001369 i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ],
Jon Hallefbd9792015-03-05 16:11:36 -08001370 timeout=2)
Jon Hall390696c2015-05-05 17:13:41 -07001371 response = main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001372 if i == 0:
Jon Hall390696c2015-05-05 17:13:41 -07001373 response = self.stopNet()
Jon Halld61331b2015-02-17 16:35:47 -08001374 elif i == 1:
1375 return main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001376 # print "Disconnecting Mininet"
1377 if self.handle:
1378 self.handle.sendline( "exit" )
1379 self.handle.expect( "exit" )
1380 self.handle.expect( "(.*)" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001381 else:
1382 main.log.error( "Connection failed to the host" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001383 return response
1384
Hari Krishnab35c6d02015-03-18 11:13:51 -07001385 def stopNet( self, fileName = "", timeout=5):
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001386 """
Jon Hall21270ac2015-02-16 17:59:55 -08001387 Stops mininet.
Jon Hallefbd9792015-03-05 16:11:36 -08001388 Returns main.TRUE if the mininet successfully stops and
Jon Hall21270ac2015-02-16 17:59:55 -08001389 main.FALSE if the pexpect handle does not exist.
1390
Jon Halld61331b2015-02-17 16:35:47 -08001391 Will cleanup and exit the test if mininet fails to stop
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001392 """
Jon Hall21270ac2015-02-16 17:59:55 -08001393
Jon Halld61331b2015-02-17 16:35:47 -08001394 main.log.info( self.name + ": Stopping mininet..." )
adminbae64d82013-08-01 10:50:15 -07001395 response = ''
1396 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001397 try:
kelvin-onlab26bc17f2015-02-06 14:08:59 -08001398 self.handle.sendline("")
kelvin-onlab56a3f462015-02-06 14:04:43 -08001399 i = self.handle.expect( [ 'mininet>',
1400 '\$',
1401 pexpect.EOF,
1402 pexpect.TIMEOUT ],
1403 timeout )
1404 if i == 0:
1405 main.log.info( "Exiting mininet..." )
1406
Jon Hall7eb38402015-01-08 17:19:54 -08001407 response = self.execute(
1408 cmd="exit",
1409 prompt="(.*)",
1410 timeout=120 )
Jon Halld61331b2015-02-17 16:35:47 -08001411 main.log.info( self.name + ": Stopped")
Jon Hall7eb38402015-01-08 17:19:54 -08001412 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001413 response = main.TRUE
Hari Krishnab35c6d02015-03-18 11:13:51 -07001414
kelvin-onlab56a3f462015-02-06 14:04:43 -08001415 if i == 1:
1416 main.log.info( " Mininet trying to exit while not " +
1417 "in the mininet prompt" )
1418 elif i == 2:
1419 main.log.error( "Something went wrong exiting mininet" )
1420 elif i == 3: # timeout
1421 main.log.error( "Something went wrong exiting mininet " +
1422 "TIMEOUT" )
1423
Hari Krishnab35c6d02015-03-18 11:13:51 -07001424 if fileName:
1425 self.handle.sendline("")
1426 self.handle.expect('\$')
1427 self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`")
Jon Hallfbc828e2015-01-06 17:30:19 -08001428 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001429 main.log.error( self.name + ": EOF exception found" )
1430 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001431 main.cleanup()
1432 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001433 else:
1434 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001435 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001436 return response
1437
Jon Hall7eb38402015-01-08 17:19:54 -08001438 def arping( self, src, dest, destmac ):
1439 self.handle.sendline( '' )
1440 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001441
Jon Hall7eb38402015-01-08 17:19:54 -08001442 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001443 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001444 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1445 main.log.info( self.name + ": ARP successful" )
1446 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001447 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001448 except Exception:
Jon Hall7eb38402015-01-08 17:19:54 -08001449 main.log.warn( self.name + ": ARP FAILURE" )
1450 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001451 return main.FALSE
1452
Jon Hall7eb38402015-01-08 17:19:54 -08001453 def decToHex( self, num ):
1454 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001455
Jon Hall7eb38402015-01-08 17:19:54 -08001456 def getSwitchFlowCount( self, switch ):
1457 """
1458 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001459 if self.handle:
1460 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1461 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001462 response = self.execute(
1463 cmd=cmd,
1464 prompt="mininet>",
1465 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001466 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001467 main.log.error( self.name + ": EOF exception found" )
1468 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001469 main.cleanup()
1470 main.exit()
1471 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001472 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001473 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001474 main.log.info(
1475 "Couldn't find flows on switch %s, found: %s" %
1476 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001477 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001478 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001479 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001480 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001481
kelvin-onlabd3b64892015-01-20 13:26:24 -08001482 def checkFlows( self, sw, dumpFormat=None ):
1483 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001484 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001485 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001486 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001487 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001488 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001489 response = self.execute(
1490 cmd=command,
1491 prompt="mininet>",
1492 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001493 return response
1494 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001495 main.log.error( self.name + ": EOF exception found" )
1496 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001497 main.cleanup()
1498 main.exit()
admin2a9548d2014-06-17 14:08:07 -07001499
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001501 """
Jon Hallefbd9792015-03-05 16:11:36 -08001502 Runs tpdump on an interface and saves the file
Jon Hall7eb38402015-01-08 17:19:54 -08001503 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001504 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001505 self.handle.sendline( "" )
1506 self.handle.expect( "mininet>" )
1507 self.handle.sendline(
1508 "sh sudo tcpdump -n -i " +
1509 intf +
1510 " " +
1511 port +
1512 " -w " +
1513 filename.strip() +
1514 " &" )
1515 self.handle.sendline( "" )
1516 i = self.handle.expect( [ 'No\ssuch\device',
1517 'listening\son',
1518 pexpect.TIMEOUT,
1519 "mininet>" ],
1520 timeout=10 )
1521 main.log.warn( self.handle.before + self.handle.after )
1522 self.handle.sendline( "" )
1523 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001524 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001525 main.log.error(
1526 self.name +
1527 ": tcpdump - No such device exists. " +
1528 "tcpdump attempted on: " +
1529 intf )
admin2a9548d2014-06-17 14:08:07 -07001530 return main.FALSE
1531 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001532 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001533 return main.TRUE
1534 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001535 main.log.error(
1536 self.name +
1537 ": tcpdump command timed out! Check interface name," +
1538 " given interface was: " +
1539 intf )
admin2a9548d2014-06-17 14:08:07 -07001540 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001541 elif i == 3:
1542 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001543 return main.TRUE
1544 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001545 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001546 return main.FALSE
1547 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001548 main.log.error( self.name + ": EOF exception found" )
1549 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001550 main.cleanup()
1551 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001552 except Exception:
1553 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001554 main.cleanup()
1555 main.exit()
1556
kelvin-onlabd3b64892015-01-20 13:26:24 -08001557 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -08001558 """
1559 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -07001560 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001561 self.handle.sendline( "sh sudo pkill tcpdump" )
1562 self.handle.expect( "mininet>" )
1563 self.handle.sendline( "" )
1564 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001565 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001566 main.log.error( self.name + ": EOF exception found" )
1567 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001568 main.cleanup()
1569 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001570 except Exception:
1571 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001572 main.cleanup()
1573 main.exit()
1574
kelvin-onlabd3b64892015-01-20 13:26:24 -08001575 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001576 """
1577 Compare mn and onos switches
1578 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001579 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001580
Jon Hall7eb38402015-01-08 17:19:54 -08001581 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001582 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001583 output = { "switches": [] }
1584 # iterate through the MN topology and pull out switches and and port
1585 # info
1586 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001587 ports = []
1588 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001589 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001590 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001591 'name': port.name } )
1592 output[ 'switches' ].append( {
1593 "name": switch.name,
1594 "dpid": str( switch.dpid ).zfill( 16 ),
1595 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001596
Jon Hall7eb38402015-01-08 17:19:54 -08001597 # print "mn"
1598 # print json.dumps( output,
Jon Hallff6b4b22015-02-23 09:25:15 -08001599 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001600 # indent=4,
1601 # separators=( ',', ': ' ) )
1602 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001603 # print json.dumps( switchesJson,
Jon Hallff6b4b22015-02-23 09:25:15 -08001604 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001605 # indent=4,
1606 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001607
1608 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001609 mnDPIDs = []
1610 for switch in output[ 'switches' ]:
1611 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001612 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001613 # print "List of Mininet switch DPID's"
1614 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001615 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001616 main.log.error(
1617 self.name +
Jon Hallfeff3082015-05-19 10:23:26 -07001618 ".compareSwitches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001619 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001620 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001621 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001622 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001623 if switch[ 'available' ]:
1624 onosDPIDs.append(
1625 switch[ 'id' ].replace(
1626 ":",
1627 '' ).replace(
1628 "of",
1629 '' ).lower() )
1630 # else:
1631 # print "Switch is unavailable:"
1632 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001633 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001634 # print "List of ONOS switch DPID's"
1635 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001636
Jon Hall7eb38402015-01-08 17:19:54 -08001637 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001639 main.log.report( "Switches in MN but not in ONOS:" )
1640 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1641 main.log.report( str( list1 ) )
1642 main.log.report( "Switches in ONOS but not in MN:" )
1643 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001644 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001645 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001646 switchResults = main.TRUE
1647 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001648
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001650 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001651 Compare mn and onos ports
1652 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001653 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001654
Jon Hallfbc828e2015-01-06 17:30:19 -08001655 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001656 1. This uses the sts TestONTopology object
1657 2. numpy - "sudo pip install numpy"
1658
Jon Hall7eb38402015-01-08 17:19:54 -08001659 """
1660 # FIXME: this does not look for extra ports in ONOS, only checks that
1661 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001662 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001664 output = { "switches": [] }
1665 # iterate through the MN topology and pull out switches and and port
1666 # info
1667 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001668 ports = []
1669 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001670 # print port.hw_addr.toStr( separator='' )
Jon Hallefbd9792015-03-05 16:11:36 -08001671 tmpPort = { 'of_port': port.port_no,
1672 'mac': str( port.hw_addr ).replace( '\'', '' ),
1673 'name': port.name,
1674 'enabled': port.enabled }
Jon Hall39f29df2014-11-04 19:30:21 -05001675
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 ports.append( tmpPort )
Jon Hallefbd9792015-03-05 16:11:36 -08001677 tmpSwitch = { 'name': switch.name,
1678 'dpid': str( switch.dpid ).zfill( 16 ),
1679 'ports': ports }
Jon Hall39f29df2014-11-04 19:30:21 -05001680
kelvin-onlabd3b64892015-01-20 13:26:24 -08001681 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001682
Jon Hall7eb38402015-01-08 17:19:54 -08001683 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 for mnSwitch in output[ 'switches' ]:
1685 mnPorts = []
1686 onosPorts = []
1687 switchResult = main.TRUE
1688 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001689 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001690 mnPorts.append( port[ 'of_port' ] )
1691 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001692 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 # print onosSwitch
1694 if onosSwitch[ 'device' ][ 'available' ]:
1695 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001696 ':',
1697 '' ).replace(
1698 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001699 '' ) == mnSwitch[ 'dpid' ]:
1700 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001701 if port[ 'isEnabled' ]:
1702 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001703 # onosPorts.append( 'local' )
1704 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001705 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001707 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001708 mnPorts.sort( key=float )
1709 onosPorts.sort( key=float )
1710 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1711 # print "\tmn_ports[] = ", mnPorts
1712 # print "\tonos_ports[] = ", onosPorts
1713 mnPortsLog = mnPorts
1714 onosPortsLog = onosPorts
1715 mnPorts = [ x for x in mnPorts ]
1716 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001717
Jon Hall7eb38402015-01-08 17:19:54 -08001718 # TODO: handle other reserved port numbers besides LOCAL
1719 # NOTE: Reserved ports
1720 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1721 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001722 for mnPort in mnPortsLog:
1723 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001724 # don't set results to true here as this is just one of
1725 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001726 mnPorts.remove( mnPort )
1727 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001728 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001729 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001730 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 if 65534 in mnPorts:
1732 mnPorts.remove( 65534 )
1733 if long( uint64( -2 ) ) in onosPorts:
1734 onosPorts.remove( long( uint64( -2 ) ) )
1735 if len( mnPorts ): # the ports of this switch don't match
1736 switchResult = main.FALSE
1737 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1738 if len( onosPorts ): # the ports of this switch don't match
1739 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001740 main.log.warn(
1741 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001742 str( onosPorts ) )
1743 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001744 main.log.report(
1745 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1747 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1748 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1749 portsResults = portsResults and switchResult
1750 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001751
kelvin-onlabd3b64892015-01-20 13:26:24 -08001752 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001753 """
1754 Compare mn and onos links
1755 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001757
Jon Hall7eb38402015-01-08 17:19:54 -08001758 This uses the sts TestONTopology object"""
1759 # FIXME: this does not look for extra links in ONOS, only checks that
Jon Hallefbd9792015-03-05 16:11:36 -08001760 # ONOS has what is in MN
Jon Hall7eb38402015-01-08 17:19:54 -08001761 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001762 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001763 # iterate through the MN topology and pull out switches and and port
1764 # info
1765 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001766 # print "Iterating though switches as seen by Mininet"
1767 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001768 ports = []
1769 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001770 # print port.hw_addr.toStr( separator='' )
1771 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001772 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001773 'name': port.name } )
1774 output[ 'switches' ].append( {
1775 "name": switch.name,
1776 "dpid": str( switch.dpid ).zfill( 16 ),
1777 "ports": ports } )
1778 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001779
kelvin-onlabd3b64892015-01-20 13:26:24 -08001780 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001781 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001782 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001783 if 2 * len( mnLinks ) == len( onos ):
1784 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001785 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001786 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001787 main.log.report(
Jon Hall328ddca2015-01-28 15:57:15 -08001788 "Mininet has " + str( len( mnLinks ) ) +
1789 " bidirectional links and ONOS has " +
1790 str( len( onos ) ) + " unidirectional links" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001791
Jon Hall7eb38402015-01-08 17:19:54 -08001792 # iterate through MN links and check if an ONOS link exists in
1793 # both directions
1794 # NOTE: Will currently only show mn links as down if they are
1795 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001796 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001797 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001798 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001799 # print "Link: %s" % link
1800 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001801 node1 = None
1802 port1 = None
1803 node2 = None
1804 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001805 firstDir = main.FALSE
1806 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001807 for switch in output[ 'switches' ]:
1808 # print "Switch: %s" % switch[ 'name' ]
1809 if switch[ 'name' ] == link.node1.name:
1810 node1 = switch[ 'dpid' ]
1811 for port in switch[ 'ports' ]:
1812 if str( port[ 'name' ] ) == str( link.port1 ):
1813 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001814 if node1 is not None and node2 is not None:
1815 break
Jon Hall7eb38402015-01-08 17:19:54 -08001816 if switch[ 'name' ] == link.node2.name:
1817 node2 = switch[ 'dpid' ]
1818 for port in switch[ 'ports' ]:
1819 if str( port[ 'name' ] ) == str( link.port2 ):
1820 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001821 if node1 is not None and node2 is not None:
1822 break
1823
kelvin-onlabd3b64892015-01-20 13:26:24 -08001824 for onosLink in onos:
1825 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001826 ":",
1827 '' ).replace(
1828 "of",
1829 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001831 ":",
1832 '' ).replace(
1833 "of",
1834 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001835 onosPort1 = onosLink[ 'src' ][ 'port' ]
1836 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001837
Jon Hall72cf1dc2014-10-20 21:04:50 -04001838 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001839 if str( onosNode1 ) == str( node1 ) and str(
1840 onosNode2 ) == str( node2 ):
1841 if int( onosPort1 ) == int( port1 ) and int(
1842 onosPort2 ) == int( port2 ):
1843 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001844 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001845 main.log.warn(
1846 'The port numbers do not match for ' +
1847 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001848 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001849 'link %s/%s -> %s/%s' %
1850 ( node1,
1851 port1,
1852 node2,
1853 port2 ) +
1854 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001855 ( onosNode1,
1856 onosPort1,
1857 onosNode2,
1858 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001859
1860 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001861 elif ( str( onosNode1 ) == str( node2 ) and
1862 str( onosNode2 ) == str( node1 ) ):
1863 if ( int( onosPort1 ) == int( port2 )
1864 and int( onosPort2 ) == int( port1 ) ):
1865 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001866 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001867 main.log.warn(
1868 'The port numbers do not match for ' +
1869 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001870 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001871 'link %s/%s -> %s/%s' %
1872 ( node2,
1873 port2,
1874 node1,
1875 port1 ) +
1876 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001877 ( onosNode2,
1878 onosPort2,
1879 onosNode1,
1880 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001881 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001882 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001883 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001884 main.log.report(
1885 'ONOS does not have the link %s/%s -> %s/%s' %
1886 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001887 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001888 main.log.report(
1889 'ONOS does not have the link %s/%s -> %s/%s' %
1890 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001891 linkResults = linkResults and firstDir and secondDir
1892 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001893
Jon Hallff6b4b22015-02-23 09:25:15 -08001894 def compareHosts( self, topo, hostsJson ):
1895 """
1896 Compare mn and onos Hosts.
1897 Since Mininet hosts are quiet, ONOS will only know of them when they
1898 speak. For this reason, we will only check that the hosts in ONOS
1899 stores are in Mininet, and not vice versa.
1900 topo: sts TestONTopology object
1901 hostsJson: parsed json object from the onos hosts api
1902
1903 This uses the sts TestONTopology object"""
1904 import json
1905 hostResults = main.TRUE
1906 hosts = []
1907 # iterate through the MN topology and pull out hosts
1908 for mnHost in topo.graph.hosts:
1909 interfaces = []
1910 for intf in mnHost.interfaces:
1911 interfaces.append( {
1912 "name": intf.name, # str
1913 "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs
1914 # hw_addr is of type EthAddr, Not JSON serializable
1915 "hw_addr": str( intf.hw_addr ) } )
1916 hosts.append( {
1917 "name": mnHost.name, # str
1918 "interfaces": interfaces } ) # list
1919 for onosHost in hostsJson:
1920 onosMAC = onosHost[ 'mac' ].lower()
1921 match = False
1922 for mnHost in hosts:
1923 for mnIntf in mnHost[ 'interfaces' ]:
1924 if onosMAC == mnIntf[ 'hw_addr' ].lower() :
1925 match = True
1926 for ip in mnIntf[ 'ips' ]:
Jon Hallfeff3082015-05-19 10:23:26 -07001927 if ip in onosHost[ 'ipAddresses' ]:
Jon Hallff6b4b22015-02-23 09:25:15 -08001928 pass # all is well
1929 else:
1930 # misssing ip
1931 main.log.error( "ONOS host " + onosHost[ 'id' ]
1932 + " has a different IP than " +
1933 "the Mininet host." )
1934 output = json.dumps(
1935 onosHost,
1936 sort_keys=True,
1937 indent=4,
1938 separators=( ',', ': ' ) )
1939 main.log.info( output )
1940 hostResults = main.FALSE
1941 if not match:
1942 hostResults = main.FALSE
1943 main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " +
1944 "corresponding Mininet host." )
1945 output = json.dumps( onosHost,
1946 sort_keys=True,
1947 indent=4,
1948 separators=( ',', ': ' ) )
1949 main.log.info( output )
Jon Hallff6b4b22015-02-23 09:25:15 -08001950 return hostResults
1951
kelvin-onlabd3b64892015-01-20 13:26:24 -08001952 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001953 """
1954 Returns a list of all hosts
1955 Don't ask questions just use it"""
1956 self.handle.sendline( "" )
1957 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001958
Jon Hall7eb38402015-01-08 17:19:54 -08001959 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1960 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001961
kelvin-onlabd3b64892015-01-20 13:26:24 -08001962 handlePy = self.handle.before
1963 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1964 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001965
Jon Hall7eb38402015-01-08 17:19:54 -08001966 self.handle.sendline( "" )
1967 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001968
kelvin-onlabd3b64892015-01-20 13:26:24 -08001969 hostStr = handlePy.replace( "]", "" )
1970 hostStr = hostStr.replace( "'", "" )
1971 hostStr = hostStr.replace( "[", "" )
1972 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001973
kelvin-onlabd3b64892015-01-20 13:26:24 -08001974 return hostList
adminbae64d82013-08-01 10:50:15 -07001975
Jon Hall7eb38402015-01-08 17:19:54 -08001976 def update( self ):
1977 """
1978 updates the port address and status information for
1979 each port in mn"""
1980 # TODO: Add error checking. currently the mininet command has no output
Jon Hallefbd9792015-03-05 16:11:36 -08001981 main.log.info( "Updating MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001982 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001983 self.handle.sendline( "" )
1984 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001985
Jon Hall7eb38402015-01-08 17:19:54 -08001986 self.handle.sendline( "update" )
1987 self.handle.expect( "update" )
1988 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001989
Jon Hall7eb38402015-01-08 17:19:54 -08001990 self.handle.sendline( "" )
1991 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001992
Jon Hallb1290e82014-11-18 16:17:48 -05001993 return main.TRUE
1994 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001995 main.log.error( self.name + ": EOF exception found" )
1996 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001997 main.cleanup()
1998 main.exit()
1999
adminbae64d82013-08-01 10:50:15 -07002000if __name__ != "__main__":
2001 import sys
kelvin-onlab50907142015-04-01 13:37:45 -07002002 sys.modules[ __name__ ] = MininetCliDriver()