blob: df678c2d1f954e6d3b1fe5978c0e885ccbefd098 [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."""
admin2a9548d2014-06-17 14:08:07 -070038import traceback
adminbae64d82013-08-01 10:50:15 -070039import pexpect
adminbae64d82013-08-01 10:50:15 -070040import re
41import sys
Jon Hall7eb38402015-01-08 17:19:54 -080042sys.path.append( "../" )
Jon Hall1ccf82c2014-10-15 14:55:16 -040043from math import pow
adminbae64d82013-08-01 10:50:15 -070044from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070045
Jon Hall7eb38402015-01-08 17:19:54 -080046
47class MininetCliDriver( Emulator ):
48
49 """
50 MininetCliDriver is the basic driver which will handle
51 the Mininet functions"""
52 def __init__( self ):
53 super( Emulator, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070054 self.handle = self
Jon 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-onlaba0ce3222015-01-27 17:25:15 -080062 try:
63 for key in connectargs:
64 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080065
kelvin-onlaba0ce3222015-01-27 17:25:15 -080066 self.name = self.options[ 'name' ]
67 self.handle = super(
68 MininetCliDriver,
69 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-onlaba0ce3222015-01-27 17:25:15 -080075 if self.handle:
kelvin-onlab5edb77f2015-01-28 16:27:12 -080076 main.log.info("Connection successful to the host " +
kelvin-onlaba0ce3222015-01-27 17:25:15 -080077 self.user_name +
78 "@" +
79 self.ip_address )
80 return main.TRUE
81 else:
82 main.log.error( "Connection failed to the host " +
83 self.user_name +
84 "@" +
85 self.ip_address )
86 msin.log.error( "Failed to connect to the Mininet CLI" )
87 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()
93 except:
94 main.log.info( self.name + ":::::::::::::::::::::::" )
95 main.log.error( traceback.print_exc() )
96 main.log.info( ":::::::::::::::::::::::" )
97 main.cleanup()
98 main.exit()
99
100
kelvin-onlab97d64e72015-01-28 11:14:55 -0800101 def startNet( self, topoFile = '', args = '', timeout = 120 ):
Jon Hallfbc828e2015-01-06 17:30:19 -0800102
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800103 if self.handle:
104 main.log.info(
105 self.name +
106 ": Clearing any residual state or processes" )
107 self.handle.sendline( "sudo mn -c" )
108 i = self.handle.expect( [ 'password\sfor\s',
109 'Cleanup\scomplete',
110 pexpect.EOF,
111 pexpect.TIMEOUT ],
kelvin-onlab97d64e72015-01-28 11:14:55 -0800112 timeout )
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800113 if i == 0:
114 main.log.info( self.name + ": Sending sudo password" )
115 self.handle.sendline( self.pwd )
116 i = self.handle.expect( [ '%s:' % ( self.user ),
117 '\$',
Jon Hall7eb38402015-01-08 17:19:54 -0800118 pexpect.EOF,
119 pexpect.TIMEOUT ],
kelvin-onlab97d64e72015-01-28 11:14:55 -0800120 timeout )
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800121 if i == 1:
122 main.log.info( self.name + ": Clean" )
123 elif i == 2:
124 main.log.error( self.name + ": Connection terminated" )
125 elif i == 3: # timeout
126 main.log.error(
127 self.name +
128 ": Something while cleaning MN took too long... " )
kelvin-onlab97d64e72015-01-28 11:14:55 -0800129 if topoFile == '' and args == '':
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800130 main.log.info( self.name + ": building fresh mininet" )
131 # for reactive/PARP enabled tests
132 cmdString = "sudo mn " + self.options[ 'arg1' ] +\
133 " " + self.options[ 'arg2' ] +\
134 " --mac --controller " +\
135 self.options[ 'controller' ] + " " +\
136 self.options[ 'arg3' ]
137
138 argList = self.options[ 'arg1' ].split( "," )
139 global topoArgList
140 topoArgList = argList[ 0 ].split( " " )
141 argList = map( int, argList[ 1: ] )
142 topoArgList = topoArgList[ 1: ] + argList
143
144 self.handle.sendline( cmdString )
145 self.handle.expect( [ "sudo mn",
146 pexpect.EOF,
147 pexpect.TIMEOUT ] )
148 while True:
149 i = self.handle.expect( [ 'mininet>',
150 '\*\*\*',
151 'Exception',
152 pexpect.EOF,
153 pexpect.TIMEOUT ],
kelvin-onlab97d64e72015-01-28 11:14:55 -0800154 timeout )
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800155 if i == 0:
156 main.log.info( self.name + ": mininet built" )
157 return main.TRUE
158 if i == 1:
159 self.handle.expect(
160 [ "\n", pexpect.EOF, pexpect.TIMEOUT ] )
161 main.log.info( self.handle.before )
162 elif i == 2:
163 main.log.error(
164 self.name +
165 ": Launching mininet failed..." )
166 return main.FALSE
167 elif i == 3:
168 main.log.error( self.name + ": Connection timeout" )
169 return main.FALSE
170 elif i == 4: # timeout
171 main.log.error(
172 self.name +
173 ": Something took too long... " )
174 return main.FALSE
175 return main.TRUE
176 else:
kelvin-onlab97d64e72015-01-28 11:14:55 -0800177 main.log.info( "Starting topo file " + topoFile )
178 if args == None:
179 args = ''
180 else:
181 main.log.info( "args = " + args)
182 self.handle.sendline( 'sudo ' + topoFile + ' ' + args)
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800183 i = 1
184 i = self.handle.expect( [ 'mininet>',
185 pexpect.EOF ,
kelvin-onlab97d64e72015-01-28 11:14:55 -0800186 pexpect.TIMEOUT ],
187 timeout)
kelvin-onlaba0ce3222015-01-27 17:25:15 -0800188 main.log.info(self.name + ": Network started")
189 return main.TRUE
190
Jon Hall7eb38402015-01-08 17:19:54 -0800191 else: # if no handle
192 main.log.error(
193 self.name +
194 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800195 self.user_name +
Jon Hall7eb38402015-01-08 17:19:54 -0800196 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800197 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 Hall1ccf82c2014-10-15 14:55:16 -0400233 topoDict = {}
Jon Hall7eb38402015-01-08 17:19:54 -0800234 topoDict = {
kelvin-onlabd3b64892015-01-20 13:26:24 -0800235 "num_switches": int( numSwitches ),
236 "num_corelinks": int( numLinks ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400237 return topoDict
238
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 def calculateSwAndLinks( self ):
240 topoDict = self.numSwitchesN_links( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400241 return topoDict
242
Jon Hall7eb38402015-01-08 17:19:54 -0800243 def pingall( self, timeout=300 ):
244 """
245 Verifies the reachability of the hosts using pingall command.
246 Optional parameter timeout allows you to specify how long to
247 wait for pingall to complete
248 Returns:
249 main.TRUE if pingall completes with no pings dropped
250 otherwise main.FALSE"""
251 if self.handle:
252 main.log.info(
253 self.name +
254 ": Checking reachabilty to the hosts using pingall" )
Jon Hall6094a362014-04-11 14:46:56 -0700255 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800256 response = self.execute(
257 cmd="pingall",
258 prompt="mininet>",
259 timeout=int( timeout ) )
Jon Hallb1290e82014-11-18 16:17:48 -0500260 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800261 main.log.error( self.name + ": EOF exception found" )
262 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500263 main.cleanup()
264 main.exit()
265 except pexpect.TIMEOUT:
Jon Hall7eb38402015-01-08 17:19:54 -0800266 # We may not want to kill the test if pexpect times out
267 main.log.error( self.name + ": TIMEOUT exception found" )
268 main.log.error( self.name +
269 ": " +
270 str( self.handle.before ) )
271 # NOTE: mininet's pingall rounds, so we will check the number of
272 # passed and number of failed
273 pattern = "Results\:\s0\%\sdropped\s\(" +\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800274 "(?P<passed>[\d]+)/(?P=passed)"
Jon Hall7eb38402015-01-08 17:19:54 -0800275 if re.search( pattern, response ):
276 main.log.info( self.name + ": All hosts are reachable" )
adminbae64d82013-08-01 10:50:15 -0700277 return main.TRUE
278 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800279 main.log.error( self.name + ": Unable to reach all the hosts" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800280 main.log.info( "Pingall output: " + str( response ) )
Jon Hall7eb38402015-01-08 17:19:54 -0800281 # NOTE: Send ctrl-c to make sure pingall is done
282 self.handle.send( "\x03" )
283 self.handle.expect( "Interrupt" )
284 self.handle.expect( "mininet>" )
adminbae64d82013-08-01 10:50:15 -0700285 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800286 else:
287 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallb1290e82014-11-18 16:17:48 -0500288 main.cleanup()
289 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700290
Jon Hall7eb38402015-01-08 17:19:54 -0800291 def fpingHost( self, **pingParams ):
292 """
293 Uses the fping package for faster pinging...
294 *requires fping to be installed on machine running mininet"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800295 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800296 command = args[ "SRC" ] + \
297 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
298 self.handle.sendline( command )
299 self.handle.expect(
300 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
301 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
302 response = self.handle.before
303 if re.search( ":\s-", response ):
304 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700305 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800306 elif re.search( ":\s\d{1,2}\.\d\d", response ):
307 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700308 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800309 main.log.info( self.name + ": Install fping on mininet machine... " )
310 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700311 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800312
Jon Hall7eb38402015-01-08 17:19:54 -0800313 def pingHost( self, **pingParams ):
314 """
315 Ping from one mininet host to another
316 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800317 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800318 command = args[ "SRC" ] + " ping " + \
319 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700320 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800321 main.log.warn( "Sending: " + command )
322 self.handle.sendline( command )
323 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700324 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800325 main.log.error(
326 self.name +
327 ": timeout when waiting for response from mininet" )
328 main.log.error( "response: " + str( self.handle.before ) )
329 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700330 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800331 main.log.error(
332 self.name +
333 ": timeout when waiting for response from mininet" )
334 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700335 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800336 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800337 main.log.error( self.name + ": EOF exception found" )
338 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700339 main.cleanup()
340 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800341 main.log.info( self.name + ": Ping Response: " + response )
342 if re.search( ',\s0\%\spacket\sloss', response ):
343 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800344 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700345 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800346 else:
347 main.log.error(
348 self.name +
349 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700351 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800352
Jon Hall7eb38402015-01-08 17:19:54 -0800353 def checkIP( self, host ):
354 """
355 Verifies the host's ip configured or not."""
356 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700357 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800358 response = self.execute(
359 cmd=host +
360 " ifconfig",
361 prompt="mininet>",
362 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800363 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800364 main.log.error( self.name + ": EOF exception found" )
365 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700366 main.cleanup()
367 main.exit()
adminbae64d82013-08-01 10:50:15 -0700368
Jon Hall7eb38402015-01-08 17:19:54 -0800369 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800370 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
371 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
372 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
373 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
374 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800375 # pattern = "inet addr:10.0.0.6"
376 if re.search( pattern, response ):
377 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700378 return main.TRUE
379 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800380 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700381 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800382 else:
383 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800384
Jon Hall7eb38402015-01-08 17:19:54 -0800385 def verifySSH( self, **connectargs ):
Jon Hall6094a362014-04-11 14:46:56 -0700386 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800387 response = self.execute(
388 cmd="h1 /usr/sbin/sshd -D&",
389 prompt="mininet>",
390 timeout=10 )
391 response = self.execute(
392 cmd="h4 /usr/sbin/sshd -D&",
393 prompt="mininet>",
394 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700395 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800396 vars( self )[ key ] = connectargs[ key ]
397 response = self.execute(
398 cmd="xterm h1 h4 ",
399 prompt="mininet>",
400 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800401 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800402 main.log.error( self.name + ": EOF exception found" )
403 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700404 main.cleanup()
405 main.exit()
adminbae64d82013-08-01 10:50:15 -0700406 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800407 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700408 if self.flag == 0:
409 self.flag = 1
410 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800411 else:
adminbae64d82013-08-01 10:50:15 -0700412 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800413
Jon Hall7eb38402015-01-08 17:19:54 -0800414 def changeIP( self, host, intf, newIP, newNetmask ):
415 """
416 Changes the ip address of a host on the fly
417 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800418 if self.handle:
419 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800420 cmd = host + " ifconfig " + intf + " " + \
421 newIP + " " + 'netmask' + " " + newNetmask
422 self.handle.sendline( cmd )
423 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800424 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800425 main.log.info( "response = " + response )
426 main.log.info(
427 "Ip of host " +
428 host +
429 " changed to new IP " +
430 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800431 return main.TRUE
432 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800433 main.log.error( self.name + ": EOF exception found" )
434 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800435 return main.FALSE
436
Jon Hall7eb38402015-01-08 17:19:54 -0800437 def changeDefaultGateway( self, host, newGW ):
438 """
439 Changes the default gateway of a host
440 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800441 if self.handle:
442 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800443 cmd = host + " route add default gw " + newGW
444 self.handle.sendline( cmd )
445 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800446 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800447 main.log.info( "response = " + response )
448 main.log.info(
449 "Default gateway of host " +
450 host +
451 " changed to " +
452 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800453 return main.TRUE
454 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 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800457 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800458
Jon Hall7eb38402015-01-08 17:19:54 -0800459 def addStaticMACAddress( self, host, GW, macaddr ):
460 """
461 Changes the mac address of a geateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800462 if self.handle:
463 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800464 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
465 cmd = host + " arp -s " + GW + " " + macaddr
466 self.handle.sendline( cmd )
467 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800468 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800469 main.log.info( "response = " + response )
470 main.log.info(
471 "Mac adrress of gateway " +
472 GW +
473 " changed to " +
474 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800475 return main.TRUE
476 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800477 main.log.error( self.name + ": EOF exception found" )
478 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800479 return main.FALSE
480
Jon Hall7eb38402015-01-08 17:19:54 -0800481 def verifyStaticGWandMAC( self, host ):
482 """
483 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800484 if self.handle:
485 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800486 # h1 arp -an
487 cmd = host + " arp -an "
488 self.handle.sendline( cmd )
489 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800490 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800491 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800492 return main.TRUE
493 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800494 main.log.error( self.name + ": EOF exception found" )
495 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800496 return main.FALSE
497
Jon Hall7eb38402015-01-08 17:19:54 -0800498 def getMacAddress( self, host ):
499 """
500 Verifies the host's ip configured or not."""
501 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700502 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800503 response = self.execute(
504 cmd=host +
505 " ifconfig",
506 prompt="mininet>",
507 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800508 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800509 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
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700514 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 macAddressSearch = re.search( pattern, response, re.I )
516 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800517 main.log.info(
518 self.name +
519 ": Mac-Address of Host " +
520 host +
521 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800522 macAddress )
523 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700524 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800525 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700526
Jon Hall7eb38402015-01-08 17:19:54 -0800527 def getInterfaceMACAddress( self, host, interface ):
528 """
529 Return the IP address of the interface on the given host"""
530 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700531 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800532 response = self.execute( cmd=host + " ifconfig " + interface,
533 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800534 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800535 main.log.error( self.name + ": EOF exception found" )
536 main.log.error( self.name + ": " + self.handle.before )
537 main.cleanup()
538 main.exit()
539
540 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 macAddressSearch = re.search( pattern, response, re.I )
542 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800543 main.log.info( "No mac address found in %s" % response )
544 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800545 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800546 main.log.info(
547 "Mac-Address of " +
548 host +
549 ":" +
550 interface +
551 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800552 macAddress )
553 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800554 else:
555 main.log.error( "Connection failed to the host" )
556
557 def getIPAddress( self, host ):
558 """
559 Verifies the host's ip configured or not."""
560 if self.handle:
561 try:
562 response = self.execute(
563 cmd=host +
564 " ifconfig",
565 prompt="mininet>",
566 timeout=10 )
567 except pexpect.EOF:
568 main.log.error( self.name + ": EOF exception found" )
569 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700570 main.cleanup()
571 main.exit()
adminbae64d82013-08-01 10:50:15 -0700572
573 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800575 main.log.info(
576 self.name +
577 ": IP-Address of Host " +
578 host +
579 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800580 ipAddressSearch.group( 1 ) )
581 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800582 else:
583 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800584
Jon Hall7eb38402015-01-08 17:19:54 -0800585 def getSwitchDPID( self, switch ):
586 """
587 return the datapath ID of the switch"""
588 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700589 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700590 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800591 response = self.execute(
592 cmd=cmd,
593 prompt="mininet>",
594 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800595 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800596 main.log.error( self.name + ": EOF exception found" )
597 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700598 main.cleanup()
599 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800600 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800601 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700602 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800603 main.log.info(
604 "Couldn't find DPID for switch %s, found: %s" %
605 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700606 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800607 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700608 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800609 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700610
Jon Hall7eb38402015-01-08 17:19:54 -0800611 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700612 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800613 self.handle.sendline( "" )
614 self.expect( "mininet>" )
615 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700616 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800617 response = self.execute(
618 cmd=cmd,
619 prompt="mininet>",
620 timeout=10 )
621 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700622 response = self.handle.before
623 return response
624 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 )
admin2580a0e2014-07-29 11:24:34 -0700627 main.cleanup()
628 main.exit()
629
Jon Hall7eb38402015-01-08 17:19:54 -0800630 def getInterfaces( self, node ):
631 """
632 return information dict about interfaces connected to the node"""
633 if self.handle:
634 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800635 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700636 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700637 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800638 response = self.execute(
639 cmd=cmd,
640 prompt="mininet>",
641 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800642 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700645 main.cleanup()
646 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700647 return response
648 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800649 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700650
Jon Hall7eb38402015-01-08 17:19:54 -0800651 def dump( self ):
652 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700653 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800654 response = self.execute(
655 cmd='dump',
656 prompt='mininet>',
657 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800658 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800659 main.log.error( self.name + ": EOF exception found" )
660 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700661 main.cleanup()
662 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700663 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800664
Jon Hall7eb38402015-01-08 17:19:54 -0800665 def intfs( self ):
666 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700667 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800668 response = self.execute(
669 cmd='intfs',
670 prompt='mininet>',
671 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800672 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800673 main.log.error( self.name + ": EOF exception found" )
674 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700675 main.cleanup()
676 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700677 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800678
Jon Hall7eb38402015-01-08 17:19:54 -0800679 def net( self ):
680 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700681 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800682 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800683 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800684 main.log.error( self.name + ": EOF exception found" )
685 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700686 main.cleanup()
687 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700688 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800689
690 def iperf( self, host1, host2 ):
691 main.log.info(
692 self.name +
693 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700694 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800695 cmd1 = 'iperf ' + host1 + " " + host2
696 self.handle.sendline( cmd1 )
697 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800698 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800699 if re.search( 'Results:', response ):
700 main.log.info( self.name + ": iperf test succssful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800701 return main.TRUE
702 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800703 main.log.error( self.name + ": iperf test failed" )
704 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800705 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 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800708 main.cleanup()
709 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800710
Jon Hall7eb38402015-01-08 17:19:54 -0800711 def iperfudp( self ):
712 main.log.info(
713 self.name +
714 ": Simple iperf TCP test between two " +
715 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700716 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800717 response = self.execute(
718 cmd='iperfudp',
719 prompt='mininet>',
720 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800721 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800722 main.log.error( self.name + ": EOF exception found" )
723 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700724 main.cleanup()
725 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700726 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800727
Jon Hall7eb38402015-01-08 17:19:54 -0800728 def nodes( self ):
729 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700730 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800731 response = self.execute(
732 cmd='nodes',
733 prompt='mininet>',
734 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800735 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800736 main.log.error( self.name + ": EOF exception found" )
737 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700738 main.cleanup()
739 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700740 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800741
Jon Hall7eb38402015-01-08 17:19:54 -0800742 def pingpair( self ):
743 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700744 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800745 response = self.execute(
746 cmd='pingpair',
747 prompt='mininet>',
748 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800749 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800750 main.log.error( self.name + ": EOF exception found" )
751 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700752 main.cleanup()
753 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800754
Jon Hall7eb38402015-01-08 17:19:54 -0800755 if re.search( ',\s0\%\spacket\sloss', response ):
756 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800757 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700758 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800759 else:
760 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800761 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700762 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800763
Jon Hall7eb38402015-01-08 17:19:54 -0800764 def link( self, **linkargs ):
765 """
766 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800767 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800768 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
769 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
770 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
771 main.log.info(
772 "Bring link between '" +
773 end1 +
774 "' and '" +
775 end2 +
776 "' '" +
777 option +
778 "'" )
779 command = "link " + \
780 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700781 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800782 self.handle.sendline( command )
783 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800784 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800785 main.log.error( self.name + ": EOF exception found" )
786 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700787 main.cleanup()
788 main.exit()
adminbae64d82013-08-01 10:50:15 -0700789 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800790
Jon Hall7eb38402015-01-08 17:19:54 -0800791 def yank( self, **yankargs ):
792 """
793 yank a mininet switch interface to a host"""
794 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800795 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800796 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
797 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
798 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700799 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800800 response = self.execute(
801 cmd=command,
802 prompt="mininet>",
803 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800804 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800805 main.log.error( self.name + ": EOF exception found" )
806 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700807 main.cleanup()
808 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700809 return main.TRUE
810
Jon Hall7eb38402015-01-08 17:19:54 -0800811 def plug( self, **plugargs ):
812 """
813 plug the yanked mininet switch interface to a switch"""
814 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800815 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800816 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
817 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
818 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700819 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800820 response = self.execute(
821 cmd=command,
822 prompt="mininet>",
823 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800824 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800825 main.log.error( self.name + ": EOF exception found" )
826 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700827 main.cleanup()
828 main.exit()
adminbae64d82013-08-01 10:50:15 -0700829 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800830
Jon Hall7eb38402015-01-08 17:19:54 -0800831 def dpctl( self, **dpctlargs ):
832 """
833 Run dpctl command on all switches."""
834 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800835 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800836 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
837 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
838 command = "dpctl " + cmd + " " + str( cmdargs )
839 try:
840 response = self.execute(
841 cmd=command,
842 prompt="mininet>",
843 timeout=10 )
844 except pexpect.EOF:
845 main.log.error( self.name + ": EOF exception found" )
846 main.log.error( self.name + ": " + self.handle.before )
847 main.cleanup()
848 main.exit()
849 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800850
kelvin-onlabd3b64892015-01-20 13:26:24 -0800851 def getVersion( self ):
852 fileInput = path + '/lib/Mininet/INSTALL'
853 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -0700854 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -0800856 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700857 if result:
Jon Hall7eb38402015-01-08 17:19:54 -0800858 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -0500859 return version
adminbae64d82013-08-01 10:50:15 -0700860
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800862 """
Jon Hallec3c21e2014-11-10 22:22:37 -0500863 Parameters:
864 sw: The name of an OVS switch. Example "s1"
865 Return:
Jon Hall7eb38402015-01-08 17:19:54 -0800866 The output of the command from the mininet cli
867 or main.FALSE on timeout"""
868 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -0700869 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800870 response = self.execute(
871 cmd=command,
872 prompt="mininet>",
873 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -0700874 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500875 return response
admin2a9548d2014-06-17 14:08:07 -0700876 else:
877 return main.FALSE
878 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800879 main.log.error( self.name + ": EOF exception found" )
880 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700881 main.cleanup()
882 main.exit()
adminbae64d82013-08-01 10:50:15 -0700883
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800885 """
886 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800887 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800888 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -0700889
890 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -0800891 for j in range( count ):
892 argstring = argstring + ",IP" + \
893 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800894 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -0700895
Jon Hall7eb38402015-01-08 17:19:54 -0800896 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
897 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800898 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -0800899 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -0800900
Jon Hall7eb38402015-01-08 17:19:54 -0800901 command = "sh ovs-vsctl set-controller s" + \
902 str( sw ) + " " + ptcpB + " "
903 for j in range( count ):
904 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800905 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -0800906 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
907 ip = args[
908 "IP" +
909 str( i ) ] if args[
910 "IP" +
911 str( i ) ] is not None else ""
912 port = args[
913 "PORT" +
914 str( i ) ] if args[
915 "PORT" +
916 str( i ) ] is not None else ""
917 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800918 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700919 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700920 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800921 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -0700922 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800923 main.log.error( self.name + ": EOF exception found" )
924 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700925 main.cleanup()
926 main.exit()
927 except:
Jon Hall7eb38402015-01-08 17:19:54 -0800928 main.log.info( self.name + ":" * 50 )
kelvin-onlab64b33712015-01-21 15:26:15 -0800929 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800930 main.log.info( ":" * 50 )
Jon Hall6094a362014-04-11 14:46:56 -0700931 main.cleanup()
932 main.exit()
adminbae64d82013-08-01 10:50:15 -0700933
kelvin-onlabd3b64892015-01-20 13:26:24 -0800934 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800935 """
936 Removes the controller target from sw"""
937 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -0700938 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800939 response = self.execute(
940 cmd=command,
941 prompt="mininet>",
942 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800943 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800944 main.log.error( self.name + ": EOF exception found" )
945 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -0700946 main.cleanup()
947 main.exit()
948 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800949 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -0700950
kelvin-onlabd3b64892015-01-20 13:26:24 -0800951 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800952 """
Jon Hallb1290e82014-11-18 16:17:48 -0500953 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -0800954 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -0800955 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -0500956 NOTE: cannot currently specify what type of switch
957 required params:
958 switchname = name of the new switch as a string
959 optional keyvalues:
960 dpid = "dpid"
961 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800962 """
963 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -0800964 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500965 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800966 response = self.execute(
967 cmd=command,
968 prompt="mininet>",
969 timeout=10 )
970 if re.search( "already exists!", response ):
971 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500972 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800973 elif re.search( "Error", response ):
974 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500975 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800976 elif re.search( "usage:", response ):
977 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500978 return main.FALSE
979 else:
980 return main.TRUE
981 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800982 main.log.error( self.name + ": EOF exception found" )
983 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500984 main.cleanup()
985 main.exit()
986
kelvin-onlabd3b64892015-01-20 13:26:24 -0800987 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800988 """
Jon Hallbe6dfc42015-01-12 17:37:25 -0800989 delete a switch from the mininet topology
990 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -0800991 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -0800992 required params:
Jon Hallb1290e82014-11-18 16:17:48 -0500993 switchname = name of the switch as a string
Jon Hallbe6dfc42015-01-12 17:37:25 -0800994 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800995 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500996 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800997 response = self.execute(
998 cmd=command,
999 prompt="mininet>",
1000 timeout=10 )
1001 if re.search( "no switch named", response ):
1002 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001003 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001004 elif re.search( "Error", response ):
1005 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001006 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001007 elif re.search( "usage:", response ):
1008 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001009 return main.FALSE
1010 else:
1011 return main.TRUE
1012 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001013 main.log.error( self.name + ": EOF exception found" )
1014 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001015 main.cleanup()
1016 main.exit()
1017
kelvin-onlabd3b64892015-01-20 13:26:24 -08001018 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001019 """
1020 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001021 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001022 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001023 NOTE: cannot currently specify what type of link
1024 required params:
1025 node1 = the string node name of the first endpoint of the link
1026 node2 = the string node name of the second endpoint of the link
1027 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001028 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001029 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001030 response = self.execute(
1031 cmd=command,
1032 prompt="mininet>",
1033 timeout=10 )
1034 if re.search( "doesnt exist!", response ):
1035 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001036 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001037 elif re.search( "Error", response ):
1038 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001039 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001040 elif re.search( "usage:", response ):
1041 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001042 return main.FALSE
1043 else:
1044 return main.TRUE
1045 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001046 main.log.error( self.name + ": EOF exception found" )
1047 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001048 main.cleanup()
1049 main.exit()
1050
kelvin-onlabd3b64892015-01-20 13:26:24 -08001051 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001052 """
1053 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001054 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001055 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001056 required params:
1057 node1 = the string node name of the first endpoint of the link
1058 node2 = the string node name of the second endpoint of the link
1059 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001060 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001061 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001062 response = self.execute(
1063 cmd=command,
1064 prompt="mininet>",
1065 timeout=10 )
1066 if re.search( "no node named", response ):
1067 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001068 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001069 elif re.search( "Error", response ):
1070 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001071 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001072 elif re.search( "usage:", response ):
1073 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001074 return main.FALSE
1075 else:
1076 return main.TRUE
1077 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001078 main.log.error( self.name + ": EOF exception found" )
1079 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001080 main.cleanup()
1081 main.exit()
1082
kelvin-onlabd3b64892015-01-20 13:26:24 -08001083 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001084 """
Jon Hallb1290e82014-11-18 16:17:48 -05001085 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001086 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001087 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001088 NOTE: cannot currently specify what type of host
1089 required params:
1090 hostname = the string hostname
1091 optional key-value params
1092 switch = "switch name"
1093 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001094 """
1095 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001096 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001097 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001098 response = self.execute(
1099 cmd=command,
1100 prompt="mininet>",
1101 timeout=10 )
1102 if re.search( "already exists!", response ):
1103 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001104 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001105 elif re.search( "doesnt exists!", response ):
1106 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001107 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001108 elif re.search( "Error", response ):
1109 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001110 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001111 elif re.search( "usage:", response ):
1112 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001113 return main.FALSE
1114 else:
1115 return main.TRUE
1116 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001117 main.log.error( self.name + ": EOF exception found" )
1118 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001119 main.cleanup()
1120 main.exit()
1121
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001123 """
1124 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001125 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001126 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001127 NOTE: this uses a custom mn function
1128 required params:
1129 hostname = the string hostname
1130 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001131 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001132 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001133 response = self.execute(
1134 cmd=command,
1135 prompt="mininet>",
1136 timeout=10 )
1137 if re.search( "no host named", response ):
1138 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001139 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001140 elif re.search( "Error", response ):
1141 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001142 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001143 elif re.search( "usage:", response ):
1144 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001145 return main.FALSE
1146 else:
1147 return main.TRUE
1148 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001149 main.log.error( self.name + ": EOF exception found" )
1150 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001151 main.cleanup()
1152 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001153
Jon Hall7eb38402015-01-08 17:19:54 -08001154 def disconnect( self ):
kelvin-onlaba0ce3222015-01-27 17:25:15 -08001155 """
1156 Called at the end of the test to disconnect the handle.
1157 """
1158 self.handle.sendline('')
1159 i = 1
1160 i = self.handle.expect( ['mininet>',pexpect.EOF,pexpect.TIMEOUT ], timeout = 2)
1161 if i == 0:
1162 self.stopNet()
1163 response = ''
1164 # print "Disconnecting Mininet"
1165 if self.handle:
1166 self.handle.sendline( "exit" )
1167 self.handle.expect( "exit" )
1168 self.handle.expect( "(.*)" )
1169 main.log.info( "Mininet CLI is successfully disconnected" )
1170 response = self.handle.before
1171 else:
1172 main.log.error( "Connection failed to the host" )
1173 response = main.FALSE
1174
1175 return response
1176
1177 def stopNet( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001178 main.log.info( self.name + ": Disconnecting mininet..." )
adminbae64d82013-08-01 10:50:15 -07001179 response = ''
1180 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001181 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001182 response = self.execute(
1183 cmd="exit",
1184 prompt="(.*)",
1185 timeout=120 )
kelvin-onlaba0ce3222015-01-27 17:25:15 -08001186 main.log.info( self.name + ": Disconnected")
Jon Hall7eb38402015-01-08 17:19:54 -08001187 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001188 response = main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001189 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001190 main.log.error( self.name + ": EOF exception found" )
1191 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001192 main.cleanup()
1193 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001194 else:
1195 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001196 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001197 return response
1198
kelvin-onlaba0ce3222015-01-27 17:25:15 -08001199
1200
Jon Hall7eb38402015-01-08 17:19:54 -08001201 def arping( self, src, dest, destmac ):
1202 self.handle.sendline( '' )
1203 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001204
Jon Hall7eb38402015-01-08 17:19:54 -08001205 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001206 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001207 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1208 main.log.info( self.name + ": ARP successful" )
1209 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001210 return main.TRUE
1211 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001212 main.log.warn( self.name + ": ARP FAILURE" )
1213 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001214 return main.FALSE
1215
Jon Hall7eb38402015-01-08 17:19:54 -08001216 def decToHex( self, num ):
1217 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001218
Jon Hall7eb38402015-01-08 17:19:54 -08001219 def getSwitchFlowCount( self, switch ):
1220 """
1221 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001222 if self.handle:
1223 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1224 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001225 response = self.execute(
1226 cmd=cmd,
1227 prompt="mininet>",
1228 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001229 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001230 main.log.error( self.name + ": EOF exception found" )
1231 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001232 main.cleanup()
1233 main.exit()
1234 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001235 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001236 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001237 main.log.info(
1238 "Couldn't find flows on switch %s, found: %s" %
1239 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001240 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001241 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001242 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001243 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001244
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 def checkFlows( self, sw, dumpFormat=None ):
1246 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001247 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001249 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001250 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001251 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001252 response = self.execute(
1253 cmd=command,
1254 prompt="mininet>",
1255 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001256 return response
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 )
admin2a9548d2014-06-17 14:08:07 -07001260 main.cleanup()
1261 main.exit()
1262 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001263 main.log.info( response )
admin2a9548d2014-06-17 14:08:07 -07001264
kelvin-onlabd3b64892015-01-20 13:26:24 -08001265 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001266 """
1267 Runs tpdump on an intferface and saves the file
1268 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001269 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001270 self.handle.sendline( "" )
1271 self.handle.expect( "mininet>" )
1272 self.handle.sendline(
1273 "sh sudo tcpdump -n -i " +
1274 intf +
1275 " " +
1276 port +
1277 " -w " +
1278 filename.strip() +
1279 " &" )
1280 self.handle.sendline( "" )
1281 i = self.handle.expect( [ 'No\ssuch\device',
1282 'listening\son',
1283 pexpect.TIMEOUT,
1284 "mininet>" ],
1285 timeout=10 )
1286 main.log.warn( self.handle.before + self.handle.after )
1287 self.handle.sendline( "" )
1288 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001289 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001290 main.log.error(
1291 self.name +
1292 ": tcpdump - No such device exists. " +
1293 "tcpdump attempted on: " +
1294 intf )
admin2a9548d2014-06-17 14:08:07 -07001295 return main.FALSE
1296 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001297 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001298 return main.TRUE
1299 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001300 main.log.error(
1301 self.name +
1302 ": tcpdump command timed out! Check interface name," +
1303 " given interface was: " +
1304 intf )
admin2a9548d2014-06-17 14:08:07 -07001305 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001306 elif i == 3:
1307 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001308 return main.TRUE
1309 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001310 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001311 return main.FALSE
1312 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001313 main.log.error( self.name + ": EOF exception found" )
1314 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001315 main.cleanup()
1316 main.exit()
1317 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001318 main.log.info( self.name + ":" * 50 )
kelvin-onlab64b33712015-01-21 15:26:15 -08001319 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001320 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001321 main.cleanup()
1322 main.exit()
1323
kelvin-onlabd3b64892015-01-20 13:26:24 -08001324 def stopTcpdump( self ):
admin2a9548d2014-06-17 14:08:07 -07001325 "pkills tcpdump"
1326 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001327 self.handle.sendline( "sh sudo pkill tcpdump" )
1328 self.handle.expect( "mininet>" )
1329 self.handle.sendline( "" )
1330 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001331 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001332 main.log.error( self.name + ": EOF exception found" )
1333 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001334 main.cleanup()
1335 main.exit()
1336 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001337 main.log.info( self.name + ":" * 50 )
kelvin-onlab64b33712015-01-21 15:26:15 -08001338 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001339 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001340 main.cleanup()
1341 main.exit()
1342
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001344 """
1345 Compare mn and onos switches
1346 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001347 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001348
Jon Hall7eb38402015-01-08 17:19:54 -08001349 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001350 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001351 output = { "switches": [] }
1352 # iterate through the MN topology and pull out switches and and port
1353 # info
1354 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001355 ports = []
1356 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001357 ports.append( { 'of_port': port.port_no,
1358 'mac': str( port.hw_addr ).replace( '\'',
kelvin-onlabd3b64892015-01-20 13:26:24 -08001359 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001360 'name': port.name } )
1361 output[ 'switches' ].append( {
1362 "name": switch.name,
1363 "dpid": str( switch.dpid ).zfill( 16 ),
1364 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001365
Jon Hall7eb38402015-01-08 17:19:54 -08001366 # print "mn"
1367 # print json.dumps( output,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001368 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001369 # indent=4,
1370 # separators=( ',', ': ' ) )
1371 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001372 # print json.dumps( switchesJson,
1373 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001374 # indent=4,
1375 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001376
1377 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001378 mnDPIDs = []
1379 for switch in output[ 'switches' ]:
1380 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001381 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001382 # print "List of Mininet switch DPID's"
1383 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001384 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001385 main.log.error(
1386 self.name +
1387 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001388 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001390 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001391 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001392 if switch[ 'available' ]:
1393 onosDPIDs.append(
1394 switch[ 'id' ].replace(
1395 ":",
1396 '' ).replace(
1397 "of",
1398 '' ).lower() )
1399 # else:
1400 # print "Switch is unavailable:"
1401 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001402 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001403 # print "List of ONOS switch DPID's"
1404 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001405
Jon Hall7eb38402015-01-08 17:19:54 -08001406 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001407 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001408 main.log.report( "Switches in MN but not in ONOS:" )
1409 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1410 main.log.report( str( list1 ) )
1411 main.log.report( "Switches in ONOS but not in MN:" )
1412 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001413 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001414 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001415 switchResults = main.TRUE
1416 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001417
kelvin-onlabd3b64892015-01-20 13:26:24 -08001418 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001419 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001420 Compare mn and onos ports
1421 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001422 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001423
Jon Hallfbc828e2015-01-06 17:30:19 -08001424 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001425 1. This uses the sts TestONTopology object
1426 2. numpy - "sudo pip install numpy"
1427
Jon Hall7eb38402015-01-08 17:19:54 -08001428 """
1429 # FIXME: this does not look for extra ports in ONOS, only checks that
1430 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001431 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001433 output = { "switches": [] }
1434 # iterate through the MN topology and pull out switches and and port
1435 # info
1436 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001437 ports = []
1438 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001439 # print port.hw_addr.toStr( separator='' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001440 tmpPort = {}
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001441 tmpPort[ 'of_port' ] = port.port_no
1442 tmpPort[ 'mac' ] = str( port.hw_addr ).replace( '\'', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 tmpPort[ 'name' ] = port.name
1444 tmpPort[ 'enabled' ] = port.enabled
Jon Hall39f29df2014-11-04 19:30:21 -05001445
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 ports.append( tmpPort )
1447 tmpSwitch = {}
1448 tmpSwitch[ 'name' ] = switch.name
1449 tmpSwitch[ 'dpid' ] = str( switch.dpid ).zfill( 16 )
1450 tmpSwitch[ 'ports' ] = ports
Jon Hall39f29df2014-11-04 19:30:21 -05001451
kelvin-onlabd3b64892015-01-20 13:26:24 -08001452 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001453
Jon Hall7eb38402015-01-08 17:19:54 -08001454 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001455 for mnSwitch in output[ 'switches' ]:
1456 mnPorts = []
1457 onosPorts = []
1458 switchResult = main.TRUE
1459 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001460 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001461 mnPorts.append( port[ 'of_port' ] )
1462 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001463 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001464 # print onosSwitch
1465 if onosSwitch[ 'device' ][ 'available' ]:
1466 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001467 ':',
1468 '' ).replace(
1469 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001470 '' ) == mnSwitch[ 'dpid' ]:
1471 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001472 if port[ 'isEnabled' ]:
1473 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001474 # onosPorts.append( 'local' )
1475 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001476 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001477 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001478 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001479 mnPorts.sort( key=float )
1480 onosPorts.sort( key=float )
1481 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1482 # print "\tmn_ports[] = ", mnPorts
1483 # print "\tonos_ports[] = ", onosPorts
1484 mnPortsLog = mnPorts
1485 onosPortsLog = onosPorts
1486 mnPorts = [ x for x in mnPorts ]
1487 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001488
Jon Hall7eb38402015-01-08 17:19:54 -08001489 # TODO: handle other reserved port numbers besides LOCAL
1490 # NOTE: Reserved ports
1491 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1492 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 for mnPort in mnPortsLog:
1494 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001495 # don't set results to true here as this is just one of
1496 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001497 mnPorts.remove( mnPort )
1498 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001499 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001500 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001501 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001502 if 65534 in mnPorts:
1503 mnPorts.remove( 65534 )
1504 if long( uint64( -2 ) ) in onosPorts:
1505 onosPorts.remove( long( uint64( -2 ) ) )
1506 if len( mnPorts ): # the ports of this switch don't match
1507 switchResult = main.FALSE
1508 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1509 if len( onosPorts ): # the ports of this switch don't match
1510 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001511 main.log.warn(
1512 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001513 str( onosPorts ) )
1514 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001515 main.log.report(
1516 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1518 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1519 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1520 portsResults = portsResults and switchResult
1521 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001522
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001524 """
1525 Compare mn and onos links
1526 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001527 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001528
Jon Hall7eb38402015-01-08 17:19:54 -08001529 This uses the sts TestONTopology object"""
1530 # FIXME: this does not look for extra links in ONOS, only checks that
1531 # ONOS has what is in MN
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 linkResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001533 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001535 # iterate through the MN topology and pull out switches and and port
1536 # info
1537 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001538 # print "Iterating though switches as seen by Mininet"
1539 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001540 ports = []
1541 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001542 # print port.hw_addr.toStr( separator='' )
1543 ports.append( { 'of_port': port.port_no,
1544 'mac': str( port.hw_addr ).replace( '\'',
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001546 'name': port.name } )
1547 output[ 'switches' ].append( {
1548 "name": switch.name,
1549 "dpid": str( switch.dpid ).zfill( 16 ),
1550 "ports": ports } )
1551 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001552
kelvin-onlabd3b64892015-01-20 13:26:24 -08001553 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001554 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001555 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001556 if 2 * len( mnLinks ) == len( onos ):
1557 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001558 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001559 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001560 main.log.report(
1561 "Mininet has %i bidirectional links and " +
1562 "ONOS has %i unidirectional links" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001563 ( len( mnLinks ), len( onos ) ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001564
Jon Hall7eb38402015-01-08 17:19:54 -08001565 # iterate through MN links and check if an ONOS link exists in
1566 # both directions
1567 # NOTE: Will currently only show mn links as down if they are
1568 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001569 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001570 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001572 # print "Link: %s" % link
1573 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001574 node1 = None
1575 port1 = None
1576 node2 = None
1577 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 firstDir = main.FALSE
1579 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001580 for switch in output[ 'switches' ]:
1581 # print "Switch: %s" % switch[ 'name' ]
1582 if switch[ 'name' ] == link.node1.name:
1583 node1 = switch[ 'dpid' ]
1584 for port in switch[ 'ports' ]:
1585 if str( port[ 'name' ] ) == str( link.port1 ):
1586 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001587 if node1 is not None and node2 is not None:
1588 break
Jon Hall7eb38402015-01-08 17:19:54 -08001589 if switch[ 'name' ] == link.node2.name:
1590 node2 = switch[ 'dpid' ]
1591 for port in switch[ 'ports' ]:
1592 if str( port[ 'name' ] ) == str( link.port2 ):
1593 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001594 if node1 is not None and node2 is not None:
1595 break
1596
kelvin-onlabd3b64892015-01-20 13:26:24 -08001597 for onosLink in onos:
1598 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001599 ":",
1600 '' ).replace(
1601 "of",
1602 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001603 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001604 ":",
1605 '' ).replace(
1606 "of",
1607 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001608 onosPort1 = onosLink[ 'src' ][ 'port' ]
1609 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001610
Jon Hall72cf1dc2014-10-20 21:04:50 -04001611 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001612 if str( onosNode1 ) == str( node1 ) and str(
1613 onosNode2 ) == str( node2 ):
1614 if int( onosPort1 ) == int( port1 ) and int(
1615 onosPort2 ) == int( port2 ):
1616 firstDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001617 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001618 main.log.warn(
1619 'The port numbers do not match for ' +
1620 str( link ) +
1621 ' between ONOS and MN. When cheking ONOS for ' +
1622 'link %s/%s -> %s/%s' %
1623 ( node1,
1624 port1,
1625 node2,
1626 port2 ) +
1627 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001628 ( onosNode1,
1629 onosPort1,
1630 onosNode2,
1631 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001632
1633 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001634 elif ( str( onosNode1 ) == str( node2 ) and
1635 str( onosNode2 ) == str( node1 ) ):
1636 if ( int( onosPort1 ) == int( port2 )
1637 and int( onosPort2 ) == int( port1 ) ):
1638 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001639 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001640 main.log.warn(
1641 'The port numbers do not match for ' +
1642 str( link ) +
1643 ' between ONOS and MN. When cheking ONOS for ' +
1644 'link %s/%s -> %s/%s' %
1645 ( node2,
1646 port2,
1647 node1,
1648 port1 ) +
1649 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001650 ( onosNode2,
1651 onosPort2,
1652 onosNode1,
1653 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001654 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001655 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001656 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001657 main.log.report(
1658 'ONOS does not have the link %s/%s -> %s/%s' %
1659 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001660 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001661 main.log.report(
1662 'ONOS does not have the link %s/%s -> %s/%s' %
1663 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001664 linkResults = linkResults and firstDir and secondDir
1665 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001666
kelvin-onlabd3b64892015-01-20 13:26:24 -08001667 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001668 """
1669 Returns a list of all hosts
1670 Don't ask questions just use it"""
1671 self.handle.sendline( "" )
1672 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001673
Jon Hall7eb38402015-01-08 17:19:54 -08001674 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1675 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001676
kelvin-onlabd3b64892015-01-20 13:26:24 -08001677 handlePy = self.handle.before
1678 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1679 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001680
Jon Hall7eb38402015-01-08 17:19:54 -08001681 self.handle.sendline( "" )
1682 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001683
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 hostStr = handlePy.replace( "]", "" )
1685 hostStr = hostStr.replace( "'", "" )
1686 hostStr = hostStr.replace( "[", "" )
1687 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001688
kelvin-onlabd3b64892015-01-20 13:26:24 -08001689 return hostList
adminbae64d82013-08-01 10:50:15 -07001690
Jon Hall7eb38402015-01-08 17:19:54 -08001691 def update( self ):
1692 """
1693 updates the port address and status information for
1694 each port in mn"""
1695 # TODO: Add error checking. currently the mininet command has no output
1696 main.log.info( "Updateing MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001697 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001698 self.handle.sendline( "" )
1699 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001700
Jon Hall7eb38402015-01-08 17:19:54 -08001701 self.handle.sendline( "update" )
1702 self.handle.expect( "update" )
1703 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001704
Jon Hall7eb38402015-01-08 17:19:54 -08001705 self.handle.sendline( "" )
1706 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001707
Jon Hallb1290e82014-11-18 16:17:48 -05001708 return main.TRUE
1709 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001710 main.log.error( self.name + ": EOF exception found" )
1711 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001712 main.cleanup()
1713 main.exit()
1714
adminbae64d82013-08-01 10:50:15 -07001715if __name__ != "__main__":
1716 import sys
Jon Hall7eb38402015-01-08 17:19:54 -08001717 sys.modules[ __name__ ] = MininetCliDriver()