2 Utility library for retrieving entity related data from ODL.
5 from logging import info
6 from requests import codes as status_codes
7 from requests import get
8 from requests import post
12 def get_entities(restconf_url):
14 url=restconf_url + "/operations/odl-entity-owners:get-entities",
16 "Content-Type": "application/json",
17 "Accept": "application/json",
18 "User-Agent": "csit agent",
20 auth=("admin", "admin"),
31 def get_entity_name(e_type, e_name):
33 Get the effective entity name for the given entity type.
34 If the entity type is not for odl-general-entity, entity name
35 should be the full instance identifier.
36 :param e_type: entity type
37 :param e_name: entity name
38 :return: updated entity name
41 "ovsdb": "/network-topology:network-topology/topology[topology-id='ovsdb:1']/node[node-id='%s']",
44 if e_type in name_templates:
45 return name_templates[e_type] % e_name
50 def get_entity(restconf_url, e_type, e_name):
51 """Calls the get-entity rpc on the controller and returns the result in a
52 dictionary that contains the parsed response in two keys:
53 "candidates" and "owner"
58 "odl-entity-owners:input" : {
65 get_entity_name(e_type, e_name),
71 url=restconf_url + "/operations/odl-entity-owners:get-entity",
73 "Content-Type": "application/json",
74 "Accept": "application/json",
75 "User-Agent": "csit agent",
78 auth=("admin", "admin"),
86 if resp.status_code == status_codes["bad_request"]:
88 "Status code is '%s' - trying operational data instead.",
91 result = get_entity_data(restconf_url, e_type, e_name)
94 "candidates": resp.json()["odl-entity-owners:output"]["candidate-nodes"],
95 "owner": resp.json()["odl-entity-owners:output"]["owner-node"],
101 def get_entity_owner(restconf_url, e_type, e_name):
104 "odl-entity-owners:input" : {
111 get_entity_name(e_type, e_name),
114 info("Data %s", data)
117 url=restconf_url + "/operations/odl-entity-owners:get-entity-owner",
119 "Content-Type": "application/json",
120 "Accept": "application/json",
121 "User-Agent": "csit agent",
124 auth=("admin", "admin"),
132 if resp.status_code == status_codes["bad_request"]:
134 "Status code is '%s' - trying operational data instead.",
137 result = get_entity_owner_data(restconf_url, e_type, e_name)
139 result = resp.json()["odl-entity-owners:output"]["owner-node"]
144 def get_entity_type_data(restconf_url, e_type):
146 Get the entity information for the given entity type from the datastore
147 for Silicon or earlier versions.
148 :param restconf_url: RESTCONF URL up to the RESTCONF root
149 :param e_type: entity type
154 + "/data/entity-owners:entity-owners"
155 + "/entity-type=%s" % e_type,
157 "Accept": "application/yang-data+json",
158 "User-Agent": "csit agent",
160 auth=("admin", "admin"),
172 return resp.json()["entity-owners:entity-type"][0]
175 def get_entity_data(restconf_url, e_type, e_name):
177 Get the entity owner & candidates for the given entity type
178 and entity name from the datastore for Silicon or earlier versions
179 :param restconf_url: RESTCONF URL up to the RESTCONF root
180 :param e_type: entity type
181 :param e_name: entity name
182 :return: entity owner & candidates
185 "org.opendaylight.mdsal.ServiceEntityType": "/odl-general-entity:entity[name='%s']",
186 "org.opendaylight.mdsal.AsyncServiceCloseEntityType": "/odl-general-entity:entity[name='%s']",
187 "ovsdb": "/network-topology:network-topology/topology[topology-id='ovsdb:1']/node[node-id='%s']",
189 id_template = id_templates[e_type]
191 entity_type = get_entity_type_data(restconf_url, e_type)
192 entity = [e for e in entity_type["entity"] if e["id"] == id_template % e_name][0]
195 "candidates": [c["name"] for c in entity["candidate"]],
196 "owner": entity["owner"],
202 def get_entity_owner_data(restconf_url, e_type, e_name):
204 Get the entity owner for the given entity type and entity name
205 from the datastore for Silicon or earlier versions
206 :param restconf_url: RESTCONF URL up to the RESTCONF root
207 :param e_type: entity type
208 :param e_name: entity name
209 :return: entity owner
211 entity = get_entity_data(restconf_url, e_type, e_name)
212 return entity["owner"]
216 if args[0] == "get-entities":
218 restconf_url="http://127.0.0.1:8181/rests",
221 elif args[0] == "get-entity":
223 restconf_url="http://127.0.0.1:8181/rests",
228 elif args[0] == "get-entity-owner":
229 json = get_entity_owner(
230 restconf_url="http://127.0.0.1:8181/rests",
236 raise Exception("Unhandled argument %s" % args[0])
239 if __name__ == "__main__":
240 # i.e. main does not depend on name of the binary