blob: 08f691a6ba32b0d9a4022e9fe211ee40ce116abd [file] [log] [blame]
Pier6a0c4de2018-03-18 16:01:30 -07001#!/usr/bin/env python
2"""
3Copyright 2018 Open Networking Foundation (ONF)
4
5Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
6the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
7or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
8
9TestON is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 2 of the License, or
12( at your option ) any later version.
13
14TestON is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with TestON. If not, see <http://www.gnu.org/licenses/>.
21"""
22
23import pexpect
24import re
25import json
26import types
27import time
28import os
29from drivers.common.clidriver import CLI
30from core import utilities
31from shutil import copyfile
32
33class OFDPASwitchDriver( CLI ):
34
35 def __init__( self ):
36 """
37 Initialize client
38 """
39 super( CLI, self ).__init__()
40 self.name = None
You Wangb1665b52019-02-01 15:49:48 -080041 self.shortName = None
Pier6a0c4de2018-03-18 16:01:30 -070042 self.handle = None
43 self.prompt = "~#"
44 # Respect to bin folder
45 self.home = "../drivers/common/cli/ofdpa/"
46 # Local home for functions using scp
47 self.tempDirectory = "/tmp/"
48 self.conf = "ofagent.conf"
49 self.switchDirectory = "/etc/ofagent/"
You Wangb1665b52019-02-01 15:49:48 -080050 self.ports = []
51 self.isup = False
Pier6a0c4de2018-03-18 16:01:30 -070052
53 def connect( self, **connectargs ):
54 """
55 Creates ssh handle for Accton cli.
56 """
57 try:
58 # Parse keys in xml object
59 for key in connectargs:
60 vars( self )[ key ] = connectargs[ key ]
61 # Get the name
You Wang4cc61912018-08-28 10:10:58 -070062 self.name = self.options[ 'name' ]
You Wangb1665b52019-02-01 15:49:48 -080063 self.shortName = self.options[ 'shortName' ]
Pier6a0c4de2018-03-18 16:01:30 -070064 # Get the dpid
65 self.dpid = self.options[ 'dpid' ]
You Wang4cc61912018-08-28 10:10:58 -070066 # Get ofagent patch
You Wangb1665b52019-02-01 15:49:48 -080067 for key, value in self.options.items():
68 if re.match( 'link[\d]+', key ):
69 self.ports.append( { 'enabled': True,
70 'ips': [ None ],
71 'mac': None,
72 'name': None,
73 'node2': value[ 'node2' ],
74 'port2': value[ 'port2' ],
75 'of_port': value[ 'port1' ] } )
You Wang4cc61912018-08-28 10:10:58 -070076 if 'confDir' in self.options:
77 self.switchDirectory = self.options[ 'confDir' ]
Pier6a0c4de2018-03-18 16:01:30 -070078 # Parse the IP address
79 try:
80 if os.getenv( str( self.ip_address ) ) is not None:
81 self.ip_address = os.getenv( str( self.ip_address ) )
82 # Otherwise is an ip address
83 else:
84 main.log.info( self.name + ": Trying to connect to " + self.ip_address )
85 # Error handling
86 except KeyError:
87 main.log.info( "Invalid host name," + " connecting to local host instead" )
88 self.ip_address = 'localhost'
89 except Exception as inst:
90 main.log.error( "Uncaught exception: " + str( inst ) )
91 # Build the handle using the above information
92 self.handle = super(OFDPASwitchDriver, self ).connect(
93 user_name=self.user_name,
94 ip_address=self.ip_address,
95 port=None,
96 pwd=self.pwd)
97 # Successful connection
98 if self.handle:
99 main.log.info( "Connection successful to the host " + self.user_name + "@" + self.ip_address )
100 self.handle.sendline( "" )
101 self.handle.expect( self.prompt )
102 return main.TRUE
103 # Connection failed
104 else:
105 main.log.error( "Connection failed to the host " + self.user_name + "@" + self.ip_address )
106 main.log.error( "Failed to connect to the OFDPA CLI" )
107 return main.FALSE
108 # Error handling
109 except TypeError:
110 main.log.exception( self.name + ": Object not as expected" )
111 return None
112 except pexpect.EOF:
113 main.log.error( self.name + ": EOF exception found" )
114 main.log.error( self.name + ": " + self.handle.before )
115 main.cleanup()
116 main.exit()
117 except Exception:
118 main.log.exception( self.name + ": Uncaught exception!" )
119 main.cleanup()
120 main.exit()
121
122 def disconnect( self ):
123 """
124 Called when Test is complete to disconnect the OFDPASwitchDriver handle.
125 """
126 response = main.TRUE
127 try:
128 if self.handle:
129 # Stop the ofagent
130 self.stopOfAgent()
131 # Disconnect from the device
132 self.handle.sendline( "" )
133 self.handle.expect( self.prompt )
134 self.handle.sendline( "exit" )
135 self.handle.expect( "closed" )
136 # Errors handling
137 except pexpect.TIMEOUT:
138 main.log.error( self.name + ": pexpect.TIMEOUT found" )
139 return main.FALSE
140 except TypeError:
141 main.log.exception( self.name + ": Object not as expected" )
142 response = main.FALSE
143 except pexpect.EOF:
144 main.log.error( self.name + ": EOF exception found" )
145 main.log.error( self.name + ": " + self.handle.before )
146 except ValueError:
147 main.log.exception( "Exception in disconnect of " + self.name )
148 response = main.TRUE
149 except Exception:
150 main.log.exception( self.name + ": Connection failed to the host" )
151 response = main.FALSE
152 return response
153
You Wang4cc61912018-08-28 10:10:58 -0700154 def assignSwController( self, ip, port="6653", ptcp="", updateConf=False ):
Pier6a0c4de2018-03-18 16:01:30 -0700155 """
156 Description:
157 The assignment is realized properly creating the agent.conf
158 for each switch and then pushing it into the device.
159 Required:
160 ip - Ip addresses of controllers. This can be a list or a string.
161 Optional:
162 port - controller port is ignored
163 ptcp - ptcp information is ignored
You Wang4cc61912018-08-28 10:10:58 -0700164 updateConf - create new ofagent conf file and push to the switch if
165 set to True; otherwise will use the existing conf file
166 on the switch.
Pier6a0c4de2018-03-18 16:01:30 -0700167 Return:
168 Returns main.TRUE if the switch is correctly assigned to controllers,
169 otherwise it will return main.FALSE or an appropriate exception(s)
170 """
171 assignResult = main.TRUE
172 # Initial arguments for OFDPA
173 opt_args = 'OPT_ARGS="-d 2 -c 2 -c 4 '
174 onosIp = ""
175 # Parses the controller option
176 try:
177 if isinstance( ip, types.StringType ):
178 onosIp = "-t " + str( ip )
179 elif isinstance( ip, types.ListType ):
180 for ipAddress in ip:
You Wangb1665b52019-02-01 15:49:48 -0800181 onosIp += "-t " + str( ipAddress ) + " "
Pier6a0c4de2018-03-18 16:01:30 -0700182 else:
183 main.log.error( self.name + ": Invalid ip address" )
184 return main.FALSE
185 # Complete the arguments adding the dpid
186 opt_args += onosIp + '-i %s' % self.dpid + '"'
You Wang4cc61912018-08-28 10:10:58 -0700187 if updateConf:
188 # Create a copy of the cfg file using the template
189 self.createCfg()
190 # Load the cfg file and adds the missing option
191 self.updateCfg( opt_args )
192 # Backup the cfg on the switch
193 self.backupCfg()
194 # Push the new cfg on the device
195 self.pushCfg()
196 # Start the ofagent on the device
Pier6a0c4de2018-03-18 16:01:30 -0700197 self.startOfAgent()
198 # Enable all the ports
199 assignResult = utilities.retry(
200 self.enablePorts,
201 main.FALSE,
202 kwargs={},
You Wang4cc61912018-08-28 10:10:58 -0700203 attempts=10,
Pier6a0c4de2018-03-18 16:01:30 -0700204 sleep=10)
You Wangb1665b52019-02-01 15:49:48 -0800205 if not assignResult:
206 self.isup = False
207 else:
208 self.isup = True
Pier6a0c4de2018-03-18 16:01:30 -0700209 # Done return true
210 return assignResult
211 # Errors handling
212 except pexpect.TIMEOUT:
213 main.log.error( self.name + ": pexpect.TIMEOUT found" )
214 return main.FALSE
215 except pexpect.EOF:
216 main.log.error( self.name + ": EOF exception found" )
217 main.log.error( self.name + ": " + self.handle.before )
218 main.cleanAndExit()
219 except Exception:
220 main.log.exception( self.name + ": Uncaught exception!" )
221 main.cleanAndExit()
222
223 def createCfg( self ):
224 """
225 Create in bench context a new config file starting from the template
226 """
227 copyfile(self.home + self.conf + ".template", self.tempDirectory + self.conf)
228
229 def updateCfg( self, opt_args):
230 """
231 Add the arguments related to the current switch (self)
232 """
233 with open(self.tempDirectory + self.conf, "a") as cfg:
234 cfg.write(opt_args + "\n")
235 cfg.close()
236
237 def backupCfg( self ):
238 """
239 Create a backup file of the old configuration on the switch
240 """
You Wangb1665b52019-02-01 15:49:48 -0800241 try:
242 self.handle.sendline( "" )
243 self.handle.expect( self.prompt )
244 self.handle.sendline( "cp %s%s %s%s.backup" % (self.switchDirectory, self.conf, self.switchDirectory, self.conf) )
245 self.handle.expect( self.prompt )
246 except pexpect.TIMEOUT:
247 main.log.error( self.name + ": pexpect.TIMEOUT found" )
248 return main.FALSE
249 except pexpect.EOF:
250 main.log.error( self.name + ": EOF exception found" )
251 main.log.error( self.name + ": " + self.handle.before )
252 main.cleanAndExit()
253 except Exception:
254 main.log.exception( self.name + ": Uncaught exception!" )
255 main.cleanAndExit()
Pier6a0c4de2018-03-18 16:01:30 -0700256
257 def pushCfg( self ):
258 """
259 Push the new configuration from the network bench
260 """
261 # We use os.system to send the command from TestON cluster
262 # to the switches. This means that passwordless access is
263 # necessary in order to push the configuration file
264 os.system( "scp " + self.tempDirectory + self.conf + " " +
265 self.user_name + "@" + self.ip_address + ":" + self.switchDirectory)
266
You Wangb1665b52019-02-01 15:49:48 -0800267 def ofagentIsRunning( self ):
268 """
269 Return main.TRUE if service ofagentd is running on the
270 switch; otherwise main.FALSE
271 """
272 try:
273 self.handle.sendline( "" )
274 self.handle.expect( self.prompt )
275 self.handle.sendline( "service ofagentd status" )
276 self.handle.expect( self.prompt )
277 response = self.handle.before
278 if "ofagentd is running" in response:
279 return main.TRUE
280 else:
281 return main.FALSE
282 except pexpect.TIMEOUT:
283 main.log.error( self.name + ": pexpect.TIMEOUT found" )
284 return main.FALSE
285 except pexpect.EOF:
286 main.log.error( self.name + ": EOF exception found" )
287 main.log.error( self.name + ": " + self.handle.before )
288 main.cleanAndExit()
289 except Exception:
290 main.log.exception( self.name + ": Uncaught exception!" )
291 main.cleanAndExit()
292
Pier6a0c4de2018-03-18 16:01:30 -0700293 def startOfAgent( self ):
294 """
295 Start the ofagent on the device
296 """
You Wangb1665b52019-02-01 15:49:48 -0800297 try:
298 if self.ofagentIsRunning():
299 main.log.warn( self.name + ": ofagentd is already running" )
300 return main.TRUE
301 self.handle.sendline( "" )
302 self.handle.expect( self.prompt )
303 self.handle.sendline( "service ofagentd start" )
304 self.handle.expect( self.prompt )
305 except pexpect.TIMEOUT:
306 main.log.error( self.name + ": pexpect.TIMEOUT found" )
307 return main.FALSE
308 except pexpect.EOF:
309 main.log.error( self.name + ": EOF exception found" )
310 main.log.error( self.name + ": " + self.handle.before )
311 main.cleanAndExit()
312 except Exception:
313 main.log.exception( self.name + ": Uncaught exception!" )
314 main.cleanAndExit()
Pier6a0c4de2018-03-18 16:01:30 -0700315
316 def stopOfAgent( self ):
317 """
318 Stop the ofagent on the device
319 """
You Wangb1665b52019-02-01 15:49:48 -0800320 try:
321 self.handle.sendline( "" )
322 self.handle.expect( self.prompt )
323 self.handle.sendline( "service ofagentd stop" )
324 self.handle.expect( self.prompt )
325 self.isup = False
326 except pexpect.TIMEOUT:
327 main.log.error( self.name + ": pexpect.TIMEOUT found" )
328 return main.FALSE
329 except pexpect.EOF:
330 main.log.error( self.name + ": EOF exception found" )
331 main.log.error( self.name + ": " + self.handle.before )
332 main.cleanAndExit()
333 except Exception:
334 main.log.exception( self.name + ": Uncaught exception!" )
335 main.cleanAndExit()
Pier6a0c4de2018-03-18 16:01:30 -0700336
337 def dumpFlows( self ):
338 """
339 Dump the flows from the devices
340 FIXME need changes in the workflow in order to be used
341 """
342 try:
343 self.handle.sendline( "" )
344 self.handle.expect( self.prompt )
345 # Create the dump of the flows locally on the switches
346 self.handle.sendline( "client_flowtable_dump" )
347 self.handle.expect( self.prompt )
348 response = self.handle.before
349 # Write back in the tmp folder - needs to be changed in future
350 with open(self.tempDirectory + "flows_%s.txt" % self.dpid, "w") as flows:
351 flows.write(response + "\n")
352 flows.close()
353 # Done return for further processing
354 return response
355 # Errors handling
356 except pexpect.TIMEOUT:
357 main.log.error( self.name + ": pexpect.TIMEOUT found" )
358 return main.FALSE
359 except pexpect.EOF:
360 main.log.error( self.name + ": EOF exception found" )
361 main.log.error( self.name + ": " + self.handle.before )
362 main.cleanAndExit()
363 except Exception:
364 main.log.exception( self.name + ": Uncaught exception!" )
365 main.cleanAndExit()
366
367 def dumpGroups( self ):
368 """
369 Dump the groups from the devices
370 FIXME need changes in the workflow in order to be used
371 """
372 try:
373 self.handle.sendline( "" )
374 self.handle.expect( self.prompt )
375 self.handle.sendline( "client_grouptable_dump > groups.txt" )
376 self.handle.expect( self.prompt )
377 response = self.handle.before
378 # Write back in the tmp folder - needs to be changed in future
379 with open(self.tempDirectory + "groups_%s.txt" % self.dpid, "w") as groups:
380 groups.write(response + "\n")
381 groups.close()
382 # Done return for further processing
383 return response
384 # Errors handling
385 except pexpect.TIMEOUT:
386 main.log.error( self.name + ": pexpect.TIMEOUT found" )
387 return main.FALSE
388 except pexpect.EOF:
389 main.log.error( self.name + ": EOF exception found" )
390 main.log.error( self.name + ": " + self.handle.before )
391 main.cleanAndExit()
392 except Exception:
393 main.log.exception( self.name + ": Uncaught exception!" )
394 main.cleanAndExit()
395
396 def enablePorts( self ):
397 """
398 Enable all the ports on the devices
399 It needs to wait for the boot
400 """
You Wangb1665b52019-02-01 15:49:48 -0800401 try:
402 self.handle.sendline( "" )
403 self.handle.expect( self.prompt )
404 self.handle.sendline( "client_port_table_dump" )
405 self.handle.expect( self.prompt )
406 response = self.handle.before
407 if "Error from ofdpaClientInitialize()" in response:
408 main.log.warn( self.name + ": Not yet started" )
409 return main.FALSE
410 # Change port speed
411 self.handle.sendline( "sh portspeed" )
412 self.handle.expect( self.prompt )
413 response = self.handle.before
414 if "Failure calling" in response:
415 main.log.warn( self.name + ": failed to change port speed" )
416 return main.FALSE
417 return main.TRUE
418 except pexpect.TIMEOUT:
419 main.log.error( self.name + ": pexpect.TIMEOUT found" )
Pier6a0c4de2018-03-18 16:01:30 -0700420 return main.FALSE
You Wangb1665b52019-02-01 15:49:48 -0800421 except pexpect.EOF:
422 main.log.error( self.name + ": EOF exception found" )
423 main.log.error( self.name + ": " + self.handle.before )
424 main.cleanAndExit()
425 except Exception:
426 main.log.exception( self.name + ": Uncaught exception!" )
427 main.cleanAndExit()
428
429 def setPortSpeed( self, index, speed=40000 ):
430 """
431 Run client_drivshell on the switch to set speed for a
432 specific port
433 index: port index, e.g. 1
434 speed: port speed, e.g. 40000
435 """
436 try:
437 self.handle.sendline( "" )
438 self.handle.expect( self.prompt )
439 cmd = "client_drivshell port {} sp={}".format( index, speed )
440 self.handle.sendline( cmd )
441 self.handle.expect( self.prompt )
442 response = self.handle.before
443 return main.TRUE
444 except pexpect.TIMEOUT:
445 main.log.error( self.name + ": pexpect.TIMEOUT found" )
You Wang4cc61912018-08-28 10:10:58 -0700446 return main.FALSE
You Wangb1665b52019-02-01 15:49:48 -0800447 except pexpect.EOF:
448 main.log.error( self.name + ": EOF exception found" )
449 main.log.error( self.name + ": " + self.handle.before )
450 main.cleanAndExit()
451 except Exception:
452 main.log.exception( self.name + ": Uncaught exception!" )
453 main.cleanAndExit()
454
455 def updatePorts( self ):
456 """
457 Get latest port status on the switch by running
458 client_port_table_dump commmand and parsing the output
459 """
460 try:
461 self.handle.sendline( "" )
462 self.handle.expect( self.prompt )
463 self.handle.sendline( "client_port_table_dump" )
464 self.handle.expect( self.prompt )
465 ports = self.handle.before
466 if "Error from ofdpaClientInitialize()" in ports:
467 main.log.warn( self.name + ": Not yet started" )
468 return main.FALSE
469 ports = re.findall( r"0x[\d]+.*port[\d]+:\r\r\n.*\r\r\n.*PeerFeature:.*\r\r\n", ports )
470 for port in ports:
471 m = re.match( r".*port([\d]+):\r\r\n.*state = (.*), mac", port )
472 index = m.group( 1 )
473 enabled = True if m.group( 2 ) == '0x00000000' else False
474 for p in self.ports:
475 if p[ 'of_port' ] == index:
476 p[ 'enabled' ] = enabled
477 except pexpect.TIMEOUT:
478 main.log.error( self.name + ": pexpect.TIMEOUT found" )
479 return main.FALSE
480 except pexpect.EOF:
481 main.log.error( self.name + ": EOF exception found" )
482 main.log.error( self.name + ": " + self.handle.before )
483 main.cleanAndExit()
484 except Exception:
485 main.log.exception( self.name + ": Uncaught exception!" )
486 main.cleanAndExit()