blob: 7c2a43dbb9519ef8b44cf5953e51cbb09f264d83 [file] [log] [blame]
zhanghaoyu451c1392015-08-07 19:21:16 +08001#!/usr/bin/env python
2
3"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07004Copyright 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 Ronquillo23fb2162017-09-15 14:59:57 -070013 ( 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 Ronquillob27ce4c2017-07-17 12:41:28 -070024"""
zhanghaoyu451c1392015-08-07 19:21:16 +080025drivers for ovsdb commands.
26
27zhanghaoyu7@huawei.com
28AUG 10 2015
29"""
30import pexpect
31import re
32import json
33import types
34import time
35import os
36from drivers.common.clidriver import CLI
37
38
39class OvsdbDriver( CLI ):
40
41 def __init__( self ):
42 """
43 Initialize client
44 """
45 self.name = None
46 self.home = None
47 self.handle = None
Devin Limdc78e202017-06-09 18:30:07 -070048 super( OvsdbDriver, self ).__init__()
zhanghaoyu451c1392015-08-07 19:21:16 +080049
50 def connect( self, **connectargs ):
51 try:
52 for key in connectargs:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070053 vars( self )[ key ] = connectargs[ key ]
zhanghaoyu451c1392015-08-07 19:21:16 +080054
55 self.name = self.options[ 'name' ]
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070056 if os.getenv( str( self.ip_address ) ) is not None:
57 self.ip_address = os.getenv( str( self.ip_address ) )
zhanghaoyu451c1392015-08-07 19:21:16 +080058 else:
59 main.log.info( self.name + ": Trying to connect to " +
60 self.ip_address )
61 self.handle = super( OvsdbDriver, self ).connect(
62 user_name=self.user_name,
63 ip_address=self.ip_address,
64 port=self.port,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070065 pwd=self.pwd )
zhanghaoyu451c1392015-08-07 19:21:16 +080066
67 if self.handle:
You Wang90038fb2016-10-26 13:55:29 -070068 main.log.info( "Connection successful to the ovsdb node " +
zhanghaoyu451c1392015-08-07 19:21:16 +080069 self.name )
alison6acef9f2016-09-28 12:29:08 -070070 return self.handle
zhanghaoyu451c1392015-08-07 19:21:16 +080071 else:
72 main.log.error( "Connection failed to the ovsdb node " +
73 self.name )
74 except pexpect.EOF:
75 main.log.error( self.name + ": EOF exception found" )
76 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -070077 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +080078 except Exception:
79 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -070080 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +080081
82 def disconnect( self ):
83 try:
84 self.handle.sendline( "exit" )
85 self.handle.expect( "closed" )
86 response = main.TRUE
87 except pexpect.ExceptionPexpect:
88 response = main.FALSE
89 main.log.exception( self.name + ": Uncaught exception!" )
90 return response
91
92 def setManager( self, ip, port, delaytime="5" ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070093 command = "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port )
zhanghaoyu451c1392015-08-07 19:21:16 +080094 try:
95 handle = self.execute(
96 cmd=command,
97 timeout=10 )
98 if re.search( "Error", handle ):
99 main.log.error( "Error in set ovsdb manager" )
100 main.log.error( handle )
101 return main.FALSE
102 else:
103 main.log.info( "Ovsdb manager " + str( ip ) + " set" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700104 # delay time for ovsdb connection create
zhanghaoyu451c1392015-08-07 19:21:16 +0800105 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" )
106 time.sleep( int( delaytime ) )
107 return main.TRUE
108 except pexpect.EOF:
109 main.log.error( self.name + ": EOF exception found" )
110 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700111 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800112
113 def delManager( self, delaytime="5" ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700114 command = "sudo ovs-vsctl del-manager"
zhanghaoyu451c1392015-08-07 19:21:16 +0800115 try:
116 handle = self.execute(
117 cmd=command,
118 timeout=10 )
119 if re.search( "Error", handle ):
120 main.log.error( "Error in delete ovsdb manager" )
121 main.log.error( handle )
122 return main.FALSE
123 else:
124 main.log.info( "Ovsdb manager delete" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700125 # delay time for ovsdb connection delete
zhanghaoyu451c1392015-08-07 19:21:16 +0800126 main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" )
127 time.sleep( int( delaytime ) )
128 return main.TRUE
129 except pexpect.EOF:
130 main.log.error( self.name + ": EOF exception found" )
131 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700132 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800133
134 def getManager( self ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700135 command = "sudo ovs-vsctl get-manager"
zhanghaoyu451c1392015-08-07 19:21:16 +0800136 try:
137 response = self.execute(
138 cmd=command,
139 timeout=10 )
140 return response
141 except pexpect.EOF:
142 main.log.error( self.name + ": EOF exception found" )
143 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700144 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800145
146 def listBr( self ):
147 """
148 Parameters:
149 none
150 Return:
151 The output of the command from the linux
152 or main.FALSE on timeout
153 """
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700154 command = "sudo ovs-vsctl list-br"
zhanghaoyu451c1392015-08-07 19:21:16 +0800155 try:
156 response = self.execute(
157 cmd=command,
158 timeout=10 )
159 if response:
160 return response
161 else:
162 return main.FALSE
163 except pexpect.EOF:
164 main.log.error( self.name + ": EOF exception found" )
165 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700166 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800167
168 def listPorts( self, sw ):
169 """
170 Parameters:
171 sw: The name of an OVS switch. Example "s1"
172 Return:
173 The output of the command from the linux
174 or main.FALSE on timeout
175 """
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700176 command = "sudo ovs-vsctl list-ports " + str( sw )
zhanghaoyu451c1392015-08-07 19:21:16 +0800177 try:
178 response = self.execute(
179 cmd=command,
180 timeout=10 )
181 if response:
182 return response
183 else:
184 return main.FALSE
185 except pexpect.EOF:
186 main.log.error( self.name + ": EOF exception found" )
187 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700188 main.cleanAndExit()
zhanghaoyu451c1392015-08-07 19:21:16 +0800189
190 def getController( self, sw ):
191 """
192 Parameters:
193 sw: The name of an OVS switch. Example "s1"
194 Return:
195 The output of the command from the mininet cli
196 or main.FALSE on timeout"""
197 command = "sudo ovs-vsctl get-controller " + str( sw )
198 try:
199 response = self.execute(
200 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700201 timeout=10 )
zhanghaoyu451c1392015-08-07 19:21:16 +0800202 if response:
203 return response
204 else:
205 return main.FALSE
206 except pexpect.EOF:
207 main.log.error( self.name + ": EOF exception found" )
208 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700209 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800210
zhanghaoyu451c1392015-08-07 19:21:16 +0800211 def show( self ):
212 """
213 Parameters:
214 none
215 Return:
216 The output of the command from the linux
217 or main.FALSE on timeout"""
218 command = "sudo ovs-vsctl show "
219 try:
220 response = self.execute(
221 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700222 timeout=10 )
zhanghaoyu451c1392015-08-07 19:21:16 +0800223 if response:
224 return response
225 else:
226 return main.FALSE
227 except pexpect.EOF:
228 main.log.error( self.name + ": EOF exception found" )
229 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700230 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800231
232 def dumpFlows( self, sw, protocols=None ):
233 """
234 Parameters:
235 sw: The name of an OVS switch. Example "s1"
236 Return:
237 The output of the command from the linux
238 or main.FALSE on timeout"""
239 if protocols:
240 command = "sudo ovs-ofctl -O " + \
241 protocols + " dump-flows " + str( sw )
242 else:
243 command = "sudo ovs-ofctl dump-flows " + str( sw )
244 try:
245 response = self.execute(
246 cmd=command,
247 timeout=10 )
248 if response:
249 return response
250 else:
251 return main.FALSE
252 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700253 main.log.error( self.name + ": EOF exception found" )
254 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700255 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800256
257 def createHost( self, hostname ):
258 command = "sudo ip netns add " + str( hostname )
259 try:
260 handle = self.execute(
261 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700262 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800263 if re.search( "Error", handle ):
264 main.log.error( "Error in create host" + str( hostname ) )
265 main.log.error( handle )
266 return main.FALSE
267 else:
268 main.log.info( "Create " + str( hostname ) + " sucess" )
269 return main.TRUE
270 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700271 main.log.error( self.name + ": EOF exception found" )
272 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700273 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800274
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700275 def createHostport( self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ):
276 command = "sudo ip link add " + str( hostport ) + " type veth peer name " + str( ovsport )
277 command += ";" + "sudo ip link set " + str( hostport ) + " up"
278 command += ";" + "sudo ip link set " + str( ovsport ) + " up"
279 command += ";" + " sudo ifconfig " + str( hostport ) + " hw ether " + str( hostportmac )
280 command += ";" + " sudo ip link set " + str( hostport ) + " netns " + str( hostname )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800281 try:
282 handle = self.execute(
283 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700284 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800285 if re.search( "Error", handle ):
286 main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) )
287 main.log.error( handle )
288 return main.FALSE
289 else:
290 main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" )
291 return main.TRUE
292 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700293 main.log.error( self.name + ": EOF exception found" )
294 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700295 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800296
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700297 def addPortToOvs( self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ):
298 command = "sudo ovs-vsctl add-port " + str( ovsname ) + " " + str( port )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800299 if ifaceId:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700300 command += " -- set Interface " + str( port ) + " external-ids:iface-id=" + str( ifaceId ) + " external-ids:iface-status=active"
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800301 if attachedMac:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700302 command += " external-ids:attached-mac=" + str( attachedMac )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800303 if vmuuid:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700304 command += " external-ids:vm-uuid=" + str( vmuuid )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800305 try:
306 handle = self.execute(
307 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700308 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800309 if re.search( "Error", handle ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700310 main.log.error( "Error in add port " + str( port ) + " to ovs " + str( ovsname ) )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800311 main.log.error( handle )
312 return main.FALSE
313 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700314 main.log.info( "Add port " + str( port ) + " to ovs " + str( ovsname ) + " sucess" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800315 return main.TRUE
316 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700317 main.log.error( self.name + ": EOF exception found" )
318 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700319 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800320
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700321 def setHostportIp( self, ip, hostname="host1", hostport1="host1-eth0" ):
322 command = "sudo ip netns exec " + str( hostname ) + " ifconfig " + str( hostport1 ) + " " + str( ip )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800323 try:
324 handle = self.execute(
325 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700326 timeout=10 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800327 if re.search( "Error", handle ):
328 main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) )
329 main.log.error( handle )
330 return main.FALSE
331 else:
332 main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" )
333 return main.TRUE
334 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700335 main.log.error( self.name + ": EOF exception found" )
336 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700337 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800338
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700339 def hostPing( self, src, target, hostname="host1" ):
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800340 if src:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700341 command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 -S " +\
342 str( src ) + " " + str( target )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800343 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700344 command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 " + str( target )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800345 try:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700346 for i in range( 1, 5 ):
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800347 handle = self.execute(
348 cmd=command,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700349 timeout=10 )
350 if re.search( ',\s0\%\spacket\sloss', handle ):
351 main.log.info( self.name + ": no packets lost, host is reachable" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800352 return main.TRUE
353 break
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700354 time.sleep( 5 )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800355 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700356 main.log.info( self.name + ": packets lost, host is unreachable" )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800357 return main.FALSE
358 except pexpect.EOF:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700359 main.log.error( self.name + ": EOF exception found" )
360 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700361 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800362
363 def delBr( self, sw ):
364 """
365 Parameters:
366 sw: The name of an OVS switch. Example "br-int"
367 Return:
368 Delete sucess return main.TRUE or main.FALSE on delete failed
369 """
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700370 command = "sudo ovs-vsctl del-br " + str( sw )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800371 try:
372 response = self.execute(
373 cmd=command,
374 timeout=10 )
375 if response:
376 return main.TRUE
377 else:
378 return main.FALSE
379 except pexpect.EOF:
380 main.log.error( self.name + ": EOF exception found" )
381 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700382 main.cleanAndExit()
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800383
384 def delHost( self, hostname ):
385 """
386 Parameters:
387 hostname: The name of an ip netns name. Example "host1"
388 Return:
389 Delete sucess return main.TRUE or main.FALSE on delete failed
390 """
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700391 command = "sudo ip netns delete " + str( hostname )
zhanghaoyu7474d8c62015-08-26 14:53:28 +0800392 try:
393 response = self.execute(
394 cmd=command,
395 timeout=10 )
396 if response:
397 return main.TRUE
398 else:
399 return main.FALSE
400 except pexpect.EOF:
401 main.log.error( self.name + ": EOF exception found" )
402 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700403 main.cleanAndExit()