zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | Copyright 2015 Open Networking Foundation (ONF) |
| 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | """ |
| 24 | |
| 25 | """ |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 26 | drivers for ovsdb commands. |
| 27 | |
| 28 | zhanghaoyu7@huawei.com |
| 29 | AUG 10 2015 |
| 30 | """ |
| 31 | import pexpect |
| 32 | import re |
| 33 | import json |
| 34 | import types |
| 35 | import time |
| 36 | import os |
| 37 | from drivers.common.clidriver import CLI |
| 38 | |
| 39 | |
| 40 | class OvsdbDriver( CLI ): |
| 41 | |
| 42 | def __init__( self ): |
| 43 | """ |
| 44 | Initialize client |
| 45 | """ |
| 46 | self.name = None |
| 47 | self.home = None |
| 48 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 49 | super( OvsdbDriver, self ).__init__() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 50 | |
| 51 | def connect( self, **connectargs ): |
| 52 | try: |
| 53 | for key in connectargs: |
| 54 | vars( self)[ key ] = connectargs[ key ] |
| 55 | |
| 56 | self.name = self.options[ 'name' ] |
| 57 | if os.getenv( str( self.ip_address ) ) != None: |
| 58 | self.ip_address = os.getenv(str ( self.ip_address ) ) |
| 59 | else: |
| 60 | main.log.info( self.name + ": Trying to connect to " + |
| 61 | self.ip_address ) |
| 62 | self.handle = super( OvsdbDriver, self ).connect( |
| 63 | user_name=self.user_name, |
| 64 | ip_address=self.ip_address, |
| 65 | port=self.port, |
| 66 | pwd=self.pwd) |
| 67 | |
| 68 | if self.handle: |
You Wang | 90038fb | 2016-10-26 13:55:29 -0700 | [diff] [blame] | 69 | main.log.info( "Connection successful to the ovsdb node " + |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 70 | self.name ) |
alison | 6acef9f | 2016-09-28 12:29:08 -0700 | [diff] [blame] | 71 | return self.handle |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 72 | else: |
| 73 | main.log.error( "Connection failed to the ovsdb node " + |
| 74 | self.name ) |
| 75 | except pexpect.EOF: |
| 76 | main.log.error( self.name + ": EOF exception found" ) |
| 77 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 78 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 79 | except Exception: |
| 80 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 81 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 82 | |
| 83 | def disconnect( self ): |
| 84 | try: |
| 85 | self.handle.sendline( "exit" ) |
| 86 | self.handle.expect( "closed" ) |
| 87 | response = main.TRUE |
| 88 | except pexpect.ExceptionPexpect: |
| 89 | response = main.FALSE |
| 90 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 91 | return response |
| 92 | |
| 93 | def setManager( self, ip, port, delaytime="5" ): |
| 94 | command= "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port ) |
| 95 | try: |
| 96 | handle = self.execute( |
| 97 | cmd=command, |
| 98 | timeout=10 ) |
| 99 | if re.search( "Error", handle ): |
| 100 | main.log.error( "Error in set ovsdb manager" ) |
| 101 | main.log.error( handle ) |
| 102 | return main.FALSE |
| 103 | else: |
| 104 | main.log.info( "Ovsdb manager " + str( ip ) + " set" ) |
| 105 | #delay time for ovsdb connection create |
| 106 | main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" ) |
| 107 | time.sleep( int( delaytime ) ) |
| 108 | return main.TRUE |
| 109 | except pexpect.EOF: |
| 110 | main.log.error( self.name + ": EOF exception found" ) |
| 111 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 112 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 113 | |
| 114 | def delManager( self, delaytime="5" ): |
| 115 | command= "sudo ovs-vsctl del-manager" |
| 116 | try: |
| 117 | handle = self.execute( |
| 118 | cmd=command, |
| 119 | timeout=10 ) |
| 120 | if re.search( "Error", handle ): |
| 121 | main.log.error( "Error in delete ovsdb manager" ) |
| 122 | main.log.error( handle ) |
| 123 | return main.FALSE |
| 124 | else: |
| 125 | main.log.info( "Ovsdb manager delete" ) |
| 126 | #delay time for ovsdb connection delete |
| 127 | main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" ) |
| 128 | time.sleep( int( delaytime ) ) |
| 129 | return main.TRUE |
| 130 | except pexpect.EOF: |
| 131 | main.log.error( self.name + ": EOF exception found" ) |
| 132 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 133 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 134 | |
| 135 | def getManager( self ): |
| 136 | command= "sudo ovs-vsctl get-manager" |
| 137 | try: |
| 138 | response = self.execute( |
| 139 | cmd=command, |
| 140 | timeout=10 ) |
| 141 | return response |
| 142 | except pexpect.EOF: |
| 143 | main.log.error( self.name + ": EOF exception found" ) |
| 144 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 145 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 146 | |
| 147 | def listBr( self ): |
| 148 | """ |
| 149 | Parameters: |
| 150 | none |
| 151 | Return: |
| 152 | The output of the command from the linux |
| 153 | or main.FALSE on timeout |
| 154 | """ |
| 155 | command= "sudo ovs-vsctl list-br" |
| 156 | try: |
| 157 | response = self.execute( |
| 158 | cmd=command, |
| 159 | timeout=10 ) |
| 160 | if response: |
| 161 | return response |
| 162 | else: |
| 163 | return main.FALSE |
| 164 | except pexpect.EOF: |
| 165 | main.log.error( self.name + ": EOF exception found" ) |
| 166 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 167 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 168 | |
| 169 | def listPorts( self, sw ): |
| 170 | """ |
| 171 | Parameters: |
| 172 | sw: The name of an OVS switch. Example "s1" |
| 173 | Return: |
| 174 | The output of the command from the linux |
| 175 | or main.FALSE on timeout |
| 176 | """ |
| 177 | command= "sudo ovs-vsctl list-ports " + str( sw ) |
| 178 | try: |
| 179 | response = self.execute( |
| 180 | cmd=command, |
| 181 | timeout=10 ) |
| 182 | if response: |
| 183 | return response |
| 184 | else: |
| 185 | return main.FALSE |
| 186 | except pexpect.EOF: |
| 187 | main.log.error( self.name + ": EOF exception found" ) |
| 188 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 189 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 190 | |
| 191 | def getController( self, sw ): |
| 192 | """ |
| 193 | Parameters: |
| 194 | sw: The name of an OVS switch. Example "s1" |
| 195 | Return: |
| 196 | The output of the command from the mininet cli |
| 197 | or main.FALSE on timeout""" |
| 198 | command = "sudo ovs-vsctl get-controller " + str( sw ) |
| 199 | try: |
| 200 | response = self.execute( |
| 201 | cmd=command, |
| 202 | timeout=10) |
| 203 | if response: |
| 204 | return response |
| 205 | else: |
| 206 | return main.FALSE |
| 207 | except pexpect.EOF: |
| 208 | main.log.error( self.name + ": EOF exception found" ) |
| 209 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 210 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 211 | |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 212 | def show( self ): |
| 213 | """ |
| 214 | Parameters: |
| 215 | none |
| 216 | Return: |
| 217 | The output of the command from the linux |
| 218 | or main.FALSE on timeout""" |
| 219 | command = "sudo ovs-vsctl show " |
| 220 | try: |
| 221 | response = self.execute( |
| 222 | cmd=command, |
| 223 | timeout=10) |
| 224 | if response: |
| 225 | return response |
| 226 | else: |
| 227 | return main.FALSE |
| 228 | except pexpect.EOF: |
| 229 | main.log.error( self.name + ": EOF exception found" ) |
| 230 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 231 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 232 | |
| 233 | def dumpFlows( self, sw, protocols=None ): |
| 234 | """ |
| 235 | Parameters: |
| 236 | sw: The name of an OVS switch. Example "s1" |
| 237 | Return: |
| 238 | The output of the command from the linux |
| 239 | or main.FALSE on timeout""" |
| 240 | if protocols: |
| 241 | command = "sudo ovs-ofctl -O " + \ |
| 242 | protocols + " dump-flows " + str( sw ) |
| 243 | else: |
| 244 | command = "sudo ovs-ofctl dump-flows " + str( sw ) |
| 245 | try: |
| 246 | response = self.execute( |
| 247 | cmd=command, |
| 248 | timeout=10 ) |
| 249 | if response: |
| 250 | return response |
| 251 | else: |
| 252 | return main.FALSE |
| 253 | except pexpect.EOF: |
| 254 | main.log.error(self.name + ": EOF exception found") |
| 255 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 256 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 257 | |
| 258 | def createHost( self, hostname ): |
| 259 | command = "sudo ip netns add " + str( hostname ) |
| 260 | try: |
| 261 | handle = self.execute( |
| 262 | cmd=command, |
| 263 | timeout=10) |
| 264 | if re.search( "Error", handle ): |
| 265 | main.log.error( "Error in create host" + str( hostname ) ) |
| 266 | main.log.error( handle ) |
| 267 | return main.FALSE |
| 268 | else: |
| 269 | main.log.info( "Create " + str( hostname ) + " sucess" ) |
| 270 | return main.TRUE |
| 271 | except pexpect.EOF: |
| 272 | main.log.error(self.name + ": EOF exception found") |
| 273 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 274 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 275 | |
| 276 | def createHostport(self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ): |
| 277 | command = "sudo ip link add " + str(hostport) +" type veth peer name " + str(ovsport) |
lanqinglong | 26c6b90 | 2015-10-27 16:00:35 +0800 | [diff] [blame] | 278 | command += ";" + "sudo ip link set " + str(hostport) + " up" |
| 279 | command += ";" + "sudo ip link set " + str(ovsport) + " up" |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 280 | command += ";" +" sudo ifconfig " + str(hostport) + " hw ether " + str(hostportmac) |
| 281 | command += ";" +" sudo ip link set " + str(hostport) + " netns " + str(hostname) |
| 282 | try: |
| 283 | handle = self.execute( |
| 284 | cmd=command, |
| 285 | timeout=10) |
| 286 | if re.search( "Error", handle ): |
| 287 | main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) ) |
| 288 | main.log.error( handle ) |
| 289 | return main.FALSE |
| 290 | else: |
| 291 | main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" ) |
| 292 | return main.TRUE |
| 293 | except pexpect.EOF: |
| 294 | main.log.error(self.name + ": EOF exception found") |
| 295 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 296 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 297 | |
| 298 | def addPortToOvs(self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ): |
| 299 | command = "sudo ovs-vsctl add-port " + str(ovsname) +" " + str(port) |
| 300 | if ifaceId: |
| 301 | command += " -- set Interface " + str(port) + " external-ids:iface-id=" + str(ifaceId) + " external-ids:iface-status=active" |
| 302 | if attachedMac: |
| 303 | command += " external-ids:attached-mac=" + str(attachedMac) |
| 304 | if vmuuid: |
| 305 | command += " external-ids:vm-uuid=" + str(vmuuid) |
| 306 | try: |
| 307 | handle = self.execute( |
| 308 | cmd=command, |
| 309 | timeout=10) |
| 310 | if re.search( "Error", handle ): |
| 311 | main.log.error( "Error in add port " + str(port) + " to ovs " + str( ovsname ) ) |
| 312 | main.log.error( handle ) |
| 313 | return main.FALSE |
| 314 | else: |
| 315 | main.log.info( "Add port " + str(port) + " to ovs " + str( ovsname ) + " sucess" ) |
| 316 | return main.TRUE |
| 317 | except pexpect.EOF: |
| 318 | main.log.error(self.name + ": EOF exception found") |
| 319 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 320 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 321 | |
| 322 | def setHostportIp(self, ip, hostname="host1", hostport1="host1-eth0" ): |
| 323 | command = "sudo ip netns exec " + str(hostname) +" ifconfig " + str(hostport1) + " " + str(ip) |
| 324 | try: |
| 325 | handle = self.execute( |
| 326 | cmd=command, |
| 327 | timeout=10) |
| 328 | if re.search( "Error", handle ): |
| 329 | main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) ) |
| 330 | main.log.error( handle ) |
| 331 | return main.FALSE |
| 332 | else: |
| 333 | main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" ) |
| 334 | return main.TRUE |
| 335 | except pexpect.EOF: |
| 336 | main.log.error(self.name + ": EOF exception found") |
| 337 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 338 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 339 | |
| 340 | def hostPing(self, src, target, hostname="host1" ): |
| 341 | if src: |
| 342 | command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 -S " +\ |
| 343 | str( src ) + " " + str( target ) |
| 344 | else: |
| 345 | command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 " + str( target ) |
| 346 | try: |
| 347 | for i in range(1,5): |
| 348 | handle = self.execute( |
| 349 | cmd=command, |
| 350 | timeout=10) |
| 351 | if re.search(',\s0\%\spacket\sloss', handle): |
| 352 | main.log.info(self.name + ": no packets lost, host is reachable") |
| 353 | return main.TRUE |
| 354 | break |
| 355 | time.sleep(5) |
| 356 | else: |
| 357 | main.log.info(self.name + ": packets lost, host is unreachable") |
| 358 | return main.FALSE |
| 359 | except pexpect.EOF: |
| 360 | main.log.error(self.name + ": EOF exception found") |
| 361 | main.log.error(self.name + ": " + self.handle.before) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 362 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 363 | |
| 364 | def delBr( self, sw ): |
| 365 | """ |
| 366 | Parameters: |
| 367 | sw: The name of an OVS switch. Example "br-int" |
| 368 | Return: |
| 369 | Delete sucess return main.TRUE or main.FALSE on delete failed |
| 370 | """ |
| 371 | command= "sudo ovs-vsctl del-br " + str( sw ) |
| 372 | try: |
| 373 | response = self.execute( |
| 374 | cmd=command, |
| 375 | timeout=10 ) |
| 376 | if response: |
| 377 | return main.TRUE |
| 378 | else: |
| 379 | return main.FALSE |
| 380 | except pexpect.EOF: |
| 381 | main.log.error( self.name + ": EOF exception found" ) |
| 382 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 383 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 384 | |
| 385 | def delHost( self, hostname ): |
| 386 | """ |
| 387 | Parameters: |
| 388 | hostname: The name of an ip netns name. Example "host1" |
| 389 | Return: |
| 390 | Delete sucess return main.TRUE or main.FALSE on delete failed |
| 391 | """ |
| 392 | command= "sudo ip netns delete " + str( hostname ) |
| 393 | try: |
| 394 | response = self.execute( |
| 395 | cmd=command, |
| 396 | timeout=10 ) |
| 397 | if response: |
| 398 | return main.TRUE |
| 399 | else: |
| 400 | return main.FALSE |
| 401 | except pexpect.EOF: |
| 402 | main.log.error( self.name + ": EOF exception found" ) |
| 403 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 404 | main.cleanAndExit() |