blob: bd55c915ac0aa41111f8619cfe1d485d7291b615 [file] [log] [blame]
zhanghaoyu451c1392015-08-07 19:21:16 +08001#!/usr/bin/env python
2
3"""
4drivers for ovsdb commands.
5
6zhanghaoyu7@huawei.com
7AUG 10 2015
8"""
9import pexpect
10import re
11import json
12import types
13import time
14import os
15from drivers.common.clidriver import CLI
16
17
18class OvsdbDriver( CLI ):
19
20 def __init__( self ):
21 """
22 Initialize client
23 """
24 self.name = None
25 self.home = None
26 self.handle = None
27 super( CLI, self ).__init__()
28
29 def connect( self, **connectargs ):
30 try:
31 for key in connectargs:
32 vars( self)[ key ] = connectargs[ key ]
33
34 self.name = self.options[ 'name' ]
35 if os.getenv( str( self.ip_address ) ) != None:
36 self.ip_address = os.getenv(str ( self.ip_address ) )
37 else:
38 main.log.info( self.name + ": Trying to connect to " +
39 self.ip_address )
40 self.handle = super( OvsdbDriver, self ).connect(
41 user_name=self.user_name,
42 ip_address=self.ip_address,
43 port=self.port,
44 pwd=self.pwd)
45
46 if self.handle:
47 return self.handle
48 main.log.onfo( "Connection successful to the ovsdb node " +
49 self.name )
50 else:
51 main.log.error( "Connection failed to the ovsdb node " +
52 self.name )
53 except pexpect.EOF:
54 main.log.error( self.name + ": EOF exception found" )
55 main.log.error( self.name + ": " + self.handle.before )
56 main.cleanup()
57 main.exit()
58 except Exception:
59 main.log.exception( self.name + ": Uncaught exception!" )
60 main.cleanup()
61 main.exit()
62
63 def disconnect( self ):
64 try:
65 self.handle.sendline( "exit" )
66 self.handle.expect( "closed" )
67 response = main.TRUE
68 except pexpect.ExceptionPexpect:
69 response = main.FALSE
70 main.log.exception( self.name + ": Uncaught exception!" )
71 return response
72
73 def setManager( self, ip, port, delaytime="5" ):
74 command= "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port )
75 try:
76 handle = self.execute(
77 cmd=command,
78 timeout=10 )
79 if re.search( "Error", handle ):
80 main.log.error( "Error in set ovsdb manager" )
81 main.log.error( handle )
82 return main.FALSE
83 else:
84 main.log.info( "Ovsdb manager " + str( ip ) + " set" )
85 #delay time for ovsdb connection create
86 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" )
87 time.sleep( int( delaytime ) )
88 return main.TRUE
89 except pexpect.EOF:
90 main.log.error( self.name + ": EOF exception found" )
91 main.log.error( self.name + ": " + self.handle.before )
92 main.cleanup()
93 main.exit()
94
95 def delManager( self, delaytime="5" ):
96 command= "sudo ovs-vsctl del-manager"
97 try:
98 handle = self.execute(
99 cmd=command,
100 timeout=10 )
101 if re.search( "Error", handle ):
102 main.log.error( "Error in delete ovsdb manager" )
103 main.log.error( handle )
104 return main.FALSE
105 else:
106 main.log.info( "Ovsdb manager delete" )
107 #delay time for ovsdb connection delete
108 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" )
109 time.sleep( int( delaytime ) )
110 return main.TRUE
111 except pexpect.EOF:
112 main.log.error( self.name + ": EOF exception found" )
113 main.log.error( self.name + ": " + self.handle.before )
114 main.cleanup()
115 main.exit()
116
117 def getManager( self ):
118 command= "sudo ovs-vsctl get-manager"
119 try:
120 response = self.execute(
121 cmd=command,
122 timeout=10 )
123 return response
124 except pexpect.EOF:
125 main.log.error( self.name + ": EOF exception found" )
126 main.log.error( self.name + ": " + self.handle.before )
127 main.cleanup()
128 main.exit()
129
130 def listBr( self ):
131 """
132 Parameters:
133 none
134 Return:
135 The output of the command from the linux
136 or main.FALSE on timeout
137 """
138 command= "sudo ovs-vsctl list-br"
139 try:
140 response = self.execute(
141 cmd=command,
142 timeout=10 )
143 if response:
144 return response
145 else:
146 return main.FALSE
147 except pexpect.EOF:
148 main.log.error( self.name + ": EOF exception found" )
149 main.log.error( self.name + ": " + self.handle.before )
150 main.cleanup()
151 main.exit()
152
153 def listPorts( self, sw ):
154 """
155 Parameters:
156 sw: The name of an OVS switch. Example "s1"
157 Return:
158 The output of the command from the linux
159 or main.FALSE on timeout
160 """
161 command= "sudo ovs-vsctl list-ports " + str( sw )
162 try:
163 response = self.execute(
164 cmd=command,
165 timeout=10 )
166 if response:
167 return response
168 else:
169 return main.FALSE
170 except pexpect.EOF:
171 main.log.error( self.name + ": EOF exception found" )
172 main.log.error( self.name + ": " + self.handle.before )
173 main.cleanup()
174 main.exit()
175
176 def getController( self, sw ):
177 """
178 Parameters:
179 sw: The name of an OVS switch. Example "s1"
180 Return:
181 The output of the command from the mininet cli
182 or main.FALSE on timeout"""
183 command = "sudo ovs-vsctl get-controller " + str( sw )
184 try:
185 response = self.execute(
186 cmd=command,
187 timeout=10)
188 if response:
189 return response
190 else:
191 return main.FALSE
192 except pexpect.EOF:
193 main.log.error( self.name + ": EOF exception found" )
194 main.log.error( self.name + ": " + self.handle.before )
195 main.cleanup()
196 main.exit()
197 def show( self ):
198 """
199 Parameters:
200 none
201 Return:
202 The output of the command from the linux
203 or main.FALSE on timeout"""
204 command = "sudo ovs-vsctl show "
205 try:
206 response = self.execute(
207 cmd=command,
208 timeout=10)
209 if response:
210 return response
211 else:
212 return main.FALSE
213 except pexpect.EOF:
214 main.log.error( self.name + ": EOF exception found" )
215 main.log.error( self.name + ": " + self.handle.before )
216 main.cleanup()
217 main.exit()