Add OVS logger
[functest.git] / testcases / features / sfc / ovs_utils.py
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 ##############################################################################
9
10 import functest.utils.functest_logger as rl
11 import os
12 import time
13
14 logger = rl.Logger('ovs_utils').getLogger()
15
16
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)
21
22     def __mkdir_p(self, dirpath):
23         if not os.path.exists(dirpath):
24             os.makedirs(dirpath)
25
26     def __ssh_host(self, ssh_conn):
27         return ssh_conn.get_transport().getpeername()[0]
28
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:
36             f.write(text)
37
38     def __remote_cmd(self, ssh_conn, cmd):
39         try:
40             _, stdout, stderr = ssh_conn.exec_command(cmd)
41             errors = stderr.readlines()
42             if len(errors) > 0:
43                 host = self.__ssh_host(ssh_conn)
44                 logger.error(''.join(errors))
45                 raise Exception('Could not execute {0} in {1}'
46                                 .format(cmd, host))
47             output = ''.join(stdout.readlines())
48             return output
49         except Exception, e:
50             logger.error('[__remote_command(ssh_client, {0})]: {1}'
51                          .format(cmd, e))
52             return None
53
54     def ofctl_dump_flows(self, ssh_conn, br='br-int',
55                          choose_table=None, timestamp=None):
56         try:
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)
64             return output
65         except Exception, e:
66             logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
67                          .format(br, choose_table, e))
68             return None
69
70     def vsctl_show(self, ssh_conn, timestamp=None):
71         try:
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)
77             return output
78         except Exception, e:
79             logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))
80             return None