Fallback to release url during testtool deploy
[integration/test.git] / csit / libraries / ClusterEntities.py
1 """
2     Utility library for retrieving entity related data from ODL.
3 """
4
5 from logging import info
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     info(
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     info("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     info(
61         "Entity json %s",
62         resp.json(),
63     )
64
65     result = {
66         "candidates": resp.json()["odl-entity-owners:output"]["candidate-nodes"],
67         "owner": resp.json()["odl-entity-owners:output"]["owner-node"],
68     }
69
70     return result
71
72
73 def get_entity_owner(restconf_url, type, name):
74     data = """
75     {
76         "odl-entity-owners:input" : {
77             "type": "%s",
78             "name": "%s"
79         }
80     }
81     """ % (
82         type,
83         name,
84     )
85
86     info("Data %s", data)
87
88     resp = post(
89         url=restconf_url + """/operations/odl-entity-owners:get-entity-owner""",
90         headers={
91             "Content-Type": "application/json",
92             "Accept": "application/json",
93             "User-Agent": "csit agent",
94         },
95         data=data,
96         auth=("admin", "admin"),
97     )
98
99     info(
100         "Response %s ",
101         resp,
102     )
103
104     return resp.json()["odl-entity-owners:output"]["owner-node"]
105
106
107 def main(args):
108     if args[0] == "get-entities":
109         json = get_entities(
110             restconf_url="http://127.0.0.1:8181/rests",
111         )
112         print(json)
113     elif args[0] == "get-entity":
114         json = get_entity(
115             restconf_url="http://127.0.0.1:8181/rests",
116             type=args[1],
117             name=args[2],
118         )
119         print(json)
120     elif args[0] == "get-entity-owner":
121         json = get_entity_owner(
122             restconf_url="http://127.0.0.1:8181/rests",
123             type=args[1],
124             name=args[2],
125         )
126         print(json)
127     else:
128         raise Exception("Unhandled argument %s" % args[0])
129
130
131 if __name__ == "__main__":
132     # i.e. main does not depend on name of the binary
133     main(argv[1:])