Set USE_RFC8040 to True
[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 traceback
15
16
17 def _parse_stdout(stdout):
18     """Transforms stdout to dict"""
19     text = stdout.replace(" ", "")
20     text = text.replace("\r", "")
21     pat = re.compile(r"(?P<key>\w+):(?P<value>.+)")
22     regroups = re.finditer(pat, text)
23     outdict = {}
24     for g in regroups:
25         print((g.group()))
26         if g.group("key") == "_uuid":
27             cntl_uuid = g.group("value")
28             outdict[cntl_uuid] = {}
29         outdict[cntl_uuid][g.group("key")] = g.group("value")
30     return outdict
31
32
33 def _postprocess_data(bridges, controllers):
34     """What is done here:
35     - merge bridges and controllers
36     - replace controller 'key' (ip instead uuid)
37     - transformed controller's connected status to bool
38     """
39     brs = copy.deepcopy(bridges)
40     cntls = copy.deepcopy(controllers)
41
42     # replacing string value of is_connected key with boolean
43     for key, cntl in cntls.items():
44         if cntl["is_connected"] == "false":
45             cntl["is_connected"] = False
46         elif cntl["is_connected"] == "true":
47             cntl["is_connected"] = True
48         else:
49             cntl["is_connected"] = None
50
51     # replacing keys with the same values
52     for key, value in bridges.items():
53         brs[value["name"][1:-1]] = brs[key]
54         del brs[key]
55
56     for key, value in brs.items():
57         # replace string with references with dict of controllers
58         ctl_refs = value["controller"][1:-1].split(",")
59         value["controller"] = {}
60         for ctl_ref in ctl_refs:
61             if ctl_ref is not "":
62                 value["controller"][ctl_ref] = cntls[ctl_ref]
63
64     for brkey, bridge in brs.items():
65         new_cntls = {}
66         for cnkey, cntl in bridge["controller"].items():
67             # port 6654 is set by OvsMAnager.robot to disconnect from controller
68             if (
69                 "6653" in cntl["target"]
70                 or "6633" in cntl["target"]
71                 or "6654" in cntl["target"]
72             ):
73                 new_key = cntl["target"].split(":")[
74                     1
75                 ]  # getting middle from "tcp:ip:6653"
76             else:
77                 new_key = cntl["target"][
78                     1:-1
79                 ]  # getting string without quotes "ptcp:6638"
80             new_cntls[new_key] = cntl
81         bridge["controller"] = new_cntls
82
83     return brs
84
85
86 def parse(bridge_stdout, cntl_stdout):
87     """Produces dictionary with data for future usege
88
89     Args:
90         :param bridge_stdout: output of 'ovs-vsclt list Bridge' command
91
92         :param cntl_stdout: output of 'ovs-vsclt list Controller' command
93
94     Returns:
95         :returns processed: processed output dictionary
96
97         :returns bridges: list bridge command output transformed to dist
98
99         :returns controllers: list controller command output transformed to dist
100     """
101
102     try:
103         bridges = _parse_stdout(bridge_stdout)
104         controllers = _parse_stdout(cntl_stdout)
105         processed = _postprocess_data(bridges, controllers)
106     except Exception:
107         traceback.print_exc()
108         raise
109     return processed, bridges, controllers