blob: 290d9bca9e943e7c55c5527c243536c1cfbf4c1f [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'
377 - 'udp'
378
379 Acceptable hostList:
380 - ['h1','h2','h3','h4']
381
382 Returns main.TRUE if all hosts specified can reach
383 each other
384
385 Returns main.FALSE if one or more of hosts specified
386 cannot reach each other"""
387
388 if pingType == "ipv4":
389 cmd = " ping -c 1 -i 1 -W 8 "
390 elif pingType == "ipv6":
391 cmd = " ping6 -c 1 -i 1 -W 8 "
392 elif pingType == "udp":
393 cmd = " ping -u -c 1 -i 1 -W 8 "
394 else:
395 main.log.warn( "Invalid pingType specified" )
396 return
397
398 try:
399 main.log.info( "Testing reachability between specified hosts" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400400
401 isReachable = main.TRUE
402
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400403 for host in hostList:
404 listIndex = hostList.index(host)
405 # List of hosts to ping other than itself
406 pingList = hostList[:listIndex] + hostList[(listIndex+1):]
407
408 for temp in pingList:
409 # Current host pings all other hosts specified
410 pingCmd = str(host) + cmd + str(temp)
411 self.handle.sendline( pingCmd )
412 i = self.handle.expect( [ pingCmd, pexpect.TIMEOUT ] )
413 j = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
414 response = self.handle.before
415 if re.search( ',\s0\%\spacket\sloss', response ):
416 main.log.info( str(host) + " -> " + str(temp) )
417 else:
418 main.log.info( str(host) + " -> X ("+str(temp)+") "
419 " Destination Unreachable" )
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400420 # One of the host to host pair is unreachable
421 isReachable = main.FALSE
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400422
andrew@onlab.usdefe38c2015-05-14 19:18:18 -0400423 return isReachable
andrew@onlab.us9fdee812015-05-14 17:23:26 -0400424
425 except pexpect.EOF:
426 main.log.error( self.name + ": EOF exception found" )
427 main.log.error( self.name + ": " + self.handle.before )
428 main.cleanup()
429 main.exit()
430
Jon Hall7eb38402015-01-08 17:19:54 -0800431 def pingHost( self, **pingParams ):
432 """
433 Ping from one mininet host to another
434 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800435 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800436 command = args[ "SRC" ] + " ping " + \
437 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700438 try:
Jon Hall61282e32015-03-19 11:34:11 -0700439 main.log.info( "Sending: " + command )
Jon Hall7eb38402015-01-08 17:19:54 -0800440 self.handle.sendline( command )
441 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700442 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800443 main.log.error(
444 self.name +
445 ": timeout when waiting for response from mininet" )
446 main.log.error( "response: " + str( self.handle.before ) )
447 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700448 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800449 main.log.error(
450 self.name +
451 ": timeout when waiting for response from mininet" )
452 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700453 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800454 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800455 main.log.error( self.name + ": EOF exception found" )
456 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700457 main.cleanup()
458 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800459 main.log.info( self.name + ": Ping Response: " + response )
460 if re.search( ',\s0\%\spacket\sloss', response ):
461 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800462 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700463 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800464 else:
465 main.log.error(
466 self.name +
467 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800468 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700469 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800470
Jon Hall7eb38402015-01-08 17:19:54 -0800471 def checkIP( self, host ):
472 """
473 Verifies the host's ip configured or not."""
474 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700475 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800476 response = self.execute(
477 cmd=host +
478 " ifconfig",
479 prompt="mininet>",
480 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800481 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800482 main.log.error( self.name + ": EOF exception found" )
483 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700484 main.cleanup()
485 main.exit()
adminbae64d82013-08-01 10:50:15 -0700486
Jon Hall7eb38402015-01-08 17:19:54 -0800487 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800488 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
489 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
490 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
491 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
492 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800493 # pattern = "inet addr:10.0.0.6"
494 if re.search( pattern, response ):
495 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700496 return main.TRUE
497 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800498 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700499 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800500 else:
501 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800502
Jon Hall7eb38402015-01-08 17:19:54 -0800503 def verifySSH( self, **connectargs ):
Jon Hallefbd9792015-03-05 16:11:36 -0800504 # FIXME: Who uses this and what is the purpose? seems very specific
Jon Hall6094a362014-04-11 14:46:56 -0700505 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800506 response = self.execute(
507 cmd="h1 /usr/sbin/sshd -D&",
508 prompt="mininet>",
509 timeout=10 )
510 response = self.execute(
511 cmd="h4 /usr/sbin/sshd -D&",
512 prompt="mininet>",
513 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700514 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800515 vars( self )[ key ] = connectargs[ key ]
516 response = self.execute(
517 cmd="xterm h1 h4 ",
518 prompt="mininet>",
519 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800520 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800521 main.log.error( self.name + ": EOF exception found" )
522 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700523 main.cleanup()
524 main.exit()
adminbae64d82013-08-01 10:50:15 -0700525 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800526 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700527 if self.flag == 0:
528 self.flag = 1
529 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800530 else:
adminbae64d82013-08-01 10:50:15 -0700531 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800532
kelvin-onlaba1484582015-02-02 15:46:20 -0800533 def moveHost( self, host, oldSw, newSw, ):
534 """
535 Moves a host from one switch to another on the fly
536 Note: The intf between host and oldSw when detached
537 using detach(), will still show up in the 'net'
538 cmd, because switch.detach() doesn't affect switch.intfs[]
539 (which is correct behavior since the interfaces
540 haven't moved).
541 """
542 if self.handle:
543 try:
544 # Bring link between oldSw-host down
Jon Hallefbd9792015-03-05 16:11:36 -0800545 cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\
546 "'," + "'down')"
kelvin-onlaba1484582015-02-02 15:46:20 -0800547 print "cmd1= ", cmd
Jon Hallefbd9792015-03-05 16:11:36 -0800548 response = self.execute( cmd=cmd,
549 prompt="mininet>",
550 timeout=10 )
kelvin-onlaba1484582015-02-02 15:46:20 -0800551
552 # Determine hostintf and Oldswitchintf
553 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800554 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800555 print "cmd2= ", cmd
556 self.handle.sendline( cmd )
557 self.handle.expect( "mininet>" )
558
shahshreya73537862015-02-11 15:15:24 -0800559 # Determine ip and mac address of the host-oldSw interface
kelvin-onlaba1484582015-02-02 15:46:20 -0800560 cmd = "px ipaddr = hintf.IP()"
561 print "cmd3= ", cmd
562 self.handle.sendline( cmd )
563 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800564
565 cmd = "px macaddr = hintf.MAC()"
566 print "cmd3= ", cmd
567 self.handle.sendline( cmd )
568 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800569
570 # Detach interface between oldSw-host
571 cmd = "px " + oldSw + ".detach( sintf )"
572 print "cmd4= ", cmd
573 self.handle.sendline( cmd )
574 self.handle.expect( "mininet>" )
575
576 # Add link between host-newSw
577 cmd = "py net.addLink(" + host + "," + newSw + ")"
578 print "cmd5= ", cmd
579 self.handle.sendline( cmd )
580 self.handle.expect( "mininet>" )
581
582 # Determine hostintf and Newswitchintf
583 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
Jon Hallefbd9792015-03-05 16:11:36 -0800584 ")[0]"
kelvin-onlaba1484582015-02-02 15:46:20 -0800585 print "cmd6= ", cmd
586 self.handle.sendline( cmd )
587 self.handle.expect( "mininet>" )
588
589 # Attach interface between newSw-host
590 cmd = "px " + newSw + ".attach( sintf )"
591 print "cmd3= ", cmd
592 self.handle.sendline( cmd )
593 self.handle.expect( "mininet>" )
594
595 # Set ipaddress of the host-newSw interface
596 cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)"
597 print "cmd7 = ", cmd
598 self.handle.sendline( cmd )
599 self.handle.expect( "mininet>" )
shahshreya73537862015-02-11 15:15:24 -0800600
601 # Set macaddress of the host-newSw interface
602 cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)"
603 print "cmd8 = ", cmd
604 self.handle.sendline( cmd )
605 self.handle.expect( "mininet>" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800606
607 cmd = "net"
shahshreya73537862015-02-11 15:15:24 -0800608 print "cmd9 = ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800609 self.handle.sendline( cmd )
610 self.handle.expect( "mininet>" )
611 print "output = ", self.handle.before
612
613 # Determine ipaddress of the host-newSw interface
shahshreya73537862015-02-11 15:15:24 -0800614 cmd = host + " ifconfig"
615 print "cmd10= ", cmd
kelvin-onlaba1484582015-02-02 15:46:20 -0800616 self.handle.sendline( cmd )
617 self.handle.expect( "mininet>" )
618 print "ifconfig o/p = ", self.handle.before
619
620 return main.TRUE
621 except pexpect.EOF:
622 main.log.error( self.name + ": EOF exception found" )
623 main.log.error( self.name + ": " + self.handle.before )
624 return main.FALSE
625
Jon Hall7eb38402015-01-08 17:19:54 -0800626 def changeIP( self, host, intf, newIP, newNetmask ):
627 """
628 Changes the ip address of a host on the fly
629 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800630 if self.handle:
631 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800632 cmd = host + " ifconfig " + intf + " " + \
633 newIP + " " + 'netmask' + " " + newNetmask
634 self.handle.sendline( cmd )
635 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800636 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800637 main.log.info( "response = " + response )
638 main.log.info(
639 "Ip of host " +
640 host +
641 " changed to new IP " +
642 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800643 return main.TRUE
644 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800645 main.log.error( self.name + ": EOF exception found" )
646 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800647 return main.FALSE
648
Jon Hall7eb38402015-01-08 17:19:54 -0800649 def changeDefaultGateway( self, host, newGW ):
650 """
651 Changes the default gateway of a host
652 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800653 if self.handle:
654 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800655 cmd = host + " route add default gw " + newGW
656 self.handle.sendline( cmd )
657 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800658 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800659 main.log.info( "response = " + response )
660 main.log.info(
661 "Default gateway of host " +
662 host +
663 " changed to " +
664 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800665 return main.TRUE
666 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800667 main.log.error( self.name + ": EOF exception found" )
668 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800669 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800670
Jon Hall7eb38402015-01-08 17:19:54 -0800671 def addStaticMACAddress( self, host, GW, macaddr ):
672 """
Jon Hallefbd9792015-03-05 16:11:36 -0800673 Changes the mac address of a gateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800674 if self.handle:
675 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800676 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
677 cmd = host + " arp -s " + GW + " " + macaddr
678 self.handle.sendline( cmd )
679 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800680 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800681 main.log.info( "response = " + response )
682 main.log.info(
Jon Hallefbd9792015-03-05 16:11:36 -0800683 "Mac address of gateway " +
Jon Hall7eb38402015-01-08 17:19:54 -0800684 GW +
685 " changed to " +
686 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800687 return main.TRUE
688 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800689 main.log.error( self.name + ": EOF exception found" )
690 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800691 return main.FALSE
692
Jon Hall7eb38402015-01-08 17:19:54 -0800693 def verifyStaticGWandMAC( self, host ):
694 """
695 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800696 if self.handle:
697 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800698 # h1 arp -an
699 cmd = host + " arp -an "
700 self.handle.sendline( cmd )
701 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800702 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800703 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800704 return main.TRUE
705 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800706 main.log.error( self.name + ": EOF exception found" )
707 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800708 return main.FALSE
709
Jon Hall7eb38402015-01-08 17:19:54 -0800710 def getMacAddress( self, host ):
711 """
712 Verifies the host's ip configured or not."""
713 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700714 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800715 response = self.execute(
716 cmd=host +
717 " ifconfig",
718 prompt="mininet>",
719 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800720 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800721 main.log.error( self.name + ": EOF exception found" )
722 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700723 main.cleanup()
724 main.exit()
adminbae64d82013-08-01 10:50:15 -0700725
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700726 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800727 macAddressSearch = re.search( pattern, response, re.I )
728 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800729 main.log.info(
730 self.name +
731 ": Mac-Address of Host " +
732 host +
733 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800734 macAddress )
735 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700736 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800737 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700738
Jon Hall7eb38402015-01-08 17:19:54 -0800739 def getInterfaceMACAddress( self, host, interface ):
740 """
741 Return the IP address of the interface on the given host"""
742 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700743 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800744 response = self.execute( cmd=host + " ifconfig " + interface,
745 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800746 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800747 main.log.error( self.name + ": EOF exception found" )
748 main.log.error( self.name + ": " + self.handle.before )
749 main.cleanup()
750 main.exit()
751
752 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800753 macAddressSearch = re.search( pattern, response, re.I )
754 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800755 main.log.info( "No mac address found in %s" % response )
756 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800757 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800758 main.log.info(
759 "Mac-Address of " +
760 host +
761 ":" +
762 interface +
763 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800764 macAddress )
765 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800766 else:
767 main.log.error( "Connection failed to the host" )
768
769 def getIPAddress( self, host ):
770 """
771 Verifies the host's ip configured or not."""
772 if self.handle:
773 try:
774 response = self.execute(
775 cmd=host +
776 " ifconfig",
777 prompt="mininet>",
778 timeout=10 )
779 except pexpect.EOF:
780 main.log.error( self.name + ": EOF exception found" )
781 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700782 main.cleanup()
783 main.exit()
adminbae64d82013-08-01 10:50:15 -0700784
785 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800786 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800787 main.log.info(
788 self.name +
789 ": IP-Address of Host " +
790 host +
791 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800792 ipAddressSearch.group( 1 ) )
793 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800794 else:
795 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800796
Jon Hall7eb38402015-01-08 17:19:54 -0800797 def getSwitchDPID( self, switch ):
798 """
799 return the datapath ID of the switch"""
800 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700801 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700802 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800803 response = self.execute(
804 cmd=cmd,
805 prompt="mininet>",
806 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800807 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800808 main.log.error( self.name + ": EOF exception found" )
809 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700810 main.cleanup()
811 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800812 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800813 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700814 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800815 main.log.info(
816 "Couldn't find DPID for switch %s, found: %s" %
817 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700818 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800819 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700820 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800821 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700822
Jon Hall7eb38402015-01-08 17:19:54 -0800823 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700824 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800825 self.handle.sendline( "" )
826 self.expect( "mininet>" )
827 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700828 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800829 response = self.execute(
830 cmd=cmd,
831 prompt="mininet>",
832 timeout=10 )
833 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700834 response = self.handle.before
835 return response
836 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800837 main.log.error( self.name + ": EOF exception found" )
838 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700839 main.cleanup()
840 main.exit()
841
Jon Hall7eb38402015-01-08 17:19:54 -0800842 def getInterfaces( self, node ):
843 """
844 return information dict about interfaces connected to the node"""
845 if self.handle:
846 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800847 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700848 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700849 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800850 response = self.execute(
851 cmd=cmd,
852 prompt="mininet>",
853 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800854 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800855 main.log.error( self.name + ": EOF exception found" )
856 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700857 main.cleanup()
858 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700859 return response
860 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800861 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700862
Jon Hall7eb38402015-01-08 17:19:54 -0800863 def dump( self ):
864 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700865 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800866 response = self.execute(
867 cmd='dump',
868 prompt='mininet>',
869 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800870 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800871 main.log.error( self.name + ": EOF exception found" )
872 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700873 main.cleanup()
874 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700875 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800876
Jon Hall7eb38402015-01-08 17:19:54 -0800877 def intfs( self ):
878 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700879 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800880 response = self.execute(
881 cmd='intfs',
882 prompt='mininet>',
883 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800884 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800885 main.log.error( self.name + ": EOF exception found" )
886 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700887 main.cleanup()
888 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700889 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800890
Jon Hall7eb38402015-01-08 17:19:54 -0800891 def net( self ):
892 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700893 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800894 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800895 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800896 main.log.error( self.name + ": EOF exception found" )
897 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700898 main.cleanup()
899 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700900 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800901
902 def iperf( self, host1, host2 ):
903 main.log.info(
904 self.name +
905 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700906 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800907 cmd1 = 'iperf ' + host1 + " " + host2
908 self.handle.sendline( cmd1 )
909 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800910 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800911 if re.search( 'Results:', response ):
Jon Hallefbd9792015-03-05 16:11:36 -0800912 main.log.info( self.name + ": iperf test successful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800913 return main.TRUE
914 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800915 main.log.error( self.name + ": iperf test failed" )
916 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800917 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800918 main.log.error( self.name + ": EOF exception found" )
919 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800920 main.cleanup()
921 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800922
Jon Hall7eb38402015-01-08 17:19:54 -0800923 def iperfudp( self ):
924 main.log.info(
925 self.name +
926 ": Simple iperf TCP test between two " +
927 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700928 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800929 response = self.execute(
930 cmd='iperfudp',
931 prompt='mininet>',
932 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800933 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800934 main.log.error( self.name + ": EOF exception found" )
935 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700936 main.cleanup()
937 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700938 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800939
Jon Hall7eb38402015-01-08 17:19:54 -0800940 def nodes( self ):
941 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700942 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800943 response = self.execute(
944 cmd='nodes',
945 prompt='mininet>',
946 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800947 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800948 main.log.error( self.name + ": EOF exception found" )
949 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700950 main.cleanup()
951 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700952 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800953
Jon Hall7eb38402015-01-08 17:19:54 -0800954 def pingpair( self ):
955 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700956 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800957 response = self.execute(
958 cmd='pingpair',
959 prompt='mininet>',
960 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800961 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800962 main.log.error( self.name + ": EOF exception found" )
963 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700964 main.cleanup()
965 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800966
Jon Hall7eb38402015-01-08 17:19:54 -0800967 if re.search( ',\s0\%\spacket\sloss', response ):
968 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800969 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700970 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800971 else:
972 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800973 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700974 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800975
Jon Hall7eb38402015-01-08 17:19:54 -0800976 def link( self, **linkargs ):
977 """
978 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800979 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800980 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
981 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
982 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
983 main.log.info(
984 "Bring link between '" +
985 end1 +
986 "' and '" +
987 end2 +
988 "' '" +
989 option +
990 "'" )
991 command = "link " + \
992 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700993 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800994 self.handle.sendline( command )
995 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800996 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800997 main.log.error( self.name + ": EOF exception found" )
998 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700999 main.cleanup()
1000 main.exit()
adminbae64d82013-08-01 10:50:15 -07001001 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001002
Jon Hall7eb38402015-01-08 17:19:54 -08001003 def yank( self, **yankargs ):
1004 """
1005 yank a mininet switch interface to a host"""
1006 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001007 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001008 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1009 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1010 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001011 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001012 response = self.execute(
1013 cmd=command,
1014 prompt="mininet>",
1015 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001016 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001017 main.log.error( self.name + ": EOF exception found" )
1018 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001019 main.cleanup()
1020 main.exit()
adminaeedddd2013-08-02 15:14:15 -07001021 return main.TRUE
1022
Jon Hall7eb38402015-01-08 17:19:54 -08001023 def plug( self, **plugargs ):
1024 """
1025 plug the yanked mininet switch interface to a switch"""
1026 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001027 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001028 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1029 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
1030 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -07001031 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001032 response = self.execute(
1033 cmd=command,
1034 prompt="mininet>",
1035 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001036 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001037 main.log.error( self.name + ": EOF exception found" )
1038 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001039 main.cleanup()
1040 main.exit()
adminbae64d82013-08-01 10:50:15 -07001041 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001042
Jon Hall7eb38402015-01-08 17:19:54 -08001043 def dpctl( self, **dpctlargs ):
1044 """
1045 Run dpctl command on all switches."""
1046 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001047 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001048 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
1049 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
1050 command = "dpctl " + cmd + " " + str( cmdargs )
1051 try:
1052 response = self.execute(
1053 cmd=command,
1054 prompt="mininet>",
1055 timeout=10 )
1056 except pexpect.EOF:
1057 main.log.error( self.name + ": EOF exception found" )
1058 main.log.error( self.name + ": " + self.handle.before )
1059 main.cleanup()
1060 main.exit()
1061 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001062
kelvin-onlabd3b64892015-01-20 13:26:24 -08001063 def getVersion( self ):
Jon Hallff6b4b22015-02-23 09:25:15 -08001064 #FIXME: What uses this? This should be refactored to get
1065 # version from MN and not some other file
kelvin-onlabd3b64892015-01-20 13:26:24 -08001066 fileInput = path + '/lib/Mininet/INSTALL'
1067 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -07001068 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -08001069 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -08001070 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -07001071 if result:
Jon Hall7eb38402015-01-08 17:19:54 -08001072 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -05001073 return version
adminbae64d82013-08-01 10:50:15 -07001074
kelvin-onlabd3b64892015-01-20 13:26:24 -08001075 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001076 """
Jon Hallec3c21e2014-11-10 22:22:37 -05001077 Parameters:
1078 sw: The name of an OVS switch. Example "s1"
1079 Return:
Jon Hall7eb38402015-01-08 17:19:54 -08001080 The output of the command from the mininet cli
1081 or main.FALSE on timeout"""
1082 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001083 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001084 response = self.execute(
1085 cmd=command,
1086 prompt="mininet>",
1087 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001088 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -05001089 return response
admin2a9548d2014-06-17 14:08:07 -07001090 else:
1091 return main.FALSE
1092 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001093 main.log.error( self.name + ": EOF exception found" )
1094 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001095 main.cleanup()
1096 main.exit()
adminbae64d82013-08-01 10:50:15 -07001097
kelvin-onlabd3b64892015-01-20 13:26:24 -08001098 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001099 """
1100 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001101 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -08001102 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -07001103
1104 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -08001105 for j in range( count ):
1106 argstring = argstring + ",IP" + \
1107 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001108 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -07001109
Jon Hall7eb38402015-01-08 17:19:54 -08001110 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
1111 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001112 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -08001113 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -08001114
Jon Hall7eb38402015-01-08 17:19:54 -08001115 command = "sh ovs-vsctl set-controller s" + \
1116 str( sw ) + " " + ptcpB + " "
1117 for j in range( count ):
1118 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -08001119 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -08001120 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
1121 ip = args[
1122 "IP" +
1123 str( i ) ] if args[
1124 "IP" +
1125 str( i ) ] is not None else ""
1126 port = args[
1127 "PORT" +
1128 str( i ) ] if args[
1129 "PORT" +
1130 str( i ) ] is not None else ""
1131 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -08001132 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -07001133 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -07001134 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001135 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -07001136 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001137 main.log.error( self.name + ": EOF exception found" )
1138 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001139 main.cleanup()
1140 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001141 except Exception:
1142 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall6094a362014-04-11 14:46:56 -07001143 main.cleanup()
1144 main.exit()
adminbae64d82013-08-01 10:50:15 -07001145
kelvin-onlabd3b64892015-01-20 13:26:24 -08001146 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001147 """
1148 Removes the controller target from sw"""
1149 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -07001150 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001151 response = self.execute(
1152 cmd=command,
1153 prompt="mininet>",
1154 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -08001155 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001156 main.log.error( self.name + ": EOF exception found" )
1157 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -07001158 main.cleanup()
1159 main.exit()
1160 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001161 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -07001162
kelvin-onlabd3b64892015-01-20 13:26:24 -08001163 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001164 """
Jon Hallb1290e82014-11-18 16:17:48 -05001165 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001166 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001167 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001168 NOTE: cannot currently specify what type of switch
1169 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001170 sw = name of the new switch as a string
1171 optional keywords:
Jon Hallb1290e82014-11-18 16:17:48 -05001172 dpid = "dpid"
Jon Hallefbd9792015-03-05 16:11:36 -08001173 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001174 """
1175 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001176 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -05001177 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001178 response = self.execute(
1179 cmd=command,
1180 prompt="mininet>",
1181 timeout=10 )
1182 if re.search( "already exists!", 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( "Error", response ):
1186 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001187 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001188 elif re.search( "usage:", response ):
1189 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001190 return main.FALSE
1191 else:
1192 return main.TRUE
1193 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001194 main.log.error( self.name + ": EOF exception found" )
1195 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001196 main.cleanup()
1197 main.exit()
1198
kelvin-onlabd3b64892015-01-20 13:26:24 -08001199 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -08001200 """
Jon Hallbe6dfc42015-01-12 17:37:25 -08001201 delete a switch from the mininet topology
1202 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001203 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -08001204 required params:
Jon Hallefbd9792015-03-05 16:11:36 -08001205 sw = name of the switch as a string
1206 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001207 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -05001208 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001209 response = self.execute(
1210 cmd=command,
1211 prompt="mininet>",
1212 timeout=10 )
1213 if re.search( "no switch named", 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( "Error", response ):
1217 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001218 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001219 elif re.search( "usage:", response ):
1220 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001221 return main.FALSE
1222 else:
1223 return main.TRUE
1224 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001225 main.log.error( self.name + ": EOF exception found" )
1226 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001227 main.cleanup()
1228 main.exit()
1229
kelvin-onlabd3b64892015-01-20 13:26:24 -08001230 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001231 """
1232 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001233 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001234 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001235 NOTE: cannot currently specify what type of link
1236 required params:
1237 node1 = the string node name of the first endpoint of the link
1238 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001239 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001240 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001241 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001242 response = self.execute(
1243 cmd=command,
1244 prompt="mininet>",
1245 timeout=10 )
1246 if re.search( "doesnt exist!", 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( "Error", response ):
1250 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001251 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001252 elif re.search( "usage:", response ):
1253 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001254 return main.FALSE
1255 else:
1256 return main.TRUE
1257 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001258 main.log.error( self.name + ": EOF exception found" )
1259 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001260 main.cleanup()
1261 main.exit()
1262
kelvin-onlabd3b64892015-01-20 13:26:24 -08001263 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001264 """
1265 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001266 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001267 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001268 required params:
1269 node1 = the string node name of the first endpoint of the link
1270 node2 = the string node name of the second endpoint of the link
Jon Hallefbd9792015-03-05 16:11:36 -08001271 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001272 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001273 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001274 response = self.execute(
1275 cmd=command,
1276 prompt="mininet>",
1277 timeout=10 )
1278 if re.search( "no node named", 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( "Error", response ):
1282 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001283 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001284 elif re.search( "usage:", response ):
1285 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001286 return main.FALSE
1287 else:
1288 return main.TRUE
1289 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001290 main.log.error( self.name + ": EOF exception found" )
1291 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001292 main.cleanup()
1293 main.exit()
1294
kelvin-onlabd3b64892015-01-20 13:26:24 -08001295 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001296 """
Jon Hallb1290e82014-11-18 16:17:48 -05001297 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001298 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001299 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001300 NOTE: cannot currently specify what type of host
1301 required params:
1302 hostname = the string hostname
1303 optional key-value params
1304 switch = "switch name"
Jon Hallefbd9792015-03-05 16:11:36 -08001305 returns: main.FALSE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001306 """
1307 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001308 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001309 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001310 response = self.execute(
1311 cmd=command,
1312 prompt="mininet>",
1313 timeout=10 )
1314 if re.search( "already 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( "doesnt exists!", 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( "Error", response ):
1321 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001322 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001323 elif re.search( "usage:", response ):
1324 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001325 return main.FALSE
1326 else:
1327 return main.TRUE
1328 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001329 main.log.error( self.name + ": EOF exception found" )
1330 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001331 main.cleanup()
1332 main.exit()
1333
kelvin-onlabd3b64892015-01-20 13:26:24 -08001334 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001335 """
1336 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001337 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001338 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001339 NOTE: this uses a custom mn function
1340 required params:
1341 hostname = the string hostname
Jon Hallefbd9792015-03-05 16:11:36 -08001342 returns: main.FALSE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001343 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001344 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001345 response = self.execute(
1346 cmd=command,
1347 prompt="mininet>",
1348 timeout=10 )
1349 if re.search( "no host named", 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( "Error", response ):
1353 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001354 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001355 elif re.search( "usage:", response ):
1356 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001357 return main.FALSE
1358 else:
1359 return main.TRUE
1360 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001361 main.log.error( self.name + ": EOF exception found" )
1362 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001363 main.cleanup()
1364 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001365
Jon Hall7eb38402015-01-08 17:19:54 -08001366 def disconnect( self ):
kelvin-onlaba1484582015-02-02 15:46:20 -08001367 """
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001368 Called at the end of the test to stop the mininet and
1369 disconnect the handle.
kelvin-onlaba1484582015-02-02 15:46:20 -08001370 """
1371 self.handle.sendline('')
Jon Halld61331b2015-02-17 16:35:47 -08001372 i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ],
Jon Hallefbd9792015-03-05 16:11:36 -08001373 timeout=2)
Jon Hall390696c2015-05-05 17:13:41 -07001374 response = main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001375 if i == 0:
Jon Hall390696c2015-05-05 17:13:41 -07001376 response = self.stopNet()
Jon Halld61331b2015-02-17 16:35:47 -08001377 elif i == 1:
1378 return main.TRUE
kelvin-onlaba1484582015-02-02 15:46:20 -08001379 # print "Disconnecting Mininet"
1380 if self.handle:
1381 self.handle.sendline( "exit" )
1382 self.handle.expect( "exit" )
1383 self.handle.expect( "(.*)" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001384 else:
1385 main.log.error( "Connection failed to the host" )
kelvin-onlaba1484582015-02-02 15:46:20 -08001386 return response
1387
Hari Krishnab35c6d02015-03-18 11:13:51 -07001388 def stopNet( self, fileName = "", timeout=5):
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001389 """
Jon Hall21270ac2015-02-16 17:59:55 -08001390 Stops mininet.
Jon Hallefbd9792015-03-05 16:11:36 -08001391 Returns main.TRUE if the mininet successfully stops and
Jon Hall21270ac2015-02-16 17:59:55 -08001392 main.FALSE if the pexpect handle does not exist.
1393
Jon Halld61331b2015-02-17 16:35:47 -08001394 Will cleanup and exit the test if mininet fails to stop
kelvin-onlab00ac67b2015-02-04 09:52:02 -08001395 """
Jon Hall21270ac2015-02-16 17:59:55 -08001396
Jon Halld61331b2015-02-17 16:35:47 -08001397 main.log.info( self.name + ": Stopping mininet..." )
adminbae64d82013-08-01 10:50:15 -07001398 response = ''
1399 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001400 try:
kelvin-onlab26bc17f2015-02-06 14:08:59 -08001401 self.handle.sendline("")
kelvin-onlab56a3f462015-02-06 14:04:43 -08001402 i = self.handle.expect( [ 'mininet>',
1403 '\$',
1404 pexpect.EOF,
1405 pexpect.TIMEOUT ],
1406 timeout )
1407 if i == 0:
1408 main.log.info( "Exiting mininet..." )
1409
Jon Hall7eb38402015-01-08 17:19:54 -08001410 response = self.execute(
1411 cmd="exit",
1412 prompt="(.*)",
1413 timeout=120 )
Jon Halld61331b2015-02-17 16:35:47 -08001414 main.log.info( self.name + ": Stopped")
Jon Hall7eb38402015-01-08 17:19:54 -08001415 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001416 response = main.TRUE
Hari Krishnab35c6d02015-03-18 11:13:51 -07001417
kelvin-onlab56a3f462015-02-06 14:04:43 -08001418 if i == 1:
1419 main.log.info( " Mininet trying to exit while not " +
1420 "in the mininet prompt" )
1421 elif i == 2:
1422 main.log.error( "Something went wrong exiting mininet" )
1423 elif i == 3: # timeout
1424 main.log.error( "Something went wrong exiting mininet " +
1425 "TIMEOUT" )
1426
Hari Krishnab35c6d02015-03-18 11:13:51 -07001427 if fileName:
1428 self.handle.sendline("")
1429 self.handle.expect('\$')
1430 self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`")
Jon Hallfbc828e2015-01-06 17:30:19 -08001431 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001432 main.log.error( self.name + ": EOF exception found" )
1433 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001434 main.cleanup()
1435 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001436 else:
1437 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001438 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001439 return response
1440
Jon Hall7eb38402015-01-08 17:19:54 -08001441 def arping( self, src, dest, destmac ):
1442 self.handle.sendline( '' )
1443 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001444
Jon Hall7eb38402015-01-08 17:19:54 -08001445 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001446 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001447 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1448 main.log.info( self.name + ": ARP successful" )
1449 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001450 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001451 except Exception:
Jon Hall7eb38402015-01-08 17:19:54 -08001452 main.log.warn( self.name + ": ARP FAILURE" )
1453 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001454 return main.FALSE
1455
Jon Hall7eb38402015-01-08 17:19:54 -08001456 def decToHex( self, num ):
1457 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001458
Jon Hall7eb38402015-01-08 17:19:54 -08001459 def getSwitchFlowCount( self, switch ):
1460 """
1461 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001462 if self.handle:
1463 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1464 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001465 response = self.execute(
1466 cmd=cmd,
1467 prompt="mininet>",
1468 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001469 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001470 main.log.error( self.name + ": EOF exception found" )
1471 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001472 main.cleanup()
1473 main.exit()
1474 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001475 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001476 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001477 main.log.info(
1478 "Couldn't find flows on switch %s, found: %s" %
1479 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001480 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001481 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001482 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001483 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001484
kelvin-onlabd3b64892015-01-20 13:26:24 -08001485 def checkFlows( self, sw, dumpFormat=None ):
1486 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001487 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001489 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001490 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001491 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001492 response = self.execute(
1493 cmd=command,
1494 prompt="mininet>",
1495 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001496 return response
1497 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001498 main.log.error( self.name + ": EOF exception found" )
1499 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001500 main.cleanup()
1501 main.exit()
admin2a9548d2014-06-17 14:08:07 -07001502
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001504 """
Jon Hallefbd9792015-03-05 16:11:36 -08001505 Runs tpdump on an interface and saves the file
Jon Hall7eb38402015-01-08 17:19:54 -08001506 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001507 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001508 self.handle.sendline( "" )
1509 self.handle.expect( "mininet>" )
1510 self.handle.sendline(
1511 "sh sudo tcpdump -n -i " +
1512 intf +
1513 " " +
1514 port +
1515 " -w " +
1516 filename.strip() +
1517 " &" )
1518 self.handle.sendline( "" )
1519 i = self.handle.expect( [ 'No\ssuch\device',
1520 'listening\son',
1521 pexpect.TIMEOUT,
1522 "mininet>" ],
1523 timeout=10 )
1524 main.log.warn( self.handle.before + self.handle.after )
1525 self.handle.sendline( "" )
1526 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001527 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001528 main.log.error(
1529 self.name +
1530 ": tcpdump - No such device exists. " +
1531 "tcpdump attempted on: " +
1532 intf )
admin2a9548d2014-06-17 14:08:07 -07001533 return main.FALSE
1534 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001535 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001536 return main.TRUE
1537 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001538 main.log.error(
1539 self.name +
1540 ": tcpdump command timed out! Check interface name," +
1541 " given interface was: " +
1542 intf )
admin2a9548d2014-06-17 14:08:07 -07001543 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001544 elif i == 3:
1545 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001546 return main.TRUE
1547 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001548 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001549 return main.FALSE
1550 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001551 main.log.error( self.name + ": EOF exception found" )
1552 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001553 main.cleanup()
1554 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001555 except Exception:
1556 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001557 main.cleanup()
1558 main.exit()
1559
kelvin-onlabd3b64892015-01-20 13:26:24 -08001560 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -08001561 """
1562 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -07001563 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001564 self.handle.sendline( "sh sudo pkill tcpdump" )
1565 self.handle.expect( "mininet>" )
1566 self.handle.sendline( "" )
1567 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001568 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001569 main.log.error( self.name + ": EOF exception found" )
1570 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001571 main.cleanup()
1572 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001573 except Exception:
1574 main.log.exception( self.name + ": Uncaught exception!" )
admin2a9548d2014-06-17 14:08:07 -07001575 main.cleanup()
1576 main.exit()
1577
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001579 """
1580 Compare mn and onos switches
1581 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001582 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001583
Jon Hall7eb38402015-01-08 17:19:54 -08001584 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001585 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001586 output = { "switches": [] }
1587 # iterate through the MN topology and pull out switches and and port
1588 # info
1589 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001590 ports = []
1591 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001592 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001593 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001594 'name': port.name } )
1595 output[ 'switches' ].append( {
1596 "name": switch.name,
1597 "dpid": str( switch.dpid ).zfill( 16 ),
1598 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001599
Jon Hall7eb38402015-01-08 17:19:54 -08001600 # print "mn"
1601 # print json.dumps( output,
Jon Hallff6b4b22015-02-23 09:25:15 -08001602 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001603 # indent=4,
1604 # separators=( ',', ': ' ) )
1605 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001606 # print json.dumps( switchesJson,
Jon Hallff6b4b22015-02-23 09:25:15 -08001607 # sort_keys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001608 # indent=4,
1609 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001610
1611 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001612 mnDPIDs = []
1613 for switch in output[ 'switches' ]:
1614 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001615 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001616 # print "List of Mininet switch DPID's"
1617 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001618 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001619 main.log.error(
1620 self.name +
1621 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001622 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001623 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001624 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001625 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001626 if switch[ 'available' ]:
1627 onosDPIDs.append(
1628 switch[ 'id' ].replace(
1629 ":",
1630 '' ).replace(
1631 "of",
1632 '' ).lower() )
1633 # else:
1634 # print "Switch is unavailable:"
1635 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001636 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001637 # print "List of ONOS switch DPID's"
1638 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001639
Jon Hall7eb38402015-01-08 17:19:54 -08001640 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001641 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001642 main.log.report( "Switches in MN but not in ONOS:" )
1643 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1644 main.log.report( str( list1 ) )
1645 main.log.report( "Switches in ONOS but not in MN:" )
1646 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001647 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001648 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 switchResults = main.TRUE
1650 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001651
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001653 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001654 Compare mn and onos ports
1655 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001656 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001657
Jon Hallfbc828e2015-01-06 17:30:19 -08001658 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001659 1. This uses the sts TestONTopology object
1660 2. numpy - "sudo pip install numpy"
1661
Jon Hall7eb38402015-01-08 17:19:54 -08001662 """
1663 # FIXME: this does not look for extra ports in ONOS, only checks that
1664 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001665 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001666 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001667 output = { "switches": [] }
1668 # iterate through the MN topology and pull out switches and and port
1669 # info
1670 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001671 ports = []
1672 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001673 # print port.hw_addr.toStr( separator='' )
Jon Hallefbd9792015-03-05 16:11:36 -08001674 tmpPort = { 'of_port': port.port_no,
1675 'mac': str( port.hw_addr ).replace( '\'', '' ),
1676 'name': port.name,
1677 'enabled': port.enabled }
Jon Hall39f29df2014-11-04 19:30:21 -05001678
kelvin-onlabd3b64892015-01-20 13:26:24 -08001679 ports.append( tmpPort )
Jon Hallefbd9792015-03-05 16:11:36 -08001680 tmpSwitch = { 'name': switch.name,
1681 'dpid': str( switch.dpid ).zfill( 16 ),
1682 'ports': ports }
Jon Hall39f29df2014-11-04 19:30:21 -05001683
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001685
Jon Hall7eb38402015-01-08 17:19:54 -08001686 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001687 for mnSwitch in output[ 'switches' ]:
1688 mnPorts = []
1689 onosPorts = []
1690 switchResult = main.TRUE
1691 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001692 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 mnPorts.append( port[ 'of_port' ] )
1694 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001695 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001696 # print onosSwitch
1697 if onosSwitch[ 'device' ][ 'available' ]:
1698 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001699 ':',
1700 '' ).replace(
1701 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001702 '' ) == mnSwitch[ 'dpid' ]:
1703 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001704 if port[ 'isEnabled' ]:
1705 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 # onosPorts.append( 'local' )
1707 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001708 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001709 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001710 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001711 mnPorts.sort( key=float )
1712 onosPorts.sort( key=float )
1713 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1714 # print "\tmn_ports[] = ", mnPorts
1715 # print "\tonos_ports[] = ", onosPorts
1716 mnPortsLog = mnPorts
1717 onosPortsLog = onosPorts
1718 mnPorts = [ x for x in mnPorts ]
1719 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001720
Jon Hall7eb38402015-01-08 17:19:54 -08001721 # TODO: handle other reserved port numbers besides LOCAL
1722 # NOTE: Reserved ports
1723 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1724 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001725 for mnPort in mnPortsLog:
1726 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001727 # don't set results to true here as this is just one of
1728 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001729 mnPorts.remove( mnPort )
1730 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001731 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001732 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001733 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001734 if 65534 in mnPorts:
1735 mnPorts.remove( 65534 )
1736 if long( uint64( -2 ) ) in onosPorts:
1737 onosPorts.remove( long( uint64( -2 ) ) )
1738 if len( mnPorts ): # the ports of this switch don't match
1739 switchResult = main.FALSE
1740 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1741 if len( onosPorts ): # the ports of this switch don't match
1742 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001743 main.log.warn(
1744 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001745 str( onosPorts ) )
1746 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001747 main.log.report(
1748 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001749 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1750 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1751 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1752 portsResults = portsResults and switchResult
1753 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001754
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001756 """
1757 Compare mn and onos links
1758 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001760
Jon Hall7eb38402015-01-08 17:19:54 -08001761 This uses the sts TestONTopology object"""
1762 # FIXME: this does not look for extra links in ONOS, only checks that
Jon Hallefbd9792015-03-05 16:11:36 -08001763 # ONOS has what is in MN
Jon Hall7eb38402015-01-08 17:19:54 -08001764 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001765 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001766 # iterate through the MN topology and pull out switches and and port
1767 # info
1768 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001769 # print "Iterating though switches as seen by Mininet"
1770 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001771 ports = []
1772 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001773 # print port.hw_addr.toStr( separator='' )
1774 ports.append( { 'of_port': port.port_no,
Jon Hallefbd9792015-03-05 16:11:36 -08001775 'mac': str( port.hw_addr ).replace( '\'', '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001776 'name': port.name } )
1777 output[ 'switches' ].append( {
1778 "name": switch.name,
1779 "dpid": str( switch.dpid ).zfill( 16 ),
1780 "ports": ports } )
1781 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001782
kelvin-onlabd3b64892015-01-20 13:26:24 -08001783 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001784 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001785 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001786 if 2 * len( mnLinks ) == len( onos ):
1787 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001788 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001789 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001790 main.log.report(
Jon Hall328ddca2015-01-28 15:57:15 -08001791 "Mininet has " + str( len( mnLinks ) ) +
1792 " bidirectional links and ONOS has " +
1793 str( len( onos ) ) + " unidirectional links" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001794
Jon Hall7eb38402015-01-08 17:19:54 -08001795 # iterate through MN links and check if an ONOS link exists in
1796 # both directions
1797 # NOTE: Will currently only show mn links as down if they are
1798 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001799 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001800 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001801 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001802 # print "Link: %s" % link
1803 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001804 node1 = None
1805 port1 = None
1806 node2 = None
1807 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001808 firstDir = main.FALSE
1809 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001810 for switch in output[ 'switches' ]:
1811 # print "Switch: %s" % switch[ 'name' ]
1812 if switch[ 'name' ] == link.node1.name:
1813 node1 = switch[ 'dpid' ]
1814 for port in switch[ 'ports' ]:
1815 if str( port[ 'name' ] ) == str( link.port1 ):
1816 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001817 if node1 is not None and node2 is not None:
1818 break
Jon Hall7eb38402015-01-08 17:19:54 -08001819 if switch[ 'name' ] == link.node2.name:
1820 node2 = switch[ 'dpid' ]
1821 for port in switch[ 'ports' ]:
1822 if str( port[ 'name' ] ) == str( link.port2 ):
1823 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001824 if node1 is not None and node2 is not None:
1825 break
1826
kelvin-onlabd3b64892015-01-20 13:26:24 -08001827 for onosLink in onos:
1828 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001829 ":",
1830 '' ).replace(
1831 "of",
1832 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001833 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001834 ":",
1835 '' ).replace(
1836 "of",
1837 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001838 onosPort1 = onosLink[ 'src' ][ 'port' ]
1839 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001840
Jon Hall72cf1dc2014-10-20 21:04:50 -04001841 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001842 if str( onosNode1 ) == str( node1 ) and str(
1843 onosNode2 ) == str( node2 ):
1844 if int( onosPort1 ) == int( port1 ) and int(
1845 onosPort2 ) == int( port2 ):
1846 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001847 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001848 main.log.warn(
1849 'The port numbers do not match for ' +
1850 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001851 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001852 'link %s/%s -> %s/%s' %
1853 ( node1,
1854 port1,
1855 node2,
1856 port2 ) +
1857 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001858 ( onosNode1,
1859 onosPort1,
1860 onosNode2,
1861 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001862
1863 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001864 elif ( str( onosNode1 ) == str( node2 ) and
1865 str( onosNode2 ) == str( node1 ) ):
1866 if ( int( onosPort1 ) == int( port2 )
1867 and int( onosPort2 ) == int( port1 ) ):
1868 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001869 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001870 main.log.warn(
1871 'The port numbers do not match for ' +
1872 str( link ) +
Jon Hallefbd9792015-03-05 16:11:36 -08001873 ' between ONOS and MN. When checking ONOS for ' +
Jon Hall7eb38402015-01-08 17:19:54 -08001874 'link %s/%s -> %s/%s' %
1875 ( node2,
1876 port2,
1877 node1,
1878 port1 ) +
1879 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001880 ( onosNode2,
1881 onosPort2,
1882 onosNode1,
1883 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001884 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001885 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001886 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001887 main.log.report(
1888 'ONOS does not have the link %s/%s -> %s/%s' %
1889 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001890 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001891 main.log.report(
1892 'ONOS does not have the link %s/%s -> %s/%s' %
1893 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001894 linkResults = linkResults and firstDir and secondDir
1895 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001896
Jon Hallff6b4b22015-02-23 09:25:15 -08001897 def compareHosts( self, topo, hostsJson ):
1898 """
1899 Compare mn and onos Hosts.
1900 Since Mininet hosts are quiet, ONOS will only know of them when they
1901 speak. For this reason, we will only check that the hosts in ONOS
1902 stores are in Mininet, and not vice versa.
1903 topo: sts TestONTopology object
1904 hostsJson: parsed json object from the onos hosts api
1905
1906 This uses the sts TestONTopology object"""
1907 import json
1908 hostResults = main.TRUE
1909 hosts = []
1910 # iterate through the MN topology and pull out hosts
1911 for mnHost in topo.graph.hosts:
1912 interfaces = []
1913 for intf in mnHost.interfaces:
1914 interfaces.append( {
1915 "name": intf.name, # str
1916 "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs
1917 # hw_addr is of type EthAddr, Not JSON serializable
1918 "hw_addr": str( intf.hw_addr ) } )
1919 hosts.append( {
1920 "name": mnHost.name, # str
1921 "interfaces": interfaces } ) # list
1922 for onosHost in hostsJson:
1923 onosMAC = onosHost[ 'mac' ].lower()
1924 match = False
1925 for mnHost in hosts:
1926 for mnIntf in mnHost[ 'interfaces' ]:
1927 if onosMAC == mnIntf[ 'hw_addr' ].lower() :
1928 match = True
1929 for ip in mnIntf[ 'ips' ]:
1930 if ip in onosHost[ 'ips' ]:
1931 pass # all is well
1932 else:
1933 # misssing ip
1934 main.log.error( "ONOS host " + onosHost[ 'id' ]
1935 + " has a different IP than " +
1936 "the Mininet host." )
1937 output = json.dumps(
1938 onosHost,
1939 sort_keys=True,
1940 indent=4,
1941 separators=( ',', ': ' ) )
1942 main.log.info( output )
1943 hostResults = main.FALSE
1944 if not match:
1945 hostResults = main.FALSE
1946 main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " +
1947 "corresponding Mininet host." )
1948 output = json.dumps( onosHost,
1949 sort_keys=True,
1950 indent=4,
1951 separators=( ',', ': ' ) )
1952 main.log.info( output )
Jon Hallff6b4b22015-02-23 09:25:15 -08001953 return hostResults
1954
kelvin-onlabd3b64892015-01-20 13:26:24 -08001955 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001956 """
1957 Returns a list of all hosts
1958 Don't ask questions just use it"""
1959 self.handle.sendline( "" )
1960 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001961
Jon Hall7eb38402015-01-08 17:19:54 -08001962 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1963 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001964
kelvin-onlabd3b64892015-01-20 13:26:24 -08001965 handlePy = self.handle.before
1966 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1967 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001968
Jon Hall7eb38402015-01-08 17:19:54 -08001969 self.handle.sendline( "" )
1970 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001971
kelvin-onlabd3b64892015-01-20 13:26:24 -08001972 hostStr = handlePy.replace( "]", "" )
1973 hostStr = hostStr.replace( "'", "" )
1974 hostStr = hostStr.replace( "[", "" )
1975 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001976
kelvin-onlabd3b64892015-01-20 13:26:24 -08001977 return hostList
adminbae64d82013-08-01 10:50:15 -07001978
Jon Hall7eb38402015-01-08 17:19:54 -08001979 def update( self ):
1980 """
1981 updates the port address and status information for
1982 each port in mn"""
1983 # TODO: Add error checking. currently the mininet command has no output
Jon Hallefbd9792015-03-05 16:11:36 -08001984 main.log.info( "Updating MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001985 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001986 self.handle.sendline( "" )
1987 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001988
Jon Hall7eb38402015-01-08 17:19:54 -08001989 self.handle.sendline( "update" )
1990 self.handle.expect( "update" )
1991 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001992
Jon Hall7eb38402015-01-08 17:19:54 -08001993 self.handle.sendline( "" )
1994 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001995
Jon Hallb1290e82014-11-18 16:17:48 -05001996 return main.TRUE
1997 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001998 main.log.error( self.name + ": EOF exception found" )
1999 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05002000 main.cleanup()
2001 main.exit()
2002
adminbae64d82013-08-01 10:50:15 -07002003if __name__ != "__main__":
2004 import sys
kelvin-onlab50907142015-04-01 13:37:45 -07002005 sys.modules[ __name__ ] = MininetCliDriver()