blob: 7032dff690a68be415f9c4583017a3e6618f3a7d [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."""
adminbae64d82013-08-01 10:50:15 -070062 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -080063 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080064
Jon Hall7eb38402015-01-08 17:19:54 -080065 self.name = self.options[ 'name' ]
66 self.handle = super(
67 MininetCliDriver,
68 self ).connect(
kelvin-onlabd3b64892015-01-20 13:26:24 -080069 userName=self.userName,
70 ipAddress=self.ipAddress,
Jon Hall7eb38402015-01-08 17:19:54 -080071 port=None,
72 pwd=self.pwd )
Jon Hallfbc828e2015-01-06 17:30:19 -080073
kelvin-onlabd3b64892015-01-20 13:26:24 -080074 self.sshHandle = self.handle
Jon Hallfbc828e2015-01-06 17:30:19 -080075
Jon Hall7eb38402015-01-08 17:19:54 -080076 if self.handle:
77 main.log.info(
78 self.name +
79 ": Clearing any residual state or processes" )
80 self.handle.sendline( "sudo mn -c" )
81 i = self.handle.expect( [ 'password\sfor\s',
82 'Cleanup\scomplete',
83 pexpect.EOF,
84 pexpect.TIMEOUT ],
85 120 )
86 if i == 0:
87 main.log.info( self.name + ": Sending sudo password" )
88 self.handle.sendline( self.pwd )
89 i = self.handle.expect( [ '%s:' % ( self.user ),
90 '\$',
91 pexpect.EOF,
92 pexpect.TIMEOUT ],
93 120 )
94 if i == 1:
95 main.log.info( self.name + ": Clean" )
96 elif i == 2:
97 main.log.error( self.name + ": Connection terminated" )
98 elif i == 3: # timeout
99 main.log.error(
100 self.name +
101 ": Something while cleaning MN took too long... " )
Jon Hallfbc828e2015-01-06 17:30:19 -0800102
Jon Hall7eb38402015-01-08 17:19:54 -0800103 main.log.info( self.name + ": building fresh mininet" )
104 # for reactive/PARP enabled tests
105 cmdString = "sudo mn " + self.options[ 'arg1' ] +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800106 " " + self.options[ 'arg2' ] +\
107 " --mac --controller " +\
108 self.options[ 'controller' ] + " " +\
109 self.options[ 'arg3' ]
Jon Hallfbc828e2015-01-06 17:30:19 -0800110
Jon Hall7eb38402015-01-08 17:19:54 -0800111 argList = self.options[ 'arg1' ].split( "," )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400112 global topoArgList
Jon Hall7eb38402015-01-08 17:19:54 -0800113 topoArgList = argList[ 0 ].split( " " )
114 argList = map( int, argList[ 1: ] )
115 topoArgList = topoArgList[ 1: ] + argList
Jon Hallfbc828e2015-01-06 17:30:19 -0800116
Jon Hall7eb38402015-01-08 17:19:54 -0800117 self.handle.sendline( cmdString )
118 self.handle.expect( [ "sudo mn", pexpect.EOF, pexpect.TIMEOUT ] )
119 while True:
120 i = self.handle.expect( [ 'mininet>',
121 '\*\*\*',
122 'Exception',
123 pexpect.EOF,
124 pexpect.TIMEOUT ],
125 300 )
126 if i == 0:
127 main.log.info( self.name + ": mininet built" )
adminbae64d82013-08-01 10:50:15 -0700128 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800129 if i == 1:
130 self.handle.expect(
131 [ "\n", pexpect.EOF, pexpect.TIMEOUT ] )
132 main.log.info( self.handle.before )
133 elif i == 2:
134 main.log.error(
135 self.name +
136 ": Launching mininet failed..." )
adminbae64d82013-08-01 10:50:15 -0700137 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800138 elif i == 3:
139 main.log.error( self.name + ": Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700140 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800141 elif i == 4: # timeout
142 main.log.error(
143 self.name +
144 ": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -0700145 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700146 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800147 else: # if no handle
148 main.log.error(
149 self.name +
150 ": Connection failed to the host " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 self.userName +
Jon Hall7eb38402015-01-08 17:19:54 -0800152 "@" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800153 self.ipAddress )
Jon Hall7eb38402015-01-08 17:19:54 -0800154 main.log.error( self.name + ": Failed to connect to the Mininet" )
adminbae64d82013-08-01 10:50:15 -0700155 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800156
kelvin-onlabfccaafa2015-01-20 13:50:44 -0800157 def numSwitchesNlinks( self, topoType, depth, fanout ):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400158 if topoType == 'tree':
Jon Hall7eb38402015-01-08 17:19:54 -0800159 # In tree topology, if fanout arg is not given, by default it is 2
160 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400161 fanout = 2
162 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500163 count = 0
Jon Hall7eb38402015-01-08 17:19:54 -0800164 while( k <= depth - 1 ):
165 count = count + pow( fanout, k )
166 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800167 numSwitches = count
Jon Hall7eb38402015-01-08 17:19:54 -0800168 while( k <= depth - 2 ):
169 # depth-2 gives you only core links and not considering
170 # edge links as seen by ONOS. If all the links including
171 # edge links are required, do depth-1
172 count = count + pow( fanout, k )
173 k = k + 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800174 numLinks = count * fanout
Jon Hall7eb38402015-01-08 17:19:54 -0800175 # print "num_switches for %s(%d,%d) = %d and links=%d" %(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 # topoType,depth,fanout,numSwitches,numLinks )
Jon Hallfbc828e2015-01-06 17:30:19 -0800177
Jon Hall7eb38402015-01-08 17:19:54 -0800178 elif topoType == 'linear':
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 # In linear topology, if fanout or numHostsPerSw is not given,
Jon Hall7eb38402015-01-08 17:19:54 -0800180 # by default it is 1
181 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400182 fanout = 1
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 numSwitches = depth
184 numHostsPerSw = fanout
185 totalNumHosts = numSwitches * numHostsPerSw
186 numLinks = totalNumHosts + ( numSwitches - 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800187 print "num_switches for %s(%d,%d) = %d and links=%d" %\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 ( topoType, depth, fanout, numSwitches, numLinks )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400189 topoDict = {}
Jon Hall7eb38402015-01-08 17:19:54 -0800190 topoDict = {
kelvin-onlabd3b64892015-01-20 13:26:24 -0800191 "num_switches": int( numSwitches ),
192 "num_corelinks": int( numLinks ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400193 return topoDict
194
kelvin-onlabd3b64892015-01-20 13:26:24 -0800195 def calculateSwAndLinks( self ):
196 topoDict = self.numSwitchesN_links( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400197 return topoDict
198
Jon Hall7eb38402015-01-08 17:19:54 -0800199 def pingall( self, timeout=300 ):
200 """
201 Verifies the reachability of the hosts using pingall command.
202 Optional parameter timeout allows you to specify how long to
203 wait for pingall to complete
204 Returns:
205 main.TRUE if pingall completes with no pings dropped
206 otherwise main.FALSE"""
207 if self.handle:
208 main.log.info(
209 self.name +
210 ": Checking reachabilty to the hosts using pingall" )
Jon Hall6094a362014-04-11 14:46:56 -0700211 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800212 response = self.execute(
213 cmd="pingall",
214 prompt="mininet>",
215 timeout=int( timeout ) )
Jon Hallb1290e82014-11-18 16:17:48 -0500216 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800217 main.log.error( self.name + ": EOF exception found" )
218 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500219 main.cleanup()
220 main.exit()
221 except pexpect.TIMEOUT:
Jon Hall7eb38402015-01-08 17:19:54 -0800222 # We may not want to kill the test if pexpect times out
223 main.log.error( self.name + ": TIMEOUT exception found" )
224 main.log.error( self.name +
225 ": " +
226 str( self.handle.before ) )
227 # NOTE: mininet's pingall rounds, so we will check the number of
228 # passed and number of failed
229 pattern = "Results\:\s0\%\sdropped\s\(" +\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 "(?P<passed>[\d]+)/(?P=passed)"
Jon Hall7eb38402015-01-08 17:19:54 -0800231 if re.search( pattern, response ):
232 main.log.info( self.name + ": All hosts are reachable" )
adminbae64d82013-08-01 10:50:15 -0700233 return main.TRUE
234 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800235 main.log.error( self.name + ": Unable to reach all the hosts" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 main.log.info( "Pingall output: " + str( response ) )
Jon Hall7eb38402015-01-08 17:19:54 -0800237 # NOTE: Send ctrl-c to make sure pingall is done
238 self.handle.send( "\x03" )
239 self.handle.expect( "Interrupt" )
240 self.handle.expect( "mininet>" )
adminbae64d82013-08-01 10:50:15 -0700241 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800242 else:
243 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallb1290e82014-11-18 16:17:48 -0500244 main.cleanup()
245 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700246
Jon Hall7eb38402015-01-08 17:19:54 -0800247 def fpingHost( self, **pingParams ):
248 """
249 Uses the fping package for faster pinging...
250 *requires fping to be installed on machine running mininet"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800251 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800252 command = args[ "SRC" ] + \
253 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
254 self.handle.sendline( command )
255 self.handle.expect(
256 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
257 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
258 response = self.handle.before
259 if re.search( ":\s-", response ):
260 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700261 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800262 elif re.search( ":\s\d{1,2}\.\d\d", response ):
263 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700264 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800265 main.log.info( self.name + ": Install fping on mininet machine... " )
266 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700267 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800268
Jon Hall7eb38402015-01-08 17:19:54 -0800269 def pingHost( self, **pingParams ):
270 """
271 Ping from one mininet host to another
272 Currently the only supported Params: SRC and TARGET"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800273 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
Jon Hall7eb38402015-01-08 17:19:54 -0800274 command = args[ "SRC" ] + " ping " + \
275 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700276 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800277 main.log.warn( "Sending: " + command )
278 self.handle.sendline( command )
279 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700280 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800281 main.log.error(
282 self.name +
283 ": timeout when waiting for response from mininet" )
284 main.log.error( "response: " + str( self.handle.before ) )
285 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700286 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800287 main.log.error(
288 self.name +
289 ": timeout when waiting for response from mininet" )
290 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700291 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800292 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800293 main.log.error( self.name + ": EOF exception found" )
294 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700295 main.cleanup()
296 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800297 main.log.info( self.name + ": Ping Response: " + response )
298 if re.search( ',\s0\%\spacket\sloss', response ):
299 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800300 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700301 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800302 else:
303 main.log.error(
304 self.name +
305 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800306 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700307 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800308
Jon Hall7eb38402015-01-08 17:19:54 -0800309 def checkIP( self, host ):
310 """
311 Verifies the host's ip configured or not."""
312 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700313 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800314 response = self.execute(
315 cmd=host +
316 " ifconfig",
317 prompt="mininet>",
318 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800319 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800320 main.log.error( self.name + ": EOF exception found" )
321 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700322 main.cleanup()
323 main.exit()
adminbae64d82013-08-01 10:50:15 -0700324
Jon Hall7eb38402015-01-08 17:19:54 -0800325 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800326 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
327 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
328 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
329 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
330 "[0-9]|25[0-5]|[0-9]{1,2})"
Jon Hall7eb38402015-01-08 17:19:54 -0800331 # pattern = "inet addr:10.0.0.6"
332 if re.search( pattern, response ):
333 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700334 return main.TRUE
335 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800336 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700337 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800338 else:
339 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800340
Jon Hall7eb38402015-01-08 17:19:54 -0800341 def verifySSH( self, **connectargs ):
Jon Hall6094a362014-04-11 14:46:56 -0700342 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800343 response = self.execute(
344 cmd="h1 /usr/sbin/sshd -D&",
345 prompt="mininet>",
346 timeout=10 )
347 response = self.execute(
348 cmd="h4 /usr/sbin/sshd -D&",
349 prompt="mininet>",
350 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700351 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800352 vars( self )[ key ] = connectargs[ key ]
353 response = self.execute(
354 cmd="xterm h1 h4 ",
355 prompt="mininet>",
356 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800357 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800358 main.log.error( self.name + ": EOF exception found" )
359 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700360 main.cleanup()
361 main.exit()
adminbae64d82013-08-01 10:50:15 -0700362 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800363 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700364 if self.flag == 0:
365 self.flag = 1
366 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800367 else:
adminbae64d82013-08-01 10:50:15 -0700368 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800369
Jon Hall7eb38402015-01-08 17:19:54 -0800370 def changeIP( self, host, intf, newIP, newNetmask ):
371 """
372 Changes the ip address of a host on the fly
373 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800374 if self.handle:
375 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800376 cmd = host + " ifconfig " + intf + " " + \
377 newIP + " " + 'netmask' + " " + newNetmask
378 self.handle.sendline( cmd )
379 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800380 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800381 main.log.info( "response = " + response )
382 main.log.info(
383 "Ip of host " +
384 host +
385 " changed to new IP " +
386 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800387 return main.TRUE
388 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800389 main.log.error( self.name + ": EOF exception found" )
390 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800391 return main.FALSE
392
Jon Hall7eb38402015-01-08 17:19:54 -0800393 def changeDefaultGateway( self, host, newGW ):
394 """
395 Changes the default gateway of a host
396 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800397 if self.handle:
398 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800399 cmd = host + " route add default gw " + newGW
400 self.handle.sendline( cmd )
401 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800402 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800403 main.log.info( "response = " + response )
404 main.log.info(
405 "Default gateway of host " +
406 host +
407 " changed to " +
408 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800409 return main.TRUE
410 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800411 main.log.error( self.name + ": EOF exception found" )
412 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800413 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800414
Jon Hall7eb38402015-01-08 17:19:54 -0800415 def addStaticMACAddress( self, host, GW, macaddr ):
416 """
417 Changes the mac address of a geateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800418 if self.handle:
419 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800420 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
421 cmd = host + " arp -s " + GW + " " + macaddr
422 self.handle.sendline( cmd )
423 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800424 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800425 main.log.info( "response = " + response )
426 main.log.info(
427 "Mac adrress of gateway " +
428 GW +
429 " changed to " +
430 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -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 )
shahshreyad0c80432014-12-04 16:56:05 -0800435 return main.FALSE
436
Jon Hall7eb38402015-01-08 17:19:54 -0800437 def verifyStaticGWandMAC( self, host ):
438 """
439 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800440 if self.handle:
441 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800442 # h1 arp -an
443 cmd = host + " arp -an "
444 self.handle.sendline( cmd )
445 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800446 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800447 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800448 return main.TRUE
449 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 )
shahshreyad0c80432014-12-04 16:56:05 -0800452 return main.FALSE
453
Jon Hall7eb38402015-01-08 17:19:54 -0800454 def getMacAddress( self, host ):
455 """
456 Verifies the host's ip configured or not."""
457 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700458 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800459 response = self.execute(
460 cmd=host +
461 " ifconfig",
462 prompt="mininet>",
463 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800464 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800465 main.log.error( self.name + ": EOF exception found" )
466 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700467 main.cleanup()
468 main.exit()
adminbae64d82013-08-01 10:50:15 -0700469
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700470 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800471 macAddressSearch = re.search( pattern, response, re.I )
472 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800473 main.log.info(
474 self.name +
475 ": Mac-Address of Host " +
476 host +
477 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800478 macAddress )
479 return macAddress
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700480 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800481 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700482
Jon Hall7eb38402015-01-08 17:19:54 -0800483 def getInterfaceMACAddress( self, host, interface ):
484 """
485 Return the IP address of the interface on the given host"""
486 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700487 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800488 response = self.execute( cmd=host + " ifconfig " + interface,
489 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800490 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800491 main.log.error( self.name + ": EOF exception found" )
492 main.log.error( self.name + ": " + self.handle.before )
493 main.cleanup()
494 main.exit()
495
496 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800497 macAddressSearch = re.search( pattern, response, re.I )
498 if macAddressSearch is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800499 main.log.info( "No mac address found in %s" % response )
500 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -0800501 macAddress = macAddressSearch.group().split( " " )[ 1 ]
Jon Hall7eb38402015-01-08 17:19:54 -0800502 main.log.info(
503 "Mac-Address of " +
504 host +
505 ":" +
506 interface +
507 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800508 macAddress )
509 return macAddress
Jon Hall7eb38402015-01-08 17:19:54 -0800510 else:
511 main.log.error( "Connection failed to the host" )
512
513 def getIPAddress( self, host ):
514 """
515 Verifies the host's ip configured or not."""
516 if self.handle:
517 try:
518 response = self.execute(
519 cmd=host +
520 " ifconfig",
521 prompt="mininet>",
522 timeout=10 )
523 except pexpect.EOF:
524 main.log.error( self.name + ": EOF exception found" )
525 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700526 main.cleanup()
527 main.exit()
adminbae64d82013-08-01 10:50:15 -0700528
529 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800530 ipAddressSearch = re.search( pattern, response )
Jon Hall7eb38402015-01-08 17:19:54 -0800531 main.log.info(
532 self.name +
533 ": IP-Address of Host " +
534 host +
535 " is " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800536 ipAddressSearch.group( 1 ) )
537 return ipAddressSearch.group( 1 )
Jon Hall7eb38402015-01-08 17:19:54 -0800538 else:
539 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800540
Jon Hall7eb38402015-01-08 17:19:54 -0800541 def getSwitchDPID( self, switch ):
542 """
543 return the datapath ID of the switch"""
544 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700545 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700546 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800547 response = self.execute(
548 cmd=cmd,
549 prompt="mininet>",
550 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800551 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800552 main.log.error( self.name + ": EOF exception found" )
553 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700554 main.cleanup()
555 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800556 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800557 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700558 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800559 main.log.info(
560 "Couldn't find DPID for switch %s, found: %s" %
561 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700562 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800563 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700564 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800565 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700566
Jon Hall7eb38402015-01-08 17:19:54 -0800567 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700568 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800569 self.handle.sendline( "" )
570 self.expect( "mininet>" )
571 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700572 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800573 response = self.execute(
574 cmd=cmd,
575 prompt="mininet>",
576 timeout=10 )
577 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700578 response = self.handle.before
579 return response
580 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800581 main.log.error( self.name + ": EOF exception found" )
582 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700583 main.cleanup()
584 main.exit()
585
Jon Hall7eb38402015-01-08 17:19:54 -0800586 def getInterfaces( self, node ):
587 """
588 return information dict about interfaces connected to the node"""
589 if self.handle:
590 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
kelvin-onlabedcff052015-01-16 12:53:55 -0800591 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700592 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700593 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800594 response = self.execute(
595 cmd=cmd,
596 prompt="mininet>",
597 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800598 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800599 main.log.error( self.name + ": EOF exception found" )
600 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700601 main.cleanup()
602 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700603 return response
604 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800605 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700606
Jon Hall7eb38402015-01-08 17:19:54 -0800607 def dump( self ):
608 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700609 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800610 response = self.execute(
611 cmd='dump',
612 prompt='mininet>',
613 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800614 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800615 main.log.error( self.name + ": EOF exception found" )
616 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700617 main.cleanup()
618 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700619 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800620
Jon Hall7eb38402015-01-08 17:19:54 -0800621 def intfs( self ):
622 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700623 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800624 response = self.execute(
625 cmd='intfs',
626 prompt='mininet>',
627 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800628 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800629 main.log.error( self.name + ": EOF exception found" )
630 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700631 main.cleanup()
632 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700633 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800634
Jon Hall7eb38402015-01-08 17:19:54 -0800635 def net( self ):
636 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700637 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800638 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800639 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800640 main.log.error( self.name + ": EOF exception found" )
641 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700642 main.cleanup()
643 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700644 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800645
646 def iperf( self, host1, host2 ):
647 main.log.info(
648 self.name +
649 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700650 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800651 cmd1 = 'iperf ' + host1 + " " + host2
652 self.handle.sendline( cmd1 )
653 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800654 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800655 if re.search( 'Results:', response ):
656 main.log.info( self.name + ": iperf test succssful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800657 return main.TRUE
658 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800659 main.log.error( self.name + ": iperf test failed" )
660 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800661 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800662 main.log.error( self.name + ": EOF exception found" )
663 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800664 main.cleanup()
665 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800666
Jon Hall7eb38402015-01-08 17:19:54 -0800667 def iperfudp( self ):
668 main.log.info(
669 self.name +
670 ": Simple iperf TCP test between two " +
671 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700672 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800673 response = self.execute(
674 cmd='iperfudp',
675 prompt='mininet>',
676 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800677 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800678 main.log.error( self.name + ": EOF exception found" )
679 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700680 main.cleanup()
681 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700682 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800683
Jon Hall7eb38402015-01-08 17:19:54 -0800684 def nodes( self ):
685 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700686 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800687 response = self.execute(
688 cmd='nodes',
689 prompt='mininet>',
690 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800691 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800692 main.log.error( self.name + ": EOF exception found" )
693 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700694 main.cleanup()
695 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700696 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800697
Jon Hall7eb38402015-01-08 17:19:54 -0800698 def pingpair( self ):
699 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700700 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800701 response = self.execute(
702 cmd='pingpair',
703 prompt='mininet>',
704 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -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 )
Jon Hall6094a362014-04-11 14:46:56 -0700708 main.cleanup()
709 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800710
Jon Hall7eb38402015-01-08 17:19:54 -0800711 if re.search( ',\s0\%\spacket\sloss', response ):
712 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800713 main.lastResult = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700714 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800715 else:
716 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 main.lastResult = main.FALSE
adminbae64d82013-08-01 10:50:15 -0700718 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800719
Jon Hall7eb38402015-01-08 17:19:54 -0800720 def link( self, **linkargs ):
721 """
722 Bring link( s ) between two nodes up or down"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800723 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800724 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
725 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
726 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
727 main.log.info(
728 "Bring link between '" +
729 end1 +
730 "' and '" +
731 end2 +
732 "' '" +
733 option +
734 "'" )
735 command = "link " + \
736 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700737 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800738 self.handle.sendline( command )
739 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800740 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800741 main.log.error( self.name + ": EOF exception found" )
742 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700743 main.cleanup()
744 main.exit()
adminbae64d82013-08-01 10:50:15 -0700745 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800746
Jon Hall7eb38402015-01-08 17:19:54 -0800747 def yank( self, **yankargs ):
748 """
749 yank a mininet switch interface to a host"""
750 main.log.info( 'Yank the switch interface attached to a host' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800751 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800752 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
753 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
754 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700755 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800756 response = self.execute(
757 cmd=command,
758 prompt="mininet>",
759 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800760 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800761 main.log.error( self.name + ": EOF exception found" )
762 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700763 main.cleanup()
764 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700765 return main.TRUE
766
Jon Hall7eb38402015-01-08 17:19:54 -0800767 def plug( self, **plugargs ):
768 """
769 plug the yanked mininet switch interface to a switch"""
770 main.log.info( 'Plug the switch interface attached to a switch' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800771 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800772 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
773 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
774 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700775 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800776 response = self.execute(
777 cmd=command,
778 prompt="mininet>",
779 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800780 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800781 main.log.error( self.name + ": EOF exception found" )
782 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700783 main.cleanup()
784 main.exit()
adminbae64d82013-08-01 10:50:15 -0700785 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800786
Jon Hall7eb38402015-01-08 17:19:54 -0800787 def dpctl( self, **dpctlargs ):
788 """
789 Run dpctl command on all switches."""
790 main.log.info( 'Run dpctl command on all switches' )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800791 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800792 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
793 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
794 command = "dpctl " + cmd + " " + str( cmdargs )
795 try:
796 response = self.execute(
797 cmd=command,
798 prompt="mininet>",
799 timeout=10 )
800 except pexpect.EOF:
801 main.log.error( self.name + ": EOF exception found" )
802 main.log.error( self.name + ": " + self.handle.before )
803 main.cleanup()
804 main.exit()
805 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800806
kelvin-onlabd3b64892015-01-20 13:26:24 -0800807 def getVersion( self ):
808 fileInput = path + '/lib/Mininet/INSTALL'
809 version = super( Mininet, self ).getVersion()
adminbae64d82013-08-01 10:50:15 -0700810 pattern = 'Mininet\s\w\.\w\.\w\w*'
kelvin-onlabd3b64892015-01-20 13:26:24 -0800811 for line in open( fileInput, 'r' ).readlines():
Jon Hall7eb38402015-01-08 17:19:54 -0800812 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700813 if result:
Jon Hall7eb38402015-01-08 17:19:54 -0800814 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -0500815 return version
adminbae64d82013-08-01 10:50:15 -0700816
kelvin-onlabd3b64892015-01-20 13:26:24 -0800817 def getSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800818 """
Jon Hallec3c21e2014-11-10 22:22:37 -0500819 Parameters:
820 sw: The name of an OVS switch. Example "s1"
821 Return:
Jon Hall7eb38402015-01-08 17:19:54 -0800822 The output of the command from the mininet cli
823 or main.FALSE on timeout"""
824 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -0700825 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800826 response = self.execute(
827 cmd=command,
828 prompt="mininet>",
829 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -0700830 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500831 return response
admin2a9548d2014-06-17 14:08:07 -0700832 else:
833 return main.FALSE
834 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800835 main.log.error( self.name + ": EOF exception found" )
836 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700837 main.cleanup()
838 main.exit()
adminbae64d82013-08-01 10:50:15 -0700839
kelvin-onlabd3b64892015-01-20 13:26:24 -0800840 def assignSwController( self, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800841 """
842 count is only needed if there is more than 1 controller"""
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800843 args = utilities.parse_args( [ "COUNT" ], **kwargs )
Jon Hall7eb38402015-01-08 17:19:54 -0800844 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -0700845
846 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -0800847 for j in range( count ):
848 argstring = argstring + ",IP" + \
849 str( j + 1 ) + ",PORT" + str( j + 1 )
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800850 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -0700851
Jon Hall7eb38402015-01-08 17:19:54 -0800852 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
853 ptcpA = int( args[ "PORT1" ] ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800854 int( sw ) if args[ "PORT1" ] is not None else ""
Jon Hall7eb38402015-01-08 17:19:54 -0800855 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -0800856
Jon Hall7eb38402015-01-08 17:19:54 -0800857 command = "sh ovs-vsctl set-controller s" + \
858 str( sw ) + " " + ptcpB + " "
859 for j in range( count ):
860 i = j + 1
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800861 args = utilities.parse_args(
Jon Hall7eb38402015-01-08 17:19:54 -0800862 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
863 ip = args[
864 "IP" +
865 str( i ) ] if args[
866 "IP" +
867 str( i ) ] is not None else ""
868 port = args[
869 "PORT" +
870 str( i ) ] if args[
871 "PORT" +
872 str( i ) ] is not None else ""
873 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
kelvin-onlabedcff052015-01-16 12:53:55 -0800874 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700875 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700876 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800877 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -0700878 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 )
Jon Hall6094a362014-04-11 14:46:56 -0700881 main.cleanup()
882 main.exit()
883 except:
Jon Hall7eb38402015-01-08 17:19:54 -0800884 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800885 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800886 main.log.info( ":" * 50 )
Jon Hall6094a362014-04-11 14:46:56 -0700887 main.cleanup()
888 main.exit()
adminbae64d82013-08-01 10:50:15 -0700889
kelvin-onlabd3b64892015-01-20 13:26:24 -0800890 def deleteSwController( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800891 """
892 Removes the controller target from sw"""
893 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -0700894 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800895 response = self.execute(
896 cmd=command,
897 prompt="mininet>",
898 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800899 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800900 main.log.error( self.name + ": EOF exception found" )
901 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -0700902 main.cleanup()
903 main.exit()
904 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800905 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -0700906
kelvin-onlabd3b64892015-01-20 13:26:24 -0800907 def addSwitch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800908 """
Jon Hallb1290e82014-11-18 16:17:48 -0500909 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -0800910 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -0800911 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -0500912 NOTE: cannot currently specify what type of switch
913 required params:
914 switchname = name of the new switch as a string
915 optional keyvalues:
916 dpid = "dpid"
917 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800918 """
919 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -0800920 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500921 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800922 response = self.execute(
923 cmd=command,
924 prompt="mininet>",
925 timeout=10 )
926 if re.search( "already exists!", response ):
927 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500928 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800929 elif re.search( "Error", response ):
930 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500931 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800932 elif re.search( "usage:", response ):
933 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500934 return main.FALSE
935 else:
936 return main.TRUE
937 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800938 main.log.error( self.name + ": EOF exception found" )
939 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500940 main.cleanup()
941 main.exit()
942
kelvin-onlabd3b64892015-01-20 13:26:24 -0800943 def delSwitch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800944 """
Jon Hallbe6dfc42015-01-12 17:37:25 -0800945 delete a switch from the mininet topology
946 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -0800947 dynamic_topo branch
Jon Hallbe6dfc42015-01-12 17:37:25 -0800948 required params:
Jon Hallb1290e82014-11-18 16:17:48 -0500949 switchname = name of the switch as a string
Jon Hallbe6dfc42015-01-12 17:37:25 -0800950 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800951 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500952 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800953 response = self.execute(
954 cmd=command,
955 prompt="mininet>",
956 timeout=10 )
957 if re.search( "no switch named", response ):
958 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500959 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800960 elif re.search( "Error", response ):
961 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500962 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800963 elif re.search( "usage:", response ):
964 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500965 return main.FALSE
966 else:
967 return main.TRUE
968 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800969 main.log.error( self.name + ": EOF exception found" )
970 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500971 main.cleanup()
972 main.exit()
973
kelvin-onlabd3b64892015-01-20 13:26:24 -0800974 def addLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -0800975 """
976 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -0800977 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -0800978 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -0800979 NOTE: cannot currently specify what type of link
980 required params:
981 node1 = the string node name of the first endpoint of the link
982 node2 = the string node name of the second endpoint of the link
983 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800984 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500985 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800986 response = self.execute(
987 cmd=command,
988 prompt="mininet>",
989 timeout=10 )
990 if re.search( "doesnt exist!", response ):
991 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500992 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800993 elif re.search( "Error", response ):
994 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500995 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800996 elif re.search( "usage:", response ):
997 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500998 return main.FALSE
999 else:
1000 return main.TRUE
1001 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001002 main.log.error( self.name + ": EOF exception found" )
1003 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001004 main.cleanup()
1005 main.exit()
1006
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 def delLink( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001008 """
1009 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001010 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001011 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001012 required params:
1013 node1 = the string node name of the first endpoint of the link
1014 node2 = the string node name of the second endpoint of the link
1015 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001016 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001017 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001018 response = self.execute(
1019 cmd=command,
1020 prompt="mininet>",
1021 timeout=10 )
1022 if re.search( "no node named", response ):
1023 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001024 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001025 elif re.search( "Error", response ):
1026 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001027 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001028 elif re.search( "usage:", response ):
1029 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001030 return main.FALSE
1031 else:
1032 return main.TRUE
1033 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001034 main.log.error( self.name + ": EOF exception found" )
1035 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001036 main.cleanup()
1037 main.exit()
1038
kelvin-onlabd3b64892015-01-20 13:26:24 -08001039 def addHost( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001040 """
Jon Hallb1290e82014-11-18 16:17:48 -05001041 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001042 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001043 dynamic_topo branch
Jon Hallb1290e82014-11-18 16:17:48 -05001044 NOTE: cannot currently specify what type of host
1045 required params:
1046 hostname = the string hostname
1047 optional key-value params
1048 switch = "switch name"
1049 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001050 """
1051 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001052 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001053 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001054 response = self.execute(
1055 cmd=command,
1056 prompt="mininet>",
1057 timeout=10 )
1058 if re.search( "already exists!", response ):
1059 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001060 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001061 elif re.search( "doesnt exists!", response ):
1062 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001063 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001064 elif re.search( "Error", response ):
1065 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001066 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001067 elif re.search( "usage:", response ):
1068 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001069 return main.FALSE
1070 else:
1071 return main.TRUE
1072 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001073 main.log.error( self.name + ": EOF exception found" )
1074 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001075 main.cleanup()
1076 main.exit()
1077
kelvin-onlabd3b64892015-01-20 13:26:24 -08001078 def delHost( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001079 """
1080 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001081 NOTE: This uses a custom mn function. MN repo should be on
Jon Hall272a4db2015-01-12 17:43:48 -08001082 dynamic_topo branch
Jon Hall7eb38402015-01-08 17:19:54 -08001083 NOTE: this uses a custom mn function
1084 required params:
1085 hostname = the string hostname
1086 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001087 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001088 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001089 response = self.execute(
1090 cmd=command,
1091 prompt="mininet>",
1092 timeout=10 )
1093 if re.search( "no host named", response ):
1094 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001095 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001096 elif re.search( "Error", response ):
1097 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001098 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001099 elif re.search( "usage:", response ):
1100 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001101 return main.FALSE
1102 else:
1103 return main.TRUE
1104 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001105 main.log.error( self.name + ": EOF exception found" )
1106 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001107 main.cleanup()
1108 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001109
Jon Hall7eb38402015-01-08 17:19:54 -08001110 def disconnect( self ):
1111 main.log.info( self.name + ": Disconnecting mininet..." )
adminbae64d82013-08-01 10:50:15 -07001112 response = ''
1113 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001114 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001115 response = self.execute(
1116 cmd="exit",
1117 prompt="(.*)",
1118 timeout=120 )
1119 response = self.execute(
1120 cmd="exit",
1121 prompt="(.*)",
1122 timeout=120 )
1123 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001124 response = main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001125 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001126 main.log.error( self.name + ": EOF exception found" )
1127 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001128 main.cleanup()
1129 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001130 else:
1131 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001132 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001133 return response
1134
Jon Hall7eb38402015-01-08 17:19:54 -08001135 def arping( self, src, dest, destmac ):
1136 self.handle.sendline( '' )
1137 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001138
Jon Hall7eb38402015-01-08 17:19:54 -08001139 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001140 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001141 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1142 main.log.info( self.name + ": ARP successful" )
1143 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001144 return main.TRUE
1145 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001146 main.log.warn( self.name + ": ARP FAILURE" )
1147 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001148 return main.FALSE
1149
Jon Hall7eb38402015-01-08 17:19:54 -08001150 def decToHex( self, num ):
1151 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001152
Jon Hall7eb38402015-01-08 17:19:54 -08001153 def getSwitchFlowCount( self, switch ):
1154 """
1155 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001156 if self.handle:
1157 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1158 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001159 response = self.execute(
1160 cmd=cmd,
1161 prompt="mininet>",
1162 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001163 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001164 main.log.error( self.name + ": EOF exception found" )
1165 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001166 main.cleanup()
1167 main.exit()
1168 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001169 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001170 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001171 main.log.info(
1172 "Couldn't find flows on switch %s, found: %s" %
1173 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001174 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001175 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001176 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001177 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001178
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 def checkFlows( self, sw, dumpFormat=None ):
1180 if dumpFormat:
Jon Hall7eb38402015-01-08 17:19:54 -08001181 command = "sh ovs-ofctl -F " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001182 dumpFormat + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001183 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001184 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001185 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001186 response = self.execute(
1187 cmd=command,
1188 prompt="mininet>",
1189 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001190 return response
1191 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001192 main.log.error( self.name + ": EOF exception found" )
1193 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001194 main.cleanup()
1195 main.exit()
1196 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001197 main.log.info( response )
admin2a9548d2014-06-17 14:08:07 -07001198
kelvin-onlabd3b64892015-01-20 13:26:24 -08001199 def startTcpdump( self, filename, intf="eth0", port="port 6633" ):
Jon Hall7eb38402015-01-08 17:19:54 -08001200 """
1201 Runs tpdump on an intferface and saves the file
1202 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001203 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001204 self.handle.sendline( "" )
1205 self.handle.expect( "mininet>" )
1206 self.handle.sendline(
1207 "sh sudo tcpdump -n -i " +
1208 intf +
1209 " " +
1210 port +
1211 " -w " +
1212 filename.strip() +
1213 " &" )
1214 self.handle.sendline( "" )
1215 i = self.handle.expect( [ 'No\ssuch\device',
1216 'listening\son',
1217 pexpect.TIMEOUT,
1218 "mininet>" ],
1219 timeout=10 )
1220 main.log.warn( self.handle.before + self.handle.after )
1221 self.handle.sendline( "" )
1222 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001223 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001224 main.log.error(
1225 self.name +
1226 ": tcpdump - No such device exists. " +
1227 "tcpdump attempted on: " +
1228 intf )
admin2a9548d2014-06-17 14:08:07 -07001229 return main.FALSE
1230 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001231 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001232 return main.TRUE
1233 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001234 main.log.error(
1235 self.name +
1236 ": tcpdump command timed out! Check interface name," +
1237 " given interface was: " +
1238 intf )
admin2a9548d2014-06-17 14:08:07 -07001239 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001240 elif i == 3:
1241 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001242 return main.TRUE
1243 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001244 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001245 return main.FALSE
1246 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001247 main.log.error( self.name + ": EOF exception found" )
1248 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001249 main.cleanup()
1250 main.exit()
1251 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001252 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001253 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001254 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001255 main.cleanup()
1256 main.exit()
1257
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 def stopTcpdump( self ):
admin2a9548d2014-06-17 14:08:07 -07001259 "pkills tcpdump"
1260 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001261 self.handle.sendline( "sh sudo pkill tcpdump" )
1262 self.handle.expect( "mininet>" )
1263 self.handle.sendline( "" )
1264 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001265 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001266 main.log.error( self.name + ": EOF exception found" )
1267 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001268 main.cleanup()
1269 main.exit()
1270 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001271 main.log.info( self.name + ":" * 50 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001272 main.log.error( traceback.printExc() )
kelvin-onlabedcff052015-01-16 12:53:55 -08001273 main.log.info( ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001274 main.cleanup()
1275 main.exit()
1276
kelvin-onlabd3b64892015-01-20 13:26:24 -08001277 def compareSwitches( self, topo, switchesJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001278 """
1279 Compare mn and onos switches
1280 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001281 switchesJson: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001282
Jon Hall7eb38402015-01-08 17:19:54 -08001283 This uses the sts TestONTopology object"""
kelvin-onlabd3b64892015-01-20 13:26:24 -08001284 # main.log.debug( "Switches_json string: ", switchesJson )
Jon Hall7eb38402015-01-08 17:19:54 -08001285 output = { "switches": [] }
1286 # iterate through the MN topology and pull out switches and and port
1287 # info
1288 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001289 ports = []
1290 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001291 ports.append( { 'of_port': port.port_no,
1292 'mac': str( port.hw_addr ).replace( '\'',
kelvin-onlabd3b64892015-01-20 13:26:24 -08001293 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001294 'name': port.name } )
1295 output[ 'switches' ].append( {
1296 "name": switch.name,
1297 "dpid": str( switch.dpid ).zfill( 16 ),
1298 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001299
Jon Hall7eb38402015-01-08 17:19:54 -08001300 # print "mn"
1301 # print json.dumps( output,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001302 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001303 # indent=4,
1304 # separators=( ',', ': ' ) )
1305 # print "onos"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001306 # print json.dumps( switchesJson,
1307 # sortKeys=True,
Jon Hall7eb38402015-01-08 17:19:54 -08001308 # indent=4,
1309 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001310
1311 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001312 mnDPIDs = []
1313 for switch in output[ 'switches' ]:
1314 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001315 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001316 # print "List of Mininet switch DPID's"
1317 # print mnDPIDs
kelvin-onlabd3b64892015-01-20 13:26:24 -08001318 if switchesJson == "": # if rest call fails
Jon Hall7eb38402015-01-08 17:19:54 -08001319 main.log.error(
1320 self.name +
1321 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001322 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001323 onos = switchesJson
Jon Hall7eb38402015-01-08 17:19:54 -08001324 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001325 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001326 if switch[ 'available' ]:
1327 onosDPIDs.append(
1328 switch[ 'id' ].replace(
1329 ":",
1330 '' ).replace(
1331 "of",
1332 '' ).lower() )
1333 # else:
1334 # print "Switch is unavailable:"
1335 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001336 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001337 # print "List of ONOS switch DPID's"
1338 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001339
Jon Hall7eb38402015-01-08 17:19:54 -08001340 if mnDPIDs != onosDPIDs:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 switchResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001342 main.log.report( "Switches in MN but not in ONOS:" )
1343 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1344 main.log.report( str( list1 ) )
1345 main.log.report( "Switches in ONOS but not in MN:" )
1346 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
kelvin-onlabedcff052015-01-16 12:53:55 -08001347 main.log.report( str( list2 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001348 else: # list of dpid's match in onos and mn
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 switchResults = main.TRUE
1350 return switchResults
Jon Hall3d87d502014-10-17 18:37:42 -04001351
kelvin-onlabd3b64892015-01-20 13:26:24 -08001352 def comparePorts( self, topo, portsJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001353 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001354 Compare mn and onos ports
1355 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001356 portsJson: parsed json object from the onos ports api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001357
Jon Hallfbc828e2015-01-06 17:30:19 -08001358 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001359 1. This uses the sts TestONTopology object
1360 2. numpy - "sudo pip install numpy"
1361
Jon Hall7eb38402015-01-08 17:19:54 -08001362 """
1363 # FIXME: this does not look for extra ports in ONOS, only checks that
1364 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001365 from numpy import uint64
kelvin-onlabd3b64892015-01-20 13:26:24 -08001366 portsResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001367 output = { "switches": [] }
1368 # iterate through the MN topology and pull out switches and and port
1369 # info
1370 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001371 ports = []
1372 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001373 # print port.hw_addr.toStr( separator='' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 tmpPort = {}
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001375 tmpPort[ 'of_port' ] = port.port_no
1376 tmpPort[ 'mac' ] = str( port.hw_addr ).replace( '\'', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 tmpPort[ 'name' ] = port.name
1378 tmpPort[ 'enabled' ] = port.enabled
Jon Hall39f29df2014-11-04 19:30:21 -05001379
kelvin-onlabd3b64892015-01-20 13:26:24 -08001380 ports.append( tmpPort )
1381 tmpSwitch = {}
1382 tmpSwitch[ 'name' ] = switch.name
1383 tmpSwitch[ 'dpid' ] = str( switch.dpid ).zfill( 16 )
1384 tmpSwitch[ 'ports' ] = ports
Jon Hall39f29df2014-11-04 19:30:21 -05001385
kelvin-onlabd3b64892015-01-20 13:26:24 -08001386 output[ 'switches' ].append( tmpSwitch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001387
Jon Hall7eb38402015-01-08 17:19:54 -08001388 # PORTS
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 for mnSwitch in output[ 'switches' ]:
1390 mnPorts = []
1391 onosPorts = []
1392 switchResult = main.TRUE
1393 for port in mnSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001394 if port[ 'enabled' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 mnPorts.append( port[ 'of_port' ] )
1396 for onosSwitch in portsJson:
Jon Hall7eb38402015-01-08 17:19:54 -08001397 # print "Iterating through a new switch as seen by ONOS"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001398 # print onosSwitch
1399 if onosSwitch[ 'device' ][ 'available' ]:
1400 if onosSwitch[ 'device' ][ 'id' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001401 ':',
1402 '' ).replace(
1403 "of",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001404 '' ) == mnSwitch[ 'dpid' ]:
1405 for port in onosSwitch[ 'ports' ]:
Jon Hall7eb38402015-01-08 17:19:54 -08001406 if port[ 'isEnabled' ]:
1407 if port[ 'port' ] == 'local':
kelvin-onlabd3b64892015-01-20 13:26:24 -08001408 # onosPorts.append( 'local' )
1409 onosPorts.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001410 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 onosPorts.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001412 break
kelvin-onlabd3b64892015-01-20 13:26:24 -08001413 mnPorts.sort( key=float )
1414 onosPorts.sort( key=float )
1415 # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] )
1416 # print "\tmn_ports[] = ", mnPorts
1417 # print "\tonos_ports[] = ", onosPorts
1418 mnPortsLog = mnPorts
1419 onosPortsLog = onosPorts
1420 mnPorts = [ x for x in mnPorts ]
1421 onosPorts = [ x for x in onosPorts ]
Jon Hall38481722014-11-04 16:50:05 -05001422
Jon Hall7eb38402015-01-08 17:19:54 -08001423 # TODO: handle other reserved port numbers besides LOCAL
1424 # NOTE: Reserved ports
1425 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1426 # long( uint64( -2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001427 for mnPort in mnPortsLog:
1428 if mnPort in onosPorts:
Jon Hall7eb38402015-01-08 17:19:54 -08001429 # don't set results to true here as this is just one of
1430 # many checks and it might override a failure
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 mnPorts.remove( mnPort )
1432 onosPorts.remove( mnPort )
Jon Hall7eb38402015-01-08 17:19:54 -08001433 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001434 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001435 # TODO: Come up with a better way of handling these
kelvin-onlabd3b64892015-01-20 13:26:24 -08001436 if 65534 in mnPorts:
1437 mnPorts.remove( 65534 )
1438 if long( uint64( -2 ) ) in onosPorts:
1439 onosPorts.remove( long( uint64( -2 ) ) )
1440 if len( mnPorts ): # the ports of this switch don't match
1441 switchResult = main.FALSE
1442 main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) )
1443 if len( onosPorts ): # the ports of this switch don't match
1444 switchResult = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001445 main.log.warn(
1446 "Ports in ONOS but not MN: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 str( onosPorts ) )
1448 if switchResult == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001449 main.log.report(
1450 "The list of ports for switch %s(%s) does not match:" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001451 ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) )
1452 main.log.warn( "mn_ports[] = " + str( mnPortsLog ) )
1453 main.log.warn( "onos_ports[] = " + str( onosPortsLog ) )
1454 portsResults = portsResults and switchResult
1455 return portsResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001456
kelvin-onlabd3b64892015-01-20 13:26:24 -08001457 def compareLinks( self, topo, linksJson ):
Jon Hall7eb38402015-01-08 17:19:54 -08001458 """
1459 Compare mn and onos links
1460 topo: sts TestONTopology object
kelvin-onlabd3b64892015-01-20 13:26:24 -08001461 linksJson: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001462
Jon Hall7eb38402015-01-08 17:19:54 -08001463 This uses the sts TestONTopology object"""
1464 # FIXME: this does not look for extra links in ONOS, only checks that
1465 # ONOS has what is in MN
kelvin-onlabd3b64892015-01-20 13:26:24 -08001466 linkResults = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001467 output = { "switches": [] }
kelvin-onlabd3b64892015-01-20 13:26:24 -08001468 onos = linksJson
Jon Hall7eb38402015-01-08 17:19:54 -08001469 # iterate through the MN topology and pull out switches and and port
1470 # info
1471 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001472 # print "Iterating though switches as seen by Mininet"
1473 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001474 ports = []
1475 for port in switch.ports.values():
kelvin-onlab652e1dd2015-01-20 17:01:39 -08001476 # print port.hw_addr.toStr( separator='' )
1477 ports.append( { 'of_port': port.port_no,
1478 'mac': str( port.hw_addr ).replace( '\'',
kelvin-onlabd3b64892015-01-20 13:26:24 -08001479 '' ),
Jon Hall7eb38402015-01-08 17:19:54 -08001480 'name': port.name } )
1481 output[ 'switches' ].append( {
1482 "name": switch.name,
1483 "dpid": str( switch.dpid ).zfill( 16 ),
1484 "ports": ports } )
1485 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001486
kelvin-onlabd3b64892015-01-20 13:26:24 -08001487 mnLinks = [
kelvin-onlab9592d132015-01-20 17:18:02 -08001488 link for link in topo.patch_panel.network_links if (
Jon Hall7eb38402015-01-08 17:19:54 -08001489 link.port1.enabled and link.port2.enabled ) ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001490 if 2 * len( mnLinks ) == len( onos ):
1491 linkResults = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001492 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 linkResults = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001494 main.log.report(
1495 "Mininet has %i bidirectional links and " +
1496 "ONOS has %i unidirectional links" %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001497 ( len( mnLinks ), len( onos ) ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001498
Jon Hall7eb38402015-01-08 17:19:54 -08001499 # iterate through MN links and check if an ONOS link exists in
1500 # both directions
1501 # NOTE: Will currently only show mn links as down if they are
1502 # cut through STS. We can either do everything through STS or
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 # wait for upNetworkLinks and downNetworkLinks to be
Jon Hall7eb38402015-01-08 17:19:54 -08001504 # fully implemented.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001505 for link in mnLinks:
Jon Hall7eb38402015-01-08 17:19:54 -08001506 # print "Link: %s" % link
1507 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001508 node1 = None
1509 port1 = None
1510 node2 = None
1511 port2 = None
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 firstDir = main.FALSE
1513 secondDir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001514 for switch in output[ 'switches' ]:
1515 # print "Switch: %s" % switch[ 'name' ]
1516 if switch[ 'name' ] == link.node1.name:
1517 node1 = switch[ 'dpid' ]
1518 for port in switch[ 'ports' ]:
1519 if str( port[ 'name' ] ) == str( link.port1 ):
1520 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001521 if node1 is not None and node2 is not None:
1522 break
Jon Hall7eb38402015-01-08 17:19:54 -08001523 if switch[ 'name' ] == link.node2.name:
1524 node2 = switch[ 'dpid' ]
1525 for port in switch[ 'ports' ]:
1526 if str( port[ 'name' ] ) == str( link.port2 ):
1527 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001528 if node1 is not None and node2 is not None:
1529 break
1530
kelvin-onlabd3b64892015-01-20 13:26:24 -08001531 for onosLink in onos:
1532 onosNode1 = onosLink[ 'src' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001533 ":",
1534 '' ).replace(
1535 "of",
1536 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001537 onosNode2 = onosLink[ 'dst' ][ 'device' ].replace(
Jon Hall7eb38402015-01-08 17:19:54 -08001538 ":",
1539 '' ).replace(
1540 "of",
1541 '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001542 onosPort1 = onosLink[ 'src' ][ 'port' ]
1543 onosPort2 = onosLink[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001544
Jon Hall72cf1dc2014-10-20 21:04:50 -04001545 # check onos link from node1 to node2
kelvin-onlabd3b64892015-01-20 13:26:24 -08001546 if str( onosNode1 ) == str( node1 ) and str(
1547 onosNode2 ) == str( node2 ):
1548 if int( onosPort1 ) == int( port1 ) and int(
1549 onosPort2 ) == int( port2 ):
1550 firstDir = 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 ( node1,
1558 port1,
1559 node2,
1560 port2 ) +
1561 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001562 ( onosNode1,
1563 onosPort1,
1564 onosNode2,
1565 onosPort2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001566
1567 # check onos link from node2 to node1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 elif ( str( onosNode1 ) == str( node2 ) and
1569 str( onosNode2 ) == str( node1 ) ):
1570 if ( int( onosPort1 ) == int( port2 )
1571 and int( onosPort2 ) == int( port1 ) ):
1572 secondDir = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001573 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001574 main.log.warn(
1575 'The port numbers do not match for ' +
1576 str( link ) +
1577 ' between ONOS and MN. When cheking ONOS for ' +
1578 'link %s/%s -> %s/%s' %
1579 ( node2,
1580 port2,
1581 node1,
1582 port1 ) +
1583 ' ONOS has the values %s/%s -> %s/%s' %
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 ( onosNode2,
1585 onosPort2,
1586 onosNode1,
1587 onosPort1 ) )
Jon Hall7eb38402015-01-08 17:19:54 -08001588 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001589 pass
kelvin-onlabd3b64892015-01-20 13:26:24 -08001590 if not firstDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001591 main.log.report(
1592 'ONOS does not have the link %s/%s -> %s/%s' %
1593 ( node1, port1, node2, port2 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001594 if not secondDir:
Jon Hall7eb38402015-01-08 17:19:54 -08001595 main.log.report(
1596 'ONOS does not have the link %s/%s -> %s/%s' %
1597 ( node2, port2, node1, port1 ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001598 linkResults = linkResults and firstDir and secondDir
1599 return linkResults
Jon Hall72cf1dc2014-10-20 21:04:50 -04001600
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 def getHosts( self ):
Jon Hall7eb38402015-01-08 17:19:54 -08001602 """
1603 Returns a list of all hosts
1604 Don't ask questions just use it"""
1605 self.handle.sendline( "" )
1606 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001607
Jon Hall7eb38402015-01-08 17:19:54 -08001608 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1609 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001610
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 handlePy = self.handle.before
1612 handlePy = handlePy.split( "]\r\n", 1 )[ 1 ]
1613 handlePy = handlePy.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001614
Jon Hall7eb38402015-01-08 17:19:54 -08001615 self.handle.sendline( "" )
1616 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001617
kelvin-onlabd3b64892015-01-20 13:26:24 -08001618 hostStr = handlePy.replace( "]", "" )
1619 hostStr = hostStr.replace( "'", "" )
1620 hostStr = hostStr.replace( "[", "" )
1621 hostList = hostStr.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001622
kelvin-onlabd3b64892015-01-20 13:26:24 -08001623 return hostList
adminbae64d82013-08-01 10:50:15 -07001624
Jon Hall7eb38402015-01-08 17:19:54 -08001625 def update( self ):
1626 """
1627 updates the port address and status information for
1628 each port in mn"""
1629 # TODO: Add error checking. currently the mininet command has no output
1630 main.log.info( "Updateing MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001631 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001632 self.handle.sendline( "" )
1633 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001634
Jon Hall7eb38402015-01-08 17:19:54 -08001635 self.handle.sendline( "update" )
1636 self.handle.expect( "update" )
1637 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001638
Jon Hall7eb38402015-01-08 17:19:54 -08001639 self.handle.sendline( "" )
1640 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001641
Jon Hallb1290e82014-11-18 16:17:48 -05001642 return main.TRUE
1643 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001644 main.log.error( self.name + ": EOF exception found" )
1645 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001646 main.cleanup()
1647 main.exit()
1648
adminbae64d82013-08-01 10:50:15 -07001649if __name__ != "__main__":
1650 import sys
Jon Hall7eb38402015-01-08 17:19:54 -08001651 sys.modules[ __name__ ] = MininetCliDriver()