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(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"
39 "odl-entity-owners:input" : {
52 url=restconf_url + "/operations/odl-entity-owners:get-entity",
54 "Content-Type": "application/json",
55 "Accept": "application/json",
56 "User-Agent": "csit agent",
59 auth=("admin", "admin"),
67 if resp.status_code == status_codes["bad_request"]:
69 "Status code is '%s' - trying operational data instead.",
72 result = get_entity_data(restconf_url, e_type, e_name)
75 "candidates": resp.json()["odl-entity-owners:output"]["candidate-nodes"],
76 "owner": resp.json()["odl-entity-owners:output"]["owner-node"],
82 def get_entity_owner(restconf_url, e_type, e_name):
85 "odl-entity-owners:input" : {
98 url=restconf_url + "/operations/odl-entity-owners:get-entity-owner",
100 "Content-Type": "application/json",
101 "Accept": "application/json",
102 "User-Agent": "csit agent",
105 auth=("admin", "admin"),
113 if resp.status_code == status_codes["bad_request"]:
115 "Status code is '%s' - trying operational data instead.",
118 result = get_entity_owner_data(restconf_url, e_type, e_name)
120 result = resp.json()["odl-entity-owners:output"]["owner-node"]
125 def get_entity_type_data(restconf_url, e_type):
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
135 + "/data/entity-owners:entity-owners"
136 + "/entity-type=%s" % e_type,
138 "Accept": "application/yang-data+json",
139 "User-Agent": "csit agent",
141 auth=("admin", "admin"),
153 return resp.json()["entity-owners:entity-type"][0]
156 def get_entity_data(restconf_url, e_type, e_name):
158 Get the entity owner & candidates for the given entity type
159 and entity name from the datastore for Silicon or earlier versions
160 :param restconf_url: RESTCONF URL up to the RESTCONF root
161 :param e_type: entity type
162 :param e_name: entity name
163 :return: entity owner & candidates
166 "org.opendaylight.mdsal.ServiceEntityType": "/odl-general-entity:entity[name='%s']",
167 "org.opendaylight.mdsal.AsyncServiceCloseEntityType": "/odl-general-entity:entity[name='%s']",
168 "ovsdb": "/network-topology:network-topology/topology[topology-id='ovsdb:1']/node[node-id='%s']",
170 id_template = id_templates[e_type]
172 entity_type = get_entity_type_data(restconf_url, e_type)
173 entity = [e for e in entity_type["entity"] if e["id"] == id_template % e_name][0]
176 "candidates": [c["name"] for c in entity["candidate"]],
177 "owner": entity["owner"],
183 def get_entity_owner_data(restconf_url, e_type, e_name):
185 Get the entity owner for the given entity type and entity name
186 from the datastore for Silicon or earlier versions
187 :param restconf_url: RESTCONF URL up to the RESTCONF root
188 :param e_type: entity type
189 :param e_name: entity name
190 :return: entity owner
192 entity = get_entity_data(restconf_url, e_type, e_name)
193 return entity["owner"]
197 if args[0] == "get-entities":
199 restconf_url="http://127.0.0.1:8181/rests",
202 elif args[0] == "get-entity":
204 restconf_url="http://127.0.0.1:8181/rests",
209 elif args[0] == "get-entity-owner":
210 json = get_entity_owner(
211 restconf_url="http://127.0.0.1:8181/rests",
217 raise Exception("Unhandled argument %s" % args[0])
220 if __name__ == "__main__":
221 # i.e. main does not depend on name of the binary