1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Author: George Paraskevopoulos (geopar@intracom-telecom.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 import functest.utils.functest_logger as rl
14 logger = rl.Logger('ovs_utils').getLogger()
17 class OVSLogger(object):
18 def __init__(self, basedir):
19 self.ovs_dir = os.path.join(basedir, 'odl-sfc/ovs')
20 self.__mkdir_p(self.ovs_dir)
22 def __mkdir_p(self, dirpath):
23 if not os.path.exists(dirpath):
26 def __ssh_host(self, ssh_conn):
27 return ssh_conn.get_transport().getpeername()[0]
29 def __dump_to_file(self, operation, host, text, timestamp=None):
30 ts = (timestamp if timestamp is not None
31 else time.strftime("%Y%m%d-%H%M%S"))
32 dumpdir = os.path.join(self.ovs_dir, ts)
33 self.__mkdir_p(dumpdir)
34 fname = '{0}_{1}'.format(operation, host)
35 with open(os.path.join(dumpdir, fname), 'w') as f:
38 def __remote_cmd(self, ssh_conn, cmd):
40 _, stdout, stderr = ssh_conn.exec_command(cmd)
41 errors = stderr.readlines()
43 host = self.__ssh_host(ssh_conn)
44 logger.error(''.join(errors))
45 raise Exception('Could not execute {0} in {1}'
47 output = ''.join(stdout.readlines())
50 logger.error('[__remote_command(ssh_client, {0})]: {1}'
54 def ofctl_dump_flows(self, ssh_conn, br='br-int',
55 choose_table=None, timestamp=None):
57 cmd = 'ovs-ofctl -OOpenFlow13 dump-flows {0}'.format(br)
58 if choose_table is not None:
59 cmd = '{0} table={1}'.format(cmd, choose_table)
60 output = self.__remote_cmd(ssh_conn, cmd)
61 operation = 'ofctl_dump_flows'
62 host = self.__ssh_host(ssh_conn)
63 self.__dump_to_file(operation, host, output, timestamp=timestamp)
66 logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
67 .format(br, choose_table, e))
70 def vsctl_show(self, ssh_conn, timestamp=None):
72 cmd = 'ovs-vsctl show'
73 output = self.__remote_cmd(ssh_conn, cmd)
74 operation = 'vsctl_show'
75 host = self.__ssh_host(ssh_conn)
76 self.__dump_to_file(operation, host, output, timestamp=timestamp)
79 logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))