admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 23 | ''' |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 24 | import traceback |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | import re |
| 27 | import sys |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | sys.path.append("../") |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 29 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | |
| 32 | class MininetCliDriver(Emulator): |
| 33 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 34 | MininetCliDriver is the basic driver which will handle the Mininet functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | ''' |
| 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 Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 43 | ''' |
| 44 | Here the main is the TestON instance after creating all the log handles. |
| 45 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | for key in connectargs: |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 47 | vars(self)[key] = connectargs[key] |
| 48 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 51 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | self.ssh_handle = self.handle |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 53 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 55 | main.log.info(self.name+": Clearing any residual state or processes") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | self.handle.sendline("sudo mn -c") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 57 | i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 58 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 59 | main.log.info(self.name+": Sending sudo password") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 60 | self.handle.sendline(self.pwd) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 61 | i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 62 | if i==1: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 63 | main.log.info(self.name+": Clean") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 65 | main.log.error(self.name+": Connection terminated") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 66 | elif i==3: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 67 | main.log.error(self.name+": Something while cleaning MN took too long... " ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 68 | |
| 69 | main.log.info(self.name+": building fresh mininet") |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 70 | #### for reactive/PARP enabled tests |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 71 | cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] + " " + self.options['arg3'] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 72 | |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 73 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 78 | |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 79 | #### for proactive flow with static ARP entries |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 80 | #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] + " " + self.options['arg3'] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | self.handle.sendline(cmdString) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 82 | self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 83 | while 1: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300) |
| 85 | if i==0: |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 86 | main.log.info(self.name+": mininet built") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 87 | return main.TRUE |
| 88 | if i==1: |
Jon Hall | 1645caa | 2014-11-18 16:27:14 -0500 | [diff] [blame] | 89 | self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 90 | main.log.info(self.handle.before) |
| 91 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 92 | main.log.error(self.name+": Launching mininet failed...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | return main.FALSE |
| 94 | elif i==3: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 95 | main.log.error(self.name+": Connection timeout") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | return main.FALSE |
| 97 | elif i==4: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 98 | main.log.error(self.name+": Something took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 99 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "): |
| 101 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 102 | else:#if no handle |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 103 | main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 104 | main.log.error(self.name+": Failed to connect to the Mininet") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 106 | |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 107 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 112 | count = 0 |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 113 | while(k <= depth-1): |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 114 | count = count + pow(fanout,k) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 115 | k = k+1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 116 | num_switches = count |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 117 | while(k <= depth-2): |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 118 | '''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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 121 | count = count + pow(fanout,k) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 122 | k = k+1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 123 | num_links = count * fanout |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 124 | #print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 125 | |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 126 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 133 | print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 134 | 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 Hall | 1645caa | 2014-11-18 16:27:14 -0500 | [diff] [blame] | 143 | def pingall(self, timeout=300): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 145 | Verifies the reachability of the hosts using pingall command. |
Jon Hall | 1645caa | 2014-11-18 16:27:14 -0500 | [diff] [blame] | 146 | 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 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | ''' |
| 151 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 152 | main.log.info(self.name+": Checking reachabilty to the hosts using pingall") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 153 | try: |
Jon Hall | 1645caa | 2014-11-18 16:27:14 -0500 | [diff] [blame] | 154 | response = self.execute(cmd="pingall",prompt="mininet>",timeout=int(timeout)) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 155 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 156 | main.log.error(self.name + ": EOF exception found") |
| 157 | main.log.error(self.name + ": " + self.handle.before) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 158 | 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 Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 166 | if re.search(pattern,response): |
| 167 | main.log.info(self.name+": All hosts are reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | return main.TRUE |
| 169 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 170 | main.log.error(self.name+": Unable to reach all the hosts") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 171 | 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>") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | return main.FALSE |
| 177 | else : |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 178 | main.log.error(self.name+": Connection failed to the host") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 179 | main.cleanup() |
| 180 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 181 | |
| 182 | def fpingHost(self,**pingParams): |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 183 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 184 | Uses the fping package for faster pinging... |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 185 | *requires fping to be installed on machine running mininet |
| 186 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 187 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 188 | command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 189 | self.handle.sendline(command) |
| 190 | self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 191 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 192 | response = self.handle.before |
| 193 | if re.search(":\s-" ,response): |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 194 | main.log.info(self.name+": Ping fail") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 195 | return main.FALSE |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 196 | elif re.search(":\s\d{1,2}\.\d\d", response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 197 | main.log.info(self.name+": Ping good!") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 198 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 199 | main.log.info(self.name+": Install fping on mininet machine... ") |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 200 | main.log.info(self.name+": \n---\n"+response) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 201 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 202 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 203 | def pingHost(self,**pingParams): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 204 | ''' |
| 205 | Ping from one mininet host to another |
| 206 | Currently the only supported Params: SRC and TARGET |
| 207 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 208 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
| 209 | #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"] |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 210 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 211 | try: |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 212 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 224 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 225 | main.log.error(self.name + ": EOF exception found") |
| 226 | main.log.error(self.name + ": " + self.handle.before) |
| 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 229 | 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 Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 232 | main.log.info(self.name+": no packets lost, host is reachable") |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 233 | main.last_result = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 234 | return main.TRUE |
| 235 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 236 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 237 | main.last_result = main.FALSE |
| 238 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 239 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 240 | def checkIP(self,host): |
| 241 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 242 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | ''' |
| 244 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 245 | try: |
| 246 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 247 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 248 | main.log.error(self.name + ": EOF exception found") |
| 249 | main.log.error(self.name + ": " + self.handle.before) |
| 250 | main.cleanup() |
| 251 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 252 | |
| 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 254 | #pattern = "inet addr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 255 | #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") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 258 | return main.TRUE |
| 259 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 260 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 261 | return main.FALSE |
| 262 | else : |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 263 | main.log.error(self.name+": Connection failed to the host") |
| 264 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 265 | def verifySSH(self,**connectargs): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 266 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 272 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 273 | main.log.error(self.name + ": EOF exception found") |
| 274 | main.log.error(self.name + ": " + self.handle.before) |
| 275 | main.cleanup() |
| 276 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 277 | 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 |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 284 | |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 285 | |
| 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: |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 293 | cmd = host+" ifconfig "+intf+" "+newIP+" "+'netmask'+" "+newNetmask |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 294 | self.handle.sendline(cmd) |
| 295 | self.handle.expect("mininet>") |
| 296 | response = self.handle.before |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 297 | main.log.info("response = "+response) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 298 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 312 | cmd = host+" route add default gw "+newGW |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 313 | self.handle.sendline(cmd) |
| 314 | self.handle.expect("mininet>") |
| 315 | response = self.handle.before |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 316 | main.log.info("response = "+response) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 317 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 323 | |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 324 | def addStaticMACAddress(self,host,GW,macaddr): |
| 325 | ''' |
| 326 | Changes the mac address of a geateway host |
| 327 | ''' |
| 328 | if self.handle: |
| 329 | try: |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 330 | #h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 331 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 345 | Verify if the static gateway and mac address assignment |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 346 | ''' |
| 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 | |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 362 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 363 | def getMacAddress(self,host): |
| 364 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 365 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 366 | ''' |
| 367 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 368 | try: |
| 369 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 370 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 371 | main.log.error(self.name + ": EOF exception found") |
| 372 | main.log.error(self.name + ": " + self.handle.before) |
| 373 | main.cleanup() |
| 374 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 375 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 376 | 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 Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 379 | main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address) |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 380 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 381 | else : |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 382 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 383 | |
| 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 389 | try: |
| 390 | response = self.execute(cmd=host+" ifconfig " + interface, |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 391 | prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 392 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 393 | 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-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 397 | |
| 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 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 409 | def getIPAddress(self,host): |
| 410 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 411 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 412 | ''' |
| 413 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 414 | try: |
| 415 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 416 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 417 | main.log.error(self.name + ": EOF exception found") |
| 418 | main.log.error(self.name + ": " + self.handle.before) |
| 419 | main.cleanup() |
| 420 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 421 | |
| 422 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 423 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 424 | main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 425 | return ip_address_search.group(1) |
| 426 | else : |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 427 | main.log.error(self.name+": Connection failed to the host") |
| 428 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 429 | 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 435 | try: |
| 436 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 437 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 438 | main.log.error(self.name + ": EOF exception found") |
| 439 | main.log.error(self.name + ": " + self.handle.before) |
| 440 | main.cleanup() |
| 441 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 442 | pattern = r'^(?P<dpid>\w)+' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 443 | 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 Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 447 | return str(result.group(0)).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 448 | else: |
| 449 | main.log.error("Connection failed to the host") |
| 450 | |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 451 | 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-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 468 | def getInterfaces(self, node): |
| 469 | ''' |
| 470 | return information dict about interfaces connected to the node |
| 471 | ''' |
| 472 | if self.handle : |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 473 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s" % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 474 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 475 | try: |
| 476 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 477 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 478 | 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-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 482 | return response |
| 483 | else: |
| 484 | main.log.error("Connection failed to the node") |
| 485 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 486 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 487 | main.log.info(self.name+": Dump node info") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 488 | try: |
| 489 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 490 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 491 | 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-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 495 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 496 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 497 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 498 | main.log.info(self.name+": List interfaces") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 499 | try: |
| 500 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 501 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 502 | main.log.error(self.name + ": EOF exception found") |
| 503 | main.log.error(self.name + ": " + self.handle.before) |
| 504 | main.cleanup() |
| 505 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 506 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 507 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 508 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 509 | main.log.info(self.name+": List network connections") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 510 | try: |
| 511 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 512 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 513 | main.log.error(self.name + ": EOF exception found") |
| 514 | main.log.error(self.name + ": " + self.handle.before) |
| 515 | main.cleanup() |
| 516 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 517 | return response |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 518 | ''' |
| 519 | def iperf(self,host1,host2): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 520 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 521 | try: |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 522 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 527 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 528 | main.log.error(self.name + ": EOF exception found") |
| 529 | main.log.error(self.name + ": " + self.handle.before) |
| 530 | main.cleanup() |
| 531 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 532 | return response |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 533 | ''' |
| 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 539 | self.handle.expect("mininet>") |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 540 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 552 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 553 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 554 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 555 | try: |
| 556 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 557 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 558 | main.log.error(self.name + ": EOF exception found") |
| 559 | main.log.error(self.name + ": " + self.handle.before) |
| 560 | main.cleanup() |
| 561 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 562 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 563 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 564 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 565 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 566 | try: |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 567 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 568 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 569 | main.log.error(self.name + ": EOF exception found") |
| 570 | main.log.error(self.name + ": " + self.handle.before) |
| 571 | main.cleanup() |
| 572 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 573 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 574 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 575 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 576 | main.log.info(self.name+": Ping between first two hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 577 | try: |
| 578 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 579 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 580 | main.log.error(self.name + ": EOF exception found") |
| 581 | main.log.error(self.name + ": " + self.handle.before) |
| 582 | main.cleanup() |
| 583 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 584 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 585 | #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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 588 | main.last_result = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 589 | return main.TRUE |
| 590 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 591 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 592 | main.last_result = main.FALSE |
| 593 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 594 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 595 | def link(self,**linkargs): |
| 596 | ''' |
| 597 | Bring link(s) between two nodes up or down |
| 598 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 599 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 603 | main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 604 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 605 | try: |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 606 | #response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 607 | self.handle.sendline(command) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 608 | self.handle.expect("mininet>") |
| 609 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 610 | main.log.error(self.name + ": EOF exception found") |
| 611 | main.log.error(self.name + ": " + self.handle.before) |
| 612 | main.cleanup() |
| 613 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 614 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 615 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 616 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 617 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 618 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 619 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 620 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 621 | main.log.info('Yank the switch interface attached to a host') |
| 622 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 623 | 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 626 | try: |
| 627 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 628 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 629 | main.log.error(self.name + ": EOF exception found") |
| 630 | main.log.error(self.name + ": " + self.handle.before) |
| 631 | main.cleanup() |
| 632 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 633 | 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') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 640 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 641 | 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 644 | try: |
| 645 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 646 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 647 | main.log.error(self.name + ": EOF exception found") |
| 648 | main.log.error(self.name + ": " + self.handle.before) |
| 649 | main.cleanup() |
| 650 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 651 | return main.TRUE |
| 652 | |
| 653 | |
| 654 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 655 | def dpctl(self,**dpctlargs): |
| 656 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 657 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 658 | ''' |
| 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 664 | try: |
| 665 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 666 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 667 | main.log.error(self.name + ": EOF exception found") |
| 668 | main.log.error(self.name + ": " + self.handle.before) |
| 669 | main.cleanup() |
| 670 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 671 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 672 | |
| 673 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 674 | 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 Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 682 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 683 | |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 684 | 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 | ''' |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 691 | command = "sh ovs-vsctl get-controller "+str(sw) |
| 692 | try: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 693 | response = self.execute(cmd=command, prompt="mininet>", timeout=10) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 694 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 695 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 696 | 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() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 703 | |
| 704 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 705 | ''' |
| 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 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 716 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 717 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 718 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 719 | |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 720 | 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 Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 728 | 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() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 741 | |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 742 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 750 | except pexpect.EOF: |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 751 | 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 758 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 770 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 771 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 798 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 799 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 828 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 829 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 857 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 858 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 889 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 890 | 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 Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 920 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 921 | 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 Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 939 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 940 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 941 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 942 | response = '' |
| 943 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 944 | try: |
| 945 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 946 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 947 | self.handle.sendline("sudo mn -c") |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 948 | response = main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 949 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 950 | main.log.error(self.name + ": EOF exception found") |
| 951 | main.log.error(self.name + ": " + self.handle.before) |
| 952 | main.cleanup() |
| 953 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 954 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 955 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 956 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 957 | return response |
| 958 | |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 959 | def arping(self, src, dest, destmac): |
| 960 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 961 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 962 | |
| 963 | self.handle.sendline(src + ' arping ' + dest) |
| 964 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 965 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 966 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 967 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 968 | return main.TRUE |
| 969 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 970 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 971 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 972 | return main.FALSE |
| 973 | |
| 974 | def decToHex(num): |
| 975 | return hex(num).split('x')[1] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 976 | |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 977 | 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: |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 993 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 998 | |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 999 | 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) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1004 | 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("") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1025 | 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1027 | self.handle.sendline("") |
| 1028 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1029 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1038 | elif i ==3: |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1039 | 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1060 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1061 | 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 Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1075 | 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 Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1085 | #main.log.debug("Switches_json string: ", switches_json) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1086 | output = {"switches":[]} |
| 1087 | for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1088 | ports = [] |
| 1089 | for port in switch.ports.values(): |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1090 | 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 Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1092 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1093 | #print "mn" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1094 | #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': ')) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1095 | #print "onos" |
| 1096 | #print json.dumps(switches_json, sort_keys=True,indent=4,separators=(',', ': ')) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1097 | |
| 1098 | |
| 1099 | # created sorted list of dpid's in MN and ONOS for comparison |
| 1100 | mnDPIDs=[] |
| 1101 | for switch in output['switches']: |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 1102 | mnDPIDs.append(switch['dpid'].lower()) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1103 | mnDPIDs.sort() |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1104 | #print "List of Mininet switch DPID's" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1105 | #print mnDPIDs |
| 1106 | if switches_json == "":#if rest call fails |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1107 | main.log.error(self.name + ".compare_switches(): Empty JSON object given from ONOS") |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1108 | return main.FALSE |
| 1109 | onos=switches_json |
| 1110 | onosDPIDs=[] |
| 1111 | for switch in onos: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1112 | if switch['available'] == True: |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 1113 | onosDPIDs.append(switch['id'].replace(":",'').replace("of",'').lower()) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1114 | #else: |
| 1115 | #print "Switch is unavailable:" |
| 1116 | #print switch |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1117 | onosDPIDs.sort() |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1118 | #print "List of ONOS switch DPID's" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1119 | #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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1134 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1140 | Dependencies: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1141 | 1. This uses the sts TestONTopology object |
| 1142 | 2. numpy - "sudo pip install numpy" |
| 1143 | |
| 1144 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1145 | #FIXME: this does not look for extra ports in ONOS, only checks that ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1146 | import json |
| 1147 | from numpy import uint64 |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1148 | ports_results = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1149 | output = {"switches":[]} |
| 1150 | for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1151 | ports = [] |
| 1152 | for port in switch.ports.values(): |
| 1153 | #print port.hw_addr.toStr(separator = '') |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1154 | 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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1167 | |
| 1168 | |
| 1169 | ################ports############# |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1170 | for mn_switch in output['switches']: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1171 | mn_ports = [] |
| 1172 | onos_ports = [] |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1173 | switch_result = main.TRUE |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1174 | for port in mn_switch['ports']: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1175 | if port['enabled'] == True: |
| 1176 | mn_ports.append(port['of_port']) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1177 | #else: #DEBUG only |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1178 | # main.log.warn("Port %s on switch %s is down" % ( str(port['of_port']) , str(mn_switch['name'])) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1179 | for onos_switch in ports_json: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1180 | #print "Iterating through a new switch as seen by ONOS" |
| 1181 | #print onos_switch |
| 1182 | if onos_switch['device']['available'] == True: |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1183 | if onos_switch['device']['id'].replace(':','').replace("of", '') == mn_switch['dpid']: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1184 | for port in onos_switch['ports']: |
| 1185 | if port['isEnabled']: |
| 1186 | #print "Iterating through available ports on the switch" |
| 1187 | #print port |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1188 | 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 Hall | 1645caa | 2014-11-18 16:27:14 -0500 | [diff] [blame] | 1197 | #else: #DEBUG |
| 1198 | # main.log.warn("Port %s on switch %s is down" % ( str(port['port']) , str(onos_switch['device']['id'])) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1199 | break |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1200 | mn_ports.sort(key=float) |
| 1201 | onos_ports.sort(key=float) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1202 | #print "\nPorts for Switch %s:" % (mn_switch['name']) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1203 | #print "\tmn_ports[] = ", mn_ports |
| 1204 | #print "\tonos_ports[] = ", onos_ports |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1205 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1209 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1210 | #TODO: handle other reserved port numbers besides LOCAL |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1211 | #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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1215 | #don't set results to true here as this is just one of many checks and it might override a failure |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1216 | 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 Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1232 | main.log.report("The list of ports for switch %s(%s) does not match:" % (mn_switch['name'], mn_switch['dpid']) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1233 | 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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1237 | |
| 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 Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1250 | #FIXME: this does not look for extra links in ONOS, only checks that ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1251 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1256 | # print "Iterating though switches as seen by Mininet" |
| 1257 | # print switch |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1258 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1265 | 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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1269 | link_results = main.TRUE |
| 1270 | else: |
| 1271 | link_results = main.FALSE |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1272 | main.log.report("Mininet has %i bidirectional links and ONOS has %i unidirectional links" % (len(mn_links), len(onos) )) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1273 | |
| 1274 | |
| 1275 | # iterate through MN links and check if an ONOS link exists in both directions |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1276 | # 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 Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1278 | # and down_network_links to be fully implemented. |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1279 | for link in mn_links: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1280 | #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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1294 | port1 = port['of_port'] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1295 | 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1319 | main.log.warn('The port numbers do not match for ' +str(link) +\ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1320 | ' 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1330 | main.log.warn('The port numbers do not match for ' +str(link) +\ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1331 | ' 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 Hall | 62df924 | 2014-10-22 12:20:17 -0400 | [diff] [blame] | 1342 | return link_results |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1343 | |
| 1344 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1345 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1352 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1353 | self.handle.sendline("py [ host.name for host in net.hosts ]") |
| 1354 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1355 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1356 | handle_py = self.handle.before |
| 1357 | handle_py = handle_py.split("]\r\n",1)[1] |
| 1358 | handle_py = handle_py.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1359 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1360 | self.handle.sendline("") |
| 1361 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1362 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1363 | 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame^] | 1368 | return host_list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1369 | |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1370 | |
| 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 Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1377 | try: |
| 1378 | self.handle.sendline("") |
| 1379 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1380 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1381 | self.handle.sendline("update") |
| 1382 | self.handle.expect("update") |
| 1383 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1384 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1385 | self.handle.sendline("") |
| 1386 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1387 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1388 | 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 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1395 | if __name__ != "__main__": |
| 1396 | import sys |
| 1397 | sys.modules[__name__] = MininetCliDriver() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1398 | |