blob: 6579e6de5f1b9df5bfd72af55ab385d6bd9bff91 [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 Hall7eb38402015-01-08 17:19:54 -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 Hall7eb38402015-01-08 17:19:54 -080022MininetCliDriver is the basic driver which will handle the Mininet functions"""
admin2a9548d2014-06-17 14:08:07 -070023import traceback
adminbae64d82013-08-01 10:50:15 -070024import pexpect
adminbae64d82013-08-01 10:50:15 -070025import re
26import sys
Jon Hall7eb38402015-01-08 17:19:54 -080027sys.path.append( "../" )
Jon Hall1ccf82c2014-10-15 14:55:16 -040028from math import pow
adminbae64d82013-08-01 10:50:15 -070029from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070030
Jon Hall7eb38402015-01-08 17:19:54 -080031
32class MininetCliDriver( Emulator ):
33
34 """
35 MininetCliDriver is the basic driver which will handle
36 the Mininet functions"""
37 def __init__( self ):
38 super( Emulator, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070039 self.handle = self
Jon Hall7eb38402015-01-08 17:19:54 -080040 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070041 self.flag = 0
42
Jon Hall7eb38402015-01-08 17:19:54 -080043 def connect( self, **connectargs ):
44 """
45 Here the main is the TestON instance after creating
46 all the log handles."""
adminbae64d82013-08-01 10:50:15 -070047 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -080048 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080049
Jon Hall7eb38402015-01-08 17:19:54 -080050 self.name = self.options[ 'name' ]
51 self.handle = super(
52 MininetCliDriver,
53 self ).connect(
kelvin-onlabd3b64892015-01-20 13:26:24 -080054 userName=self.userName,
55 ipAddress=self.ipAddress,
Jon Hall7eb38402015-01-08 17:19:54 -080056 port=None,
57 pwd=self.pwd )
Jon Hallfbc828e2015-01-06 17:30:19 -080058
kelvin-onlabd3b64892015-01-20 13:26:24 -080059 self.sshHandle = self.handle
Jon Hallfbc828e2015-01-06 17:30:19 -080060
Jon Hall7eb38402015-01-08 17:19:54 -080061 if self.handle:
62 main.log.info(
63 self.name +
64 ": Clearing any residual state or processes" )
65 self.handle.sendline( "sudo mn -c" )
66 i = self.handle.expect( [ 'password\sfor\s',
67 'Cleanup\scomplete',
68 pexpect.EOF,
69 pexpect.TIMEOUT ],
70 120 )
71 if i == 0:
72 main.log.info( self.name + ": Sending sudo password" )
73 self.handle.sendline( self.pwd )
74 i = self.handle.expect( [ '%s:' % ( self.user ),
75 '\$',
76 pexpect.EOF,
77 pexpect.TIMEOUT ],
78 120 )
79 if i == 1:
80 main.log.info( self.name + ": Clean" )
81 elif i == 2:
82 main.log.error( self.name + ": Connection terminated" )
83 elif i == 3: # timeout
84 main.log.error(
85 self.name +
86 ": Something while cleaning MN took too long... " )
Jon Hallfbc828e2015-01-06 17:30:19 -080087
Jon Hall7eb38402015-01-08 17:19:54 -080088 main.log.info( self.name + ": building fresh mininet" )
89 # for reactive/PARP enabled tests
90 cmdString = "sudo mn " + self.options[ 'arg1' ] +\
kelvin-onlabedcff052015-01-16 12:53:55 -080091 " " + self.options[ 'arg2' ] +\
92 " --mac --controller " +\
93 self.options[ 'controller' ] + " " +\
94 self.options[ 'arg3' ]
Jon Hallfbc828e2015-01-06 17:30:19 -080095
Jon Hall7eb38402015-01-08 17:19:54 -080096 argList = self.options[ 'arg1' ].split( "," )
Jon Hall1ccf82c2014-10-15 14:55:16 -040097 global topoArgList
Jon Hall7eb38402015-01-08 17:19:54 -080098 topoArgList = argList[ 0 ].split( " " )
99 argList = map( int, argList[ 1: ] )
100 topoArgList = topoArgList[ 1: ] + argList
Jon Hallfbc828e2015-01-06 17:30:19 -0800101
Jon Hall7eb38402015-01-08 17:19:54 -0800102 self.handle.sendline( cmdString )
103 self.handle.expect( [ "sudo mn", pexpect.EOF, pexpect.TIMEOUT ] )
104 while True:
105 i = self.handle.expect( [ 'mininet>',
106 '\*\*\*',
107 'Exception',
108 pexpect.EOF,
109 pexpect.TIMEOUT ],
110 300 )
111 if i == 0:
112 main.log.info( self.name + ": mininet built" )
adminbae64d82013-08-01 10:50:15 -0700113 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800114 if i == 1:
115 self.handle.expect(
116 [ "\n", pexpect.EOF, pexpect.TIMEOUT ] )
117 main.log.info( self.handle.before )
118 elif i == 2:
119 main.log.error(
120 self.name +
121 ": Launching mininet failed..." )
adminbae64d82013-08-01 10:50:15 -0700122 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800123 elif i == 3:
124 main.log.error( self.name + ": Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700125 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800126 elif i == 4: # timeout
127 main.log.error(
128 self.name +
129 ": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -0700130 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700131 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800132 else: # if no handle
133 main.log.error(
134 self.name +
135 ": Connection failed to the host " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800136 self.userName +
Jon Hall7eb38402015-01-08 17:19:54 -0800137 "@" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800138 self.ipAddress )
Jon Hall7eb38402015-01-08 17:19:54 -0800139 main.log.error( self.name + ": Failed to connect to the Mininet" )
adminbae64d82013-08-01 10:50:15 -0700140 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800141
kelvin-onlabfccaafa2015-01-20 13:50:44 -0800142 def numSwitchesNlinks( self, topoType, depth, fanout ):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400143 if topoType == 'tree':
Jon Hall7eb38402015-01-08 17:19:54 -0800144 # In tree topology, if fanout arg is not given, by default it is 2
145 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400146 fanout = 2
147 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500148 count = 0
Jon Hall7eb38402015-01-08 17:19:54 -0800149 while( k <= depth - 1 ):
150 count = count + pow( fanout, k )
151 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800152 numSwitches = count
Jon Hall7eb38402015-01-08 17:19:54 -0800153 while( k <= depth - 2 ):
154 # depth-2 gives you only core links and not considering
155 # edge links as seen by ONOS. If all the links including
156 # edge links are required, do depth-1
157 count = count + pow( fanout, k )
158 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800159 numLinks = count * fanout
Jon Hall7eb38402015-01-08 17:19:54 -0800160 # print "num_switches for %s(%d,%d) = %d and links=%d" %(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800161 # topoType,depth,fanout,numSwitches,numLinks )
Jon Hallfbc828e2015-01-06 17:30:19 -0800162
Jon Hall7eb38402015-01-08 17:19:54 -0800163 elif topoType == 'linear':
kelvin-onlabd3b64892015-01-20 13:26:24 -0800164 # In linear topology, if fanout or numHostsPerSw is not given,
Jon Hall7eb38402015-01-08 17:19:54 -0800165 # by default it is 1
166 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400167 fanout = 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800168 numSwitches = depth
169 numHostsPerSw = fanout
170 totalNumHosts = numSwitches * numHostsPerSw
171 numLinks = totalNumHosts + ( numSwitches - 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800172 print "num_switches for %s(%d,%d) = %d and links=%d" %\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800173 ( topoType, depth, fanout, numSwitches, numLinks )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400174 topoDict = {}
Jon Hall7eb38402015-01-08 17:19:54 -0800175 topoDict = {
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 "num_switches": int( numSwitches ),
177 "num_corelinks": int( numLinks ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400178 return topoDict
179
kelvin-onlabd3b64892015-01-20 13:26:24 -0800180 def calculateSwAndLinks( self ):
181 topoDict = self.numSwitchesN_links( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400182 return topoDict
183
Jon Hall7eb38402015-01-08 17:19:54 -0800184 def pingall( self, timeout=300 ):
185 """
186 Verifies the reachability of the hosts using pingall command.
187 Optional parameter timeout allows you to specify how long to
188 wait for pingall to complete
189 Returns:
190 main.TRUE if pingall completes with no pings dropped
191 otherwise main.FALSE"""
192 if self.handle:
193 main.log.info(
194 self.name +
195 ": Checking reachabilty to the hosts using pingall" )
Jon Hall6094a362014-04-11 14:46:56 -0700196 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800197 response = self.execute(
198 cmd="pingall",
199 prompt="mininet>",
200 timeout=int( timeout ) )
Jon Hallb1290e82014-11-18 16:17:48 -0500201 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800202 main.log.error( self.name + ": EOF exception found" )
203 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500204 main.cleanup()
205 main.exit()
206 except pexpect.TIMEOUT:
Jon Hall7eb38402015-01-08 17:19:54 -0800207 # We may not want to kill the test if pexpect times out
208 main.log.error( self.name + ": TIMEOUT exception found" )
209 main.log.error( self.name +
210 ": " +
211 str( self.handle.before ) )
212 # NOTE: mininet's pingall rounds, so we will check the number of
213 # passed and number of failed
214 pattern = "Results\:\s0\%\sdropped\s\(" +\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 "(?P<passed>[\d]+)/(?P=passed)"
Jon Hall7eb38402015-01-08 17:19:54 -0800216 if re.search( pattern, response ):
217 main.log.info( self.name + ": All hosts are reachable" )
adminbae64d82013-08-01 10:50:15 -0700218 return main.TRUE
219 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800220 main.log.error( self.name + ": Unable to reach all the hosts" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 main.log.info( "Pingall output: " + str( response ) )
Jon Hall7eb38402015-01-08 17:19:54 -0800222 # NOTE: Send ctrl-c to make sure pingall is done
223 self.handle.send( "\x03" )
224 self.handle.expect( "Interrupt" )
225 self.handle.expect( "mininet>" )
adminbae64d82013-08-01 10:50:15 -0700226 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800227 else:
228 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallb1290e82014-11-18 16:17:48 -0500229 main.cleanup()
230 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700231
Jon Hall7eb38402015-01-08 17:19:54 -0800232 def fpingHost( self, **pingParams ):
233 """
234 Uses the fping package for faster pinging...
235 *requires fping to be installed on machine running mininet"""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 args = utilities.parseArgs( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800237 command = args[ "SRC" ] + \
238 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
239 self.handle.sendline( command )
240 self.handle.expect(
241 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
242 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
243 response = self.handle.before
244 if re.search( ":\s-", response ):
245 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700246 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800247 elif re.search( ":\s\d{1,2}\.\d\d", response ):
248 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700249 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800250 main.log.info( self.name + ": Install fping on mininet machine... " )
251 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700252 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800253
Jon Hall7eb38402015-01-08 17:19:54 -0800254 def pingHost( self, **pingParams ):
255 """
256 Ping from one mininet host to another
257 Currently the only supported Params: SRC and TARGET"""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800258 args = utilities.parseArgs( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800259 command = args[ "SRC" ] + " ping " + \
260 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700261 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800262 main.log.warn( "Sending: " + command )
263 self.handle.sendline( command )
264 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700265 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800266 main.log.error(
267 self.name +
268 ": timeout when waiting for response from mininet" )
269 main.log.error( "response: " + str( self.handle.before ) )
270 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700271 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800272 main.log.error(
273 self.name +
274 ": timeout when waiting for response from mininet" )
275 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700276 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800277 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700280 main.cleanup()
281 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800282 main.log.info( self.name + ": Ping Response: " + response )
283 if re.search( ',\s0\%\spacket\sloss', response ):
284 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800285 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700286 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800287 else:
288 main.log.error(
289 self.name +
290 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800291 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700292 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800293
Jon Hall7eb38402015-01-08 17:19:54 -0800294 def checkIP( self, host ):
295 """
296 Verifies the host's ip configured or not."""
297 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700298 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800299 response = self.execute(
300 cmd=host +
301 " ifconfig",
302 prompt="mininet>",
303 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800304 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800305 main.log.error( self.name + ": EOF exception found" )
306 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700307 main.cleanup()
308 main.exit()
adminbae64d82013-08-01 10:50:15 -0700309
Jon Hall7eb38402015-01-08 17:19:54 -0800310 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800311 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
312 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
313 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
314 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
315 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800316 # pattern = "inet addr:10.0.0.6"
317 if re.search( pattern, response ):
318 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700319 return main.TRUE
320 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800321 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700322 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800323 else:
324 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800325
Jon Hall7eb38402015-01-08 17:19:54 -0800326 def verifySSH( self, **connectargs ):
Jon Hall6094a362014-04-11 14:46:56 -0700327 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800328 response = self.execute(
329 cmd="h1 /usr/sbin/sshd -D&",
330 prompt="mininet>",
331 timeout=10 )
332 response = self.execute(
333 cmd="h4 /usr/sbin/sshd -D&",
334 prompt="mininet>",
335 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700336 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800337 vars( self )[ key ] = connectargs[ key ]
338 response = self.execute(
339 cmd="xterm h1 h4 ",
340 prompt="mininet>",
341 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800342 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800343 main.log.error( self.name + ": EOF exception found" )
344 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700345 main.cleanup()
346 main.exit()
adminbae64d82013-08-01 10:50:15 -0700347 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800348 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700349 if self.flag == 0:
350 self.flag = 1
351 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800352 else:
adminbae64d82013-08-01 10:50:15 -0700353 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800354
Jon Hall7eb38402015-01-08 17:19:54 -0800355 def changeIP( self, host, intf, newIP, newNetmask ):
356 """
357 Changes the ip address of a host on the fly
358 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800359 if self.handle:
360 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800361 cmd = host + " ifconfig " + intf + " " + \
362 newIP + " " + 'netmask' + " " + newNetmask
363 self.handle.sendline( cmd )
364 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800365 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800366 main.log.info( "response = " + response )
367 main.log.info(
368 "Ip of host " +
369 host +
370 " changed to new IP " +
371 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800372 return main.TRUE
373 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800374 main.log.error( self.name + ": EOF exception found" )
375 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800376 return main.FALSE
377
Jon Hall7eb38402015-01-08 17:19:54 -0800378 def changeDefaultGateway( self, host, newGW ):
379 """
380 Changes the default gateway of a host
381 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800382 if self.handle:
383 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800384 cmd = host + " route add default gw " + newGW
385 self.handle.sendline( cmd )
386 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800387 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800388 main.log.info( "response = " + response )
389 main.log.info(
390 "Default gateway of host " +
391 host +
392 " changed to " +
393 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800394 return main.TRUE
395 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800398 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800399
Jon Hall7eb38402015-01-08 17:19:54 -0800400 def addStaticMACAddress( self, host, GW, macaddr ):
401 """
402 Changes the mac address of a geateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800403 if self.handle:
404 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800405 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
406 cmd = host + " arp -s " + GW + " " + macaddr
407 self.handle.sendline( cmd )
408 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800409 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800410 main.log.info( "response = " + response )
411 main.log.info(
412 "Mac adrress of gateway " +
413 GW +
414 " changed to " +
415 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800416 return main.TRUE
417 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800418 main.log.error( self.name + ": EOF exception found" )
419 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800420 return main.FALSE
421
Jon Hall7eb38402015-01-08 17:19:54 -0800422 def verifyStaticGWandMAC( self, host ):
423 """
424 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800425 if self.handle:
426 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800427 # h1 arp -an
428 cmd = host + " arp -an "
429 self.handle.sendline( cmd )
430 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800431 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800432 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800433 return main.TRUE
434 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800435 main.log.error( self.name + ": EOF exception found" )
436 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800437 return main.FALSE
438
Jon Hall7eb38402015-01-08 17:19:54 -0800439 def getMacAddress( self, host ):
440 """
441 Verifies the host's ip configured or not."""
442 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700443 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800444 response = self.execute(
445 cmd=host +
446 " ifconfig",
447 prompt="mininet>",
448 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800449 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800450 main.log.error( self.name + ": EOF exception found" )
451 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700452 main.cleanup()
453 main.exit()
adminbae64d82013-08-01 10:50:15 -0700454
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700455 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800456 macAddressSearch = re.search( pattern, response, re.I )
457 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800458 main.log.info(
459 self.name +
460 ": Mac-Address of Host " +
461 host +
462 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800463 macAddress )
464 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700465 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800466 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700467
Jon Hall7eb38402015-01-08 17:19:54 -0800468 def getInterfaceMACAddress( self, host, interface ):
469 """
470 Return the IP address of the interface on the given host"""
471 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700472 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800473 response = self.execute( cmd=host + " ifconfig " + interface,
474 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800475 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800476 main.log.error( self.name + ": EOF exception found" )
477 main.log.error( self.name + ": " + self.handle.before )
478 main.cleanup()
479 main.exit()
480
481 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 macAddressSearch = re.search( pattern, response, re.I )
483 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800484 main.log.info( "No mac address found in %s" % response )
485 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800486 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800487 main.log.info(
488 "Mac-Address of " +
489 host +
490 ":" +
491 interface +
492 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800493 macAddress )
494 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800495 else:
496 main.log.error( "Connection failed to the host" )
497
498 def getIPAddress( self, host ):
499 """
500 Verifies the host's ip configured or not."""
501 if self.handle:
502 try:
503 response = self.execute(
504 cmd=host +
505 " ifconfig",
506 prompt="mininet>",
507 timeout=10 )
508 except pexpect.EOF:
509 main.log.error( self.name + ": EOF exception found" )
510 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700511 main.cleanup()
512 main.exit()
adminbae64d82013-08-01 10:50:15 -0700513
514 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800516 main.log.info(
517 self.name +
518 ": IP-Address of Host " +
519 host +
520 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 ipAddressSearch.group( 1 ) )
522 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800523 else:
524 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800525
Jon Hall7eb38402015-01-08 17:19:54 -0800526 def getSwitchDPID( self, switch ):
527 """
528 return the datapath ID of the switch"""
529 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700530 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700531 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800532 response = self.execute(
533 cmd=cmd,
534 prompt="mininet>",
535 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800536 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800537 main.log.error( self.name + ": EOF exception found" )
538 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700539 main.cleanup()
540 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800541 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800542 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700543 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800544 main.log.info(
545 "Couldn't find DPID for switch %s, found: %s" %
546 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700547 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800548 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700549 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800550 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700551
Jon Hall7eb38402015-01-08 17:19:54 -0800552 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700553 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800554 self.handle.sendline( "" )
555 self.expect( "mininet>" )
556 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700557 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800558 response = self.execute(
559 cmd=cmd,
560 prompt="mininet>",
561 timeout=10 )
562 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700563 response = self.handle.before
564 return response
565 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800566 main.log.error( self.name + ": EOF exception found" )
567 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700568 main.cleanup()
569 main.exit()
570
Jon Hall7eb38402015-01-08 17:19:54 -0800571 def getInterfaces( self, node ):
572 """
573 return information dict about interfaces connected to the node"""
574 if self.handle:
575 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800576 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700577 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700578 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800579 response = self.execute(
580 cmd=cmd,
581 prompt="mininet>",
582 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800583 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800584 main.log.error( self.name + ": EOF exception found" )
585 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700586 main.cleanup()
587 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700588 return response
589 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800590 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700591
Jon Hall7eb38402015-01-08 17:19:54 -0800592 def dump( self ):
593 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700594 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800595 response = self.execute(
596 cmd='dump',
597 prompt='mininet>',
598 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800599 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800600 main.log.error( self.name + ": EOF exception found" )
601 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700602 main.cleanup()
603 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700604 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800605
Jon Hall7eb38402015-01-08 17:19:54 -0800606 def intfs( self ):
607 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700608 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800609 response = self.execute(
610 cmd='intfs',
611 prompt='mininet>',
612 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800613 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800614 main.log.error( self.name + ": EOF exception found" )
615 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700616 main.cleanup()
617 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700618 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800619
Jon Hall7eb38402015-01-08 17:19:54 -0800620 def net( self ):
621 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700622 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800623 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800624 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800625 main.log.error( self.name + ": EOF exception found" )
626 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700627 main.cleanup()
628 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700629 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800630
631 def iperf( self, host1, host2 ):
632 main.log.info(
633 self.name +
634 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700635 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800636 cmd1 = 'iperf ' + host1 + " " + host2
637 self.handle.sendline( cmd1 )
638 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800639 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800640 if re.search( 'Results:', response ):
641 main.log.info( self.name + ": iperf test succssful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800642 return main.TRUE
643 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800644 main.log.error( self.name + ": iperf test failed" )
645 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800646 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800647 main.log.error( self.name + ": EOF exception found" )
648 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800649 main.cleanup()
650 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800651
Jon Hall7eb38402015-01-08 17:19:54 -0800652 def iperfudp( self ):
653 main.log.info(
654 self.name +
655 ": Simple iperf TCP test between two " +
656 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700657 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800658 response = self.execute(
659 cmd='iperfudp',
660 prompt='mininet>',
661 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800662 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800663 main.log.error( self.name + ": EOF exception found" )
664 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700665 main.cleanup()
666 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700667 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800668
Jon Hall7eb38402015-01-08 17:19:54 -0800669 def nodes( self ):
670 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700671 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800672 response = self.execute(
673 cmd='nodes',
674 prompt='mininet>',
675 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800676 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800677 main.log.error( self.name + ": EOF exception found" )
678 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700679 main.cleanup()
680 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700681 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800682
Jon Hall7eb38402015-01-08 17:19:54 -0800683 def pingpair( self ):
684 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700685 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800686 response = self.execute(
687 cmd='pingpair',
688 prompt='mininet>',
689 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800690 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800691 main.log.error( self.name + ": EOF exception found" )
692 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700693 main.cleanup()
694 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800695
Jon Hall7eb38402015-01-08 17:19:54 -0800696 if re.search( ',\s0\%\spacket\sloss', response ):
697 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700699 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800700 else:
701 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800702 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700703 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800704
Jon Hall7eb38402015-01-08 17:19:54 -0800705 def link( self, **linkargs ):
706 """
707 Bring link( s ) between two nodes up or down"""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800708 args = utilities.parseArgs( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800709 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
710 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
711 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
712 main.log.info(
713 "Bring link between '" +
714 end1 +
715 "' and '" +
716 end2 +
717 "' '" +
718 option +
719 "'" )
720 command = "link " + \
721 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700722 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800723 self.handle.sendline( command )
724 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800725 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800726 main.log.error( self.name + ": EOF exception found" )
727 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700728 main.cleanup()
729 main.exit()
adminbae64d82013-08-01 10:50:15 -0700730 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800731
Jon Hall7eb38402015-01-08 17:19:54 -0800732 def yank( self, **yankargs ):
733 """
734 yank a mininet switch interface to a host"""
735 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 args = utilities.parseArgs( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800737 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
738 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
739 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700740 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800741 response = self.execute(
742 cmd=command,
743 prompt="mininet>",
744 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800745 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800746 main.log.error( self.name + ": EOF exception found" )
747 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700748 main.cleanup()
749 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700750 return main.TRUE
751
Jon Hall7eb38402015-01-08 17:19:54 -0800752 def plug( self, **plugargs ):
753 """
754 plug the yanked mininet switch interface to a switch"""
755 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800756 args = utilities.parseArgs( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800757 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
758 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
759 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700760 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800761 response = self.execute(
762 cmd=command,
763 prompt="mininet>",
764 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800765 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800766 main.log.error( self.name + ": EOF exception found" )
767 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700768 main.cleanup()
769 main.exit()
adminbae64d82013-08-01 10:50:15 -0700770 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800771
Jon Hall7eb38402015-01-08 17:19:54 -0800772 def dpctl( self, **dpctlargs ):
773 """
774 Run dpctl command on all switches."""
775 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800776 args = utilities.parseArgs( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800777 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
778 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
779 command = "dpctl " + cmd + " " + str( cmdargs )
780 try:
781 response = self.execute(
782 cmd=command,
783 prompt="mininet>",
784 timeout=10 )
785 except pexpect.EOF:
786 main.log.error( self.name + ": EOF exception found" )
787 main.log.error( self.name + ": " + self.handle.before )
788 main.cleanup()
789 main.exit()
790 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800791
kelvin-onlabd3b64892015-01-20 13:26:24 -0800792 def getVersion( self ):
793 fileInput = path + '/lib/Mininet/INSTALL'
794 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -0700795 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800796 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -0800797 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700798 if result:
Jon Hall7eb38402015-01-08 17:19:54 -0800799 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -0500800 return version
adminbae64d82013-08-01 10:50:15 -0700801
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800803 """
Jon Hallec3c21e2014-11-10 22:22:37 -0500804 Parameters:
805 sw: The name of an OVS switch. Example "s1"
806 Return:
Jon Hall7eb38402015-01-08 17:19:54 -0800807 The output of the command from the mininet cli
808 or main.FALSE on timeout"""
809 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -0700810 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800811 response = self.execute(
812 cmd=command,
813 prompt="mininet>",
814 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -0700815 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500816 return response
admin2a9548d2014-06-17 14:08:07 -0700817 else:
818 return main.FALSE
819 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800820 main.log.error( self.name + ": EOF exception found" )
821 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700822 main.cleanup()
823 main.exit()
adminbae64d82013-08-01 10:50:15 -0700824
kelvin-onlabd3b64892015-01-20 13:26:24 -0800825 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800826 """
827 count is only needed if there is more than 1 controller"""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800828 args = utilities.parseArgs( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800829 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -0700830
831 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -0800832 for j in range( count ):
833 argstring = argstring + ",IP" + \
834 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 args = utilities.parseArgs( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -0700836
Jon Hall7eb38402015-01-08 17:19:54 -0800837 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
838 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800839 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -0800840 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -0800841
Jon Hall7eb38402015-01-08 17:19:54 -0800842 command = "sh ovs-vsctl set-controller s" + \
843 str( sw ) + " " + ptcpB + " "
844 for j in range( count ):
845 i = j + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800846 args = utilities.parseArgs(
Jon Hall7eb38402015-01-08 17:19:54 -0800847 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
848 ip = args[
849 "IP" +
850 str( i ) ] if args[
851 "IP" +
852 str( i ) ] is not None else ""
853 port = args[
854 "PORT" +
855 str( i ) ] if args[
856 "PORT" +
857 str( i ) ] is not None else ""
858 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800859 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700860 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700861 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800862 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -0700863 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800864 main.log.error( self.name + ": EOF exception found" )
865 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700866 main.cleanup()
867 main.exit()
868 except:
Jon Hall7eb38402015-01-08 17:19:54 -0800869 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800870 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800871 main.log.info( ":" * 50 )
Jon Hall6094a362014-04-11 14:46:56 -0700872 main.cleanup()
873 main.exit()
adminbae64d82013-08-01 10:50:15 -0700874
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800876 """
877 Removes the controller target from sw"""
878 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -0700879 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800880 response = self.execute(
881 cmd=command,
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 Hall0819fd92014-05-23 12:08:13 -0700887 main.cleanup()
888 main.exit()
889 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800890 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -0700891
kelvin-onlabd3b64892015-01-20 13:26:24 -0800892 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800893 """
Jon Hallb1290e82014-11-18 16:17:48 -0500894 adds a switch to the mininet topology
895 NOTE: this uses a custom mn function
896 NOTE: cannot currently specify what type of switch
897 required params:
898 switchname = name of the new switch as a string
899 optional keyvalues:
900 dpid = "dpid"
901 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800902 """
903 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -0800904 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500905 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800906 response = self.execute(
907 cmd=command,
908 prompt="mininet>",
909 timeout=10 )
910 if re.search( "already exists!", response ):
911 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500912 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800913 elif re.search( "Error", response ):
914 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500915 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800916 elif re.search( "usage:", response ):
917 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500918 return main.FALSE
919 else:
920 return main.TRUE
921 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800922 main.log.error( self.name + ": EOF exception found" )
923 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500924 main.cleanup()
925 main.exit()
926
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800928 """
929 delete a switch from the mininet topology
930 NOTE: this uses a custom mn function
931 required params:
Jon Hallb1290e82014-11-18 16:17:48 -0500932 switchname = name of the switch as a string
Jon Hall7eb38402015-01-08 17:19:54 -0800933 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800934 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500935 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800936 response = self.execute(
937 cmd=command,
938 prompt="mininet>",
939 timeout=10 )
940 if re.search( "no switch named", response ):
941 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500942 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800943 elif re.search( "Error", response ):
944 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500945 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800946 elif re.search( "usage:", response ):
947 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500948 return main.FALSE
949 else:
950 return main.TRUE
951 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800952 main.log.error( self.name + ": EOF exception found" )
953 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500954 main.cleanup()
955 main.exit()
956
kelvin-onlabd3b64892015-01-20 13:26:24 -0800957 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -0800958 """
959 add a link to the mininet topology
960 NOTE: this uses a custom mn function
961 NOTE: cannot currently specify what type of link
962 required params:
963 node1 = the string node name of the first endpoint of the link
964 node2 = the string node name of the second endpoint of the link
965 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800966 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500967 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800968 response = self.execute(
969 cmd=command,
970 prompt="mininet>",
971 timeout=10 )
972 if re.search( "doesnt exist!", response ):
973 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500974 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800975 elif re.search( "Error", response ):
976 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500977 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800978 elif re.search( "usage:", response ):
979 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500980 return main.FALSE
981 else:
982 return main.TRUE
983 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800984 main.log.error( self.name + ": EOF exception found" )
985 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500986 main.cleanup()
987 main.exit()
988
kelvin-onlabd3b64892015-01-20 13:26:24 -0800989 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -0800990 """
991 delete a link from the mininet topology
992 NOTE: this uses a custom mn function
993 required params:
994 node1 = the string node name of the first endpoint of the link
995 node2 = the string node name of the second endpoint of the link
996 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800997 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500998 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800999 response = self.execute(
1000 cmd=command,
1001 prompt="mininet>",
1002 timeout=10 )
1003 if re.search( "no node named", response ):
1004 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001005 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001006 elif re.search( "Error", response ):
1007 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001008 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001009 elif re.search( "usage:", response ):
1010 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001011 return main.FALSE
1012 else:
1013 return main.TRUE
1014 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001015 main.log.error( self.name + ": EOF exception found" )
1016 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001017 main.cleanup()
1018 main.exit()
1019
kelvin-onlabd3b64892015-01-20 13:26:24 -08001020 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001021 """
Jon Hallb1290e82014-11-18 16:17:48 -05001022 Add a host to the mininet topology
1023 NOTE: this uses a custom mn function
1024 NOTE: cannot currently specify what type of host
1025 required params:
1026 hostname = the string hostname
1027 optional key-value params
1028 switch = "switch name"
1029 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001030 """
1031 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001032 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001033 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001034 response = self.execute(
1035 cmd=command,
1036 prompt="mininet>",
1037 timeout=10 )
1038 if re.search( "already exists!", response ):
1039 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001040 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001041 elif re.search( "doesnt exists!", response ):
1042 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001043 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001044 elif re.search( "Error", response ):
1045 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001046 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001047 elif re.search( "usage:", response ):
1048 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001049 return main.FALSE
1050 else:
1051 return main.TRUE
1052 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001053 main.log.error( self.name + ": EOF exception found" )
1054 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001055 main.cleanup()
1056 main.exit()
1057
kelvin-onlabd3b64892015-01-20 13:26:24 -08001058 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001059 """
1060 delete a host from the mininet topology
1061 NOTE: this uses a custom mn function
1062 required params:
1063 hostname = the string hostname
1064 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001065 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001066 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001067 response = self.execute(
1068 cmd=command,
1069 prompt="mininet>",
1070 timeout=10 )
1071 if re.search( "no host named", response ):
1072 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001073 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001074 elif re.search( "Error", response ):
1075 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001076 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001077 elif re.search( "usage:", response ):
1078 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001079 return main.FALSE
1080 else:
1081 return main.TRUE
1082 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001083 main.log.error( self.name + ": EOF exception found" )
1084 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001085 main.cleanup()
1086 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001087
Jon Hall7eb38402015-01-08 17:19:54 -08001088 def disconnect( self ):
1089 main.log.info( self.name + ": Disconnecting mininet..." )
adminbae64d82013-08-01 10:50:15 -07001090 response = ''
1091 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001092 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001093 response = self.execute(
1094 cmd="exit",
1095 prompt="(.*)",
1096 timeout=120 )
1097 response = self.execute(
1098 cmd="exit",
1099 prompt="(.*)",
1100 timeout=120 )
1101 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001102 response = main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001103 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001104 main.log.error( self.name + ": EOF exception found" )
1105 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001106 main.cleanup()
1107 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001108 else:
1109 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001110 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001111 return response
1112
Jon Hall7eb38402015-01-08 17:19:54 -08001113 def arping( self, src, dest, destmac ):
1114 self.handle.sendline( '' )
1115 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001116
Jon Hall7eb38402015-01-08 17:19:54 -08001117 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001118 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001119 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1120 main.log.info( self.name + ": ARP successful" )
1121 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001122 return main.TRUE
1123 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001124 main.log.warn( self.name + ": ARP FAILURE" )
1125 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001126 return main.FALSE
1127
Jon Hall7eb38402015-01-08 17:19:54 -08001128 def decToHex( self, num ):
1129 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001130
Jon Hall7eb38402015-01-08 17:19:54 -08001131 def getSwitchFlowCount( self, switch ):
1132 """
1133 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001134 if self.handle:
1135 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1136 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001137 response = self.execute(
1138 cmd=cmd,
1139 prompt="mininet>",
1140 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001141 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001142 main.log.error( self.name + ": EOF exception found" )
1143 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001144 main.cleanup()
1145 main.exit()
1146 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001147 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001148 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001149 main.log.info(
1150 "Couldn't find flows on switch %s, found: %s" %
1151 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001152 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001153 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001154 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001155 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001156
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 def checkFlows( self, sw, dumpFormat=None ):
1158 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001159 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001160 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001161 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001162 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001163 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001164 response = self.execute(
1165 cmd=command,
1166 prompt="mininet>",
1167 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001168 return response
1169 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001170 main.log.error( self.name + ": EOF exception found" )
1171 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001172 main.cleanup()
1173 main.exit()
1174 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001175 main.log.info( response )
admin2a9548d2014-06-17 14:08:07 -07001176
kelvin-onlabd3b64892015-01-20 13:26:24 -08001177 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001178 """
1179 Runs tpdump on an intferface and saves the file
1180 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001181 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001182 self.handle.sendline( "" )
1183 self.handle.expect( "mininet>" )
1184 self.handle.sendline(
1185 "sh sudo tcpdump -n -i " +
1186 intf +
1187 " " +
1188 port +
1189 " -w " +
1190 filename.strip() +
1191 " &" )
1192 self.handle.sendline( "" )
1193 i = self.handle.expect( [ 'No\ssuch\device',
1194 'listening\son',
1195 pexpect.TIMEOUT,
1196 "mininet>" ],
1197 timeout=10 )
1198 main.log.warn( self.handle.before + self.handle.after )
1199 self.handle.sendline( "" )
1200 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001201 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001202 main.log.error(
1203 self.name +
1204 ": tcpdump - No such device exists. " +
1205 "tcpdump attempted on: " +
1206 intf )
admin2a9548d2014-06-17 14:08:07 -07001207 return main.FALSE
1208 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001209 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001210 return main.TRUE
1211 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001212 main.log.error(
1213 self.name +
1214 ": tcpdump command timed out! Check interface name," +
1215 " given interface was: " +
1216 intf )
admin2a9548d2014-06-17 14:08:07 -07001217 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001218 elif i == 3:
1219 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001220 return main.TRUE
1221 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001222 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001223 return main.FALSE
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 )
admin2a9548d2014-06-17 14:08:07 -07001227 main.cleanup()
1228 main.exit()
1229 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001230 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001231 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001232 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001233 main.cleanup()
1234 main.exit()
1235
kelvin-onlabd3b64892015-01-20 13:26:24 -08001236 def stopTcpdump( self ):
admin2a9548d2014-06-17 14:08:07 -07001237 "pkills tcpdump"
1238 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001239 self.handle.sendline( "sh sudo pkill tcpdump" )
1240 self.handle.expect( "mininet>" )
1241 self.handle.sendline( "" )
1242 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001243 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001244 main.log.error( self.name + ": EOF exception found" )
1245 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001246 main.cleanup()
1247 main.exit()
1248 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001249 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001251 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001252 main.cleanup()
1253 main.exit()
1254
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001256 """
1257 Compare mn and onos switches
1258 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001259 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001260
Jon Hall7eb38402015-01-08 17:19:54 -08001261 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001262 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001263 output = { "switches": [] }
1264 # iterate through the MN topology and pull out switches and and port
1265 # info
1266 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001267 ports = []
1268 for port in switch.ports.values():
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 ports.append( { 'of_port': port.portNo,
1270 'mac': str( port.hwAddr ).replace( '\'',
1271 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001272 'name': port.name } )
1273 output[ 'switches' ].append( {
1274 "name": switch.name,
1275 "dpid": str( switch.dpid ).zfill( 16 ),
1276 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001277
Jon Hall7eb38402015-01-08 17:19:54 -08001278 # print "mn"
1279 # print json.dumps( output,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001281 # indent=4,
1282 # separators=( ',', ': ' ) )
1283 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001284 # print json.dumps( switchesJson,
1285 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001286 # indent=4,
1287 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001288
1289 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001290 mnDPIDs = []
1291 for switch in output[ 'switches' ]:
1292 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001293 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001294 # print "List of Mininet switch DPID's"
1295 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001296 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001297 main.log.error(
1298 self.name +
1299 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001300 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001301 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001302 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001303 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001304 if switch[ 'available' ]:
1305 onosDPIDs.append(
1306 switch[ 'id' ].replace(
1307 ":",
1308 '' ).replace(
1309 "of",
1310 '' ).lower() )
1311 # else:
1312 # print "Switch is unavailable:"
1313 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001314 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001315 # print "List of ONOS switch DPID's"
1316 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001317
Jon Hall7eb38402015-01-08 17:19:54 -08001318 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001320 main.log.report( "Switches in MN but not in ONOS:" )
1321 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1322 main.log.report( str( list1 ) )
1323 main.log.report( "Switches in ONOS but not in MN:" )
1324 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001325 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001326 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001327 switchResults = main.TRUE
1328 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001329
kelvin-onlabd3b64892015-01-20 13:26:24 -08001330 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001331 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001332 Compare mn and onos ports
1333 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001334 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001335
Jon Hallfbc828e2015-01-06 17:30:19 -08001336 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001337 1. This uses the sts TestONTopology object
1338 2. numpy - "sudo pip install numpy"
1339
Jon Hall7eb38402015-01-08 17:19:54 -08001340 """
1341 # FIXME: this does not look for extra ports in ONOS, only checks that
1342 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001343 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001344 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001345 output = { "switches": [] }
1346 # iterate through the MN topology and pull out switches and and port
1347 # info
1348 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001349 ports = []
1350 for port in switch.ports.values():
kelvin-onlabd3b64892015-01-20 13:26:24 -08001351 # print port.hwAddr.toStr( separator='' )
1352 tmpPort = {}
1353 tmpPort[ 'of_port' ] = port.portNo
1354 tmpPort[ 'mac' ] = str( port.hwAddr ).replace( '\'', '' )
1355 tmpPort[ 'name' ] = port.name
1356 tmpPort[ 'enabled' ] = port.enabled
Jon Hall39f29df2014-11-04 19:30:21 -05001357
kelvin-onlabd3b64892015-01-20 13:26:24 -08001358 ports.append( tmpPort )
1359 tmpSwitch = {}
1360 tmpSwitch[ 'name' ] = switch.name
1361 tmpSwitch[ 'dpid' ] = str( switch.dpid ).zfill( 16 )
1362 tmpSwitch[ 'ports' ] = ports
Jon Hall39f29df2014-11-04 19:30:21 -05001363
kelvin-onlabd3b64892015-01-20 13:26:24 -08001364 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001365
Jon Hall7eb38402015-01-08 17:19:54 -08001366 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001367 for mnSwitch in output[ 'switches' ]:
1368 mnPorts = []
1369 onosPorts = []
1370 switchResult = main.TRUE
1371 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001372 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 mnPorts.append( port[ 'of_port' ] )
1374 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001375 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001376 # print onosSwitch
1377 if onosSwitch[ 'device' ][ 'available' ]:
1378 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001379 ':',
1380 '' ).replace(
1381 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001382 '' ) == mnSwitch[ 'dpid' ]:
1383 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001384 if port[ 'isEnabled' ]:
1385 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001386 # onosPorts.append( 'local' )
1387 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001388 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001390 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001391 mnPorts.sort( key=float )
1392 onosPorts.sort( key=float )
1393 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1394 # print "\tmn_ports[] = ", mnPorts
1395 # print "\tonos_ports[] = ", onosPorts
1396 mnPortsLog = mnPorts
1397 onosPortsLog = onosPorts
1398 mnPorts = [ x for x in mnPorts ]
1399 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001400
Jon Hall7eb38402015-01-08 17:19:54 -08001401 # TODO: handle other reserved port numbers besides LOCAL
1402 # NOTE: Reserved ports
1403 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1404 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001405 for mnPort in mnPortsLog:
1406 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001407 # don't set results to true here as this is just one of
1408 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 mnPorts.remove( mnPort )
1410 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001411 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001412 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001413 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001414 if 65534 in mnPorts:
1415 mnPorts.remove( 65534 )
1416 if long( uint64( -2 ) ) in onosPorts:
1417 onosPorts.remove( long( uint64( -2 ) ) )
1418 if len( mnPorts ): # the ports of this switch don't match
1419 switchResult = main.FALSE
1420 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1421 if len( onosPorts ): # the ports of this switch don't match
1422 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001423 main.log.warn(
1424 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 str( onosPorts ) )
1426 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001427 main.log.report(
1428 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001429 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1430 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1431 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1432 portsResults = portsResults and switchResult
1433 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001434
kelvin-onlabd3b64892015-01-20 13:26:24 -08001435 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001436 """
1437 Compare mn and onos links
1438 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001439 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001440
Jon Hall7eb38402015-01-08 17:19:54 -08001441 This uses the sts TestONTopology object"""
1442 # FIXME: this does not look for extra links in ONOS, only checks that
1443 # ONOS has what is in MN
kelvin-onlabd3b64892015-01-20 13:26:24 -08001444 linkResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001445 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001447 # iterate through the MN topology and pull out switches and and port
1448 # info
1449 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001450 # print "Iterating though switches as seen by Mininet"
1451 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001452 ports = []
1453 for port in switch.ports.values():
kelvin-onlabd3b64892015-01-20 13:26:24 -08001454 # print port.hwAddr.toStr( separator='' )
1455 ports.append( { 'of_port': port.portNo,
1456 'mac': str( port.hwAddr ).replace( '\'',
1457 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001458 'name': port.name } )
1459 output[ 'switches' ].append( {
1460 "name": switch.name,
1461 "dpid": str( switch.dpid ).zfill( 16 ),
1462 "ports": ports } )
1463 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001464
kelvin-onlabd3b64892015-01-20 13:26:24 -08001465 mnLinks = [
1466 link for link in topo.patchPanel.networkLinks if (
Jon Hall7eb38402015-01-08 17:19:54 -08001467 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001468 if 2 * len( mnLinks ) == len( onos ):
1469 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001470 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001471 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001472 main.log.report(
1473 "Mininet has %i bidirectional links and " +
1474 "ONOS has %i unidirectional links" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001475 ( len( mnLinks ), len( onos ) ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001476
Jon Hall7eb38402015-01-08 17:19:54 -08001477 # iterate through MN links and check if an ONOS link exists in
1478 # both directions
1479 # NOTE: Will currently only show mn links as down if they are
1480 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001481 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001482 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001484 # print "Link: %s" % link
1485 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001486 node1 = None
1487 port1 = None
1488 node2 = None
1489 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001490 firstDir = main.FALSE
1491 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001492 for switch in output[ 'switches' ]:
1493 # print "Switch: %s" % switch[ 'name' ]
1494 if switch[ 'name' ] == link.node1.name:
1495 node1 = switch[ 'dpid' ]
1496 for port in switch[ 'ports' ]:
1497 if str( port[ 'name' ] ) == str( link.port1 ):
1498 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001499 if node1 is not None and node2 is not None:
1500 break
Jon Hall7eb38402015-01-08 17:19:54 -08001501 if switch[ 'name' ] == link.node2.name:
1502 node2 = switch[ 'dpid' ]
1503 for port in switch[ 'ports' ]:
1504 if str( port[ 'name' ] ) == str( link.port2 ):
1505 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001506 if node1 is not None and node2 is not None:
1507 break
1508
kelvin-onlabd3b64892015-01-20 13:26:24 -08001509 for onosLink in onos:
1510 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001511 ":",
1512 '' ).replace(
1513 "of",
1514 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001515 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001516 ":",
1517 '' ).replace(
1518 "of",
1519 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001520 onosPort1 = onosLink[ 'src' ][ 'port' ]
1521 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001522
Jon Hall72cf1dc2014-10-20 21:04:50 -04001523 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 if str( onosNode1 ) == str( node1 ) and str(
1525 onosNode2 ) == str( node2 ):
1526 if int( onosPort1 ) == int( port1 ) and int(
1527 onosPort2 ) == int( port2 ):
1528 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001529 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001530 main.log.warn(
1531 'The port numbers do not match for ' +
1532 str( link ) +
1533 ' between ONOS and MN. When cheking ONOS for ' +
1534 'link %s/%s -> %s/%s' %
1535 ( node1,
1536 port1,
1537 node2,
1538 port2 ) +
1539 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001540 ( onosNode1,
1541 onosPort1,
1542 onosNode2,
1543 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001544
1545 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001546 elif ( str( onosNode1 ) == str( node2 ) and
1547 str( onosNode2 ) == str( node1 ) ):
1548 if ( int( onosPort1 ) == int( port2 )
1549 and int( onosPort2 ) == int( port1 ) ):
1550 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001551 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001552 main.log.warn(
1553 'The port numbers do not match for ' +
1554 str( link ) +
1555 ' between ONOS and MN. When cheking ONOS for ' +
1556 'link %s/%s -> %s/%s' %
1557 ( node2,
1558 port2,
1559 node1,
1560 port1 ) +
1561 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001562 ( onosNode2,
1563 onosPort2,
1564 onosNode1,
1565 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001566 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001567 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001569 main.log.report(
1570 'ONOS does not have the link %s/%s -> %s/%s' %
1571 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001572 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001573 main.log.report(
1574 'ONOS does not have the link %s/%s -> %s/%s' %
1575 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001576 linkResults = linkResults and firstDir and secondDir
1577 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001578
kelvin-onlabd3b64892015-01-20 13:26:24 -08001579 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001580 """
1581 Returns a list of all hosts
1582 Don't ask questions just use it"""
1583 self.handle.sendline( "" )
1584 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001585
Jon Hall7eb38402015-01-08 17:19:54 -08001586 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1587 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001588
kelvin-onlabd3b64892015-01-20 13:26:24 -08001589 handlePy = self.handle.before
1590 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1591 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001592
Jon Hall7eb38402015-01-08 17:19:54 -08001593 self.handle.sendline( "" )
1594 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001595
kelvin-onlabd3b64892015-01-20 13:26:24 -08001596 hostStr = handlePy.replace( "]", "" )
1597 hostStr = hostStr.replace( "'", "" )
1598 hostStr = hostStr.replace( "[", "" )
1599 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001600
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 return hostList
adminbae64d82013-08-01 10:50:15 -07001602
Jon Hall7eb38402015-01-08 17:19:54 -08001603 def update( self ):
1604 """
1605 updates the port address and status information for
1606 each port in mn"""
1607 # TODO: Add error checking. currently the mininet command has no output
1608 main.log.info( "Updateing MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001609 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001610 self.handle.sendline( "" )
1611 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001612
Jon Hall7eb38402015-01-08 17:19:54 -08001613 self.handle.sendline( "update" )
1614 self.handle.expect( "update" )
1615 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001616
Jon Hall7eb38402015-01-08 17:19:54 -08001617 self.handle.sendline( "" )
1618 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001619
Jon Hallb1290e82014-11-18 16:17:48 -05001620 return main.TRUE
1621 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001622 main.log.error( self.name + ": EOF exception found" )
1623 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001624 main.cleanup()
1625 main.exit()
1626
adminbae64d82013-08-01 10:50:15 -07001627if __name__ != "__main__":
1628 import sys
Jon Hall7eb38402015-01-08 17:19:54 -08001629 sys.modules[ __name__ ] = MininetCliDriver()