Rework old entity datastore lookups to new rpcs
[integration/test.git] / csit / libraries / ClusterEntities.py
1 """
2     Utility library for retrieving entity related data from ODL.
3 """
4
5 from logging import debug, warning
6 from requests import post
7 from sys import argv
8
9
10 def get_entities(restconf_url):
11     resp = post(
12         url=restconf_url + """/operations/odl-entity-owners:get-entities""",
13         headers={
14             "Content-Type": "application/json",
15             "Accept": "application/json",
16             "User-Agent": "csit agent",
17         },
18         auth=("admin", "admin"),
19     )
20
21     debug(
22         "Response %s ",
23         resp,
24     )
25
26     return resp.json()
27
28
29 def get_entity(restconf_url, type, name):
30     """Calls the get-entity rpc on the controller and returns the result in a
31     dictionary that contains the parsed response in two keys:
32     "candidates" and "owner"
33     """
34
35     data = """
36     {
37         "odl-entity-owners:input" : {
38             "type": "%s",
39             "name": "%s"
40         }
41     }
42     """ % (
43         type,
44         name,
45     )
46
47     debug("Data %s", data)
48
49     resp = post(
50         url=restconf_url + """/operations/odl-entity-owners:get-entity""",
51         headers={
52             "Content-Type": "application/json",
53             "Accept": "application/json",
54             "User-Agent": "csit agent",
55         },
56         data=data,
57         auth=("admin", "admin"),
58     )
59
60     warning(
61         "Response %s ",
62         resp,
63     )
64
65     warning(
66         "Json %s",
67         resp.json(),
68     )
69
70     all_entities = get_entities(restconf_url)
71
72     warning(
73         "All entities %s",
74         all_entities,
75     )
76
77     result = {
78         "candidates": resp.json()["odl-entity-owners:output"]["candidate-nodes"],
79         "owner": resp.json()["odl-entity-owners:output"]["owner-node"],
80     }
81
82     return result
83
84
85 def get_entity_owner(restconf_url, type, name):
86     data = """
87     {
88         "odl-entity-owners:input" : {
89             "type": "%s",
90             "name": "%s"
91         }
92     }
93     """ % (
94         type,
95         name,
96     )
97
98     debug("Data %s", data)
99
100     resp = post(
101         url=restconf_url + """/operations/odl-entity-owners:get-entity-owner""",
102         headers={
103             "Content-Type": "application/json",
104             "Accept": "application/json",
105             "User-Agent": "csit agent",
106         },
107         data=data,
108         auth=("admin", "admin"),
109     )
110
111     debug(
112         "Response %s ",
113         resp,
114     )
115
116     return resp.json()["odl-entity-owners:output"]["owner-node"]
117
118
119 def main(args):
120     if args[0] == "get-entities":
121         json = get_entities(
122             restconf_url="http://127.0.0.1:8181/rests",
123         )
124         print(json)
125     elif args[0] == "get-entity":
126         json = get_entity(
127             restconf_url="http://127.0.0.1:8181/rests",
128             type=args[1],
129             name=args[2],
130         )
131         print(json)
132     elif args[0] == "get-entity-owner":
133         json = get_entity_owner(
134             restconf_url="http://127.0.0.1:8181/rests",
135             type=args[1],
136             name=args[2],
137         )
138         print(json)
139     else:
140         raise Exception("Unhandled argument %s" % args[0])
141
142
143 if __name__ == "__main__":
144     # i.e. main does not depend on name of the binary
145     main(argv[1:])