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 |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 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 |
| 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import signal |
| 30 | import re |
| 31 | import sys |
| 32 | import core.teston |
| 33 | sys.path.append("../") |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 34 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | from drivers.common.cli.emulatordriver import Emulator |
| 36 | from drivers.common.clidriver import CLI |
| 37 | |
| 38 | class MininetCliDriver(Emulator): |
| 39 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 40 | MininetCliDriver is the basic driver which will handle the Mininet functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | ''' |
| 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 Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 49 | ''' |
| 50 | Here the main is the TestON instance after creating all the log handles. |
| 51 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | 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 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 61 | main.log.info(self.name+": Clearing any residual state or processes") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 62 | self.handle.sendline("sudo mn -c") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 63 | 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] | 64 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 65 | main.log.info(self.name+": Sending sudo password") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 66 | self.handle.sendline(self.pwd) |
| 67 | i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 68 | if i==1: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 69 | main.log.info(self.name+": Clean") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 70 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 71 | main.log.error(self.name+": Connection terminated") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 72 | elif i==3: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 73 | main.log.error(self.name+": Something while cleaning MN took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 75 | main.log.info(self.name+": building fresh mininet") |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 76 | #### for reactive/PARP enabled tests |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 77 | cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] + " " + self.options['arg3'] |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 78 | |
| 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 |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 86 | #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] | 87 | self.handle.sendline(cmdString) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 88 | self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | while 1: |
| 90 | i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300) |
| 91 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 92 | main.log.info(self.name+": mininet built") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | return main.TRUE |
| 94 | if i==1: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 95 | self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | main.log.info(self.handle.before) |
| 97 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 98 | main.log.error(self.name+": Launching mininet failed...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 99 | return main.FALSE |
| 100 | elif i==3: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 101 | main.log.error(self.name+": Connection timeout") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 102 | return main.FALSE |
| 103 | elif i==4: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 104 | main.log.error(self.name+": Something took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 106 | #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "): |
| 107 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 108 | 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") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | return main.FALSE |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 112 | |
| 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 118 | count = 0 |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 119 | while(k <= depth-1): |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 120 | count = count + pow(fanout,k) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 121 | k = k+1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 122 | num_switches = count |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 123 | 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 Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 127 | count = count + pow(fanout,k) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 128 | k = k+1 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 129 | num_links = count * fanout |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 130 | #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 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | def pingall(self): |
| 150 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 151 | Verifies the reachability of the hosts using pingall command. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 152 | ''' |
| 153 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 154 | main.log.info(self.name+": Checking reachabilty to the hosts using pingall") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 155 | try: |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 156 | response = self.execute(cmd="pingall",prompt="mininet>",timeout=300) |
| 157 | except pexpect.EOF: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 158 | main.log.error(self.name + ": EOF exception found") |
| 159 | main.log.error(self.name + ": " + self.handle.before) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 160 | main.cleanup() |
| 161 | main.exit() |
| 162 | except pexpect.TIMEOUT: |
| 163 | #We may not want to kill the test if pexpect times out |
| 164 | main.log.error(self.name + ": TIMEOUT exception found") |
| 165 | main.log.error(self.name + ": " + str(self.handle.before) ) |
| 166 | #NOTE: mininet's pingall rounds, so we will check the number of passed and number of failed |
| 167 | pattern = "Results\:\s0\%\sdropped\s\((?P<passed>[\d]+)/(?P=passed)" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 168 | if re.search(pattern,response): |
| 169 | main.log.info(self.name+": All hosts are reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | return main.TRUE |
| 171 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 172 | main.log.error(self.name+": Unable to reach all the hosts") |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 173 | main.log.info("Pingall ouput: " + str(response)) |
| 174 | #NOTE: Send ctrl-c to make sure pingall is done |
| 175 | self.handle.send("\x03") |
| 176 | self.handle.expect("Interrupt") |
| 177 | self.handle.expect("mininet>") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 178 | return main.FALSE |
| 179 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 180 | main.log.error(self.name+": Connection failed to the host") |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 181 | main.cleanup() |
| 182 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 183 | |
| 184 | def fpingHost(self,**pingParams): |
| 185 | ''' |
| 186 | Uses the fping package for faster pinging... |
| 187 | *requires fping to be installed on machine running mininet |
| 188 | ''' |
| 189 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 190 | command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"] |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 191 | self.handle.sendline(command) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 192 | self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT]) |
| 193 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 194 | response = self.handle.before |
| 195 | if re.search(":\s-" ,response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 196 | main.log.info(self.name+": Ping fail") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 197 | return main.FALSE |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 198 | elif re.search(":\s\d{1,2}\.\d\d", response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 199 | main.log.info(self.name+": Ping good!") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 200 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 201 | main.log.info(self.name+": Install fping on mininet machine... ") |
| 202 | main.log.info(self.name+": \n---\n"+response) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 203 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | |
| 205 | def pingHost(self,**pingParams): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 206 | ''' |
| 207 | Ping from one mininet host to another |
| 208 | Currently the only supported Params: SRC and TARGET |
| 209 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
| 211 | #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"] |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 212 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 213 | try: |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 214 | main.log.warn("Sending: " + command) |
| 215 | #response = self.execute(cmd=command,prompt="mininet",timeout=10 ) |
| 216 | self.handle.sendline(command) |
| 217 | i = self.handle.expect([command,pexpect.TIMEOUT]) |
| 218 | if i == 1: |
| 219 | main.log.error(self.name + ": timeout when waiting for response from mininet") |
| 220 | main.log.error("response: " + str(self.handle.before)) |
| 221 | i = self.handle.expect(["mininet>",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 | response = self.handle.before |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 226 | except pexpect.EOF: |
| 227 | main.log.error(self.name + ": EOF exception found") |
| 228 | main.log.error(self.name + ": " + self.handle.before) |
| 229 | main.cleanup() |
| 230 | main.exit() |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 231 | main.log.info(self.name+": Ping Response: "+ response ) |
| 232 | #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 233 | if re.search(',\s0\%\spacket\sloss',response): |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 234 | main.log.info(self.name+": no packets lost, host is reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | main.last_result = main.TRUE |
| 236 | return main.TRUE |
| 237 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 238 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 239 | main.last_result = main.FALSE |
| 240 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 241 | |
| 242 | def checkIP(self,host): |
| 243 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 244 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | ''' |
| 246 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 247 | try: |
| 248 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 249 | except pexpect.EOF: |
| 250 | main.log.error(self.name + ": EOF exception found") |
| 251 | main.log.error(self.name + ": " + self.handle.before) |
| 252 | main.cleanup() |
| 253 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | |
| 255 | 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})" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 256 | #pattern = "inet addr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 257 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") : |
| 258 | if re.search(pattern,response): |
| 259 | main.log.info(self.name+": Host Ip configured properly") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 260 | return main.TRUE |
| 261 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 262 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 263 | return main.FALSE |
| 264 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 265 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 266 | |
| 267 | def verifySSH(self,**connectargs): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 268 | try: |
| 269 | response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 270 | response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 271 | for key in connectargs: |
| 272 | vars(self)[key] = connectargs[key] |
| 273 | response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10) |
| 274 | except pexpect.EOF: |
| 275 | main.log.error(self.name + ": EOF exception found") |
| 276 | main.log.error(self.name + ": " + self.handle.before) |
| 277 | main.cleanup() |
| 278 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 279 | import time |
| 280 | time.sleep(20) |
| 281 | if self.flag == 0: |
| 282 | self.flag = 1 |
| 283 | return main.FALSE |
| 284 | else : |
| 285 | return main.TRUE |
| 286 | |
| 287 | def getMacAddress(self,host): |
| 288 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 289 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 290 | ''' |
| 291 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 292 | try: |
| 293 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 294 | except pexpect.EOF: |
| 295 | main.log.error(self.name + ": EOF exception found") |
| 296 | main.log.error(self.name + ": " + self.handle.before) |
| 297 | main.cleanup() |
| 298 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 299 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 300 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 301 | mac_address_search = re.search(pattern, response, re.I) |
| 302 | mac_address = mac_address_search.group().split(" ")[1] |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 303 | 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] | 304 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 305 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 306 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 307 | |
| 308 | def getInterfaceMACAddress(self,host, interface): |
| 309 | ''' |
| 310 | Return the IP address of the interface on the given host |
| 311 | ''' |
| 312 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 313 | try: |
| 314 | response = self.execute(cmd=host+" ifconfig " + interface, |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 315 | prompt="mininet>",timeout=10) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 316 | except pexpect.EOF: |
| 317 | main.log.error(self.name + ": EOF exception found") |
| 318 | main.log.error(self.name + ": " + self.handle.before) |
| 319 | main.cleanup() |
| 320 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 321 | |
| 322 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 323 | mac_address_search = re.search(pattern, response, re.I) |
| 324 | if mac_address_search is None: |
| 325 | main.log.info("No mac address found in %s" % response) |
| 326 | return main.FALSE |
| 327 | mac_address = mac_address_search.group().split(" ")[1] |
| 328 | main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address) |
| 329 | return mac_address |
| 330 | else: |
| 331 | main.log.error("Connection failed to the host") |
| 332 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 333 | def getIPAddress(self,host): |
| 334 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 335 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 336 | ''' |
| 337 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 338 | try: |
| 339 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 340 | except pexpect.EOF: |
| 341 | main.log.error(self.name + ": EOF exception found") |
| 342 | main.log.error(self.name + ": " + self.handle.before) |
| 343 | main.cleanup() |
| 344 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 345 | |
| 346 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 347 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 348 | 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] | 349 | return ip_address_search.group(1) |
| 350 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 351 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 352 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 353 | def getSwitchDPID(self,switch): |
| 354 | ''' |
| 355 | return the datapath ID of the switch |
| 356 | ''' |
| 357 | if self.handle : |
| 358 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 359 | try: |
| 360 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 361 | except pexpect.EOF: |
| 362 | main.log.error(self.name + ": EOF exception found") |
| 363 | main.log.error(self.name + ": " + self.handle.before) |
| 364 | main.cleanup() |
| 365 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 366 | pattern = r'^(?P<dpid>\d)+' |
| 367 | result = re.search(pattern, response, re.MULTILINE) |
| 368 | if result is None: |
| 369 | main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response)) |
| 370 | return main.FALSE |
Jon Hall | c1a1d24 | 2014-07-21 16:03:33 -0700 | [diff] [blame] | 371 | return str(result.group(0)) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 372 | else: |
| 373 | main.log.error("Connection failed to the host") |
| 374 | |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 375 | def getDPID(self, switch): |
| 376 | if self.handle: |
| 377 | self.handle.sendline("") |
| 378 | self.expect("mininet>") |
| 379 | cmd = "py %s.dpid" %switch |
| 380 | try: |
| 381 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 382 | self.handle.expect("mininet>") |
| 383 | response = self.handle.before |
| 384 | return response |
| 385 | except pexpect.EOF: |
| 386 | main.log.error(self.name + ": EOF exception found") |
| 387 | main.log.error(self.name + ": " + self.handle.before) |
| 388 | main.cleanup() |
| 389 | main.exit() |
| 390 | |
| 391 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 392 | def getInterfaces(self, node): |
| 393 | ''' |
| 394 | return information dict about interfaces connected to the node |
| 395 | ''' |
| 396 | if self.handle : |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 397 | 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] | 398 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 399 | try: |
| 400 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 401 | except pexpect.EOF: |
| 402 | main.log.error(self.name + ": EOF exception found") |
| 403 | main.log.error(self.name + ": " + self.handle.before) |
| 404 | main.cleanup() |
| 405 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 406 | return response |
| 407 | else: |
| 408 | main.log.error("Connection failed to the node") |
| 409 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 410 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 411 | main.log.info(self.name+": Dump node info") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 412 | try: |
| 413 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
| 414 | except pexpect.EOF: |
| 415 | main.log.error(self.name + ": EOF exception found") |
| 416 | main.log.error(self.name + ": " + self.handle.before) |
| 417 | main.cleanup() |
| 418 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 419 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 420 | |
| 421 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 422 | main.log.info(self.name+": List interfaces") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 423 | try: |
| 424 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
| 425 | except pexpect.EOF: |
| 426 | main.log.error(self.name + ": EOF exception found") |
| 427 | main.log.error(self.name + ": " + self.handle.before) |
| 428 | main.cleanup() |
| 429 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 430 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 431 | |
| 432 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 433 | main.log.info(self.name+": List network connections") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 434 | try: |
| 435 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
| 436 | except pexpect.EOF: |
| 437 | main.log.error(self.name + ": EOF exception found") |
| 438 | main.log.error(self.name + ": " + self.handle.before) |
| 439 | main.cleanup() |
| 440 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 441 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 442 | |
| 443 | def iperf(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 444 | 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] | 445 | try: |
| 446 | response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10) |
| 447 | except pexpect.EOF: |
| 448 | main.log.error(self.name + ": EOF exception found") |
| 449 | main.log.error(self.name + ": " + self.handle.before) |
| 450 | main.cleanup() |
| 451 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 452 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 453 | |
| 454 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 455 | 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] | 456 | try: |
| 457 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
| 458 | except pexpect.EOF: |
| 459 | main.log.error(self.name + ": EOF exception found") |
| 460 | main.log.error(self.name + ": " + self.handle.before) |
| 461 | main.cleanup() |
| 462 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 463 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 464 | |
| 465 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 466 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 467 | try: |
| 468 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 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() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 474 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 475 | |
| 476 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 477 | main.log.info(self.name+": Ping between first two hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 478 | try: |
| 479 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
| 480 | except pexpect.EOF: |
| 481 | main.log.error(self.name + ": EOF exception found") |
| 482 | main.log.error(self.name + ": " + self.handle.before) |
| 483 | main.cleanup() |
| 484 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 485 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 486 | #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"): |
| 487 | if re.search(',\s0\%\spacket\sloss',response): |
| 488 | main.log.info(self.name+": Ping between two hosts SUCCESSFUL") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 489 | main.last_result = main.TRUE |
| 490 | return main.TRUE |
| 491 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 492 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 493 | main.last_result = main.FALSE |
| 494 | return main.FALSE |
| 495 | |
| 496 | def link(self,**linkargs): |
| 497 | ''' |
| 498 | Bring link(s) between two nodes up or down |
| 499 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 500 | args = utilities.parse_args(["END1","END2","OPTION"],**linkargs) |
| 501 | end1 = args["END1"] if args["END1"] != None else "" |
| 502 | end2 = args["END2"] if args["END2"] != None else "" |
| 503 | option = args["OPTION"] if args["OPTION"] != None else "" |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 504 | main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 505 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 506 | try: |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 507 | #response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 508 | self.handle.sendline(command) |
| 509 | self.handle.expect("mininet>") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 510 | except pexpect.EOF: |
| 511 | main.log.error(self.name + ": EOF exception found") |
| 512 | main.log.error(self.name + ": " + self.handle.before) |
| 513 | main.cleanup() |
| 514 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 515 | return main.TRUE |
| 516 | |
| 517 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 518 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 519 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 520 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 521 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 522 | main.log.info('Yank the switch interface attached to a host') |
| 523 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 524 | sw = args["SW"] if args["SW"] !=None else "" |
| 525 | intf = args["INTF"] if args["INTF"] != None else "" |
| 526 | command = "py "+ str(sw) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 527 | try: |
| 528 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 529 | except pexpect.EOF: |
| 530 | main.log.error(self.name + ": EOF exception found") |
| 531 | main.log.error(self.name + ": " + self.handle.before) |
| 532 | main.cleanup() |
| 533 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 534 | return main.TRUE |
| 535 | |
| 536 | def plug(self, **plugargs): |
| 537 | ''' |
| 538 | plug the yanked mininet switch interface to a switch |
| 539 | ''' |
| 540 | main.log.info('Plug the switch interface attached to a switch') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 541 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 542 | sw = args["SW"] if args["SW"] !=None else "" |
| 543 | intf = args["INTF"] if args["INTF"] != None else "" |
| 544 | command = "py "+ str(sw) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 545 | try: |
| 546 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 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() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 552 | return main.TRUE |
| 553 | |
| 554 | |
| 555 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 556 | def dpctl(self,**dpctlargs): |
| 557 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 558 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 559 | ''' |
| 560 | main.log.info('Run dpctl command on all switches') |
| 561 | args = utilities.parse_args(["CMD","ARGS"],**dpctlargs) |
| 562 | cmd = args["CMD"] if args["CMD"] != None else "" |
| 563 | cmdargs = args["ARGS"] if args["ARGS"] != None else "" |
| 564 | command = "dpctl "+cmd + " " + str(cmdargs) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 565 | try: |
| 566 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 567 | except pexpect.EOF: |
| 568 | main.log.error(self.name + ": EOF exception found") |
| 569 | main.log.error(self.name + ": " + self.handle.before) |
| 570 | main.cleanup() |
| 571 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 572 | return main.TRUE |
| 573 | |
| 574 | |
| 575 | def get_version(self): |
| 576 | file_input = path+'/lib/Mininet/INSTALL' |
| 577 | version = super(Mininet, self).get_version() |
| 578 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 579 | for line in open(file_input,'r').readlines(): |
| 580 | result = re.match(pattern, line) |
| 581 | if result: |
| 582 | version = result.group(0) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 583 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 584 | |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 585 | def get_sw_controller(self, sw): |
| 586 | ''' |
| 587 | Parameters: |
| 588 | sw: The name of an OVS switch. Example "s1" |
| 589 | Return: |
| 590 | The output of the command from the mininet cli or main.FALSE on timeout |
| 591 | ''' |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 592 | command = "sh ovs-vsctl get-controller "+str(sw) |
| 593 | try: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 594 | response = self.execute(cmd=command, prompt="mininet>", timeout=10) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 595 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 596 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 597 | else: |
| 598 | return main.FALSE |
| 599 | except pexpect.EOF: |
| 600 | main.log.error(self.name + ": EOF exception found") |
| 601 | main.log.error(self.name + ": " + self.handle.before) |
| 602 | main.cleanup() |
| 603 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 604 | |
| 605 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 606 | ''' |
| 607 | count is only needed if there is more than 1 controller |
| 608 | ''' |
| 609 | args = utilities.parse_args(["COUNT"],**kwargs) |
| 610 | count = args["COUNT"] if args!={} else 1 |
| 611 | |
| 612 | argstring = "SW" |
| 613 | for j in range(count): |
| 614 | argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1) |
| 615 | args = utilities.parse_args(argstring.split(","),**kwargs) |
| 616 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 617 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 618 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 619 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
| 620 | |
| 621 | command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " " |
| 622 | for j in range(count): |
| 623 | i=j+1 |
| 624 | args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs) |
| 625 | ip = args["IP"+str(i)] if args["IP"+str(i)] != None else "" |
| 626 | port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else "" |
| 627 | tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else "" |
| 628 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 629 | try: |
| 630 | self.execute(cmd=command,prompt="mininet>",timeout=5) |
| 631 | except pexpect.EOF: |
| 632 | main.log.error(self.name + ": EOF exception found") |
| 633 | main.log.error(self.name + ": " + self.handle.before) |
| 634 | main.cleanup() |
| 635 | main.exit() |
| 636 | except: |
| 637 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 638 | main.log.error( traceback.print_exc() ) |
| 639 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 640 | main.cleanup() |
| 641 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 642 | |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 643 | def delete_sw_controller(self,sw): |
| 644 | ''' |
| 645 | Removes the controller target from sw |
| 646 | ''' |
| 647 | |
| 648 | command = "sh ovs-vsctl del-controller "+str(sw) |
| 649 | try: |
| 650 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 651 | except pexpect.EOF: |
| 652 | main.log.error(self.name + ": EOF exception found") |
| 653 | main.log.error(self.name + ": " + self.handle.before) |
| 654 | main.cleanup() |
| 655 | main.exit() |
| 656 | else: |
| 657 | main.log.info(response) |
| 658 | |
| 659 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 660 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 661 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 662 | response = '' |
| 663 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 664 | try: |
| 665 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 666 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 667 | self.handle.sendline("sudo mn -c") |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 668 | response = main.TRUE |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 669 | except pexpect.EOF: |
| 670 | main.log.error(self.name + ": EOF exception found") |
| 671 | main.log.error(self.name + ": " + self.handle.before) |
| 672 | main.cleanup() |
| 673 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 674 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 675 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 676 | response = main.FALSE |
| 677 | return response |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 678 | |
| 679 | def arping(self, src, dest, destmac): |
| 680 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 681 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 682 | |
| 683 | self.handle.sendline(src + ' arping ' + dest) |
| 684 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 685 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 686 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 687 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 688 | return main.TRUE |
| 689 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 690 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 691 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 692 | return main.FALSE |
| 693 | |
| 694 | def decToHex(num): |
| 695 | return hex(num).split('x')[1] |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 696 | |
| 697 | def getSwitchFlowCount(self, switch): |
| 698 | ''' |
| 699 | return the Flow Count of the switch |
| 700 | ''' |
| 701 | if self.handle: |
| 702 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 703 | try: |
| 704 | response = self.execute(cmd=cmd, prompt="mininet>", timeout=10) |
| 705 | except pexpect.EOF: |
| 706 | main.log.error(self.name + ": EOF exception found") |
| 707 | main.log.error(self.name + " " + self.handle.before) |
| 708 | main.cleanup() |
| 709 | main.exit() |
| 710 | pattern = "flow_count=(\d+)" |
| 711 | result = re.search(pattern, response, re.MULTILINE) |
| 712 | if result is None: |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 713 | main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response)) |
| 714 | return main.FALSE |
| 715 | return result.group(1) |
| 716 | else: |
| 717 | main.log.error("Connection failed to the Mininet host") |
| 718 | |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 719 | def check_flows(self, sw, dump_format=None): |
| 720 | if dump_format: |
| 721 | command = "sh ovs-ofctl -F " + dump_format + " dump-flows " + str(sw) |
| 722 | else: |
| 723 | command = "sh ovs-ofctl dump-flows "+str(sw) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 724 | try: |
| 725 | response=self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 726 | return response |
| 727 | except pexpect.EOF: |
| 728 | main.log.error(self.name + ": EOF exception found") |
| 729 | main.log.error(self.name + ": " + self.handle.before) |
| 730 | main.cleanup() |
| 731 | main.exit() |
| 732 | else: |
| 733 | main.log.info(response) |
| 734 | |
| 735 | def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"): |
| 736 | ''' |
| 737 | Runs tpdump on an intferface and saves the file |
| 738 | intf can be specified, or the default eth0 is used |
| 739 | ''' |
| 740 | try: |
| 741 | self.handle.sendline("") |
| 742 | self.handle.expect("mininet>") |
| 743 | self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &") |
| 744 | self.handle.sendline("") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 745 | i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10) |
| 746 | main.log.warn(self.handle.before + self.handle.after) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 747 | self.handle.sendline("") |
| 748 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 749 | if i == 0: |
| 750 | main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf) |
| 751 | return main.FALSE |
| 752 | elif i == 1: |
| 753 | main.log.info(self.name + ": tcpdump started on " + intf) |
| 754 | return main.TRUE |
| 755 | elif i == 2: |
| 756 | main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf) |
| 757 | return main.FALSE |
| 758 | elif i ==3: |
| 759 | main.log.info(self.name +": " + self.handle.before) |
| 760 | return main.TRUE |
| 761 | else: |
| 762 | main.log.error(self.name + ": tcpdump - unexpected response") |
| 763 | return main.FALSE |
| 764 | except pexpect.EOF: |
| 765 | main.log.error(self.name + ": EOF exception found") |
| 766 | main.log.error(self.name + ": " + self.handle.before) |
| 767 | main.cleanup() |
| 768 | main.exit() |
| 769 | except: |
| 770 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 771 | main.log.error( traceback.print_exc() ) |
| 772 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 773 | main.cleanup() |
| 774 | main.exit() |
| 775 | |
| 776 | def stop_tcpdump(self): |
| 777 | "pkills tcpdump" |
| 778 | try: |
| 779 | self.handle.sendline("sh sudo pkill tcpdump") |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 780 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 781 | self.handle.sendline("") |
| 782 | self.handle.expect("mininet>") |
| 783 | except pexpect.EOF: |
| 784 | main.log.error(self.name + ": EOF exception found") |
| 785 | main.log.error(self.name + ": " + self.handle.before) |
| 786 | main.cleanup() |
| 787 | main.exit() |
| 788 | except: |
| 789 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 790 | main.log.error( traceback.print_exc() ) |
| 791 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 792 | main.cleanup() |
| 793 | main.exit() |
| 794 | |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 795 | def compare_switches(self, topo, switches_json): |
| 796 | ''' |
| 797 | Compare mn and onos switches |
| 798 | topo: sts TestONTopology object |
| 799 | switches_json: parsed json object from the onos devices api |
| 800 | |
| 801 | This uses the sts TestONTopology object |
| 802 | |
| 803 | ''' |
| 804 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 805 | #main.log.debug("Switches_json string: ", switches_json) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 806 | output = {"switches":[]} |
| 807 | 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] | 808 | ports = [] |
| 809 | for port in switch.ports.values(): |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 810 | ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name}) |
| 811 | 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] | 812 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 813 | #print "mn" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 814 | #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': ')) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 815 | #print "onos" |
| 816 | #print json.dumps(switches_json, sort_keys=True,indent=4,separators=(',', ': ')) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 817 | |
| 818 | |
| 819 | # created sorted list of dpid's in MN and ONOS for comparison |
| 820 | mnDPIDs=[] |
| 821 | for switch in output['switches']: |
| 822 | mnDPIDs.append(switch['dpid']) |
| 823 | mnDPIDs.sort() |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 824 | #print "List of Mininet switch DPID's" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 825 | #print mnDPIDs |
| 826 | if switches_json == "":#if rest call fails |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 827 | 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] | 828 | return main.FALSE |
| 829 | onos=switches_json |
| 830 | onosDPIDs=[] |
| 831 | for switch in onos: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 832 | if switch['available'] == True: |
| 833 | onosDPIDs.append(switch['id'].replace(":",'').replace("of",'')) |
| 834 | #else: |
| 835 | #print "Switch is unavailable:" |
| 836 | #print switch |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 837 | onosDPIDs.sort() |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 838 | #print "List of ONOS switch DPID's" |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 839 | #print onosDPIDs |
| 840 | |
| 841 | if mnDPIDs!=onosDPIDs: |
| 842 | switch_results = main.FALSE |
| 843 | main.log.report( "Switches in MN but not in ONOS:") |
| 844 | main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs])) |
| 845 | main.log.report( "Switches in ONOS but not in MN:") |
| 846 | main.log.report( str([switch for switch in onosDPIDs if switch not in mnDPIDs])) |
| 847 | else:#list of dpid's match in onos and mn |
| 848 | #main.log.report("DEBUG: The dpid's of the switches in Mininet and ONOS match") |
| 849 | switch_results = main.TRUE |
| 850 | return switch_results |
| 851 | |
| 852 | |
| 853 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 854 | def compare_ports(self, topo, ports_json): |
| 855 | ''' |
| 856 | Compare mn and onos ports |
| 857 | topo: sts TestONTopology object |
| 858 | ports_json: parsed json object from the onos ports api |
| 859 | |
| 860 | Dependencies: |
| 861 | 1. This uses the sts TestONTopology object |
| 862 | 2. numpy - "sudo pip install numpy" |
| 863 | |
| 864 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 865 | #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] | 866 | import json |
| 867 | from numpy import uint64 |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 868 | ports_results = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 869 | output = {"switches":[]} |
| 870 | 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] | 871 | ports = [] |
| 872 | for port in switch.ports.values(): |
| 873 | #print port.hw_addr.toStr(separator = '') |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 874 | tmp_port = {} |
| 875 | tmp_port['of_port'] = port.port_no |
| 876 | tmp_port['mac'] = str(port.hw_addr).replace('\'','') |
| 877 | tmp_port['name'] = port.name |
| 878 | tmp_port['enabled'] = port.enabled |
| 879 | |
| 880 | ports.append(tmp_port) |
| 881 | tmp_switch = {} |
| 882 | tmp_switch['name'] = switch.name |
| 883 | tmp_switch['dpid'] = str(switch.dpid).zfill(16) |
| 884 | tmp_switch['ports'] = ports |
| 885 | |
| 886 | output['switches'].append(tmp_switch) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 887 | |
| 888 | |
| 889 | ################ports############# |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 890 | for mn_switch in output['switches']: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 891 | mn_ports = [] |
| 892 | onos_ports = [] |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 893 | switch_result = main.TRUE |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 894 | for port in mn_switch['ports']: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 895 | if port['enabled'] == True: |
| 896 | mn_ports.append(port['of_port']) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 897 | #else: #DEBUG only |
| 898 | # 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] | 899 | for onos_switch in ports_json: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 900 | #print "Iterating through a new switch as seen by ONOS" |
| 901 | #print onos_switch |
| 902 | if onos_switch['device']['available'] == True: |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 903 | if onos_switch['device']['id'].replace(':','').replace("of", '') == mn_switch['dpid']: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 904 | for port in onos_switch['ports']: |
| 905 | if port['isEnabled']: |
| 906 | #print "Iterating through available ports on the switch" |
| 907 | #print port |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 908 | if port['port'] == 'local': |
| 909 | #onos_ports.append('local') |
| 910 | onos_ports.append(long(uint64(-2))) |
| 911 | else: |
| 912 | onos_ports.append(int(port['port'])) |
| 913 | ''' |
| 914 | else: #This is likely a new reserved port implemented |
| 915 | main.log.error("unkown port '" + str(port['port']) ) |
| 916 | ''' |
| 917 | #else: #DEBUG |
| 918 | # main.log.warn("Port %s on switch %s is down" % ( str(port['port']) , str(onos_switch['device']['id'])) ) |
| 919 | break |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 920 | mn_ports.sort(key=float) |
| 921 | onos_ports.sort(key=float) |
| 922 | #print "\nPorts for Switch %s:" % (switch['name']) |
| 923 | #print "\tmn_ports[] = ", mn_ports |
| 924 | #print "\tonos_ports[] = ", onos_ports |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 925 | mn_ports_log = mn_ports |
| 926 | onos_ports_log = onos_ports |
| 927 | mn_ports = [x for x in mn_ports] |
| 928 | onos_ports = [x for x in onos_ports] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 929 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 930 | #TODO: handle other reserved port numbers besides LOCAL |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 931 | #NOTE: List of Reserved Ports |
| 932 | # Local port: -2 in unsigned int in Openflow, the size depends on |
| 933 | # openflow version( 1.0 is 16 bit, 1.3 is 32 bit), ONOS shows |
| 934 | # 'local', we store as long(uint64(-2)) |
| 935 | for mn_port in mn_ports_log: |
| 936 | if mn_port in onos_ports: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 937 | #don't set results to true here as this is just one of many checks and it might override a failure |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 938 | mn_ports.remove(mn_port) |
| 939 | onos_ports.remove(mn_port) |
| 940 | #NOTE: OVS reports this as down since there is no link |
| 941 | # So ignoring these for now |
| 942 | #TODO: Come up with a better way of handling these |
| 943 | if 65534 in mn_ports: |
| 944 | mn_ports.remove(65534) |
| 945 | if long(uint64(-2)) in onos_ports: |
| 946 | onos_ports.remove( long(uint64(-2)) ) |
| 947 | if len(mn_ports): #the ports of this switch don't match |
| 948 | switch_result = main.FALSE |
| 949 | main.log.warn("Ports in MN but not ONOS: " + str(mn_ports) ) |
| 950 | if len(onos_ports): #the ports of this switch don't match |
| 951 | switch_result = main.FALSE |
| 952 | main.log.warn("Ports in ONOS but not MN: " + str(onos_ports) ) |
| 953 | if switch_result == main.FALSE: |
| 954 | main.log.report("The list of active ports for switch %s(%s) does not match:" % (mn_switch['name'], mn_switch['dpid']) ) |
| 955 | main.log.warn("mn_ports[] = " + str(mn_ports_log)) |
| 956 | main.log.warn("onos_ports[] = " + str(onos_ports_log)) |
| 957 | ports_results = ports_results and switch_result |
| 958 | return ports_results |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | def compare_links(self, topo, links_json): |
| 964 | ''' |
| 965 | Compare mn and onos links |
| 966 | topo: sts TestONTopology object |
| 967 | links_json: parsed json object from the onos links api |
| 968 | |
| 969 | This uses the sts TestONTopology object |
| 970 | |
| 971 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 972 | #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] | 973 | import json |
| 974 | link_results = main.TRUE |
| 975 | output = {"switches":[]} |
| 976 | onos = links_json |
| 977 | 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] | 978 | # print "Iterating though switches as seen by Mininet" |
| 979 | # print switch |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 980 | ports = [] |
| 981 | for port in switch.ports.values(): |
| 982 | #print port.hw_addr.toStr(separator = '') |
| 983 | ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name}) |
| 984 | output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports }) |
| 985 | #######Links######## |
| 986 | |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 987 | mn_links = [link for link in topo.patch_panel.network_links if (link.port1.enabled and link.port2.enabled)] |
| 988 | #print "mn_links:" |
| 989 | #print mn_links |
| 990 | if 2*len(mn_links) == len(onos): |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 991 | link_results = main.TRUE |
| 992 | else: |
| 993 | link_results = main.FALSE |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 994 | 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] | 995 | |
| 996 | |
| 997 | # iterate through MN links and check if an ONOS link exists in both directions |
| 998 | # NOTE: Will currently only show mn links as down if they are cut through STS. |
| 999 | # We can either do everything through STS or wait for up_network_links |
| 1000 | # and down_network_links to be fully implemented. |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1001 | for link in mn_links: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1002 | #print "Link: %s" % link |
| 1003 | #TODO: Find a more efficient search method |
| 1004 | node1 = None |
| 1005 | port1 = None |
| 1006 | node2 = None |
| 1007 | port2 = None |
| 1008 | first_dir = main.FALSE |
| 1009 | second_dir = main.FALSE |
| 1010 | for switch in output['switches']: |
| 1011 | #print "Switch: %s" % switch['name'] |
| 1012 | if switch['name'] == link.node1.name: |
| 1013 | node1 = switch['dpid'] |
| 1014 | for port in switch['ports']: |
| 1015 | if str(port['name']) == str(link.port1): |
| 1016 | port1 = port['of_port'] |
| 1017 | if node1 is not None and node2 is not None: |
| 1018 | break |
| 1019 | if switch['name'] == link.node2.name: |
| 1020 | node2 = switch['dpid'] |
| 1021 | for port in switch['ports']: |
| 1022 | if str(port['name']) == str(link.port2): |
| 1023 | port2 = port['of_port'] |
| 1024 | if node1 is not None and node2 is not None: |
| 1025 | break |
| 1026 | |
| 1027 | |
| 1028 | for onos_link in onos: |
| 1029 | onos_node1 = onos_link['src']['device'].replace(":",'').replace("of", '') |
| 1030 | onos_node2 = onos_link['dst']['device'].replace(":",'').replace("of", '') |
| 1031 | onos_port1 = onos_link['src']['port'] |
| 1032 | onos_port2 = onos_link['dst']['port'] |
| 1033 | |
| 1034 | #print "Checking ONOS for link %s/%s -> %s/%s and" % (node1, port1, node2, port2) |
| 1035 | #print "Checking ONOS for link %s/%s -> %s/%s" % (node2, port2, node1, port1) |
| 1036 | # check onos link from node1 to node2 |
| 1037 | if str(onos_node1) == str(node1) and str(onos_node2) == str(node2): |
| 1038 | if int(onos_port1) == int(port1) and int(onos_port2) == int(port2): |
| 1039 | first_dir = main.TRUE |
| 1040 | else: |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1041 | main.log.warn('The port numbers do not match for ' +str(link) +\ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1042 | ' between ONOS and MN. When cheking ONOS for link '+\ |
| 1043 | '%s/%s -> %s/%s' % (node1, port1, node2, port2)+\ |
| 1044 | ' ONOS has the values %s/%s -> %s/%s' %\ |
| 1045 | (onos_node1, onos_port1, onos_node2, onos_port2)) |
| 1046 | |
| 1047 | # check onos link from node2 to node1 |
| 1048 | elif ( str(onos_node1) == str(node2) and str(onos_node2) == str(node1) ): |
| 1049 | if ( int(onos_port1) == int(port2) and int(onos_port2) == int(port1) ): |
| 1050 | second_dir = main.TRUE |
| 1051 | else: |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1052 | main.log.warn('The port numbers do not match for ' +str(link) +\ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1053 | ' between ONOS and MN. When cheking ONOS for link '+\ |
| 1054 | '%s/%s -> %s/%s' % (node2, port2, node1, port1)+\ |
| 1055 | ' ONOS has the values %s/%s -> %s/%s' %\ |
| 1056 | (onos_node2, onos_port2, onos_node1, onos_port1)) |
| 1057 | else:#this is not the link you're looking for |
| 1058 | pass |
| 1059 | if not first_dir: |
| 1060 | main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node1, port1, node2, port2)) |
| 1061 | if not second_dir: |
| 1062 | main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node2, port2, node1, port1)) |
| 1063 | link_results = link_results and first_dir and second_dir |
Jon Hall | 62df924 | 2014-10-22 12:20:17 -0400 | [diff] [blame] | 1064 | return link_results |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1065 | |
| 1066 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1067 | def get_hosts(self): |
| 1068 | ''' |
| 1069 | Returns a list of all hosts |
| 1070 | Don't ask questions just use it |
| 1071 | ''' |
| 1072 | self.handle.sendline("") |
| 1073 | self.handle.expect("mininet>") |
| 1074 | |
| 1075 | self.handle.sendline("py [ host.name for host in net.hosts ]") |
| 1076 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1077 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1078 | handle_py = self.handle.before |
| 1079 | handle_py = handle_py.split("]\r\n",1)[1] |
| 1080 | handle_py = handle_py.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1081 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1082 | self.handle.sendline("") |
| 1083 | self.handle.expect("mininet>") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1084 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1085 | host_str = handle_py.replace("]", "") |
| 1086 | host_str = host_str.replace("'", "") |
| 1087 | host_str = host_str.replace("[", "") |
| 1088 | host_list = host_str.split(",") |
| 1089 | |
| 1090 | return host_list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1091 | |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1092 | |
| 1093 | def update(self): |
| 1094 | ''' |
| 1095 | updates the port address and status information for each port in mn |
| 1096 | ''' |
| 1097 | #TODO: Add error checking. currently the mininet command has no output |
| 1098 | main.log.info("Updateing MN port information") |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1099 | try: |
| 1100 | self.handle.sendline("") |
| 1101 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1102 | |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1103 | self.handle.sendline("update") |
| 1104 | self.handle.expect("update") |
| 1105 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1106 | |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1107 | self.handle.sendline("") |
| 1108 | self.handle.expect("mininet>") |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1109 | |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 1110 | return main.TRUE |
| 1111 | except pexpect.EOF: |
| 1112 | main.log.error(self.name + ": EOF exception found") |
| 1113 | main.log.error(self.name + ": " + self.handle.before) |
| 1114 | main.cleanup() |
| 1115 | main.exit() |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1116 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1117 | if __name__ != "__main__": |
| 1118 | import sys |
| 1119 | sys.modules[__name__] = MininetCliDriver() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1120 | |