blob: 18ccb2e26e8d87d0a705542cee00ae7f879cc6d2 [file] [log] [blame]
#!/usr/bin/env python
"""
Copyright 2015 Open Networking Foundation (ONF)
Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
TestON is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TestON is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TestON. If not, see <http://www.gnu.org/licenses/>.
"""
"""
drivers for ovsdb commands.
zhanghaoyu7@huawei.com
AUG 10 2015
"""
import pexpect
import re
import json
import types
import time
import os
from drivers.common.clidriver import CLI
class OvsdbDriver( CLI ):
def __init__( self ):
"""
Initialize client
"""
self.name = None
self.home = None
self.handle = None
super( OvsdbDriver, self ).__init__()
def connect( self, **connectargs ):
try:
for key in connectargs:
vars( self)[ key ] = connectargs[ key ]
self.name = self.options[ 'name' ]
if os.getenv( str( self.ip_address ) ) != None:
self.ip_address = os.getenv(str ( self.ip_address ) )
else:
main.log.info( self.name + ": Trying to connect to " +
self.ip_address )
self.handle = super( OvsdbDriver, self ).connect(
user_name=self.user_name,
ip_address=self.ip_address,
port=self.port,
pwd=self.pwd)
if self.handle:
main.log.info( "Connection successful to the ovsdb node " +
self.name )
return self.handle
else:
main.log.error( "Connection failed to the ovsdb node " +
self.name )
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
except Exception:
main.log.exception( self.name + ": Uncaught exception!" )
main.cleanup()
main.exit()
def disconnect( self ):
try:
self.handle.sendline( "exit" )
self.handle.expect( "closed" )
response = main.TRUE
except pexpect.ExceptionPexpect:
response = main.FALSE
main.log.exception( self.name + ": Uncaught exception!" )
return response
def setManager( self, ip, port, delaytime="5" ):
command= "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port )
try:
handle = self.execute(
cmd=command,
timeout=10 )
if re.search( "Error", handle ):
main.log.error( "Error in set ovsdb manager" )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Ovsdb manager " + str( ip ) + " set" )
#delay time for ovsdb connection create
main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" )
time.sleep( int( delaytime ) )
return main.TRUE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def delManager( self, delaytime="5" ):
command= "sudo ovs-vsctl del-manager"
try:
handle = self.execute(
cmd=command,
timeout=10 )
if re.search( "Error", handle ):
main.log.error( "Error in delete ovsdb manager" )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Ovsdb manager delete" )
#delay time for ovsdb connection delete
main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" )
time.sleep( int( delaytime ) )
return main.TRUE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def getManager( self ):
command= "sudo ovs-vsctl get-manager"
try:
response = self.execute(
cmd=command,
timeout=10 )
return response
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def listBr( self ):
"""
Parameters:
none
Return:
The output of the command from the linux
or main.FALSE on timeout
"""
command= "sudo ovs-vsctl list-br"
try:
response = self.execute(
cmd=command,
timeout=10 )
if response:
return response
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def listPorts( self, sw ):
"""
Parameters:
sw: The name of an OVS switch. Example "s1"
Return:
The output of the command from the linux
or main.FALSE on timeout
"""
command= "sudo ovs-vsctl list-ports " + str( sw )
try:
response = self.execute(
cmd=command,
timeout=10 )
if response:
return response
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def getController( self, sw ):
"""
Parameters:
sw: The name of an OVS switch. Example "s1"
Return:
The output of the command from the mininet cli
or main.FALSE on timeout"""
command = "sudo ovs-vsctl get-controller " + str( sw )
try:
response = self.execute(
cmd=command,
timeout=10)
if response:
return response
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def show( self ):
"""
Parameters:
none
Return:
The output of the command from the linux
or main.FALSE on timeout"""
command = "sudo ovs-vsctl show "
try:
response = self.execute(
cmd=command,
timeout=10)
if response:
return response
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def dumpFlows( self, sw, protocols=None ):
"""
Parameters:
sw: The name of an OVS switch. Example "s1"
Return:
The output of the command from the linux
or main.FALSE on timeout"""
if protocols:
command = "sudo ovs-ofctl -O " + \
protocols + " dump-flows " + str( sw )
else:
command = "sudo ovs-ofctl dump-flows " + str( sw )
try:
response = self.execute(
cmd=command,
timeout=10 )
if response:
return response
else:
return main.FALSE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def createHost( self, hostname ):
command = "sudo ip netns add " + str( hostname )
try:
handle = self.execute(
cmd=command,
timeout=10)
if re.search( "Error", handle ):
main.log.error( "Error in create host" + str( hostname ) )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Create " + str( hostname ) + " sucess" )
return main.TRUE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def createHostport(self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ):
command = "sudo ip link add " + str(hostport) +" type veth peer name " + str(ovsport)
command += ";" + "sudo ip link set " + str(hostport) + " up"
command += ";" + "sudo ip link set " + str(ovsport) + " up"
command += ";" +" sudo ifconfig " + str(hostport) + " hw ether " + str(hostportmac)
command += ";" +" sudo ip link set " + str(hostport) + " netns " + str(hostname)
try:
handle = self.execute(
cmd=command,
timeout=10)
if re.search( "Error", handle ):
main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
return main.TRUE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def addPortToOvs(self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
command = "sudo ovs-vsctl add-port " + str(ovsname) +" " + str(port)
if ifaceId:
command += " -- set Interface " + str(port) + " external-ids:iface-id=" + str(ifaceId) + " external-ids:iface-status=active"
if attachedMac:
command += " external-ids:attached-mac=" + str(attachedMac)
if vmuuid:
command += " external-ids:vm-uuid=" + str(vmuuid)
try:
handle = self.execute(
cmd=command,
timeout=10)
if re.search( "Error", handle ):
main.log.error( "Error in add port " + str(port) + " to ovs " + str( ovsname ) )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Add port " + str(port) + " to ovs " + str( ovsname ) + " sucess" )
return main.TRUE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def setHostportIp(self, ip, hostname="host1", hostport1="host1-eth0" ):
command = "sudo ip netns exec " + str(hostname) +" ifconfig " + str(hostport1) + " " + str(ip)
try:
handle = self.execute(
cmd=command,
timeout=10)
if re.search( "Error", handle ):
main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
main.log.error( handle )
return main.FALSE
else:
main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
return main.TRUE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def hostPing(self, src, target, hostname="host1" ):
if src:
command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 -S " +\
str( src ) + " " + str( target )
else:
command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 " + str( target )
try:
for i in range(1,5):
handle = self.execute(
cmd=command,
timeout=10)
if re.search(',\s0\%\spacket\sloss', handle):
main.log.info(self.name + ": no packets lost, host is reachable")
return main.TRUE
break
time.sleep(5)
else:
main.log.info(self.name + ": packets lost, host is unreachable")
return main.FALSE
except pexpect.EOF:
main.log.error(self.name + ": EOF exception found")
main.log.error(self.name + ": " + self.handle.before)
main.cleanup()
main.exit()
def delBr( self, sw ):
"""
Parameters:
sw: The name of an OVS switch. Example "br-int"
Return:
Delete sucess return main.TRUE or main.FALSE on delete failed
"""
command= "sudo ovs-vsctl del-br " + str( sw )
try:
response = self.execute(
cmd=command,
timeout=10 )
if response:
return main.TRUE
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()
def delHost( self, hostname ):
"""
Parameters:
hostname: The name of an ip netns name. Example "host1"
Return:
Delete sucess return main.TRUE or main.FALSE on delete failed
"""
command= "sudo ip netns delete " + str( hostname )
try:
response = self.execute(
cmd=command,
timeout=10 )
if response:
return main.TRUE
else:
return main.FALSE
except pexpect.EOF:
main.log.error( self.name + ": EOF exception found" )
main.log.error( self.name + ": " + self.handle.before )
main.cleanup()
main.exit()