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