blob: 4b78665bd1882e82795faf0bf7c90f11d176e2c7 [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()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800197
zhanghaoyu451c1392015-08-07 19:21:16 +0800198 def show( self ):
199 """
200 Parameters:
201 none
202 Return:
203 The output of the command from the linux
204 or main.FALSE on timeout"""
205 command = "sudo ovs-vsctl show "
206 try:
207 response = self.execute(
208 cmd=command,
209 timeout=10)
210 if response:
211 return response
212 else:
213 return main.FALSE
214 except pexpect.EOF:
215 main.log.error( self.name + ": EOF exception found" )
216 main.log.error( self.name + ": " + self.handle.before )
217 main.cleanup()
218 main.exit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800219
220 def dumpFlows( self, sw, protocols=None ):
221 """
222 Parameters:
223 sw: The name of an OVS switch. Example "s1"
224 Return:
225 The output of the command from the linux
226 or main.FALSE on timeout"""
227 if protocols:
228 command = "sudo ovs-ofctl -O " + \
229 protocols + " dump-flows " + str( sw )
230 else:
231 command = "sudo ovs-ofctl dump-flows " + str( sw )
232 try:
233 response = self.execute(
234 cmd=command,
235 timeout=10 )
236 if response:
237 return response
238 else:
239 return main.FALSE
240 except pexpect.EOF:
241 main.log.error(self.name + ": EOF exception found")
242 main.log.error(self.name + ": " + self.handle.before)
243 main.cleanup()
244 main.exit()
245
246 def createHost( self, hostname ):
247 command = "sudo ip netns add " + str( hostname )
248 try:
249 handle = self.execute(
250 cmd=command,
251 timeout=10)
252 if re.search( "Error", handle ):
253 main.log.error( "Error in create host" + str( hostname ) )
254 main.log.error( handle )
255 return main.FALSE
256 else:
257 main.log.info( "Create " + str( hostname ) + " sucess" )
258 return main.TRUE
259 except pexpect.EOF:
260 main.log.error(self.name + ": EOF exception found")
261 main.log.error(self.name + ": " + self.handle.before)
262 main.cleanup()
263 main.exit()
264
265 def createHostport(self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ):
266 command = "sudo ip link add " + str(hostport) +" type veth peer name " + str(ovsport)
267 command += ";" +" sudo ifconfig " + str(hostport) + " hw ether " + str(hostportmac)
268 command += ";" +" sudo ip link set " + str(hostport) + " netns " + str(hostname)
269 try:
270 handle = self.execute(
271 cmd=command,
272 timeout=10)
273 if re.search( "Error", handle ):
274 main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
275 main.log.error( handle )
276 return main.FALSE
277 else:
278 main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
279 return main.TRUE
280 except pexpect.EOF:
281 main.log.error(self.name + ": EOF exception found")
282 main.log.error(self.name + ": " + self.handle.before)
283 main.cleanup()
284 main.exit()
285
286 def addPortToOvs(self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
287 command = "sudo ovs-vsctl add-port " + str(ovsname) +" " + str(port)
288 if ifaceId:
289 command += " -- set Interface " + str(port) + " external-ids:iface-id=" + str(ifaceId) + " external-ids:iface-status=active"
290 if attachedMac:
291 command += " external-ids:attached-mac=" + str(attachedMac)
292 if vmuuid:
293 command += " external-ids:vm-uuid=" + str(vmuuid)
294 try:
295 handle = self.execute(
296 cmd=command,
297 timeout=10)
298 if re.search( "Error", handle ):
299 main.log.error( "Error in add port " + str(port) + " to ovs " + str( ovsname ) )
300 main.log.error( handle )
301 return main.FALSE
302 else:
303 main.log.info( "Add port " + str(port) + " to ovs " + str( ovsname ) + " sucess" )
304 return main.TRUE
305 except pexpect.EOF:
306 main.log.error(self.name + ": EOF exception found")
307 main.log.error(self.name + ": " + self.handle.before)
308 main.cleanup()
309 main.exit()
310
311 def setHostportIp(self, ip, hostname="host1", hostport1="host1-eth0" ):
312 command = "sudo ip netns exec " + str(hostname) +" ifconfig " + str(hostport1) + " " + str(ip)
313 try:
314 handle = self.execute(
315 cmd=command,
316 timeout=10)
317 if re.search( "Error", handle ):
318 main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
319 main.log.error( handle )
320 return main.FALSE
321 else:
322 main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
323 return main.TRUE
324 except pexpect.EOF:
325 main.log.error(self.name + ": EOF exception found")
326 main.log.error(self.name + ": " + self.handle.before)
327 main.cleanup()
328 main.exit()
329
330 def hostPing(self, src, target, hostname="host1" ):
331 if src:
332 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 -S " +\
333 str( src ) + " " + str( target )
334 else:
335 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 " + str( target )
336 try:
337 for i in range(1,5):
338 handle = self.execute(
339 cmd=command,
340 timeout=10)
341 if re.search(',\s0\%\spacket\sloss', handle):
342 main.log.info(self.name + ": no packets lost, host is reachable")
343 return main.TRUE
344 break
345 time.sleep(5)
346 else:
347 main.log.info(self.name + ": packets lost, host is unreachable")
348 return main.FALSE
349 except pexpect.EOF:
350 main.log.error(self.name + ": EOF exception found")
351 main.log.error(self.name + ": " + self.handle.before)
352 main.cleanup()
353 main.exit()
354
355 def delBr( self, sw ):
356 """
357 Parameters:
358 sw: The name of an OVS switch. Example "br-int"
359 Return:
360 Delete sucess return main.TRUE or main.FALSE on delete failed
361 """
362 command= "sudo ovs-vsctl del-br " + str( sw )
363 try:
364 response = self.execute(
365 cmd=command,
366 timeout=10 )
367 if response:
368 return main.TRUE
369 else:
370 return main.FALSE
371 except pexpect.EOF:
372 main.log.error( self.name + ": EOF exception found" )
373 main.log.error( self.name + ": " + self.handle.before )
374 main.cleanup()
375 main.exit()
376
377 def delHost( self, hostname ):
378 """
379 Parameters:
380 hostname: The name of an ip netns name. Example "host1"
381 Return:
382 Delete sucess return main.TRUE or main.FALSE on delete failed
383 """
384 command= "sudo ip netns delete " + str( hostname )
385 try:
386 response = self.execute(
387 cmd=command,
388 timeout=10 )
389 if response:
390 return main.TRUE
391 else:
392 return main.FALSE
393 except pexpect.EOF:
394 main.log.error( self.name + ": EOF exception found" )
395 main.log.error( self.name + ": " + self.handle.before )
396 main.cleanup()
397 main.exit()