136f109a3d09f0d36709497fcd3bf40ff0c70911
[integration/test.git] / csit / libraries / VsctlListParser.py
1 """
2 Library for the robot based system test tool of the OpenDaylight project.
3
4 This library will parse 'ovs-vstcl list Bridge' and 'ovs-vstcl list Controller'
5 commands and create dictionaries with parsed details which will be used
6 for another pruposes.
7
8 Authors: pgubka@cisco.com
9 Created: 2016-04-04
10 """
11
12 import re
13 import copy
14 import sys
15 import traceback
16
17
18 def _parse_stdout(stdout):
19     """ Transforms stdout to dict """
20     text = stdout.replace(" ", "")
21     text = text.replace("\r", "")
22     pat = re.compile(r'(?P<key>\w+):(?P<value>.+)')
23     regroups = re.finditer(pat, text)
24     outdict = {}
25     for g in regroups:
26         print g.group()
27         if g.group('key') == '_uuid':
28             cntl_uuid = g.group('value')
29             outdict[cntl_uuid] = {}
30         outdict[cntl_uuid][g.group('key')] = g.group('value')
31     return outdict
32
33
34 def _postprocess_data(bridges, controllers):
35     """What is done here:
36     - merge bridges and controllers
37     - replace controller 'key' (ip instead uuid)
38     - transformed controller's connected status to bool
39     """
40     brs = copy.deepcopy(bridges)
41     cntls = copy.deepcopy(controllers)
42
43     # replacing string value of is_connected key with boolean
44     for key, cntl in cntls.iteritems():
45         if cntl['is_connected'] == 'false':
46             cntl['is_connected'] = False
47         elif cntl['is_connected'] == 'true':
48             cntl['is_connected'] = True
49         else:
50             cntl['is_connected'] = None
51
52     # replacing keys with the same values
53     for key, value in bridges.iteritems():
54         brs[value['name'][1:-1]] = brs[key]
55         del brs[key]
56
57     for key, value in brs.iteritems():
58         # replace string with references with dict of controllers
59         ctl_refs = value['controller'][1:-1].split(',')
60         value['controller'] = {}
61         for ctl_ref in ctl_refs:
62             if ctl_ref is not '':
63                 value['controller'][ctl_ref] = cntls[ctl_ref]
64
65     for brkey, bridge in brs.iteritems():
66         new_cntls = {}
67         for cnkey, cntl in bridge['controller'].iteritems():
68             # port 6654 is set by OvsMAnager.robot to disconnect from controller
69             if '6653' in cntl['target'] or '6633' in cntl['target'] or '6654' in cntl['target']:
70                 new_key = cntl['target'].split(":")[1]     # getting middle from "tcp:ip:6653"
71             else:
72                 new_key = cntl['target'][1:-1]  # getting string without quotes "ptcp:6638"
73             new_cntls[new_key] = cntl
74         bridge['controller'] = new_cntls
75
76     return brs
77
78
79 def parse(bridge_stdout, cntl_stdout):
80     """Produces dictionary with data for future usege
81
82     Args:
83         :param bridge_stdout: output of 'ovs-vsclt list Bridge' command
84
85         :param cntl_stdout: output of 'ovs-vsclt list Controller' command
86
87     Returns:
88         :returns processed: processed output dictionary
89
90         :returns bridges: list bridge command output transformed to dist
91
92         :returns controllers: list controller command output transformed to dist
93     """
94
95     try:
96         bridges = _parse_stdout(bridge_stdout)
97         controllers = _parse_stdout(cntl_stdout)
98         processed = _postprocess_data(bridges, controllers)
99     except Exception:
100         traceback.print_exc()
101         raise
102     return processed, bridges, controllers