blob: 18ccb2e26e8d87d0a705542cee00ae7f879cc6d2 [file] [log] [blame]
zhanghaoyu451c1392015-08-07 19:21:16 +08001#!/usr/bin/env python
2
3"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07004Copyright 2015 Open Networking Foundation (ONF)
5
6Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or 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"""
zhanghaoyu451c1392015-08-07 19:21:16 +080026drivers for ovsdb commands.
27
28zhanghaoyu7@huawei.com
29AUG 10 2015
30"""
31import pexpect
32import re
33import json
34import types
35import time
36import os
37from drivers.common.clidriver import CLI
38
39
40class 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 Limdc78e202017-06-09 18:30:07 -070049 super( OvsdbDriver, self ).__init__()
zhanghaoyu451c1392015-08-07 19:21:16 +080050
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 Wang90038fb2016-10-26 13:55:29 -070069 main.log.info( "Connection successful to the ovsdb node " +
zhanghaoyu451c1392015-08-07 19:21:16 +080070 self.name )
alison6acef9f2016-09-28 12:29:08 -070071 return self.handle
zhanghaoyu451c1392015-08-07 19:21:16 +080072 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 )
78 main.cleanup()
79 main.exit()
80 except Exception:
81 main.log.exception( self.name + ": Uncaught exception!" )
82 main.cleanup()
83 main.exit()
84
85 def disconnect( self ):
86 try:
87 self.handle.sendline( "exit" )
88 self.handle.expect( "closed" )
89 response = main.TRUE
90 except pexpect.ExceptionPexpect:
91 response = main.FALSE
92 main.log.exception( self.name + ": Uncaught exception!" )
93 return response
94
95 def setManager( self, ip, port, delaytime="5" ):
96 command= "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port )
97 try:
98 handle = self.execute(
99 cmd=command,
100 timeout=10 )
101 if re.search( "Error", handle ):
102 main.log.error( "Error in set ovsdb manager" )
103 main.log.error( handle )
104 return main.FALSE
105 else:
106 main.log.info( "Ovsdb manager " + str( ip ) + " set" )
107 #delay time for ovsdb connection create
108 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" )
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 delManager( self, delaytime="5" ):
118 command= "sudo ovs-vsctl del-manager"
119 try:
120 handle = self.execute(
121 cmd=command,
122 timeout=10 )
123 if re.search( "Error", handle ):
124 main.log.error( "Error in delete ovsdb manager" )
125 main.log.error( handle )
126 return main.FALSE
127 else:
128 main.log.info( "Ovsdb manager delete" )
129 #delay time for ovsdb connection delete
130 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" )
131 time.sleep( int( delaytime ) )
132 return main.TRUE
133 except pexpect.EOF:
134 main.log.error( self.name + ": EOF exception found" )
135 main.log.error( self.name + ": " + self.handle.before )
136 main.cleanup()
137 main.exit()
138
139 def getManager( self ):
140 command= "sudo ovs-vsctl get-manager"
141 try:
142 response = self.execute(
143 cmd=command,
144 timeout=10 )
145 return response
146 except pexpect.EOF:
147 main.log.error( self.name + ": EOF exception found" )
148 main.log.error( self.name + ": " + self.handle.before )
149 main.cleanup()
150 main.exit()
151
152 def listBr( self ):
153 """
154 Parameters:
155 none
156 Return:
157 The output of the command from the linux
158 or main.FALSE on timeout
159 """
160 command= "sudo ovs-vsctl list-br"
161 try:
162 response = self.execute(
163 cmd=command,
164 timeout=10 )
165 if response:
166 return response
167 else:
168 return main.FALSE
169 except pexpect.EOF:
170 main.log.error( self.name + ": EOF exception found" )
171 main.log.error( self.name + ": " + self.handle.before )
172 main.cleanup()
173 main.exit()
174
175 def listPorts( self, sw ):
176 """
177 Parameters:
178 sw: The name of an OVS switch. Example "s1"
179 Return:
180 The output of the command from the linux
181 or main.FALSE on timeout
182 """
183 command= "sudo ovs-vsctl list-ports " + 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
198 def getController( self, sw ):
199 """
200 Parameters:
201 sw: The name of an OVS switch. Example "s1"
202 Return:
203 The output of the command from the mininet cli
204 or main.FALSE on timeout"""
205 command = "sudo ovs-vsctl get-controller " + str( sw )
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
zhanghaoyu451c1392015-08-07 19:21:16 +0800220 def show( self ):
221 """
222 Parameters:
223 none
224 Return:
225 The output of the command from the linux
226 or main.FALSE on timeout"""
227 command = "sudo ovs-vsctl show "
228 try:
229 response = self.execute(
230 cmd=command,
231 timeout=10)
232 if response:
233 return response
234 else:
235 return main.FALSE
236 except pexpect.EOF:
237 main.log.error( self.name + ": EOF exception found" )
238 main.log.error( self.name + ": " + self.handle.before )
239 main.cleanup()
240 main.exit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800241
242 def dumpFlows( self, sw, protocols=None ):
243 """
244 Parameters:
245 sw: The name of an OVS switch. Example "s1"
246 Return:
247 The output of the command from the linux
248 or main.FALSE on timeout"""
249 if protocols:
250 command = "sudo ovs-ofctl -O " + \
251 protocols + " dump-flows " + str( sw )
252 else:
253 command = "sudo ovs-ofctl dump-flows " + str( sw )
254 try:
255 response = self.execute(
256 cmd=command,
257 timeout=10 )
258 if response:
259 return response
260 else:
261 return main.FALSE
262 except pexpect.EOF:
263 main.log.error(self.name + ": EOF exception found")
264 main.log.error(self.name + ": " + self.handle.before)
265 main.cleanup()
266 main.exit()
267
268 def createHost( self, hostname ):
269 command = "sudo ip netns add " + str( hostname )
270 try:
271 handle = self.execute(
272 cmd=command,
273 timeout=10)
274 if re.search( "Error", handle ):
275 main.log.error( "Error in create host" + str( hostname ) )
276 main.log.error( handle )
277 return main.FALSE
278 else:
279 main.log.info( "Create " + str( hostname ) + " sucess" )
280 return main.TRUE
281 except pexpect.EOF:
282 main.log.error(self.name + ": EOF exception found")
283 main.log.error(self.name + ": " + self.handle.before)
284 main.cleanup()
285 main.exit()
286
287 def createHostport(self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ):
288 command = "sudo ip link add " + str(hostport) +" type veth peer name " + str(ovsport)
lanqinglong26c6b902015-10-27 16:00:35 +0800289 command += ";" + "sudo ip link set " + str(hostport) + " up"
290 command += ";" + "sudo ip link set " + str(ovsport) + " up"
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800291 command += ";" +" sudo ifconfig " + str(hostport) + " hw ether " + str(hostportmac)
292 command += ";" +" sudo ip link set " + str(hostport) + " netns " + str(hostname)
293 try:
294 handle = self.execute(
295 cmd=command,
296 timeout=10)
297 if re.search( "Error", handle ):
298 main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
299 main.log.error( handle )
300 return main.FALSE
301 else:
302 main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
303 return main.TRUE
304 except pexpect.EOF:
305 main.log.error(self.name + ": EOF exception found")
306 main.log.error(self.name + ": " + self.handle.before)
307 main.cleanup()
308 main.exit()
309
310 def addPortToOvs(self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
311 command = "sudo ovs-vsctl add-port " + str(ovsname) +" " + str(port)
312 if ifaceId:
313 command += " -- set Interface " + str(port) + " external-ids:iface-id=" + str(ifaceId) + " external-ids:iface-status=active"
314 if attachedMac:
315 command += " external-ids:attached-mac=" + str(attachedMac)
316 if vmuuid:
317 command += " external-ids:vm-uuid=" + str(vmuuid)
318 try:
319 handle = self.execute(
320 cmd=command,
321 timeout=10)
322 if re.search( "Error", handle ):
323 main.log.error( "Error in add port " + str(port) + " to ovs " + str( ovsname ) )
324 main.log.error( handle )
325 return main.FALSE
326 else:
327 main.log.info( "Add port " + str(port) + " to ovs " + str( ovsname ) + " sucess" )
328 return main.TRUE
329 except pexpect.EOF:
330 main.log.error(self.name + ": EOF exception found")
331 main.log.error(self.name + ": " + self.handle.before)
332 main.cleanup()
333 main.exit()
334
335 def setHostportIp(self, ip, hostname="host1", hostport1="host1-eth0" ):
336 command = "sudo ip netns exec " + str(hostname) +" ifconfig " + str(hostport1) + " " + str(ip)
337 try:
338 handle = self.execute(
339 cmd=command,
340 timeout=10)
341 if re.search( "Error", handle ):
342 main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
343 main.log.error( handle )
344 return main.FALSE
345 else:
346 main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
347 return main.TRUE
348 except pexpect.EOF:
349 main.log.error(self.name + ": EOF exception found")
350 main.log.error(self.name + ": " + self.handle.before)
351 main.cleanup()
352 main.exit()
353
354 def hostPing(self, src, target, hostname="host1" ):
355 if src:
356 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 -S " +\
357 str( src ) + " " + str( target )
358 else:
359 command = "sudo ip netns exec " + str( hostname ) +" ping -c 1 " + str( target )
360 try:
361 for i in range(1,5):
362 handle = self.execute(
363 cmd=command,
364 timeout=10)
365 if re.search(',\s0\%\spacket\sloss', handle):
366 main.log.info(self.name + ": no packets lost, host is reachable")
367 return main.TRUE
368 break
369 time.sleep(5)
370 else:
371 main.log.info(self.name + ": packets lost, host is unreachable")
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 delBr( self, sw ):
380 """
381 Parameters:
382 sw: The name of an OVS switch. Example "br-int"
383 Return:
384 Delete sucess return main.TRUE or main.FALSE on delete failed
385 """
386 command= "sudo ovs-vsctl del-br " + str( sw )
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()
399 main.exit()
400
401 def delHost( self, hostname ):
402 """
403 Parameters:
404 hostname: The name of an ip netns name. Example "host1"
405 Return:
406 Delete sucess return main.TRUE or main.FALSE on delete failed
407 """
408 command= "sudo ip netns delete " + str( hostname )
409 try:
410 response = self.execute(
411 cmd=command,
412 timeout=10 )
413 if response:
414 return main.TRUE
415 else:
416 return main.FALSE
417 except pexpect.EOF:
418 main.log.error( self.name + ": EOF exception found" )
419 main.log.error( self.name + ": " + self.handle.before )
420 main.cleanup()
You Wang90038fb2016-10-26 13:55:29 -0700421 main.exit()