Support Silicon for retrieval of entity ownership information
[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 codes as status_codes
7 from requests import get
8 from requests import post
9 from sys import argv
10
11
12 def get_entities(restconf_url):
13     resp = post(
14         url=restconf_url + "/operations/odl-entity-owners:get-entities",
15         headers={
16             "Content-Type": "application/json",
17             "Accept": "application/json",
18             "User-Agent": "csit agent",
19         },
20         auth=("admin", "admin"),
21     )
22
23     info(
24         "Response %s ",
25         resp,
26     )
27
28     return resp.json()
29
30
31 def get_entity(restconf_url, e_type, e_name):
32     """Calls the get-entity rpc on the controller and returns the result in a
33     dictionary that contains the parsed response in two keys:
34     "candidates" and "owner"
35     """
36
37     data = """
38     {
39         "odl-entity-owners:input" : {
40             "type": "%s",
41             "name": "%s"
42         }
43     }
44     """ % (
45         e_type,
46         e_name,
47     )
48
49     info("Data %s", data)
50
51     resp = post(
52         url=restconf_url + "/operations/odl-entity-owners:get-entity",
53         headers={
54             "Content-Type": "application/json",
55             "Accept": "application/json",
56             "User-Agent": "csit agent",
57         },
58         data=data,
59         auth=("admin", "admin"),
60     )
61
62     info(
63         "Entity json %s",
64         resp.json(),
65     )
66
67     if resp.status_code == status_codes["bad_request"]:
68         info(
69             "Status code is '%s' - trying operational data instead.",
70             resp.status_code,
71         )
72         result = get_entity_data(restconf_url, e_type, e_name)
73     else:
74         result = {
75             "candidates": resp.json()["odl-entity-owners:output"]["candidate-nodes"],
76             "owner": resp.json()["odl-entity-owners:output"]["owner-node"],
77         }
78
79     return result
80
81
82 def get_entity_owner(restconf_url, e_type, e_name):
83     data = """
84     {
85         "odl-entity-owners:input" : {
86             "type": "%s",
87             "name": "%s"
88         }
89     }
90     """ % (
91         e_type,
92         e_name,
93     )
94
95     info("Data %s", data)
96
97     resp = post(
98         url=restconf_url + "/operations/odl-entity-owners:get-entity-owner",
99         headers={
100             "Content-Type": "application/json",
101             "Accept": "application/json",
102             "User-Agent": "csit agent",
103         },
104         data=data,
105         auth=("admin", "admin"),
106     )
107
108     info(
109         "Response %s ",
110         resp,
111     )
112
113     if resp.status_code == status_codes["bad_request"]:
114         info(
115             "Status code is '%s' - trying operational data instead.",
116             resp.status_code,
117         )
118         result = get_entity_owner_data(restconf_url, e_type, e_name)
119     else:
120         result = resp.json()["odl-entity-owners:output"]["owner-node"]
121
122     return result
123
124
125 def get_entity_type_data(restconf_url, e_type):
126     """
127     Get the entity information for the given entity type from the datastore
128     for Silicon or earlier versions.
129     :param restconf_url: RESTCONF URL up to the RESTCONF root
130     :param e_type: entity type
131     :return: entity-type
132     """
133     resp = get(
134         url=restconf_url
135         + "/data/entity-owners:entity-owners"
136         + "/entity-type=%s" % e_type,
137         headers={
138             "Accept": "application/yang-data+json",
139             "User-Agent": "csit agent",
140         },
141         auth=("admin", "admin"),
142     )
143
144     return resp.json()["entity-owners:entity-type"][0]
145
146
147 def get_entity_data(restconf_url, e_type, e_name):
148     """
149     Get the entity owner & candidates for the given entity type
150     and entity name from the datastore for Silicon or earlier versions
151     :param restconf_url: RESTCONF URL up to the RESTCONF root
152     :param e_type: entity type
153     :param e_name: entity name
154     :return: entity owner & candidates
155     """
156     entity_type = get_entity_type_data(restconf_url, e_type)
157     entity = [
158         e
159         for e in entity_type["entity"]
160         if e["id"] == "/odl-general-entity:entity[name='%s']" % e_name
161     ][0]
162
163     result = {
164         "candidates": [c["name"] for c in entity["candidate"]],
165         "owner": entity["owner"],
166     }
167
168     return result
169
170
171 def get_entity_owner_data(restconf_url, e_type, e_name):
172     """
173     Get the entity owner for the given entity type and entity name
174     from the datastore for Silicon or earlier versions
175     :param restconf_url: RESTCONF URL up to the RESTCONF root
176     :param e_type: entity type
177     :param e_name: entity name
178     :return: entity owner
179     """
180     entity = get_entity_data(restconf_url, e_type, e_name)
181     return entity["owner"]
182
183
184 def main(args):
185     if args[0] == "get-entities":
186         json = get_entities(
187             restconf_url="http://127.0.0.1:8181/rests",
188         )
189         print(json)
190     elif args[0] == "get-entity":
191         json = get_entity(
192             restconf_url="http://127.0.0.1:8181/rests",
193             e_type=args[1],
194             e_name=args[2],
195         )
196         print(json)
197     elif args[0] == "get-entity-owner":
198         json = get_entity_owner(
199             restconf_url="http://127.0.0.1:8181/rests",
200             e_type=args[1],
201             e_name=args[2],
202         )
203         print(json)
204     else:
205         raise Exception("Unhandled argument %s" % args[0])
206
207
208 if __name__ == "__main__":
209     # i.e. main does not depend on name of the binary
210     main(argv[1:])