blob: 150e5fa008781c43618a140cc09c6b778b1334b6 [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:
You Wang90038fb2016-10-26 13:55:29 -070047 main.log.info( "Connection successful to the ovsdb node " +
zhanghaoyu451c1392015-08-07 19:21:16 +080048 self.name )
alison6acef9f2016-09-28 12:29:08 -070049 return self.handle
zhanghaoyu451c1392015-08-07 19:21:16 +080050 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)
lanqinglong26c6b902015-10-27 16:00:35 +0800267 command += ";" + "sudo ip link set " + str(hostport) + " up"
268 command += ";" + "sudo ip link set " + str(ovsport) + " up"
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800269 command += ";" +" sudo ifconfig " + str(hostport) + " hw ether " + str(hostportmac)
270 command += ";" +" sudo ip link set " + str(hostport) + " netns " + str(hostname)
271 try:
272 handle = self.execute(
273 cmd=command,
274 timeout=10)
275 if re.search( "Error", handle ):
276 main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
277 main.log.error( handle )
278 return main.FALSE
279 else:
280 main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
281 return main.TRUE
282 except pexpect.EOF:
283 main.log.error(self.name + ": EOF exception found")
284 main.log.error(self.name + ": " + self.handle.before)
285 main.cleanup()
286 main.exit()
287
288 def addPortToOvs(self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
289 command = "sudo ovs-vsctl add-port " + str(ovsname) +" " + str(port)
290 if ifaceId:
291 command += " -- set Interface " + str(port) + " external-ids:iface-id=" + str(ifaceId) + " external-ids:iface-status=active"
292 if attachedMac:
293 command += " external-ids:attached-mac=" + str(attachedMac)
294 if vmuuid:
295 command += " external-ids:vm-uuid=" + str(vmuuid)
296 try:
297 handle = self.execute(
298 cmd=command,
299 timeout=10)
300 if re.search( "Error", handle ):
301 main.log.error( "Error in add port " + str(port) + " to ovs " + str( ovsname ) )
302 main.log.error( handle )
303 return main.FALSE
304 else:
305 main.log.info( "Add port " + str(port) + " to ovs " + str( ovsname ) + " sucess" )
306 return main.TRUE
307 except pexpect.EOF:
308 main.log.error(self.name + ": EOF exception found")
309 main.log.error(self.name + ": " + self.handle.before)
310 main.cleanup()
311 main.exit()
312
313 def setHostportIp(self, ip, hostname="host1", hostport1="host1-eth0" ):
314 command = "sudo ip netns exec " + str(hostname) +" ifconfig " + str(hostport1) + " " + str(ip)
315 try:
316 handle = self.execute(
317 cmd=command,
318 timeout=10)
319 if re.search( "Error", handle ):
320 main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
321 main.log.error( handle )
322 return main.FALSE
323 else:
324 main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
325 return main.TRUE
326 except pexpect.EOF:
327 main.log.error(self.name + ": EOF exception found")
328 main.log.error(self.name + ": " + self.handle.before)
329 main.cleanup()
330 main.exit()
331
332 def hostPing(self, src, target, hostname="host1" ):
333 if src:
334 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 -S " +\
335 str( src ) + " " + str( target )
336 else:
337 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 " + str( target )
338 try:
339 for i in range(1,5):
340 handle = self.execute(
341 cmd=command,
342 timeout=10)
343 if re.search(',\s0\%\spacket\sloss', handle):
344 main.log.info(self.name + ": no packets lost, host is reachable")
345 return main.TRUE
346 break
347 time.sleep(5)
348 else:
349 main.log.info(self.name + ": packets lost, host is unreachable")
350 return main.FALSE
351 except pexpect.EOF:
352 main.log.error(self.name + ": EOF exception found")
353 main.log.error(self.name + ": " + self.handle.before)
354 main.cleanup()
355 main.exit()
356
357 def delBr( self, sw ):
358 """
359 Parameters:
360 sw: The name of an OVS switch. Example "br-int"
361 Return:
362 Delete sucess return main.TRUE or main.FALSE on delete failed
363 """
364 command= "sudo ovs-vsctl del-br " + str( sw )
365 try:
366 response = self.execute(
367 cmd=command,
368 timeout=10 )
369 if response:
370 return main.TRUE
371 else:
372 return main.FALSE
373 except pexpect.EOF:
374 main.log.error( self.name + ": EOF exception found" )
375 main.log.error( self.name + ": " + self.handle.before )
376 main.cleanup()
377 main.exit()
378
379 def delHost( self, hostname ):
380 """
381 Parameters:
382 hostname: The name of an ip netns name. Example "host1"
383 Return:
384 Delete sucess return main.TRUE or main.FALSE on delete failed
385 """
386 command= "sudo ip netns delete " + str( hostname )
387 try:
388 response = self.execute(
389 cmd=command,
390 timeout=10 )
391 if response:
392 return main.TRUE
393 else:
394 return main.FALSE
395 except pexpect.EOF:
396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
398 main.cleanup()
You Wang90038fb2016-10-26 13:55:29 -0700399 main.exit()