blob: 52754dfff2ff726e14bb8496284d2bcf44e77877 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 26-Oct-2012
4
5@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
6
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
Jon Hallfbc828e2015-01-06 17:30:19 -080019 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
22MininetCliDriver is the basic driver which will handle the Mininet functions
23'''
admin2a9548d2014-06-17 14:08:07 -070024import traceback
adminbae64d82013-08-01 10:50:15 -070025import pexpect
adminbae64d82013-08-01 10:50:15 -070026import re
27import sys
adminbae64d82013-08-01 10:50:15 -070028sys.path.append("../")
Jon Hall1ccf82c2014-10-15 14:55:16 -040029from math import pow
adminbae64d82013-08-01 10:50:15 -070030from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070031
32class MininetCliDriver(Emulator):
33 '''
Jon Hall41f40e82014-04-08 16:43:17 -070034 MininetCliDriver is the basic driver which will handle the Mininet functions
adminbae64d82013-08-01 10:50:15 -070035 '''
36 def __init__(self):
37 super(Emulator, self).__init__()
38 self.handle = self
39 self.wrapped = sys.modules[__name__]
40 self.flag = 0
41
42 def connect(self, **connectargs):
Jon Hall41f40e82014-04-08 16:43:17 -070043 '''
44 Here the main is the TestON instance after creating all the log handles.
45 '''
adminbae64d82013-08-01 10:50:15 -070046 for key in connectargs:
Jon Hallfbc828e2015-01-06 17:30:19 -080047 vars(self)[key] = connectargs[key]
48
adminbae64d82013-08-01 10:50:15 -070049 self.name = self.options['name']
50 self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd)
Jon Hallfbc828e2015-01-06 17:30:19 -080051
adminbae64d82013-08-01 10:50:15 -070052 self.ssh_handle = self.handle
Jon Hallfbc828e2015-01-06 17:30:19 -080053
adminbae64d82013-08-01 10:50:15 -070054 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -070055 main.log.info(self.name+": Clearing any residual state or processes")
adminbae64d82013-08-01 10:50:15 -070056 self.handle.sendline("sudo mn -c")
adminf939f8b2014-04-03 17:22:56 -070057 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070058 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070059 main.log.info(self.name+": Sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070060 self.handle.sendline(self.pwd)
Jon Hallfbc828e2015-01-06 17:30:19 -080061 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070062 if i==1:
Jon Hallf2942ce2014-04-10 16:00:16 -070063 main.log.info(self.name+": Clean")
adminbae64d82013-08-01 10:50:15 -070064 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070065 main.log.error(self.name+": Connection terminated")
adminbae64d82013-08-01 10:50:15 -070066 elif i==3: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070067 main.log.error(self.name+": Something while cleaning MN took too long... " )
Jon Hallfbc828e2015-01-06 17:30:19 -080068
69 main.log.info(self.name+": building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080070 #### for reactive/PARP enabled tests
shahshreyaf4d4d0c2014-10-10 12:11:10 -070071 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] + " " + self.options['arg3']
Jon Hallfbc828e2015-01-06 17:30:19 -080072
Jon Hall1ccf82c2014-10-15 14:55:16 -040073 argList = self.options['arg1'].split(",")
74 global topoArgList
75 topoArgList = argList[0].split(" ")
76 argList = map(int, argList[1:])
77 topoArgList = topoArgList[1:] + argList
Jon Hallfbc828e2015-01-06 17:30:19 -080078
Jon Hall1ccf82c2014-10-15 14:55:16 -040079 #### for proactive flow with static ARP entries
shahshreyaf4d4d0c2014-10-10 12:11:10 -070080 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] + " " + self.options['arg3']
adminbae64d82013-08-01 10:50:15 -070081 self.handle.sendline(cmdString)
Jon Hall333fa8c2014-04-11 11:24:58 -070082 self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT])
Jon Hallfbc828e2015-01-06 17:30:19 -080083 while 1:
adminbae64d82013-08-01 10:50:15 -070084 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
85 if i==0:
Jon Hallfbc828e2015-01-06 17:30:19 -080086 main.log.info(self.name+": mininet built")
adminbae64d82013-08-01 10:50:15 -070087 return main.TRUE
88 if i==1:
Jon Hall1645caa2014-11-18 16:27:14 -050089 self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070090 main.log.info(self.handle.before)
91 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070092 main.log.error(self.name+": Launching mininet failed...")
adminbae64d82013-08-01 10:50:15 -070093 return main.FALSE
94 elif i==3:
Jon Hallf2942ce2014-04-10 16:00:16 -070095 main.log.error(self.name+": Connection timeout")
adminbae64d82013-08-01 10:50:15 -070096 return main.FALSE
97 elif i==4: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070098 main.log.error(self.name+": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -070099 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700100 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
101 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700102 else:#if no handle
Jon Hallfbc828e2015-01-06 17:30:19 -0800103 main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address)
Jon Hallf2942ce2014-04-10 16:00:16 -0700104 main.log.error(self.name+": Failed to connect to the Mininet")
adminbae64d82013-08-01 10:50:15 -0700105 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800106
Jon Hall1ccf82c2014-10-15 14:55:16 -0400107 def num_switches_n_links(self,topoType,depth,fanout):
108 if topoType == 'tree':
109 if fanout is None: #In tree topology, if fanout arg is not given, by default it is 2
110 fanout = 2
111 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500112 count = 0
Jon Hallfbc828e2015-01-06 17:30:19 -0800113 while(k <= depth-1):
Jon Hall38481722014-11-04 16:50:05 -0500114 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400115 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500116 num_switches = count
Jon Hallfbc828e2015-01-06 17:30:19 -0800117 while(k <= depth-2):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400118 '''depth-2 gives you only core links and not considering edge links as seen by ONOS
119 If all the links including edge links are required, do depth-1
120 '''
Jon Hall38481722014-11-04 16:50:05 -0500121 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400122 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500123 num_links = count * fanout
Jon Hall1ccf82c2014-10-15 14:55:16 -0400124 #print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
Jon Hallfbc828e2015-01-06 17:30:19 -0800125
Jon Hall1ccf82c2014-10-15 14:55:16 -0400126 elif topoType =='linear':
127 if fanout is None: #In linear topology, if fanout or num_hosts_per_sw is not given, by default it is 1
128 fanout = 1
129 num_switches = depth
130 num_hosts_per_sw = fanout
131 total_num_hosts = num_switches * num_hosts_per_sw
132 num_links = total_num_hosts + (num_switches - 1)
Jon Hallfbc828e2015-01-06 17:30:19 -0800133 print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400134 topoDict = {}
135 topoDict = {"num_switches":int(num_switches), "num_corelinks":int(num_links)}
136 return topoDict
137
138
139 def calculate_sw_and_links(self):
140 topoDict = self.num_switches_n_links(*topoArgList)
141 return topoDict
142
Jon Hall1645caa2014-11-18 16:27:14 -0500143 def pingall(self, timeout=300):
adminbae64d82013-08-01 10:50:15 -0700144 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700145 Verifies the reachability of the hosts using pingall command.
Jon Hall1645caa2014-11-18 16:27:14 -0500146 Optional parameter timeout allows you to specify how long to wait for pingall to complete
147 Returns:
148 main.TRUE if pingall completes with no pings dropped
149 otherwise main.FALSE
adminbae64d82013-08-01 10:50:15 -0700150 '''
151 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -0700152 main.log.info(self.name+": Checking reachabilty to the hosts using pingall")
Jon Hall6094a362014-04-11 14:46:56 -0700153 try:
Jon Hall1645caa2014-11-18 16:27:14 -0500154 response = self.execute(cmd="pingall",prompt="mininet>",timeout=int(timeout))
Jon Hallb1290e82014-11-18 16:17:48 -0500155 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700156 main.log.error(self.name + ": EOF exception found")
157 main.log.error(self.name + ": " + self.handle.before)
Jon Hallb1290e82014-11-18 16:17:48 -0500158 main.cleanup()
159 main.exit()
160 except pexpect.TIMEOUT:
161 #We may not want to kill the test if pexpect times out
162 main.log.error(self.name + ": TIMEOUT exception found")
163 main.log.error(self.name + ": " + str(self.handle.before) )
164 #NOTE: mininet's pingall rounds, so we will check the number of passed and number of failed
165 pattern = "Results\:\s0\%\sdropped\s\((?P<passed>[\d]+)/(?P=passed)"
Jon Hallf2942ce2014-04-10 16:00:16 -0700166 if re.search(pattern,response):
167 main.log.info(self.name+": All hosts are reachable")
adminbae64d82013-08-01 10:50:15 -0700168 return main.TRUE
169 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700170 main.log.error(self.name+": Unable to reach all the hosts")
Jon Hallb1290e82014-11-18 16:17:48 -0500171 main.log.info("Pingall ouput: " + str(response))
172 #NOTE: Send ctrl-c to make sure pingall is done
173 self.handle.send("\x03")
174 self.handle.expect("Interrupt")
175 self.handle.expect("mininet>")
adminbae64d82013-08-01 10:50:15 -0700176 return main.FALSE
177 else :
Jon Hallfbc828e2015-01-06 17:30:19 -0800178 main.log.error(self.name+": Connection failed to the host")
Jon Hallb1290e82014-11-18 16:17:48 -0500179 main.cleanup()
180 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700181
182 def fpingHost(self,**pingParams):
Jon Hallfbc828e2015-01-06 17:30:19 -0800183 '''
adminaeedddd2013-08-02 15:14:15 -0700184 Uses the fping package for faster pinging...
Jon Hallfbc828e2015-01-06 17:30:19 -0800185 *requires fping to be installed on machine running mininet
186 '''
adminaeedddd2013-08-02 15:14:15 -0700187 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700188 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
Jon Hallfbc828e2015-01-06 17:30:19 -0800189 self.handle.sendline(command)
190 self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT])
Jon Hall333fa8c2014-04-11 11:24:58 -0700191 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
adminaeedddd2013-08-02 15:14:15 -0700192 response = self.handle.before
193 if re.search(":\s-" ,response):
Jon Hallfbc828e2015-01-06 17:30:19 -0800194 main.log.info(self.name+": Ping fail")
adminaeedddd2013-08-02 15:14:15 -0700195 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700196 elif re.search(":\s\d{1,2}\.\d\d", response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700197 main.log.info(self.name+": Ping good!")
adminaeedddd2013-08-02 15:14:15 -0700198 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800199 main.log.info(self.name+": Install fping on mininet machine... ")
Jon Hallf2942ce2014-04-10 16:00:16 -0700200 main.log.info(self.name+": \n---\n"+response)
adminaeedddd2013-08-02 15:14:15 -0700201 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800202
adminbae64d82013-08-01 10:50:15 -0700203 def pingHost(self,**pingParams):
Jon Hallf2942ce2014-04-10 16:00:16 -0700204 '''
205 Ping from one mininet host to another
206 Currently the only supported Params: SRC and TARGET
207 '''
adminbae64d82013-08-01 10:50:15 -0700208 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
209 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hall0819fd92014-05-23 12:08:13 -0700210 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700211 try:
Jon Hall6e18c7b2014-04-23 16:26:33 -0700212 main.log.warn("Sending: " + command)
213 #response = self.execute(cmd=command,prompt="mininet",timeout=10 )
214 self.handle.sendline(command)
215 i = self.handle.expect([command,pexpect.TIMEOUT])
216 if i == 1:
217 main.log.error(self.name + ": timeout when waiting for response from mininet")
218 main.log.error("response: " + str(self.handle.before))
219 i = self.handle.expect(["mininet>",pexpect.TIMEOUT])
220 if i == 1:
221 main.log.error(self.name + ": timeout when waiting for response from mininet")
222 main.log.error("response: " + str(self.handle.before))
223 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800224 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700225 main.log.error(self.name + ": EOF exception found")
226 main.log.error(self.name + ": " + self.handle.before)
227 main.cleanup()
228 main.exit()
Jon Hallf2942ce2014-04-10 16:00:16 -0700229 main.log.info(self.name+": Ping Response: "+ response )
230 #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
231 if re.search(',\s0\%\spacket\sloss',response):
Jon Hall6e18c7b2014-04-23 16:26:33 -0700232 main.log.info(self.name+": no packets lost, host is reachable")
Jon Hallfbc828e2015-01-06 17:30:19 -0800233 main.last_result = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700234 return main.TRUE
235 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700236 main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700237 main.last_result = main.FALSE
238 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800239
adminbae64d82013-08-01 10:50:15 -0700240 def checkIP(self,host):
241 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700242 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700243 '''
244 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700245 try:
246 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800247 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700248 main.log.error(self.name + ": EOF exception found")
249 main.log.error(self.name + ": " + self.handle.before)
250 main.cleanup()
251 main.exit()
adminbae64d82013-08-01 10:50:15 -0700252
253 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})"
Jon Hallfbc828e2015-01-06 17:30:19 -0800254 #pattern = "inet addr:10.0.0.6"
Jon Hallf2942ce2014-04-10 16:00:16 -0700255 #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
256 if re.search(pattern,response):
257 main.log.info(self.name+": Host Ip configured properly")
adminbae64d82013-08-01 10:50:15 -0700258 return main.TRUE
259 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700260 main.log.error(self.name+": Host IP not found")
adminbae64d82013-08-01 10:50:15 -0700261 return main.FALSE
262 else :
Jon Hallfbc828e2015-01-06 17:30:19 -0800263 main.log.error(self.name+": Connection failed to the host")
264
adminbae64d82013-08-01 10:50:15 -0700265 def verifySSH(self,**connectargs):
Jon Hall6094a362014-04-11 14:46:56 -0700266 try:
267 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
268 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
269 for key in connectargs:
270 vars(self)[key] = connectargs[key]
271 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800272 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700273 main.log.error(self.name + ": EOF exception found")
274 main.log.error(self.name + ": " + self.handle.before)
275 main.cleanup()
276 main.exit()
adminbae64d82013-08-01 10:50:15 -0700277 import time
278 time.sleep(20)
279 if self.flag == 0:
280 self.flag = 1
281 return main.FALSE
282 else :
283 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800284
shahshreyae6c7cf42014-11-26 16:39:01 -0800285
286 def changeIP(self,host,intf,newIP,newNetmask):
287 '''
288 Changes the ip address of a host on the fly
289 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0
290 '''
291 if self.handle:
292 try:
shahshreyad0c80432014-12-04 16:56:05 -0800293 cmd = host+" ifconfig "+intf+" "+newIP+" "+'netmask'+" "+newNetmask
shahshreyae6c7cf42014-11-26 16:39:01 -0800294 self.handle.sendline(cmd)
295 self.handle.expect("mininet>")
296 response = self.handle.before
shahshreyad0c80432014-12-04 16:56:05 -0800297 main.log.info("response = "+response)
shahshreyae6c7cf42014-11-26 16:39:01 -0800298 main.log.info("Ip of host "+host+" changed to new IP "+newIP)
299 return main.TRUE
300 except pexpect.EOF:
301 main.log.error(self.name + ": EOF exception found")
302 main.log.error(self.name + ": " + self.handle.before)
303 return main.FALSE
304
305 def changeDefaultGateway(self,host,newGW):
306 '''
307 Changes the default gateway of a host
308 Ex: h1 route add default gw 10.0.1.2
309 '''
310 if self.handle:
311 try:
Jon Hallfbc828e2015-01-06 17:30:19 -0800312 cmd = host+" route add default gw "+newGW
shahshreyae6c7cf42014-11-26 16:39:01 -0800313 self.handle.sendline(cmd)
314 self.handle.expect("mininet>")
315 response = self.handle.before
shahshreyad0c80432014-12-04 16:56:05 -0800316 main.log.info("response = "+response)
shahshreyae6c7cf42014-11-26 16:39:01 -0800317 main.log.info("Default gateway of host "+host+" changed to "+newGW)
318 return main.TRUE
319 except pexpect.EOF:
320 main.log.error(self.name + ": EOF exception found")
321 main.log.error(self.name + ": " + self.handle.before)
322 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800323
shahshreyad0c80432014-12-04 16:56:05 -0800324 def addStaticMACAddress(self,host,GW,macaddr):
325 '''
326 Changes the mac address of a geateway host
327 '''
328 if self.handle:
329 try:
Jon Hallfbc828e2015-01-06 17:30:19 -0800330 #h1 arp -s 10.0.1.254 00:00:00:00:11:11
shahshreyad0c80432014-12-04 16:56:05 -0800331 cmd = host+" arp -s "+GW+" "+macaddr
332 self.handle.sendline(cmd)
333 self.handle.expect("mininet>")
334 response = self.handle.before
335 main.log.info("response = "+response)
336 main.log.info("Mac adrress of gateway "+GW+" changed to "+macaddr)
337 return main.TRUE
338 except pexpect.EOF:
339 main.log.error(self.name + ": EOF exception found")
340 main.log.error(self.name + ": " + self.handle.before)
341 return main.FALSE
342
343 def verifyStaticGWandMAC(self,host):
344 '''
Jon Hallfbc828e2015-01-06 17:30:19 -0800345 Verify if the static gateway and mac address assignment
shahshreyad0c80432014-12-04 16:56:05 -0800346 '''
347 if self.handle:
348 try:
349 #h1 arp -an
350 cmd = host+" arp -an "
351 self.handle.sendline(cmd)
352 self.handle.expect("mininet>")
353 response = self.handle.before
354 main.log.info(host+" arp -an = "+response)
355 return main.TRUE
356 except pexpect.EOF:
357 main.log.error(self.name + ": EOF exception found")
358 main.log.error(self.name + ": " + self.handle.before)
359 return main.FALSE
360
361
shahshreyae6c7cf42014-11-26 16:39:01 -0800362
adminbae64d82013-08-01 10:50:15 -0700363 def getMacAddress(self,host):
364 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700365 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700366 '''
367 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700368 try:
369 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800370 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700371 main.log.error(self.name + ": EOF exception found")
372 main.log.error(self.name + ": " + self.handle.before)
373 main.cleanup()
374 main.exit()
adminbae64d82013-08-01 10:50:15 -0700375
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700376 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
377 mac_address_search = re.search(pattern, response, re.I)
378 mac_address = mac_address_search.group().split(" ")[1]
Jon Hallf2942ce2014-04-10 16:00:16 -0700379 main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address)
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700380 return mac_address
adminbae64d82013-08-01 10:50:15 -0700381 else :
Jon Hallfbc828e2015-01-06 17:30:19 -0800382 main.log.error(self.name+": Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700383
384 def getInterfaceMACAddress(self,host, interface):
385 '''
386 Return the IP address of the interface on the given host
387 '''
388 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700389 try:
390 response = self.execute(cmd=host+" ifconfig " + interface,
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700391 prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800392 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700393 main.log.error(self.name + ": EOF exception found")
394 main.log.error(self.name + ": " + self.handle.before)
395 main.cleanup()
396 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700397
398 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
399 mac_address_search = re.search(pattern, response, re.I)
400 if mac_address_search is None:
401 main.log.info("No mac address found in %s" % response)
402 return main.FALSE
403 mac_address = mac_address_search.group().split(" ")[1]
404 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
405 return mac_address
406 else:
407 main.log.error("Connection failed to the host")
408
adminbae64d82013-08-01 10:50:15 -0700409 def getIPAddress(self,host):
410 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700411 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700412 '''
413 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700414 try:
415 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800416 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700417 main.log.error(self.name + ": EOF exception found")
418 main.log.error(self.name + ": " + self.handle.before)
419 main.cleanup()
420 main.exit()
adminbae64d82013-08-01 10:50:15 -0700421
422 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
423 ip_address_search = re.search(pattern, response)
Jon Hallf2942ce2014-04-10 16:00:16 -0700424 main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1))
adminbae64d82013-08-01 10:50:15 -0700425 return ip_address_search.group(1)
426 else :
Jon Hallfbc828e2015-01-06 17:30:19 -0800427 main.log.error(self.name+": Connection failed to the host")
428
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700429 def getSwitchDPID(self,switch):
430 '''
431 return the datapath ID of the switch
432 '''
433 if self.handle :
434 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700435 try:
436 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800437 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700438 main.log.error(self.name + ": EOF exception found")
439 main.log.error(self.name + ": " + self.handle.before)
440 main.cleanup()
441 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800442 pattern = r'^(?P<dpid>\w)+'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700443 result = re.search(pattern, response, re.MULTILINE)
444 if result is None:
445 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
446 return main.FALSE
Jon Hall28bf54b2014-12-17 16:25:44 -0800447 return str(result.group(0)).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700448 else:
449 main.log.error("Connection failed to the host")
450
admin2580a0e2014-07-29 11:24:34 -0700451 def getDPID(self, switch):
452 if self.handle:
453 self.handle.sendline("")
454 self.expect("mininet>")
455 cmd = "py %s.dpid" %switch
456 try:
457 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
458 self.handle.expect("mininet>")
459 response = self.handle.before
460 return response
461 except pexpect.EOF:
462 main.log.error(self.name + ": EOF exception found")
463 main.log.error(self.name + ": " + self.handle.before)
464 main.cleanup()
465 main.exit()
466
467
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700468 def getInterfaces(self, node):
469 '''
470 return information dict about interfaces connected to the node
471 '''
472 if self.handle :
Jon Hall38481722014-11-04 16:50:05 -0500473 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700474 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700475 try:
476 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800477 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700478 main.log.error(self.name + ": EOF exception found")
479 main.log.error(self.name + ": " + self.handle.before)
480 main.cleanup()
481 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700482 return response
483 else:
484 main.log.error("Connection failed to the node")
485
adminbae64d82013-08-01 10:50:15 -0700486 def dump(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700487 main.log.info(self.name+": Dump node info")
Jon Hall6094a362014-04-11 14:46:56 -0700488 try:
489 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800490 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700491 main.log.error(self.name + ": EOF exception found")
492 main.log.error(self.name + ": " + self.handle.before)
493 main.cleanup()
494 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700495 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800496
adminbae64d82013-08-01 10:50:15 -0700497 def intfs(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700498 main.log.info(self.name+": List interfaces")
Jon Hall6094a362014-04-11 14:46:56 -0700499 try:
500 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800501 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700502 main.log.error(self.name + ": EOF exception found")
503 main.log.error(self.name + ": " + self.handle.before)
504 main.cleanup()
505 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700506 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800507
adminbae64d82013-08-01 10:50:15 -0700508 def net(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700509 main.log.info(self.name+": List network connections")
Jon Hall6094a362014-04-11 14:46:56 -0700510 try:
511 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800512 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700513 main.log.error(self.name + ": EOF exception found")
514 main.log.error(self.name + ": " + self.handle.before)
515 main.cleanup()
516 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700517 return response
shahshreyae6c7cf42014-11-26 16:39:01 -0800518 '''
519 def iperf(self,host1,host2):
Jon Hallf2942ce2014-04-10 16:00:16 -0700520 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700521 try:
shahshreyae6c7cf42014-11-26 16:39:01 -0800522 if not host1 and not host2:
523 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
524 else:
525 cmd1 = 'iperf '+ host1 + " " + host2
526 response = self.execute(cmd = cmd1, prompt = '>',timeout = 20)
Jon Hallfbc828e2015-01-06 17:30:19 -0800527 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700528 main.log.error(self.name + ": EOF exception found")
529 main.log.error(self.name + ": " + self.handle.before)
530 main.cleanup()
531 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700532 return response
shahshreyae6c7cf42014-11-26 16:39:01 -0800533 '''
534 def iperf(self,host1,host2):
535 main.log.info(self.name+": Simple iperf TCP test between two hosts")
536 try:
537 cmd1 = 'iperf '+ host1 + " " + host2
538 self.handle.sendline(cmd1)
Jon Hallfbc828e2015-01-06 17:30:19 -0800539 self.handle.expect("mininet>")
shahshreyae6c7cf42014-11-26 16:39:01 -0800540 response = self.handle.before
541 if re.search('Results:',response):
542 main.log.info(self.name+": iperf test succssful")
543 return main.TRUE
544 else:
545 main.log.error(self.name+": iperf test failed")
546 return main.FALSE
547 except pexpect.EOF:
548 main.log.error(self.name + ": EOF exception found")
549 main.log.error(self.name + ": " + self.handle.before)
550 main.cleanup()
551 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800552
adminbae64d82013-08-01 10:50:15 -0700553 def iperfudp(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700554 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700555 try:
556 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800557 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700558 main.log.error(self.name + ": EOF exception found")
559 main.log.error(self.name + ": " + self.handle.before)
560 main.cleanup()
561 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700562 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800563
adminbae64d82013-08-01 10:50:15 -0700564 def nodes(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700565 main.log.info(self.name+": List all nodes.")
Jon Hall6094a362014-04-11 14:46:56 -0700566 try:
Jon Hallfbc828e2015-01-06 17:30:19 -0800567 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
568 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700569 main.log.error(self.name + ": EOF exception found")
570 main.log.error(self.name + ": " + self.handle.before)
571 main.cleanup()
572 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700573 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800574
adminbae64d82013-08-01 10:50:15 -0700575 def pingpair(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700576 main.log.info(self.name+": Ping between first two hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700577 try:
578 response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
Jon Hallfbc828e2015-01-06 17:30:19 -0800579 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700580 main.log.error(self.name + ": EOF exception found")
581 main.log.error(self.name + ": " + self.handle.before)
582 main.cleanup()
583 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800584
Jon Hallf2942ce2014-04-10 16:00:16 -0700585 #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
586 if re.search(',\s0\%\spacket\sloss',response):
587 main.log.info(self.name+": Ping between two hosts SUCCESSFUL")
Jon Hallfbc828e2015-01-06 17:30:19 -0800588 main.last_result = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700589 return main.TRUE
590 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700591 main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700592 main.last_result = main.FALSE
593 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800594
adminbae64d82013-08-01 10:50:15 -0700595 def link(self,**linkargs):
596 '''
597 Bring link(s) between two nodes up or down
598 '''
adminbae64d82013-08-01 10:50:15 -0700599 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
600 end1 = args["END1"] if args["END1"] != None else ""
601 end2 = args["END2"] if args["END2"] != None else ""
602 option = args["OPTION"] if args["OPTION"] != None else ""
Jon Hall38481722014-11-04 16:50:05 -0500603 main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'")
adminbae64d82013-08-01 10:50:15 -0700604 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
Jon Hall6094a362014-04-11 14:46:56 -0700605 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700606 #response = self.execute(cmd=command,prompt="mininet>",timeout=10)
607 self.handle.sendline(command)
Jon Hallfbc828e2015-01-06 17:30:19 -0800608 self.handle.expect("mininet>")
609 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700610 main.log.error(self.name + ": EOF exception found")
611 main.log.error(self.name + ": " + self.handle.before)
612 main.cleanup()
613 main.exit()
adminbae64d82013-08-01 10:50:15 -0700614 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800615
adminbae64d82013-08-01 10:50:15 -0700616
admin530b4c92013-08-14 16:54:35 -0700617 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700618 '''
admin530b4c92013-08-14 16:54:35 -0700619 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700620 '''
admin530b4c92013-08-14 16:54:35 -0700621 main.log.info('Yank the switch interface attached to a host')
622 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700623 sw = args["SW"] if args["SW"] !=None else ""
624 intf = args["INTF"] if args["INTF"] != None else ""
625 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700626 try:
627 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800628 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700629 main.log.error(self.name + ": EOF exception found")
630 main.log.error(self.name + ": " + self.handle.before)
631 main.cleanup()
632 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700633 return main.TRUE
634
635 def plug(self, **plugargs):
636 '''
637 plug the yanked mininet switch interface to a switch
638 '''
639 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700640 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700641 sw = args["SW"] if args["SW"] !=None else ""
642 intf = args["INTF"] if args["INTF"] != None else ""
643 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700644 try:
645 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800646 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700647 main.log.error(self.name + ": EOF exception found")
648 main.log.error(self.name + ": " + self.handle.before)
649 main.cleanup()
650 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700651 return main.TRUE
652
653
654
adminbae64d82013-08-01 10:50:15 -0700655 def dpctl(self,**dpctlargs):
656 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700657 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700658 '''
659 main.log.info('Run dpctl command on all switches')
660 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
661 cmd = args["CMD"] if args["CMD"] != None else ""
662 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
663 command = "dpctl "+cmd + " " + str(cmdargs)
Jon Hall6094a362014-04-11 14:46:56 -0700664 try:
665 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800666 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700667 main.log.error(self.name + ": EOF exception found")
668 main.log.error(self.name + ": " + self.handle.before)
669 main.cleanup()
670 main.exit()
adminbae64d82013-08-01 10:50:15 -0700671 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800672
673
adminbae64d82013-08-01 10:50:15 -0700674 def get_version(self):
675 file_input = path+'/lib/Mininet/INSTALL'
676 version = super(Mininet, self).get_version()
677 pattern = 'Mininet\s\w\.\w\.\w\w*'
678 for line in open(file_input,'r').readlines():
679 result = re.match(pattern, line)
680 if result:
681 version = result.group(0)
Jon Hallec3c21e2014-11-10 22:22:37 -0500682 return version
adminbae64d82013-08-01 10:50:15 -0700683
Jon Hallec3c21e2014-11-10 22:22:37 -0500684 def get_sw_controller(self, sw):
685 '''
686 Parameters:
687 sw: The name of an OVS switch. Example "s1"
688 Return:
689 The output of the command from the mininet cli or main.FALSE on timeout
690 '''
admin2a9548d2014-06-17 14:08:07 -0700691 command = "sh ovs-vsctl get-controller "+str(sw)
692 try:
Jon Hallec3c21e2014-11-10 22:22:37 -0500693 response = self.execute(cmd=command, prompt="mininet>", timeout=10)
admin2a9548d2014-06-17 14:08:07 -0700694 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500695 return response
admin2a9548d2014-06-17 14:08:07 -0700696 else:
697 return main.FALSE
698 except pexpect.EOF:
699 main.log.error(self.name + ": EOF exception found")
700 main.log.error(self.name + ": " + self.handle.before)
701 main.cleanup()
702 main.exit()
adminbae64d82013-08-01 10:50:15 -0700703
704 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700705 '''
706 count is only needed if there is more than 1 controller
707 '''
708 args = utilities.parse_args(["COUNT"],**kwargs)
709 count = args["COUNT"] if args!={} else 1
710
711 argstring = "SW"
712 for j in range(count):
713 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
714 args = utilities.parse_args(argstring.split(","),**kwargs)
715
adminbae64d82013-08-01 10:50:15 -0700716 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700717 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700718 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -0800719
Jon Hallf89c8552014-04-02 13:14:06 -0700720 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
721 for j in range(count):
722 i=j+1
723 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
724 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
725 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
726 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
727 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700728 try:
729 self.execute(cmd=command,prompt="mininet>",timeout=5)
730 except pexpect.EOF:
731 main.log.error(self.name + ": EOF exception found")
732 main.log.error(self.name + ": " + self.handle.before)
733 main.cleanup()
734 main.exit()
735 except:
736 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
737 main.log.error( traceback.print_exc() )
738 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
739 main.cleanup()
740 main.exit()
adminbae64d82013-08-01 10:50:15 -0700741
Jon Hall0819fd92014-05-23 12:08:13 -0700742 def delete_sw_controller(self,sw):
743 '''
744 Removes the controller target from sw
745 '''
746
747 command = "sh ovs-vsctl del-controller "+str(sw)
748 try:
749 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
Jon Hallfbc828e2015-01-06 17:30:19 -0800750 except pexpect.EOF:
Jon Hall0819fd92014-05-23 12:08:13 -0700751 main.log.error(self.name + ": EOF exception found")
752 main.log.error(self.name + ": " + self.handle.before)
753 main.cleanup()
754 main.exit()
755 else:
756 main.log.info(response)
757
Jon Hallb1290e82014-11-18 16:17:48 -0500758 def add_switch( self, sw, **kwargs ):
759 '''
760 adds a switch to the mininet topology
761 NOTE: this uses a custom mn function
762 NOTE: cannot currently specify what type of switch
763 required params:
764 switchname = name of the new switch as a string
765 optional keyvalues:
766 dpid = "dpid"
767 returns: main.FASLE on an error, else main.TRUE
768 '''
769 dpid = kwargs.get('dpid', '')
Jon Hallffb386d2014-11-21 13:43:38 -0800770 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500771 try:
772 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
773 if re.search("already exists!", response):
774 main.log.warn(response)
775 return main.FALSE
776 elif re.search("Error", response):
777 main.log.warn(response)
778 return main.FALSE
779 elif re.search("usage:", response):
780 main.log.warn(response)
781 return main.FALSE
782 else:
783 return main.TRUE
784 except pexpect.EOF:
785 main.log.error(self.name + ": EOF exception found")
786 main.log.error(self.name + ": " + self.handle.before)
787 main.cleanup()
788 main.exit()
789
790 def del_switch( self, sw ):
791 '''
792 delete a switch from the mininet topology
793 NOTE: this uses a custom mn function
794 required params:
795 switchname = name of the switch as a string
796 returns: main.FASLE on an error, else main.TRUE
797 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800798 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500799 try:
800 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
801 if re.search("no switch named", response):
802 main.log.warn(response)
803 return main.FALSE
804 elif re.search("Error", response):
805 main.log.warn(response)
806 return main.FALSE
807 elif re.search("usage:", response):
808 main.log.warn(response)
809 return main.FALSE
810 else:
811 return main.TRUE
812 except pexpect.EOF:
813 main.log.error(self.name + ": EOF exception found")
814 main.log.error(self.name + ": " + self.handle.before)
815 main.cleanup()
816 main.exit()
817
818 def add_link( self, node1, node2 ):
819 '''
820 add a link to the mininet topology
821 NOTE: this uses a custom mn function
822 NOTE: cannot currently specify what type of link
823 required params:
824 node1 = the string node name of the first endpoint of the link
825 node2 = the string node name of the second endpoint of the link
826 returns: main.FASLE on an error, else main.TRUE
827 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800828 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500829 try:
830 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
831 if re.search("doesnt exist!", response):
832 main.log.warn(response)
833 return main.FALSE
834 elif re.search("Error", response):
835 main.log.warn(response)
836 return main.FALSE
837 elif re.search("usage:", response):
838 main.log.warn(response)
839 return main.FALSE
840 else:
841 return main.TRUE
842 except pexpect.EOF:
843 main.log.error(self.name + ": EOF exception found")
844 main.log.error(self.name + ": " + self.handle.before)
845 main.cleanup()
846 main.exit()
847
848 def del_link( self, node1, node2 ):
849 '''
850 delete a link from the mininet topology
851 NOTE: this uses a custom mn function
852 required params:
853 node1 = the string node name of the first endpoint of the link
854 node2 = the string node name of the second endpoint of the link
855 returns: main.FASLE on an error, else main.TRUE
856 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800857 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500858 try:
859 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
860 if re.search("no node named", response):
861 main.log.warn(response)
862 return main.FALSE
863 elif re.search("Error", response):
864 main.log.warn(response)
865 return main.FALSE
866 elif re.search("usage:", response):
867 main.log.warn(response)
868 return main.FALSE
869 else:
870 return main.TRUE
871 except pexpect.EOF:
872 main.log.error(self.name + ": EOF exception found")
873 main.log.error(self.name + ": " + self.handle.before)
874 main.cleanup()
875 main.exit()
876
877 def add_host( self, hostname, **kwargs ):
878 '''
879 Add a host to the mininet topology
880 NOTE: this uses a custom mn function
881 NOTE: cannot currently specify what type of host
882 required params:
883 hostname = the string hostname
884 optional key-value params
885 switch = "switch name"
886 returns: main.FASLE on an error, else main.TRUE
887 '''
888 switch = kwargs.get('switch', '')
Jon Hallffb386d2014-11-21 13:43:38 -0800889 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -0500890 try:
891 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
892 if re.search("already exists!", response):
893 main.log.warn(response)
894 return main.FALSE
895 elif re.search("doesnt exists!", response):
896 main.log.warn(response)
897 return main.FALSE
898 elif re.search("Error", response):
899 main.log.warn(response)
900 return main.FALSE
901 elif re.search("usage:", response):
902 main.log.warn(response)
903 return main.FALSE
904 else:
905 return main.TRUE
906 except pexpect.EOF:
907 main.log.error(self.name + ": EOF exception found")
908 main.log.error(self.name + ": " + self.handle.before)
909 main.cleanup()
910 main.exit()
911
912 def del_host( self, hostname ):
913 '''
914 delete a host from the mininet topology
915 NOTE: this uses a custom mn function
916 required params:
917 hostname = the string hostname
918 returns: main.FASLE on an error, else main.TRUE
919 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800920 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -0500921 try:
922 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
923 if re.search("no host named", response):
924 main.log.warn(response)
925 return main.FALSE
926 elif re.search("Error", response):
927 main.log.warn(response)
928 return main.FALSE
929 elif re.search("usage:", response):
930 main.log.warn(response)
931 return main.FALSE
932 else:
933 return main.TRUE
934 except pexpect.EOF:
935 main.log.error(self.name + ": EOF exception found")
936 main.log.error(self.name + ": " + self.handle.before)
937 main.cleanup()
938 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -0700939
adminbae64d82013-08-01 10:50:15 -0700940 def disconnect(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700941 main.log.info(self.name+": Disconnecting mininet...")
adminbae64d82013-08-01 10:50:15 -0700942 response = ''
943 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700944 try:
945 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
946 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
Jon Halle80ef8c2014-04-29 15:29:13 -0700947 self.handle.sendline("sudo mn -c")
shahshreya328c2a72014-11-17 10:19:50 -0800948 response = main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800949 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700950 main.log.error(self.name + ": EOF exception found")
951 main.log.error(self.name + ": " + self.handle.before)
952 main.cleanup()
953 main.exit()
adminbae64d82013-08-01 10:50:15 -0700954 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700955 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700956 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800957 return response
958
admin07529932013-11-22 14:58:28 -0800959 def arping(self, src, dest, destmac):
960 self.handle.sendline('')
Jon Hall333fa8c2014-04-11 11:24:58 -0700961 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800962
963 self.handle.sendline(src + ' arping ' + dest)
964 try:
Jon Hall333fa8c2014-04-11 11:24:58 -0700965 self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT])
Jon Hallf2942ce2014-04-10 16:00:16 -0700966 main.log.info(self.name+": ARP successful")
Jon Hall333fa8c2014-04-11 11:24:58 -0700967 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800968 return main.TRUE
969 except:
Jon Hallf2942ce2014-04-10 16:00:16 -0700970 main.log.warn(self.name+": ARP FAILURE")
Jon Hall333fa8c2014-04-11 11:24:58 -0700971 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800972 return main.FALSE
973
974 def decToHex(num):
975 return hex(num).split('x')[1]
Jon Hallfbc828e2015-01-06 17:30:19 -0800976
admin2a9548d2014-06-17 14:08:07 -0700977 def getSwitchFlowCount(self, switch):
978 '''
979 return the Flow Count of the switch
980 '''
981 if self.handle:
982 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
983 try:
984 response = self.execute(cmd=cmd, prompt="mininet>", timeout=10)
985 except pexpect.EOF:
986 main.log.error(self.name + ": EOF exception found")
987 main.log.error(self.name + " " + self.handle.before)
988 main.cleanup()
989 main.exit()
990 pattern = "flow_count=(\d+)"
991 result = re.search(pattern, response, re.MULTILINE)
992 if result is None:
admin2a9548d2014-06-17 14:08:07 -0700993 main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response))
994 return main.FALSE
995 return result.group(1)
996 else:
997 main.log.error("Connection failed to the Mininet host")
Jon Hallfbc828e2015-01-06 17:30:19 -0800998
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -0700999 def check_flows(self, sw, dump_format=None):
1000 if dump_format:
1001 command = "sh ovs-ofctl -F " + dump_format + " dump-flows " + str(sw)
1002 else:
1003 command = "sh ovs-ofctl dump-flows "+str(sw)
admin2a9548d2014-06-17 14:08:07 -07001004 try:
1005 response=self.execute(cmd=command,prompt="mininet>",timeout=10)
1006 return response
1007 except pexpect.EOF:
1008 main.log.error(self.name + ": EOF exception found")
1009 main.log.error(self.name + ": " + self.handle.before)
1010 main.cleanup()
1011 main.exit()
1012 else:
1013 main.log.info(response)
1014
1015 def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"):
1016 '''
1017 Runs tpdump on an intferface and saves the file
1018 intf can be specified, or the default eth0 is used
1019 '''
1020 try:
1021 self.handle.sendline("")
1022 self.handle.expect("mininet>")
1023 self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &")
1024 self.handle.sendline("")
admin2a9548d2014-06-17 14:08:07 -07001025 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10)
1026 main.log.warn(self.handle.before + self.handle.after)
Jon Hallb1290e82014-11-18 16:17:48 -05001027 self.handle.sendline("")
1028 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001029 if i == 0:
1030 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
1031 return main.FALSE
1032 elif i == 1:
1033 main.log.info(self.name + ": tcpdump started on " + intf)
1034 return main.TRUE
1035 elif i == 2:
1036 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
1037 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001038 elif i ==3:
admin2a9548d2014-06-17 14:08:07 -07001039 main.log.info(self.name +": " + self.handle.before)
1040 return main.TRUE
1041 else:
1042 main.log.error(self.name + ": tcpdump - unexpected response")
1043 return main.FALSE
1044 except pexpect.EOF:
1045 main.log.error(self.name + ": EOF exception found")
1046 main.log.error(self.name + ": " + self.handle.before)
1047 main.cleanup()
1048 main.exit()
1049 except:
1050 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1051 main.log.error( traceback.print_exc() )
1052 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1053 main.cleanup()
1054 main.exit()
1055
1056 def stop_tcpdump(self):
1057 "pkills tcpdump"
1058 try:
1059 self.handle.sendline("sh sudo pkill tcpdump")
Jon Hallb1290e82014-11-18 16:17:48 -05001060 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001061 self.handle.sendline("")
1062 self.handle.expect("mininet>")
1063 except pexpect.EOF:
1064 main.log.error(self.name + ": EOF exception found")
1065 main.log.error(self.name + ": " + self.handle.before)
1066 main.cleanup()
1067 main.exit()
1068 except:
1069 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1070 main.log.error( traceback.print_exc() )
1071 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1072 main.cleanup()
1073 main.exit()
1074
Jon Hall3d87d502014-10-17 18:37:42 -04001075 def compare_switches(self, topo, switches_json):
1076 '''
1077 Compare mn and onos switches
1078 topo: sts TestONTopology object
1079 switches_json: parsed json object from the onos devices api
1080
1081 This uses the sts TestONTopology object
1082
1083 '''
1084 import json
Jon Hall42db6dc2014-10-24 19:03:48 -04001085 #main.log.debug("Switches_json string: ", switches_json)
Jon Hall3d87d502014-10-17 18:37:42 -04001086 output = {"switches":[]}
1087 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall3d87d502014-10-17 18:37:42 -04001088 ports = []
1089 for port in switch.ports.values():
Jon Hall3d87d502014-10-17 18:37:42 -04001090 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
1091 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
Jon Hall3d87d502014-10-17 18:37:42 -04001092
Jon Hall42db6dc2014-10-24 19:03:48 -04001093 #print "mn"
Jon Hall3d87d502014-10-17 18:37:42 -04001094 #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall42db6dc2014-10-24 19:03:48 -04001095 #print "onos"
1096 #print json.dumps(switches_json, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall3d87d502014-10-17 18:37:42 -04001097
1098
1099 # created sorted list of dpid's in MN and ONOS for comparison
1100 mnDPIDs=[]
1101 for switch in output['switches']:
Jon Hall28bf54b2014-12-17 16:25:44 -08001102 mnDPIDs.append(switch['dpid'].lower())
Jon Hall3d87d502014-10-17 18:37:42 -04001103 mnDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001104 #print "List of Mininet switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001105 #print mnDPIDs
1106 if switches_json == "":#if rest call fails
Jon Hall42db6dc2014-10-24 19:03:48 -04001107 main.log.error(self.name + ".compare_switches(): Empty JSON object given from ONOS")
Jon Hall3d87d502014-10-17 18:37:42 -04001108 return main.FALSE
1109 onos=switches_json
1110 onosDPIDs=[]
1111 for switch in onos:
Jon Hall38481722014-11-04 16:50:05 -05001112 if switch['available'] == True:
Jon Hall28bf54b2014-12-17 16:25:44 -08001113 onosDPIDs.append(switch['id'].replace(":",'').replace("of",'').lower())
Jon Hall38481722014-11-04 16:50:05 -05001114 #else:
1115 #print "Switch is unavailable:"
1116 #print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001117 onosDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001118 #print "List of ONOS switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001119 #print onosDPIDs
1120
1121 if mnDPIDs!=onosDPIDs:
1122 switch_results = main.FALSE
1123 main.log.report( "Switches in MN but not in ONOS:")
1124 main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs]))
1125 main.log.report( "Switches in ONOS but not in MN:")
1126 main.log.report( str([switch for switch in onosDPIDs if switch not in mnDPIDs]))
1127 else:#list of dpid's match in onos and mn
1128 #main.log.report("DEBUG: The dpid's of the switches in Mininet and ONOS match")
1129 switch_results = main.TRUE
1130 return switch_results
1131
1132
1133
Jon Hall72cf1dc2014-10-20 21:04:50 -04001134 def compare_ports(self, topo, ports_json):
1135 '''
1136 Compare mn and onos ports
1137 topo: sts TestONTopology object
1138 ports_json: parsed json object from the onos ports api
1139
Jon Hallfbc828e2015-01-06 17:30:19 -08001140 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001141 1. This uses the sts TestONTopology object
1142 2. numpy - "sudo pip install numpy"
1143
1144 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001145 #FIXME: this does not look for extra ports in ONOS, only checks that ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001146 import json
1147 from numpy import uint64
Jon Hallb1290e82014-11-18 16:17:48 -05001148 ports_results = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001149 output = {"switches":[]}
1150 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall72cf1dc2014-10-20 21:04:50 -04001151 ports = []
1152 for port in switch.ports.values():
1153 #print port.hw_addr.toStr(separator = '')
Jon Hall39f29df2014-11-04 19:30:21 -05001154 tmp_port = {}
1155 tmp_port['of_port'] = port.port_no
1156 tmp_port['mac'] = str(port.hw_addr).replace('\'','')
1157 tmp_port['name'] = port.name
1158 tmp_port['enabled'] = port.enabled
1159
1160 ports.append(tmp_port)
1161 tmp_switch = {}
1162 tmp_switch['name'] = switch.name
1163 tmp_switch['dpid'] = str(switch.dpid).zfill(16)
1164 tmp_switch['ports'] = ports
1165
1166 output['switches'].append(tmp_switch)
Jon Hall72cf1dc2014-10-20 21:04:50 -04001167
1168
1169 ################ports#############
Jon Hall39f29df2014-11-04 19:30:21 -05001170 for mn_switch in output['switches']:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001171 mn_ports = []
1172 onos_ports = []
Jon Hallb1290e82014-11-18 16:17:48 -05001173 switch_result = main.TRUE
Jon Hall39f29df2014-11-04 19:30:21 -05001174 for port in mn_switch['ports']:
Jon Hall38481722014-11-04 16:50:05 -05001175 if port['enabled'] == True:
1176 mn_ports.append(port['of_port'])
Jon Hallfbc828e2015-01-06 17:30:19 -08001177 #else: #DEBUG only
Jon Hallb1290e82014-11-18 16:17:48 -05001178 # main.log.warn("Port %s on switch %s is down" % ( str(port['of_port']) , str(mn_switch['name'])) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001179 for onos_switch in ports_json:
Jon Hall38481722014-11-04 16:50:05 -05001180 #print "Iterating through a new switch as seen by ONOS"
1181 #print onos_switch
1182 if onos_switch['device']['available'] == True:
Jon Hall39f29df2014-11-04 19:30:21 -05001183 if onos_switch['device']['id'].replace(':','').replace("of", '') == mn_switch['dpid']:
Jon Hall38481722014-11-04 16:50:05 -05001184 for port in onos_switch['ports']:
1185 if port['isEnabled']:
1186 #print "Iterating through available ports on the switch"
1187 #print port
Jon Hallb1290e82014-11-18 16:17:48 -05001188 if port['port'] == 'local':
1189 #onos_ports.append('local')
1190 onos_ports.append(long(uint64(-2)))
1191 else:
1192 onos_ports.append(int(port['port']))
1193 '''
1194 else: #This is likely a new reserved port implemented
1195 main.log.error("unkown port '" + str(port['port']) )
1196 '''
Jon Hall1645caa2014-11-18 16:27:14 -05001197 #else: #DEBUG
1198 # main.log.warn("Port %s on switch %s is down" % ( str(port['port']) , str(onos_switch['device']['id'])) )
Jon Hallb1290e82014-11-18 16:17:48 -05001199 break
Jon Hall72cf1dc2014-10-20 21:04:50 -04001200 mn_ports.sort(key=float)
1201 onos_ports.sort(key=float)
Jon Hall94fd0472014-12-08 11:52:42 -08001202 #print "\nPorts for Switch %s:" % (mn_switch['name'])
Jon Hall72cf1dc2014-10-20 21:04:50 -04001203 #print "\tmn_ports[] = ", mn_ports
1204 #print "\tonos_ports[] = ", onos_ports
Jon Hallb1290e82014-11-18 16:17:48 -05001205 mn_ports_log = mn_ports
1206 onos_ports_log = onos_ports
1207 mn_ports = [x for x in mn_ports]
1208 onos_ports = [x for x in onos_ports]
Jon Hall38481722014-11-04 16:50:05 -05001209
Jon Hall72cf1dc2014-10-20 21:04:50 -04001210 #TODO: handle other reserved port numbers besides LOCAL
Jon Hallb1290e82014-11-18 16:17:48 -05001211 #NOTE: Reserved ports
1212 # Local port: -2 in Openflow, ONOS shows 'local', we store as long(uint64(-2))
1213 for mn_port in mn_ports_log:
1214 if mn_port in onos_ports:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001215 #don't set results to true here as this is just one of many checks and it might override a failure
Jon Hallb1290e82014-11-18 16:17:48 -05001216 mn_ports.remove(mn_port)
1217 onos_ports.remove(mn_port)
1218 #NOTE: OVS reports this as down since there is no link
1219 # So ignoring these for now
1220 #TODO: Come up with a better way of handling these
1221 if 65534 in mn_ports:
1222 mn_ports.remove(65534)
1223 if long(uint64(-2)) in onos_ports:
1224 onos_ports.remove( long(uint64(-2)) )
1225 if len(mn_ports): #the ports of this switch don't match
1226 switch_result = main.FALSE
1227 main.log.warn("Ports in MN but not ONOS: " + str(mn_ports) )
1228 if len(onos_ports): #the ports of this switch don't match
1229 switch_result = main.FALSE
1230 main.log.warn("Ports in ONOS but not MN: " + str(onos_ports) )
1231 if switch_result == main.FALSE:
Jon Hall39f29df2014-11-04 19:30:21 -05001232 main.log.report("The list of ports for switch %s(%s) does not match:" % (mn_switch['name'], mn_switch['dpid']) )
Jon Hallb1290e82014-11-18 16:17:48 -05001233 main.log.warn("mn_ports[] = " + str(mn_ports_log))
1234 main.log.warn("onos_ports[] = " + str(onos_ports_log))
1235 ports_results = ports_results and switch_result
1236 return ports_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001237
1238
1239
1240
1241 def compare_links(self, topo, links_json):
1242 '''
1243 Compare mn and onos links
1244 topo: sts TestONTopology object
1245 links_json: parsed json object from the onos links api
1246
1247 This uses the sts TestONTopology object
1248
1249 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001250 #FIXME: this does not look for extra links in ONOS, only checks that ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001251 import json
1252 link_results = main.TRUE
1253 output = {"switches":[]}
1254 onos = links_json
1255 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall38481722014-11-04 16:50:05 -05001256 # print "Iterating though switches as seen by Mininet"
1257 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001258 ports = []
1259 for port in switch.ports.values():
1260 #print port.hw_addr.toStr(separator = '')
1261 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
1262 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
1263 #######Links########
1264
Jon Hall38481722014-11-04 16:50:05 -05001265 mn_links = [link for link in topo.patch_panel.network_links if (link.port1.enabled and link.port2.enabled)]
1266 #print "mn_links:"
1267 #print mn_links
1268 if 2*len(mn_links) == len(onos):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001269 link_results = main.TRUE
1270 else:
1271 link_results = main.FALSE
Jon Hall38481722014-11-04 16:50:05 -05001272 main.log.report("Mininet has %i bidirectional links and ONOS has %i unidirectional links" % (len(mn_links), len(onos) ))
Jon Hall72cf1dc2014-10-20 21:04:50 -04001273
1274
1275 # iterate through MN links and check if an ONOS link exists in both directions
Jon Hallfbc828e2015-01-06 17:30:19 -08001276 # NOTE: Will currently only show mn links as down if they are cut through STS.
1277 # We can either do everything through STS or wait for up_network_links
Jon Hall72cf1dc2014-10-20 21:04:50 -04001278 # and down_network_links to be fully implemented.
Jon Hallfbc828e2015-01-06 17:30:19 -08001279 for link in mn_links:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001280 #print "Link: %s" % link
1281 #TODO: Find a more efficient search method
1282 node1 = None
1283 port1 = None
1284 node2 = None
1285 port2 = None
1286 first_dir = main.FALSE
1287 second_dir = main.FALSE
1288 for switch in output['switches']:
1289 #print "Switch: %s" % switch['name']
1290 if switch['name'] == link.node1.name:
1291 node1 = switch['dpid']
1292 for port in switch['ports']:
1293 if str(port['name']) == str(link.port1):
Jon Hallfbc828e2015-01-06 17:30:19 -08001294 port1 = port['of_port']
Jon Hall72cf1dc2014-10-20 21:04:50 -04001295 if node1 is not None and node2 is not None:
1296 break
1297 if switch['name'] == link.node2.name:
1298 node2 = switch['dpid']
1299 for port in switch['ports']:
1300 if str(port['name']) == str(link.port2):
1301 port2 = port['of_port']
1302 if node1 is not None and node2 is not None:
1303 break
1304
1305
1306 for onos_link in onos:
1307 onos_node1 = onos_link['src']['device'].replace(":",'').replace("of", '')
1308 onos_node2 = onos_link['dst']['device'].replace(":",'').replace("of", '')
1309 onos_port1 = onos_link['src']['port']
1310 onos_port2 = onos_link['dst']['port']
1311
1312 #print "Checking ONOS for link %s/%s -> %s/%s and" % (node1, port1, node2, port2)
1313 #print "Checking ONOS for link %s/%s -> %s/%s" % (node2, port2, node1, port1)
1314 # check onos link from node1 to node2
1315 if str(onos_node1) == str(node1) and str(onos_node2) == str(node2):
1316 if int(onos_port1) == int(port1) and int(onos_port2) == int(port2):
1317 first_dir = main.TRUE
1318 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001319 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001320 ' between ONOS and MN. When cheking ONOS for link '+\
1321 '%s/%s -> %s/%s' % (node1, port1, node2, port2)+\
1322 ' ONOS has the values %s/%s -> %s/%s' %\
1323 (onos_node1, onos_port1, onos_node2, onos_port2))
1324
1325 # check onos link from node2 to node1
1326 elif ( str(onos_node1) == str(node2) and str(onos_node2) == str(node1) ):
1327 if ( int(onos_port1) == int(port2) and int(onos_port2) == int(port1) ):
1328 second_dir = main.TRUE
1329 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001330 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001331 ' between ONOS and MN. When cheking ONOS for link '+\
1332 '%s/%s -> %s/%s' % (node2, port2, node1, port1)+\
1333 ' ONOS has the values %s/%s -> %s/%s' %\
1334 (onos_node2, onos_port2, onos_node1, onos_port1))
1335 else:#this is not the link you're looking for
1336 pass
1337 if not first_dir:
1338 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node1, port1, node2, port2))
1339 if not second_dir:
1340 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node2, port2, node1, port1))
1341 link_results = link_results and first_dir and second_dir
Jon Hall62df9242014-10-22 12:20:17 -04001342 return link_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001343
1344
andrewonlab3f0a4af2014-10-17 12:25:14 -04001345 def get_hosts(self):
1346 '''
1347 Returns a list of all hosts
1348 Don't ask questions just use it
1349 '''
1350 self.handle.sendline("")
1351 self.handle.expect("mininet>")
Jon Hallfbc828e2015-01-06 17:30:19 -08001352
andrewonlab3f0a4af2014-10-17 12:25:14 -04001353 self.handle.sendline("py [ host.name for host in net.hosts ]")
1354 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001355
andrewonlab3f0a4af2014-10-17 12:25:14 -04001356 handle_py = self.handle.before
1357 handle_py = handle_py.split("]\r\n",1)[1]
1358 handle_py = handle_py.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001359
andrewonlab3f0a4af2014-10-17 12:25:14 -04001360 self.handle.sendline("")
1361 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001362
andrewonlab3f0a4af2014-10-17 12:25:14 -04001363 host_str = handle_py.replace("]", "")
1364 host_str = host_str.replace("'", "")
1365 host_str = host_str.replace("[", "")
1366 host_list = host_str.split(",")
1367
Jon Hallfbc828e2015-01-06 17:30:19 -08001368 return host_list
adminbae64d82013-08-01 10:50:15 -07001369
Jon Hall38481722014-11-04 16:50:05 -05001370
1371 def update(self):
1372 '''
1373 updates the port address and status information for each port in mn
1374 '''
1375 #TODO: Add error checking. currently the mininet command has no output
1376 main.log.info("Updateing MN port information")
Jon Hallb1290e82014-11-18 16:17:48 -05001377 try:
1378 self.handle.sendline("")
1379 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001380
Jon Hallb1290e82014-11-18 16:17:48 -05001381 self.handle.sendline("update")
1382 self.handle.expect("update")
1383 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001384
Jon Hallb1290e82014-11-18 16:17:48 -05001385 self.handle.sendline("")
1386 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001387
Jon Hallb1290e82014-11-18 16:17:48 -05001388 return main.TRUE
1389 except pexpect.EOF:
1390 main.log.error(self.name + ": EOF exception found")
1391 main.log.error(self.name + ": " + self.handle.before)
1392 main.cleanup()
1393 main.exit()
1394
adminbae64d82013-08-01 10:50:15 -07001395if __name__ != "__main__":
1396 import sys
1397 sys.modules[__name__] = MininetCliDriver()
admin2a9548d2014-06-17 14:08:07 -07001398