blob: bf3a2c66a8c179b08f863a5becf0f522aae7a127 [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
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20
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
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import core.teston
33sys.path.append("../")
Jon Hall1ccf82c2014-10-15 14:55:16 -040034from math import pow
adminbae64d82013-08-01 10:50:15 -070035from drivers.common.cli.emulatordriver import Emulator
36from drivers.common.clidriver import CLI
37
38class MininetCliDriver(Emulator):
39 '''
Jon Hall41f40e82014-04-08 16:43:17 -070040 MininetCliDriver is the basic driver which will handle the Mininet functions
adminbae64d82013-08-01 10:50:15 -070041 '''
42 def __init__(self):
43 super(Emulator, self).__init__()
44 self.handle = self
45 self.wrapped = sys.modules[__name__]
46 self.flag = 0
47
48 def connect(self, **connectargs):
Jon Hall41f40e82014-04-08 16:43:17 -070049 '''
50 Here the main is the TestON instance after creating all the log handles.
51 '''
adminbae64d82013-08-01 10:50:15 -070052 for key in connectargs:
53 vars(self)[key] = connectargs[key]
54
55 self.name = self.options['name']
56 self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd)
57
58 self.ssh_handle = self.handle
59
adminbae64d82013-08-01 10:50:15 -070060 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -070061 main.log.info(self.name+": Clearing any residual state or processes")
adminbae64d82013-08-01 10:50:15 -070062 self.handle.sendline("sudo mn -c")
adminf939f8b2014-04-03 17:22:56 -070063 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070064 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070065 main.log.info(self.name+": Sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070066 self.handle.sendline(self.pwd)
67 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070068 if i==1:
Jon Hallf2942ce2014-04-10 16:00:16 -070069 main.log.info(self.name+": Clean")
adminbae64d82013-08-01 10:50:15 -070070 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070071 main.log.error(self.name+": Connection terminated")
adminbae64d82013-08-01 10:50:15 -070072 elif i==3: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070073 main.log.error(self.name+": Something while cleaning MN took too long... " )
adminbae64d82013-08-01 10:50:15 -070074
Jon Hallf2942ce2014-04-10 16:00:16 -070075 main.log.info(self.name+": building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080076 #### for reactive/PARP enabled tests
shahshreyaf4d4d0c2014-10-10 12:11:10 -070077 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] + " " + self.options['arg3']
Jon Hall1ccf82c2014-10-15 14:55:16 -040078
79 argList = self.options['arg1'].split(",")
80 global topoArgList
81 topoArgList = argList[0].split(" ")
82 argList = map(int, argList[1:])
83 topoArgList = topoArgList[1:] + argList
84
85 #### for proactive flow with static ARP entries
shahshreyaf4d4d0c2014-10-10 12:11:10 -070086 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] + " " + self.options['arg3']
adminbae64d82013-08-01 10:50:15 -070087 self.handle.sendline(cmdString)
Jon Hall333fa8c2014-04-11 11:24:58 -070088 self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070089 while 1:
90 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
91 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070092 main.log.info(self.name+": mininet built")
adminbae64d82013-08-01 10:50:15 -070093 return main.TRUE
94 if i==1:
Jon Hall1645caa2014-11-18 16:27:14 -050095 self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070096 main.log.info(self.handle.before)
97 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070098 main.log.error(self.name+": Launching mininet failed...")
adminbae64d82013-08-01 10:50:15 -070099 return main.FALSE
100 elif i==3:
Jon Hallf2942ce2014-04-10 16:00:16 -0700101 main.log.error(self.name+": Connection timeout")
adminbae64d82013-08-01 10:50:15 -0700102 return main.FALSE
103 elif i==4: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -0700104 main.log.error(self.name+": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -0700105 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700106 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
107 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700108 else:#if no handle
109 main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address)
110 main.log.error(self.name+": Failed to connect to the Mininet")
adminbae64d82013-08-01 10:50:15 -0700111 return main.FALSE
Jon Hall1ccf82c2014-10-15 14:55:16 -0400112
113 def num_switches_n_links(self,topoType,depth,fanout):
114 if topoType == 'tree':
115 if fanout is None: #In tree topology, if fanout arg is not given, by default it is 2
116 fanout = 2
117 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500118 count = 0
Jon Hall1ccf82c2014-10-15 14:55:16 -0400119 while(k <= depth-1):
Jon Hall38481722014-11-04 16:50:05 -0500120 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400121 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500122 num_switches = count
Jon Hall1ccf82c2014-10-15 14:55:16 -0400123 while(k <= depth-2):
124 '''depth-2 gives you only core links and not considering edge links as seen by ONOS
125 If all the links including edge links are required, do depth-1
126 '''
Jon Hall38481722014-11-04 16:50:05 -0500127 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400128 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500129 num_links = count * fanout
Jon Hall1ccf82c2014-10-15 14:55:16 -0400130 #print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
131
132 elif topoType =='linear':
133 if fanout is None: #In linear topology, if fanout or num_hosts_per_sw is not given, by default it is 1
134 fanout = 1
135 num_switches = depth
136 num_hosts_per_sw = fanout
137 total_num_hosts = num_switches * num_hosts_per_sw
138 num_links = total_num_hosts + (num_switches - 1)
139 print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
140 topoDict = {}
141 topoDict = {"num_switches":int(num_switches), "num_corelinks":int(num_links)}
142 return topoDict
143
144
145 def calculate_sw_and_links(self):
146 topoDict = self.num_switches_n_links(*topoArgList)
147 return topoDict
148
Jon Hall1645caa2014-11-18 16:27:14 -0500149 def pingall(self, timeout=300):
adminbae64d82013-08-01 10:50:15 -0700150 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700151 Verifies the reachability of the hosts using pingall command.
Jon Hall1645caa2014-11-18 16:27:14 -0500152 Optional parameter timeout allows you to specify how long to wait for pingall to complete
153 Returns:
154 main.TRUE if pingall completes with no pings dropped
155 otherwise main.FALSE
adminbae64d82013-08-01 10:50:15 -0700156 '''
157 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -0700158 main.log.info(self.name+": Checking reachabilty to the hosts using pingall")
Jon Hall6094a362014-04-11 14:46:56 -0700159 try:
Jon Hall1645caa2014-11-18 16:27:14 -0500160 response = self.execute(cmd="pingall",prompt="mininet>",timeout=int(timeout))
Jon Hallb1290e82014-11-18 16:17:48 -0500161 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700162 main.log.error(self.name + ": EOF exception found")
163 main.log.error(self.name + ": " + self.handle.before)
Jon Hallb1290e82014-11-18 16:17:48 -0500164 main.cleanup()
165 main.exit()
166 except pexpect.TIMEOUT:
167 #We may not want to kill the test if pexpect times out
168 main.log.error(self.name + ": TIMEOUT exception found")
169 main.log.error(self.name + ": " + str(self.handle.before) )
170 #NOTE: mininet's pingall rounds, so we will check the number of passed and number of failed
171 pattern = "Results\:\s0\%\sdropped\s\((?P<passed>[\d]+)/(?P=passed)"
Jon Hallf2942ce2014-04-10 16:00:16 -0700172 if re.search(pattern,response):
173 main.log.info(self.name+": All hosts are reachable")
adminbae64d82013-08-01 10:50:15 -0700174 return main.TRUE
175 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700176 main.log.error(self.name+": Unable to reach all the hosts")
Jon Hallb1290e82014-11-18 16:17:48 -0500177 main.log.info("Pingall ouput: " + str(response))
178 #NOTE: Send ctrl-c to make sure pingall is done
179 self.handle.send("\x03")
180 self.handle.expect("Interrupt")
181 self.handle.expect("mininet>")
adminbae64d82013-08-01 10:50:15 -0700182 return main.FALSE
183 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700184 main.log.error(self.name+": Connection failed to the host")
Jon Hallb1290e82014-11-18 16:17:48 -0500185 main.cleanup()
186 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700187
188 def fpingHost(self,**pingParams):
189 '''
190 Uses the fping package for faster pinging...
191 *requires fping to be installed on machine running mininet
192 '''
193 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700194 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700195 self.handle.sendline(command)
Jon Hall333fa8c2014-04-11 11:24:58 -0700196 self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT])
197 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
adminaeedddd2013-08-02 15:14:15 -0700198 response = self.handle.before
199 if re.search(":\s-" ,response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700200 main.log.info(self.name+": Ping fail")
adminaeedddd2013-08-02 15:14:15 -0700201 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700202 elif re.search(":\s\d{1,2}\.\d\d", response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700203 main.log.info(self.name+": Ping good!")
adminaeedddd2013-08-02 15:14:15 -0700204 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700205 main.log.info(self.name+": Install fping on mininet machine... ")
206 main.log.info(self.name+": \n---\n"+response)
adminaeedddd2013-08-02 15:14:15 -0700207 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700208
209 def pingHost(self,**pingParams):
Jon Hallf2942ce2014-04-10 16:00:16 -0700210 '''
211 Ping from one mininet host to another
212 Currently the only supported Params: SRC and TARGET
213 '''
adminbae64d82013-08-01 10:50:15 -0700214 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
215 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hall0819fd92014-05-23 12:08:13 -0700216 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700217 try:
Jon Hall6e18c7b2014-04-23 16:26:33 -0700218 main.log.warn("Sending: " + command)
219 #response = self.execute(cmd=command,prompt="mininet",timeout=10 )
220 self.handle.sendline(command)
221 i = self.handle.expect([command,pexpect.TIMEOUT])
222 if i == 1:
223 main.log.error(self.name + ": timeout when waiting for response from mininet")
224 main.log.error("response: " + str(self.handle.before))
225 i = self.handle.expect(["mininet>",pexpect.TIMEOUT])
226 if i == 1:
227 main.log.error(self.name + ": timeout when waiting for response from mininet")
228 main.log.error("response: " + str(self.handle.before))
229 response = self.handle.before
Jon Hall6094a362014-04-11 14:46:56 -0700230 except pexpect.EOF:
231 main.log.error(self.name + ": EOF exception found")
232 main.log.error(self.name + ": " + self.handle.before)
233 main.cleanup()
234 main.exit()
Jon Hallf2942ce2014-04-10 16:00:16 -0700235 main.log.info(self.name+": Ping Response: "+ response )
236 #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
237 if re.search(',\s0\%\spacket\sloss',response):
Jon Hall6e18c7b2014-04-23 16:26:33 -0700238 main.log.info(self.name+": no packets lost, host is reachable")
adminbae64d82013-08-01 10:50:15 -0700239 main.last_result = main.TRUE
240 return main.TRUE
241 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700242 main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700243 main.last_result = main.FALSE
244 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700245
246 def checkIP(self,host):
247 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700248 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700249 '''
250 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700251 try:
252 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
253 except pexpect.EOF:
254 main.log.error(self.name + ": EOF exception found")
255 main.log.error(self.name + ": " + self.handle.before)
256 main.cleanup()
257 main.exit()
adminbae64d82013-08-01 10:50:15 -0700258
259 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})"
admin2a9548d2014-06-17 14:08:07 -0700260 #pattern = "inet addr:10.0.0.6"
Jon Hallf2942ce2014-04-10 16:00:16 -0700261 #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
262 if re.search(pattern,response):
263 main.log.info(self.name+": Host Ip configured properly")
adminbae64d82013-08-01 10:50:15 -0700264 return main.TRUE
265 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700266 main.log.error(self.name+": Host IP not found")
adminbae64d82013-08-01 10:50:15 -0700267 return main.FALSE
268 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700269 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700270
271 def verifySSH(self,**connectargs):
Jon Hall6094a362014-04-11 14:46:56 -0700272 try:
273 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
274 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
275 for key in connectargs:
276 vars(self)[key] = connectargs[key]
277 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
278 except pexpect.EOF:
279 main.log.error(self.name + ": EOF exception found")
280 main.log.error(self.name + ": " + self.handle.before)
281 main.cleanup()
282 main.exit()
adminbae64d82013-08-01 10:50:15 -0700283 import time
284 time.sleep(20)
285 if self.flag == 0:
286 self.flag = 1
287 return main.FALSE
288 else :
289 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800290
adminbae64d82013-08-01 10:50:15 -0700291
shahshreyae6c7cf42014-11-26 16:39:01 -0800292
293
294 def changeIP(self,host,intf,newIP,newNetmask):
295 '''
296 Changes the ip address of a host on the fly
297 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0
298 '''
299 if self.handle:
300 try:
shahshreyad0c80432014-12-04 16:56:05 -0800301 cmd = host+" ifconfig "+intf+" "+newIP+" "+'netmask'+" "+newNetmask
shahshreyae6c7cf42014-11-26 16:39:01 -0800302 self.handle.sendline(cmd)
303 self.handle.expect("mininet>")
304 response = self.handle.before
shahshreyad0c80432014-12-04 16:56:05 -0800305 main.log.info("response = "+response)
shahshreyae6c7cf42014-11-26 16:39:01 -0800306 main.log.info("Ip of host "+host+" changed to new IP "+newIP)
307 return main.TRUE
308 except pexpect.EOF:
309 main.log.error(self.name + ": EOF exception found")
310 main.log.error(self.name + ": " + self.handle.before)
311 return main.FALSE
312
313 def changeDefaultGateway(self,host,newGW):
314 '''
315 Changes the default gateway of a host
316 Ex: h1 route add default gw 10.0.1.2
317 '''
318 if self.handle:
319 try:
320 cmd = host+" route add default gw "+newGW
321 self.handle.sendline(cmd)
322 self.handle.expect("mininet>")
323 response = self.handle.before
shahshreyad0c80432014-12-04 16:56:05 -0800324 main.log.info("response = "+response)
shahshreyae6c7cf42014-11-26 16:39:01 -0800325 main.log.info("Default gateway of host "+host+" changed to "+newGW)
326 return main.TRUE
327 except pexpect.EOF:
328 main.log.error(self.name + ": EOF exception found")
329 main.log.error(self.name + ": " + self.handle.before)
330 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -0800331
332 def addStaticMACAddress(self,host,GW,macaddr):
333 '''
334 Changes the mac address of a geateway host
335 '''
336 if self.handle:
337 try:
338 #h1 arp -s 10.0.1.254 00:00:00:00:11:11
339 cmd = host+" arp -s "+GW+" "+macaddr
340 self.handle.sendline(cmd)
341 self.handle.expect("mininet>")
342 response = self.handle.before
343 main.log.info("response = "+response)
344 main.log.info("Mac adrress of gateway "+GW+" changed to "+macaddr)
345 return main.TRUE
346 except pexpect.EOF:
347 main.log.error(self.name + ": EOF exception found")
348 main.log.error(self.name + ": " + self.handle.before)
349 return main.FALSE
350
351 def verifyStaticGWandMAC(self,host):
352 '''
353 Verify if the static gateway and mac address assignment
354 '''
355 if self.handle:
356 try:
357 #h1 arp -an
358 cmd = host+" arp -an "
359 self.handle.sendline(cmd)
360 self.handle.expect("mininet>")
361 response = self.handle.before
362 main.log.info(host+" arp -an = "+response)
363 return main.TRUE
364 except pexpect.EOF:
365 main.log.error(self.name + ": EOF exception found")
366 main.log.error(self.name + ": " + self.handle.before)
367 return main.FALSE
368
369
shahshreyae6c7cf42014-11-26 16:39:01 -0800370
adminbae64d82013-08-01 10:50:15 -0700371 def getMacAddress(self,host):
372 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700373 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700374 '''
375 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700376 try:
377 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
378 except pexpect.EOF:
379 main.log.error(self.name + ": EOF exception found")
380 main.log.error(self.name + ": " + self.handle.before)
381 main.cleanup()
382 main.exit()
adminbae64d82013-08-01 10:50:15 -0700383
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700384 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
385 mac_address_search = re.search(pattern, response, re.I)
386 mac_address = mac_address_search.group().split(" ")[1]
Jon Hallf2942ce2014-04-10 16:00:16 -0700387 main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address)
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700388 return mac_address
adminbae64d82013-08-01 10:50:15 -0700389 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700390 main.log.error(self.name+": Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700391
392 def getInterfaceMACAddress(self,host, interface):
393 '''
394 Return the IP address of the interface on the given host
395 '''
396 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700397 try:
398 response = self.execute(cmd=host+" ifconfig " + interface,
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700399 prompt="mininet>",timeout=10)
Jon Hall6094a362014-04-11 14:46:56 -0700400 except pexpect.EOF:
401 main.log.error(self.name + ": EOF exception found")
402 main.log.error(self.name + ": " + self.handle.before)
403 main.cleanup()
404 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700405
406 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
407 mac_address_search = re.search(pattern, response, re.I)
408 if mac_address_search is None:
409 main.log.info("No mac address found in %s" % response)
410 return main.FALSE
411 mac_address = mac_address_search.group().split(" ")[1]
412 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
413 return mac_address
414 else:
415 main.log.error("Connection failed to the host")
416
adminbae64d82013-08-01 10:50:15 -0700417 def getIPAddress(self,host):
418 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700419 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700420 '''
421 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700422 try:
423 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
424 except pexpect.EOF:
425 main.log.error(self.name + ": EOF exception found")
426 main.log.error(self.name + ": " + self.handle.before)
427 main.cleanup()
428 main.exit()
adminbae64d82013-08-01 10:50:15 -0700429
430 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
431 ip_address_search = re.search(pattern, response)
Jon Hallf2942ce2014-04-10 16:00:16 -0700432 main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1))
adminbae64d82013-08-01 10:50:15 -0700433 return ip_address_search.group(1)
434 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700435 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700436
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700437 def getSwitchDPID(self,switch):
438 '''
439 return the datapath ID of the switch
440 '''
441 if self.handle :
442 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700443 try:
444 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
445 except pexpect.EOF:
446 main.log.error(self.name + ": EOF exception found")
447 main.log.error(self.name + ": " + self.handle.before)
448 main.cleanup()
449 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700450 pattern = r'^(?P<dpid>\d)+'
451 result = re.search(pattern, response, re.MULTILINE)
452 if result is None:
453 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
454 return main.FALSE
Jon Hallc1a1d242014-07-21 16:03:33 -0700455 return str(result.group(0))
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700456 else:
457 main.log.error("Connection failed to the host")
458
admin2580a0e2014-07-29 11:24:34 -0700459 def getDPID(self, switch):
460 if self.handle:
461 self.handle.sendline("")
462 self.expect("mininet>")
463 cmd = "py %s.dpid" %switch
464 try:
465 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
466 self.handle.expect("mininet>")
467 response = self.handle.before
468 return response
469 except pexpect.EOF:
470 main.log.error(self.name + ": EOF exception found")
471 main.log.error(self.name + ": " + self.handle.before)
472 main.cleanup()
473 main.exit()
474
475
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700476 def getInterfaces(self, node):
477 '''
478 return information dict about interfaces connected to the node
479 '''
480 if self.handle :
Jon Hall38481722014-11-04 16:50:05 -0500481 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 -0700482 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700483 try:
484 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
485 except pexpect.EOF:
486 main.log.error(self.name + ": EOF exception found")
487 main.log.error(self.name + ": " + self.handle.before)
488 main.cleanup()
489 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700490 return response
491 else:
492 main.log.error("Connection failed to the node")
493
adminbae64d82013-08-01 10:50:15 -0700494 def dump(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700495 main.log.info(self.name+": Dump node info")
Jon Hall6094a362014-04-11 14:46:56 -0700496 try:
497 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
498 except pexpect.EOF:
499 main.log.error(self.name + ": EOF exception found")
500 main.log.error(self.name + ": " + self.handle.before)
501 main.cleanup()
502 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700503 return response
adminbae64d82013-08-01 10:50:15 -0700504
505 def intfs(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700506 main.log.info(self.name+": List interfaces")
Jon Hall6094a362014-04-11 14:46:56 -0700507 try:
508 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
509 except pexpect.EOF:
510 main.log.error(self.name + ": EOF exception found")
511 main.log.error(self.name + ": " + self.handle.before)
512 main.cleanup()
513 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700514 return response
adminbae64d82013-08-01 10:50:15 -0700515
516 def net(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700517 main.log.info(self.name+": List network connections")
Jon Hall6094a362014-04-11 14:46:56 -0700518 try:
519 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
520 except pexpect.EOF:
521 main.log.error(self.name + ": EOF exception found")
522 main.log.error(self.name + ": " + self.handle.before)
523 main.cleanup()
524 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700525 return response
shahshreyae6c7cf42014-11-26 16:39:01 -0800526 '''
527 def iperf(self,host1,host2):
Jon Hallf2942ce2014-04-10 16:00:16 -0700528 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700529 try:
shahshreyae6c7cf42014-11-26 16:39:01 -0800530 if not host1 and not host2:
531 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
532 else:
533 cmd1 = 'iperf '+ host1 + " " + host2
534 response = self.execute(cmd = cmd1, prompt = '>',timeout = 20)
Jon Hall6094a362014-04-11 14:46:56 -0700535 except pexpect.EOF:
536 main.log.error(self.name + ": EOF exception found")
537 main.log.error(self.name + ": " + self.handle.before)
538 main.cleanup()
539 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700540 return response
shahshreyae6c7cf42014-11-26 16:39:01 -0800541 '''
542 def iperf(self,host1,host2):
543 main.log.info(self.name+": Simple iperf TCP test between two hosts")
544 try:
545 cmd1 = 'iperf '+ host1 + " " + host2
546 self.handle.sendline(cmd1)
547 self.handle.expect("mininet>")
548 response = self.handle.before
549 if re.search('Results:',response):
550 main.log.info(self.name+": iperf test succssful")
551 return main.TRUE
552 else:
553 main.log.error(self.name+": iperf test failed")
554 return main.FALSE
555 except pexpect.EOF:
556 main.log.error(self.name + ": EOF exception found")
557 main.log.error(self.name + ": " + self.handle.before)
558 main.cleanup()
559 main.exit()
adminbae64d82013-08-01 10:50:15 -0700560
561 def iperfudp(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700562 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700563 try:
564 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
565 except pexpect.EOF:
566 main.log.error(self.name + ": EOF exception found")
567 main.log.error(self.name + ": " + self.handle.before)
568 main.cleanup()
569 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700570 return response
adminbae64d82013-08-01 10:50:15 -0700571
572 def nodes(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700573 main.log.info(self.name+": List all nodes.")
Jon Hall6094a362014-04-11 14:46:56 -0700574 try:
575 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
576 except pexpect.EOF:
577 main.log.error(self.name + ": EOF exception found")
578 main.log.error(self.name + ": " + self.handle.before)
579 main.cleanup()
580 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700581 return response
adminbae64d82013-08-01 10:50:15 -0700582
583 def pingpair(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700584 main.log.info(self.name+": Ping between first two hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700585 try:
586 response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
587 except pexpect.EOF:
588 main.log.error(self.name + ": EOF exception found")
589 main.log.error(self.name + ": " + self.handle.before)
590 main.cleanup()
591 main.exit()
adminbae64d82013-08-01 10:50:15 -0700592
Jon Hallf2942ce2014-04-10 16:00:16 -0700593 #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
594 if re.search(',\s0\%\spacket\sloss',response):
595 main.log.info(self.name+": Ping between two hosts SUCCESSFUL")
adminbae64d82013-08-01 10:50:15 -0700596 main.last_result = main.TRUE
597 return main.TRUE
598 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700599 main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700600 main.last_result = main.FALSE
601 return main.FALSE
602
603 def link(self,**linkargs):
604 '''
605 Bring link(s) between two nodes up or down
606 '''
adminbae64d82013-08-01 10:50:15 -0700607 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
608 end1 = args["END1"] if args["END1"] != None else ""
609 end2 = args["END2"] if args["END2"] != None else ""
610 option = args["OPTION"] if args["OPTION"] != None else ""
Jon Hall38481722014-11-04 16:50:05 -0500611 main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'")
adminbae64d82013-08-01 10:50:15 -0700612 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
Jon Hall6094a362014-04-11 14:46:56 -0700613 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700614 #response = self.execute(cmd=command,prompt="mininet>",timeout=10)
615 self.handle.sendline(command)
616 self.handle.expect("mininet>")
Jon Hall6094a362014-04-11 14:46:56 -0700617 except pexpect.EOF:
618 main.log.error(self.name + ": EOF exception found")
619 main.log.error(self.name + ": " + self.handle.before)
620 main.cleanup()
621 main.exit()
adminbae64d82013-08-01 10:50:15 -0700622 return main.TRUE
623
624
admin530b4c92013-08-14 16:54:35 -0700625 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700626 '''
admin530b4c92013-08-14 16:54:35 -0700627 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700628 '''
admin530b4c92013-08-14 16:54:35 -0700629 main.log.info('Yank the switch interface attached to a host')
630 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700631 sw = args["SW"] if args["SW"] !=None else ""
632 intf = args["INTF"] if args["INTF"] != None else ""
633 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700634 try:
635 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
636 except pexpect.EOF:
637 main.log.error(self.name + ": EOF exception found")
638 main.log.error(self.name + ": " + self.handle.before)
639 main.cleanup()
640 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700641 return main.TRUE
642
643 def plug(self, **plugargs):
644 '''
645 plug the yanked mininet switch interface to a switch
646 '''
647 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700648 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700649 sw = args["SW"] if args["SW"] !=None else ""
650 intf = args["INTF"] if args["INTF"] != None else ""
651 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700652 try:
653 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
654 except pexpect.EOF:
655 main.log.error(self.name + ": EOF exception found")
656 main.log.error(self.name + ": " + self.handle.before)
657 main.cleanup()
658 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700659 return main.TRUE
660
661
662
adminbae64d82013-08-01 10:50:15 -0700663 def dpctl(self,**dpctlargs):
664 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700665 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700666 '''
667 main.log.info('Run dpctl command on all switches')
668 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
669 cmd = args["CMD"] if args["CMD"] != None else ""
670 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
671 command = "dpctl "+cmd + " " + str(cmdargs)
Jon Hall6094a362014-04-11 14:46:56 -0700672 try:
673 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
674 except pexpect.EOF:
675 main.log.error(self.name + ": EOF exception found")
676 main.log.error(self.name + ": " + self.handle.before)
677 main.cleanup()
678 main.exit()
adminbae64d82013-08-01 10:50:15 -0700679 return main.TRUE
680
681
682 def get_version(self):
683 file_input = path+'/lib/Mininet/INSTALL'
684 version = super(Mininet, self).get_version()
685 pattern = 'Mininet\s\w\.\w\.\w\w*'
686 for line in open(file_input,'r').readlines():
687 result = re.match(pattern, line)
688 if result:
689 version = result.group(0)
Jon Hallec3c21e2014-11-10 22:22:37 -0500690 return version
adminbae64d82013-08-01 10:50:15 -0700691
Jon Hallec3c21e2014-11-10 22:22:37 -0500692 def get_sw_controller(self, sw):
693 '''
694 Parameters:
695 sw: The name of an OVS switch. Example "s1"
696 Return:
697 The output of the command from the mininet cli or main.FALSE on timeout
698 '''
admin2a9548d2014-06-17 14:08:07 -0700699 command = "sh ovs-vsctl get-controller "+str(sw)
700 try:
Jon Hallec3c21e2014-11-10 22:22:37 -0500701 response = self.execute(cmd=command, prompt="mininet>", timeout=10)
admin2a9548d2014-06-17 14:08:07 -0700702 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500703 return response
admin2a9548d2014-06-17 14:08:07 -0700704 else:
705 return main.FALSE
706 except pexpect.EOF:
707 main.log.error(self.name + ": EOF exception found")
708 main.log.error(self.name + ": " + self.handle.before)
709 main.cleanup()
710 main.exit()
adminbae64d82013-08-01 10:50:15 -0700711
712 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700713 '''
714 count is only needed if there is more than 1 controller
715 '''
716 args = utilities.parse_args(["COUNT"],**kwargs)
717 count = args["COUNT"] if args!={} else 1
718
719 argstring = "SW"
720 for j in range(count):
721 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
722 args = utilities.parse_args(argstring.split(","),**kwargs)
723
adminbae64d82013-08-01 10:50:15 -0700724 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700725 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700726 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
727
728 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
729 for j in range(count):
730 i=j+1
731 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
732 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
733 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
734 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
735 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700736 try:
737 self.execute(cmd=command,prompt="mininet>",timeout=5)
738 except pexpect.EOF:
739 main.log.error(self.name + ": EOF exception found")
740 main.log.error(self.name + ": " + self.handle.before)
741 main.cleanup()
742 main.exit()
743 except:
744 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
745 main.log.error( traceback.print_exc() )
746 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
747 main.cleanup()
748 main.exit()
adminbae64d82013-08-01 10:50:15 -0700749
Jon Hall0819fd92014-05-23 12:08:13 -0700750 def delete_sw_controller(self,sw):
751 '''
752 Removes the controller target from sw
753 '''
754
755 command = "sh ovs-vsctl del-controller "+str(sw)
756 try:
757 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
758 except pexpect.EOF:
759 main.log.error(self.name + ": EOF exception found")
760 main.log.error(self.name + ": " + self.handle.before)
761 main.cleanup()
762 main.exit()
763 else:
764 main.log.info(response)
765
Jon Hallb1290e82014-11-18 16:17:48 -0500766 def add_switch( self, sw, **kwargs ):
767 '''
768 adds a switch to the mininet topology
769 NOTE: this uses a custom mn function
770 NOTE: cannot currently specify what type of switch
771 required params:
772 switchname = name of the new switch as a string
773 optional keyvalues:
774 dpid = "dpid"
775 returns: main.FASLE on an error, else main.TRUE
776 '''
777 dpid = kwargs.get('dpid', '')
Jon Hallffb386d2014-11-21 13:43:38 -0800778 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500779 try:
780 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
781 if re.search("already exists!", response):
782 main.log.warn(response)
783 return main.FALSE
784 elif re.search("Error", response):
785 main.log.warn(response)
786 return main.FALSE
787 elif re.search("usage:", response):
788 main.log.warn(response)
789 return main.FALSE
790 else:
791 return main.TRUE
792 except pexpect.EOF:
793 main.log.error(self.name + ": EOF exception found")
794 main.log.error(self.name + ": " + self.handle.before)
795 main.cleanup()
796 main.exit()
797
798 def del_switch( self, sw ):
799 '''
800 delete a switch from the mininet topology
801 NOTE: this uses a custom mn function
802 required params:
803 switchname = name of the switch as a string
804 returns: main.FASLE on an error, else main.TRUE
805 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800806 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500807 try:
808 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
809 if re.search("no switch named", response):
810 main.log.warn(response)
811 return main.FALSE
812 elif re.search("Error", response):
813 main.log.warn(response)
814 return main.FALSE
815 elif re.search("usage:", response):
816 main.log.warn(response)
817 return main.FALSE
818 else:
819 return main.TRUE
820 except pexpect.EOF:
821 main.log.error(self.name + ": EOF exception found")
822 main.log.error(self.name + ": " + self.handle.before)
823 main.cleanup()
824 main.exit()
825
826 def add_link( self, node1, node2 ):
827 '''
828 add a link to the mininet topology
829 NOTE: this uses a custom mn function
830 NOTE: cannot currently specify what type of link
831 required params:
832 node1 = the string node name of the first endpoint of the link
833 node2 = the string node name of the second endpoint of the link
834 returns: main.FASLE on an error, else main.TRUE
835 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800836 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500837 try:
838 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
839 if re.search("doesnt exist!", response):
840 main.log.warn(response)
841 return main.FALSE
842 elif re.search("Error", response):
843 main.log.warn(response)
844 return main.FALSE
845 elif re.search("usage:", response):
846 main.log.warn(response)
847 return main.FALSE
848 else:
849 return main.TRUE
850 except pexpect.EOF:
851 main.log.error(self.name + ": EOF exception found")
852 main.log.error(self.name + ": " + self.handle.before)
853 main.cleanup()
854 main.exit()
855
856 def del_link( self, node1, node2 ):
857 '''
858 delete a link from the mininet topology
859 NOTE: this uses a custom mn function
860 required params:
861 node1 = the string node name of the first endpoint of the link
862 node2 = the string node name of the second endpoint of the link
863 returns: main.FASLE on an error, else main.TRUE
864 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800865 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500866 try:
867 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
868 if re.search("no node named", response):
869 main.log.warn(response)
870 return main.FALSE
871 elif re.search("Error", response):
872 main.log.warn(response)
873 return main.FALSE
874 elif re.search("usage:", response):
875 main.log.warn(response)
876 return main.FALSE
877 else:
878 return main.TRUE
879 except pexpect.EOF:
880 main.log.error(self.name + ": EOF exception found")
881 main.log.error(self.name + ": " + self.handle.before)
882 main.cleanup()
883 main.exit()
884
885 def add_host( self, hostname, **kwargs ):
886 '''
887 Add a host to the mininet topology
888 NOTE: this uses a custom mn function
889 NOTE: cannot currently specify what type of host
890 required params:
891 hostname = the string hostname
892 optional key-value params
893 switch = "switch name"
894 returns: main.FASLE on an error, else main.TRUE
895 '''
896 switch = kwargs.get('switch', '')
Jon Hallffb386d2014-11-21 13:43:38 -0800897 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -0500898 try:
899 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
900 if re.search("already exists!", response):
901 main.log.warn(response)
902 return main.FALSE
903 elif re.search("doesnt exists!", response):
904 main.log.warn(response)
905 return main.FALSE
906 elif re.search("Error", response):
907 main.log.warn(response)
908 return main.FALSE
909 elif re.search("usage:", response):
910 main.log.warn(response)
911 return main.FALSE
912 else:
913 return main.TRUE
914 except pexpect.EOF:
915 main.log.error(self.name + ": EOF exception found")
916 main.log.error(self.name + ": " + self.handle.before)
917 main.cleanup()
918 main.exit()
919
920 def del_host( self, hostname ):
921 '''
922 delete a host from the mininet topology
923 NOTE: this uses a custom mn function
924 required params:
925 hostname = the string hostname
926 returns: main.FASLE on an error, else main.TRUE
927 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800928 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -0500929 try:
930 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
931 if re.search("no host named", response):
932 main.log.warn(response)
933 return main.FALSE
934 elif re.search("Error", response):
935 main.log.warn(response)
936 return main.FALSE
937 elif re.search("usage:", response):
938 main.log.warn(response)
939 return main.FALSE
940 else:
941 return main.TRUE
942 except pexpect.EOF:
943 main.log.error(self.name + ": EOF exception found")
944 main.log.error(self.name + ": " + self.handle.before)
945 main.cleanup()
946 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -0700947
adminbae64d82013-08-01 10:50:15 -0700948 def disconnect(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700949 main.log.info(self.name+": Disconnecting mininet...")
adminbae64d82013-08-01 10:50:15 -0700950 response = ''
951 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700952 try:
953 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
954 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
Jon Halle80ef8c2014-04-29 15:29:13 -0700955 self.handle.sendline("sudo mn -c")
shahshreya328c2a72014-11-17 10:19:50 -0800956 response = main.TRUE
Jon Hall6094a362014-04-11 14:46:56 -0700957 except pexpect.EOF:
958 main.log.error(self.name + ": EOF exception found")
959 main.log.error(self.name + ": " + self.handle.before)
960 main.cleanup()
961 main.exit()
adminbae64d82013-08-01 10:50:15 -0700962 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700963 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700964 response = main.FALSE
965 return response
admin07529932013-11-22 14:58:28 -0800966
967 def arping(self, src, dest, destmac):
968 self.handle.sendline('')
Jon Hall333fa8c2014-04-11 11:24:58 -0700969 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800970
971 self.handle.sendline(src + ' arping ' + dest)
972 try:
Jon Hall333fa8c2014-04-11 11:24:58 -0700973 self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT])
Jon Hallf2942ce2014-04-10 16:00:16 -0700974 main.log.info(self.name+": ARP successful")
Jon Hall333fa8c2014-04-11 11:24:58 -0700975 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800976 return main.TRUE
977 except:
Jon Hallf2942ce2014-04-10 16:00:16 -0700978 main.log.warn(self.name+": ARP FAILURE")
Jon Hall333fa8c2014-04-11 11:24:58 -0700979 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800980 return main.FALSE
981
982 def decToHex(num):
983 return hex(num).split('x')[1]
admin2a9548d2014-06-17 14:08:07 -0700984
985 def getSwitchFlowCount(self, switch):
986 '''
987 return the Flow Count of the switch
988 '''
989 if self.handle:
990 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
991 try:
992 response = self.execute(cmd=cmd, prompt="mininet>", timeout=10)
993 except pexpect.EOF:
994 main.log.error(self.name + ": EOF exception found")
995 main.log.error(self.name + " " + self.handle.before)
996 main.cleanup()
997 main.exit()
998 pattern = "flow_count=(\d+)"
999 result = re.search(pattern, response, re.MULTILINE)
1000 if result is None:
admin2a9548d2014-06-17 14:08:07 -07001001 main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response))
1002 return main.FALSE
1003 return result.group(1)
1004 else:
1005 main.log.error("Connection failed to the Mininet host")
1006
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001007 def check_flows(self, sw, dump_format=None):
1008 if dump_format:
1009 command = "sh ovs-ofctl -F " + dump_format + " dump-flows " + str(sw)
1010 else:
1011 command = "sh ovs-ofctl dump-flows "+str(sw)
admin2a9548d2014-06-17 14:08:07 -07001012 try:
1013 response=self.execute(cmd=command,prompt="mininet>",timeout=10)
1014 return response
1015 except pexpect.EOF:
1016 main.log.error(self.name + ": EOF exception found")
1017 main.log.error(self.name + ": " + self.handle.before)
1018 main.cleanup()
1019 main.exit()
1020 else:
1021 main.log.info(response)
1022
1023 def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"):
1024 '''
1025 Runs tpdump on an intferface and saves the file
1026 intf can be specified, or the default eth0 is used
1027 '''
1028 try:
1029 self.handle.sendline("")
1030 self.handle.expect("mininet>")
1031 self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &")
1032 self.handle.sendline("")
admin2a9548d2014-06-17 14:08:07 -07001033 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10)
1034 main.log.warn(self.handle.before + self.handle.after)
Jon Hallb1290e82014-11-18 16:17:48 -05001035 self.handle.sendline("")
1036 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001037 if i == 0:
1038 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
1039 return main.FALSE
1040 elif i == 1:
1041 main.log.info(self.name + ": tcpdump started on " + intf)
1042 return main.TRUE
1043 elif i == 2:
1044 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
1045 return main.FALSE
1046 elif i ==3:
1047 main.log.info(self.name +": " + self.handle.before)
1048 return main.TRUE
1049 else:
1050 main.log.error(self.name + ": tcpdump - unexpected response")
1051 return main.FALSE
1052 except pexpect.EOF:
1053 main.log.error(self.name + ": EOF exception found")
1054 main.log.error(self.name + ": " + self.handle.before)
1055 main.cleanup()
1056 main.exit()
1057 except:
1058 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1059 main.log.error( traceback.print_exc() )
1060 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1061 main.cleanup()
1062 main.exit()
1063
1064 def stop_tcpdump(self):
1065 "pkills tcpdump"
1066 try:
1067 self.handle.sendline("sh sudo pkill tcpdump")
Jon Hallb1290e82014-11-18 16:17:48 -05001068 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001069 self.handle.sendline("")
1070 self.handle.expect("mininet>")
1071 except pexpect.EOF:
1072 main.log.error(self.name + ": EOF exception found")
1073 main.log.error(self.name + ": " + self.handle.before)
1074 main.cleanup()
1075 main.exit()
1076 except:
1077 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1078 main.log.error( traceback.print_exc() )
1079 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1080 main.cleanup()
1081 main.exit()
1082
Jon Hall3d87d502014-10-17 18:37:42 -04001083 def compare_switches(self, topo, switches_json):
1084 '''
1085 Compare mn and onos switches
1086 topo: sts TestONTopology object
1087 switches_json: parsed json object from the onos devices api
1088
1089 This uses the sts TestONTopology object
1090
1091 '''
1092 import json
Jon Hall42db6dc2014-10-24 19:03:48 -04001093 #main.log.debug("Switches_json string: ", switches_json)
Jon Hall3d87d502014-10-17 18:37:42 -04001094 output = {"switches":[]}
1095 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 -04001096 ports = []
1097 for port in switch.ports.values():
Jon Hall3d87d502014-10-17 18:37:42 -04001098 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
1099 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
Jon Hall3d87d502014-10-17 18:37:42 -04001100
Jon Hall42db6dc2014-10-24 19:03:48 -04001101 #print "mn"
Jon Hall3d87d502014-10-17 18:37:42 -04001102 #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall42db6dc2014-10-24 19:03:48 -04001103 #print "onos"
1104 #print json.dumps(switches_json, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall3d87d502014-10-17 18:37:42 -04001105
1106
1107 # created sorted list of dpid's in MN and ONOS for comparison
1108 mnDPIDs=[]
1109 for switch in output['switches']:
1110 mnDPIDs.append(switch['dpid'])
1111 mnDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001112 #print "List of Mininet switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001113 #print mnDPIDs
1114 if switches_json == "":#if rest call fails
Jon Hall42db6dc2014-10-24 19:03:48 -04001115 main.log.error(self.name + ".compare_switches(): Empty JSON object given from ONOS")
Jon Hall3d87d502014-10-17 18:37:42 -04001116 return main.FALSE
1117 onos=switches_json
1118 onosDPIDs=[]
1119 for switch in onos:
Jon Hall38481722014-11-04 16:50:05 -05001120 if switch['available'] == True:
1121 onosDPIDs.append(switch['id'].replace(":",'').replace("of",''))
1122 #else:
1123 #print "Switch is unavailable:"
1124 #print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001125 onosDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001126 #print "List of ONOS switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001127 #print onosDPIDs
1128
1129 if mnDPIDs!=onosDPIDs:
1130 switch_results = main.FALSE
1131 main.log.report( "Switches in MN but not in ONOS:")
1132 main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs]))
1133 main.log.report( "Switches in ONOS but not in MN:")
1134 main.log.report( str([switch for switch in onosDPIDs if switch not in mnDPIDs]))
1135 else:#list of dpid's match in onos and mn
1136 #main.log.report("DEBUG: The dpid's of the switches in Mininet and ONOS match")
1137 switch_results = main.TRUE
1138 return switch_results
1139
1140
1141
Jon Hall72cf1dc2014-10-20 21:04:50 -04001142 def compare_ports(self, topo, ports_json):
1143 '''
1144 Compare mn and onos ports
1145 topo: sts TestONTopology object
1146 ports_json: parsed json object from the onos ports api
1147
1148 Dependencies:
1149 1. This uses the sts TestONTopology object
1150 2. numpy - "sudo pip install numpy"
1151
1152 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001153 #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 -04001154 import json
1155 from numpy import uint64
Jon Hallb1290e82014-11-18 16:17:48 -05001156 ports_results = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001157 output = {"switches":[]}
1158 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 -04001159 ports = []
1160 for port in switch.ports.values():
1161 #print port.hw_addr.toStr(separator = '')
Jon Hall39f29df2014-11-04 19:30:21 -05001162 tmp_port = {}
1163 tmp_port['of_port'] = port.port_no
1164 tmp_port['mac'] = str(port.hw_addr).replace('\'','')
1165 tmp_port['name'] = port.name
1166 tmp_port['enabled'] = port.enabled
1167
1168 ports.append(tmp_port)
1169 tmp_switch = {}
1170 tmp_switch['name'] = switch.name
1171 tmp_switch['dpid'] = str(switch.dpid).zfill(16)
1172 tmp_switch['ports'] = ports
1173
1174 output['switches'].append(tmp_switch)
Jon Hall72cf1dc2014-10-20 21:04:50 -04001175
1176
1177 ################ports#############
Jon Hall39f29df2014-11-04 19:30:21 -05001178 for mn_switch in output['switches']:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001179 mn_ports = []
1180 onos_ports = []
Jon Hallb1290e82014-11-18 16:17:48 -05001181 switch_result = main.TRUE
Jon Hall39f29df2014-11-04 19:30:21 -05001182 for port in mn_switch['ports']:
Jon Hall38481722014-11-04 16:50:05 -05001183 if port['enabled'] == True:
1184 mn_ports.append(port['of_port'])
Jon Hallb1290e82014-11-18 16:17:48 -05001185 #else: #DEBUG only
1186 # 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 -04001187 for onos_switch in ports_json:
Jon Hall38481722014-11-04 16:50:05 -05001188 #print "Iterating through a new switch as seen by ONOS"
1189 #print onos_switch
1190 if onos_switch['device']['available'] == True:
Jon Hall39f29df2014-11-04 19:30:21 -05001191 if onos_switch['device']['id'].replace(':','').replace("of", '') == mn_switch['dpid']:
Jon Hall38481722014-11-04 16:50:05 -05001192 for port in onos_switch['ports']:
1193 if port['isEnabled']:
1194 #print "Iterating through available ports on the switch"
1195 #print port
Jon Hallb1290e82014-11-18 16:17:48 -05001196 if port['port'] == 'local':
1197 #onos_ports.append('local')
1198 onos_ports.append(long(uint64(-2)))
1199 else:
1200 onos_ports.append(int(port['port']))
1201 '''
1202 else: #This is likely a new reserved port implemented
1203 main.log.error("unkown port '" + str(port['port']) )
1204 '''
Jon Hall1645caa2014-11-18 16:27:14 -05001205 #else: #DEBUG
1206 # 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 -05001207 break
Jon Hall72cf1dc2014-10-20 21:04:50 -04001208 mn_ports.sort(key=float)
1209 onos_ports.sort(key=float)
Jon Hall94fd0472014-12-08 11:52:42 -08001210 #print "\nPorts for Switch %s:" % (mn_switch['name'])
Jon Hall72cf1dc2014-10-20 21:04:50 -04001211 #print "\tmn_ports[] = ", mn_ports
1212 #print "\tonos_ports[] = ", onos_ports
Jon Hallb1290e82014-11-18 16:17:48 -05001213 mn_ports_log = mn_ports
1214 onos_ports_log = onos_ports
1215 mn_ports = [x for x in mn_ports]
1216 onos_ports = [x for x in onos_ports]
Jon Hall38481722014-11-04 16:50:05 -05001217
Jon Hall72cf1dc2014-10-20 21:04:50 -04001218 #TODO: handle other reserved port numbers besides LOCAL
Jon Hallb1290e82014-11-18 16:17:48 -05001219 #NOTE: Reserved ports
1220 # Local port: -2 in Openflow, ONOS shows 'local', we store as long(uint64(-2))
1221 for mn_port in mn_ports_log:
1222 if mn_port in onos_ports:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001223 #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 -05001224 mn_ports.remove(mn_port)
1225 onos_ports.remove(mn_port)
1226 #NOTE: OVS reports this as down since there is no link
1227 # So ignoring these for now
1228 #TODO: Come up with a better way of handling these
1229 if 65534 in mn_ports:
1230 mn_ports.remove(65534)
1231 if long(uint64(-2)) in onos_ports:
1232 onos_ports.remove( long(uint64(-2)) )
1233 if len(mn_ports): #the ports of this switch don't match
1234 switch_result = main.FALSE
1235 main.log.warn("Ports in MN but not ONOS: " + str(mn_ports) )
1236 if len(onos_ports): #the ports of this switch don't match
1237 switch_result = main.FALSE
1238 main.log.warn("Ports in ONOS but not MN: " + str(onos_ports) )
1239 if switch_result == main.FALSE:
Jon Hall39f29df2014-11-04 19:30:21 -05001240 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 -05001241 main.log.warn("mn_ports[] = " + str(mn_ports_log))
1242 main.log.warn("onos_ports[] = " + str(onos_ports_log))
1243 ports_results = ports_results and switch_result
1244 return ports_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001245
1246
1247
1248
1249 def compare_links(self, topo, links_json):
1250 '''
1251 Compare mn and onos links
1252 topo: sts TestONTopology object
1253 links_json: parsed json object from the onos links api
1254
1255 This uses the sts TestONTopology object
1256
1257 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001258 #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 -04001259 import json
1260 link_results = main.TRUE
1261 output = {"switches":[]}
1262 onos = links_json
1263 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 -05001264 # print "Iterating though switches as seen by Mininet"
1265 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001266 ports = []
1267 for port in switch.ports.values():
1268 #print port.hw_addr.toStr(separator = '')
1269 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
1270 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
1271 #######Links########
1272
Jon Hall38481722014-11-04 16:50:05 -05001273 mn_links = [link for link in topo.patch_panel.network_links if (link.port1.enabled and link.port2.enabled)]
1274 #print "mn_links:"
1275 #print mn_links
1276 if 2*len(mn_links) == len(onos):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001277 link_results = main.TRUE
1278 else:
1279 link_results = main.FALSE
Jon Hall38481722014-11-04 16:50:05 -05001280 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 -04001281
1282
1283 # iterate through MN links and check if an ONOS link exists in both directions
1284 # NOTE: Will currently only show mn links as down if they are cut through STS.
1285 # We can either do everything through STS or wait for up_network_links
1286 # and down_network_links to be fully implemented.
Jon Hall38481722014-11-04 16:50:05 -05001287 for link in mn_links:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001288 #print "Link: %s" % link
1289 #TODO: Find a more efficient search method
1290 node1 = None
1291 port1 = None
1292 node2 = None
1293 port2 = None
1294 first_dir = main.FALSE
1295 second_dir = main.FALSE
1296 for switch in output['switches']:
1297 #print "Switch: %s" % switch['name']
1298 if switch['name'] == link.node1.name:
1299 node1 = switch['dpid']
1300 for port in switch['ports']:
1301 if str(port['name']) == str(link.port1):
1302 port1 = port['of_port']
1303 if node1 is not None and node2 is not None:
1304 break
1305 if switch['name'] == link.node2.name:
1306 node2 = switch['dpid']
1307 for port in switch['ports']:
1308 if str(port['name']) == str(link.port2):
1309 port2 = port['of_port']
1310 if node1 is not None and node2 is not None:
1311 break
1312
1313
1314 for onos_link in onos:
1315 onos_node1 = onos_link['src']['device'].replace(":",'').replace("of", '')
1316 onos_node2 = onos_link['dst']['device'].replace(":",'').replace("of", '')
1317 onos_port1 = onos_link['src']['port']
1318 onos_port2 = onos_link['dst']['port']
1319
1320 #print "Checking ONOS for link %s/%s -> %s/%s and" % (node1, port1, node2, port2)
1321 #print "Checking ONOS for link %s/%s -> %s/%s" % (node2, port2, node1, port1)
1322 # check onos link from node1 to node2
1323 if str(onos_node1) == str(node1) and str(onos_node2) == str(node2):
1324 if int(onos_port1) == int(port1) and int(onos_port2) == int(port2):
1325 first_dir = main.TRUE
1326 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001327 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001328 ' between ONOS and MN. When cheking ONOS for link '+\
1329 '%s/%s -> %s/%s' % (node1, port1, node2, port2)+\
1330 ' ONOS has the values %s/%s -> %s/%s' %\
1331 (onos_node1, onos_port1, onos_node2, onos_port2))
1332
1333 # check onos link from node2 to node1
1334 elif ( str(onos_node1) == str(node2) and str(onos_node2) == str(node1) ):
1335 if ( int(onos_port1) == int(port2) and int(onos_port2) == int(port1) ):
1336 second_dir = main.TRUE
1337 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001338 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001339 ' between ONOS and MN. When cheking ONOS for link '+\
1340 '%s/%s -> %s/%s' % (node2, port2, node1, port1)+\
1341 ' ONOS has the values %s/%s -> %s/%s' %\
1342 (onos_node2, onos_port2, onos_node1, onos_port1))
1343 else:#this is not the link you're looking for
1344 pass
1345 if not first_dir:
1346 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node1, port1, node2, port2))
1347 if not second_dir:
1348 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node2, port2, node1, port1))
1349 link_results = link_results and first_dir and second_dir
Jon Hall62df9242014-10-22 12:20:17 -04001350 return link_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001351
1352
andrewonlab3f0a4af2014-10-17 12:25:14 -04001353 def get_hosts(self):
1354 '''
1355 Returns a list of all hosts
1356 Don't ask questions just use it
1357 '''
1358 self.handle.sendline("")
1359 self.handle.expect("mininet>")
1360
1361 self.handle.sendline("py [ host.name for host in net.hosts ]")
1362 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001363
andrewonlab3f0a4af2014-10-17 12:25:14 -04001364 handle_py = self.handle.before
1365 handle_py = handle_py.split("]\r\n",1)[1]
1366 handle_py = handle_py.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001367
andrewonlab3f0a4af2014-10-17 12:25:14 -04001368 self.handle.sendline("")
1369 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001370
andrewonlab3f0a4af2014-10-17 12:25:14 -04001371 host_str = handle_py.replace("]", "")
1372 host_str = host_str.replace("'", "")
1373 host_str = host_str.replace("[", "")
1374 host_list = host_str.split(",")
1375
1376 return host_list
adminbae64d82013-08-01 10:50:15 -07001377
Jon Hall38481722014-11-04 16:50:05 -05001378
1379 def update(self):
1380 '''
1381 updates the port address and status information for each port in mn
1382 '''
1383 #TODO: Add error checking. currently the mininet command has no output
1384 main.log.info("Updateing MN port information")
Jon Hallb1290e82014-11-18 16:17:48 -05001385 try:
1386 self.handle.sendline("")
1387 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001388
Jon Hallb1290e82014-11-18 16:17:48 -05001389 self.handle.sendline("update")
1390 self.handle.expect("update")
1391 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001392
Jon Hallb1290e82014-11-18 16:17:48 -05001393 self.handle.sendline("")
1394 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001395
Jon Hallb1290e82014-11-18 16:17:48 -05001396 return main.TRUE
1397 except pexpect.EOF:
1398 main.log.error(self.name + ": EOF exception found")
1399 main.log.error(self.name + ": " + self.handle.before)
1400 main.cleanup()
1401 main.exit()
1402
adminbae64d82013-08-01 10:50:15 -07001403if __name__ != "__main__":
1404 import sys
1405 sys.modules[__name__] = MininetCliDriver()
admin2a9548d2014-06-17 14:08:07 -07001406