blob: fc029cef637e4ed59b3f40bc4333cdf7829d210f [file] [log] [blame]
zhanghaoyu451c1392015-08-07 19:21:16 +08001#!/usr/bin/env python
2
3"""
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004Copyright 2015 Open Networking Foundation (ONF)
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07005
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
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000013 (at your option) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070014
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"""
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000024
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070025"""
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:
Jeremy Ronquillo82705492017-10-18 14:19:55 -070054 vars( self )[ key ] = connectargs[ key ]
zhanghaoyu451c1392015-08-07 19:21:16 +080055
56 self.name = self.options[ 'name' ]
Jeremy Ronquillo82705492017-10-18 14:19:55 -070057 if os.getenv( str( self.ip_address ) ) is not None:
58 self.ip_address = os.getenv( str( self.ip_address ) )
zhanghaoyu451c1392015-08-07 19:21:16 +080059 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,
Jeremy Ronquillo82705492017-10-18 14:19:55 -070066 pwd=self.pwd )
zhanghaoyu451c1392015-08-07 19:21:16 +080067
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 )
Devin Lim44075962017-08-11 10:56:37 -070078 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +080079 except Exception:
80 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -070081 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +080082
83 def disconnect( self ):
84 try:
85 self.handle.sendline( "exit" )
86 self.handle.expect( "closed" )
87 response = main.TRUE
88 except pexpect.ExceptionPexpect:
89 response = main.FALSE
90 main.log.exception( self.name + ": Uncaught exception!" )
91 return response
92
93 def setManager( self, ip, port, delaytime="5" ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -070094 command = "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port )
zhanghaoyu451c1392015-08-07 19:21:16 +080095 try:
96 handle = self.execute(
97 cmd=command,
98 timeout=10 )
99 if re.search( "Error", handle ):
100 main.log.error( "Error in set ovsdb manager" )
101 main.log.error( handle )
102 return main.FALSE
103 else:
104 main.log.info( "Ovsdb manager " + str( ip ) + " set" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700105 # delay time for ovsdb connection create
zhanghaoyu451c1392015-08-07 19:21:16 +0800106 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" )
107 time.sleep( int( delaytime ) )
108 return main.TRUE
109 except pexpect.EOF:
110 main.log.error( self.name + ": EOF exception found" )
111 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700112 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800113
114 def delManager( self, delaytime="5" ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700115 command = "sudo ovs-vsctl del-manager"
zhanghaoyu451c1392015-08-07 19:21:16 +0800116 try:
117 handle = self.execute(
118 cmd=command,
119 timeout=10 )
120 if re.search( "Error", handle ):
121 main.log.error( "Error in delete ovsdb manager" )
122 main.log.error( handle )
123 return main.FALSE
124 else:
125 main.log.info( "Ovsdb manager delete" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700126 # delay time for ovsdb connection delete
zhanghaoyu451c1392015-08-07 19:21:16 +0800127 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" )
128 time.sleep( int( delaytime ) )
129 return main.TRUE
130 except pexpect.EOF:
131 main.log.error( self.name + ": EOF exception found" )
132 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700133 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800134
135 def getManager( self ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700136 command = "sudo ovs-vsctl get-manager"
zhanghaoyu451c1392015-08-07 19:21:16 +0800137 try:
138 response = self.execute(
139 cmd=command,
140 timeout=10 )
141 return response
142 except pexpect.EOF:
143 main.log.error( self.name + ": EOF exception found" )
144 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700145 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800146
147 def listBr( self ):
148 """
149 Parameters:
150 none
151 Return:
152 The output of the command from the linux
153 or main.FALSE on timeout
154 """
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700155 command = "sudo ovs-vsctl list-br"
zhanghaoyu451c1392015-08-07 19:21:16 +0800156 try:
157 response = self.execute(
158 cmd=command,
159 timeout=10 )
160 if response:
161 return response
162 else:
163 return main.FALSE
164 except pexpect.EOF:
165 main.log.error( self.name + ": EOF exception found" )
166 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700167 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800168
169 def listPorts( self, sw ):
170 """
171 Parameters:
172 sw: The name of an OVS switch. Example "s1"
173 Return:
174 The output of the command from the linux
175 or main.FALSE on timeout
176 """
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700177 command = "sudo ovs-vsctl list-ports " + str( sw )
zhanghaoyu451c1392015-08-07 19:21:16 +0800178 try:
179 response = self.execute(
180 cmd=command,
181 timeout=10 )
182 if response:
183 return response
184 else:
185 return main.FALSE
186 except pexpect.EOF:
187 main.log.error( self.name + ": EOF exception found" )
188 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700189 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800190
191 def getController( self, sw ):
192 """
193 Parameters:
194 sw: The name of an OVS switch. Example "s1"
195 Return:
196 The output of the command from the mininet cli
197 or main.FALSE on timeout"""
198 command = "sudo ovs-vsctl get-controller " + str( sw )
199 try:
200 response = self.execute(
201 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700202 timeout=10 )
zhanghaoyu451c1392015-08-07 19:21:16 +0800203 if response:
204 return response
205 else:
206 return main.FALSE
207 except pexpect.EOF:
208 main.log.error( self.name + ": EOF exception found" )
209 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700210 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800211
zhanghaoyu451c1392015-08-07 19:21:16 +0800212 def show( self ):
213 """
214 Parameters:
215 none
216 Return:
217 The output of the command from the linux
218 or main.FALSE on timeout"""
219 command = "sudo ovs-vsctl show "
220 try:
221 response = self.execute(
222 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700223 timeout=10 )
zhanghaoyu451c1392015-08-07 19:21:16 +0800224 if response:
225 return response
226 else:
227 return main.FALSE
228 except pexpect.EOF:
229 main.log.error( self.name + ": EOF exception found" )
230 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700231 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800232
233 def dumpFlows( self, sw, protocols=None ):
234 """
235 Parameters:
236 sw: The name of an OVS switch. Example "s1"
237 Return:
238 The output of the command from the linux
239 or main.FALSE on timeout"""
240 if protocols:
241 command = "sudo ovs-ofctl -O " + \
242 protocols + " dump-flows " + str( sw )
243 else:
244 command = "sudo ovs-ofctl dump-flows " + str( sw )
245 try:
246 response = self.execute(
247 cmd=command,
248 timeout=10 )
249 if response:
250 return response
251 else:
252 return main.FALSE
253 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700254 main.log.error( self.name + ": EOF exception found" )
255 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700256 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800257
258 def createHost( self, hostname ):
259 command = "sudo ip netns add " + str( hostname )
260 try:
261 handle = self.execute(
262 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700263 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800264 if re.search( "Error", handle ):
265 main.log.error( "Error in create host" + str( hostname ) )
266 main.log.error( handle )
267 return main.FALSE
268 else:
269 main.log.info( "Create " + str( hostname ) + " sucess" )
270 return main.TRUE
271 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700272 main.log.error( self.name + ": EOF exception found" )
273 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700274 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800275
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700276 def createHostport( self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ):
277 command = "sudo ip link add " + str( hostport ) + " type veth peer name " + str( ovsport )
278 command += ";" + "sudo ip link set " + str( hostport ) + " up"
279 command += ";" + "sudo ip link set " + str( ovsport ) + " up"
280 command += ";" + " sudo ifconfig " + str( hostport ) + " hw ether " + str( hostportmac )
281 command += ";" + " sudo ip link set " + str( hostport ) + " netns " + str( hostname )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800282 try:
283 handle = self.execute(
284 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700285 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800286 if re.search( "Error", handle ):
287 main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
288 main.log.error( handle )
289 return main.FALSE
290 else:
291 main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
292 return main.TRUE
293 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700294 main.log.error( self.name + ": EOF exception found" )
295 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700296 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800297
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700298 def addPortToOvs( self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
299 command = "sudo ovs-vsctl add-port " + str( ovsname ) + " " + str( port )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800300 if ifaceId:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700301 command += " -- set Interface " + str( port ) + " external-ids:iface-id=" + str( ifaceId ) + " external-ids:iface-status=active"
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800302 if attachedMac:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700303 command += " external-ids:attached-mac=" + str( attachedMac )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800304 if vmuuid:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700305 command += " external-ids:vm-uuid=" + str( vmuuid )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800306 try:
307 handle = self.execute(
308 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700309 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800310 if re.search( "Error", handle ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700311 main.log.error( "Error in add port " + str( port ) + " to ovs " + str( ovsname ) )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800312 main.log.error( handle )
313 return main.FALSE
314 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700315 main.log.info( "Add port " + str( port ) + " to ovs " + str( ovsname ) + " sucess" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800316 return main.TRUE
317 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700318 main.log.error( self.name + ": EOF exception found" )
319 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700320 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800321
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700322 def setHostportIp( self, ip, hostname="host1", hostport1="host1-eth0" ):
323 command = "sudo ip netns exec " + str( hostname ) + " ifconfig " + str( hostport1 ) + " " + str( ip )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800324 try:
325 handle = self.execute(
326 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700327 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800328 if re.search( "Error", handle ):
329 main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
330 main.log.error( handle )
331 return main.FALSE
332 else:
333 main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
334 return main.TRUE
335 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700336 main.log.error( self.name + ": EOF exception found" )
337 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700338 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800339
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700340 def hostPing( self, src, target, hostname="host1" ):
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800341 if src:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700342 command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 -S " +\
343 str( src ) + " " + str( target )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800344 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700345 command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 " + str( target )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800346 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700347 for i in range( 1, 5 ):
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800348 handle = self.execute(
349 cmd=command,
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700350 timeout=10 )
351 if re.search( ',\s0\%\spacket\sloss', handle ):
352 main.log.info( self.name + ": no packets lost, host is reachable" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800353 return main.TRUE
354 break
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700355 time.sleep( 5 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800356 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700357 main.log.info( self.name + ": packets lost, host is unreachable" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800358 return main.FALSE
359 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700360 main.log.error( self.name + ": EOF exception found" )
361 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700362 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800363
364 def delBr( self, sw ):
365 """
366 Parameters:
367 sw: The name of an OVS switch. Example "br-int"
368 Return:
369 Delete sucess return main.TRUE or main.FALSE on delete failed
370 """
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700371 command = "sudo ovs-vsctl del-br " + str( sw )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800372 try:
373 response = self.execute(
374 cmd=command,
375 timeout=10 )
376 if response:
377 return main.TRUE
378 else:
379 return main.FALSE
380 except pexpect.EOF:
381 main.log.error( self.name + ": EOF exception found" )
382 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700383 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800384
385 def delHost( self, hostname ):
386 """
387 Parameters:
388 hostname: The name of an ip netns name. Example "host1"
389 Return:
390 Delete sucess return main.TRUE or main.FALSE on delete failed
391 """
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700392 command = "sudo ip netns delete " + str( hostname )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800393 try:
394 response = self.execute(
395 cmd=command,
396 timeout=10 )
397 if response:
398 return main.TRUE
399 else:
400 return main.FALSE
401 except pexpect.EOF:
402 main.log.error( self.name + ": EOF exception found" )
403 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700404 main.cleanAndExit()