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"),
144 return resp.json()["entity-owners:entity-type"][0]
147 def get_entity_data(restconf_url, e_type, e_name):
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
156 entity_type = get_entity_type_data(restconf_url, e_type)
159 for e in entity_type["entity"]
160 if e["id"] == "/odl-general-entity:entity[name='%s']" % e_name
164 "candidates": [c["name"] for c in entity["candidate"]],
165 "owner": entity["owner"],
171 def get_entity_owner_data(restconf_url, e_type, e_name):
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
180 entity = get_entity_data(restconf_url, e_type, e_name)
181 return entity["owner"]
185 if args[0] == "get-entities":
187 restconf_url="http://127.0.0.1:8181/rests",
190 elif args[0] == "get-entity":
192 restconf_url="http://127.0.0.1:8181/rests",
197 elif args[0] == "get-entity-owner":
198 json = get_entity_owner(
199 restconf_url="http://127.0.0.1:8181/rests",
205 raise Exception("Unhandled argument %s" % args[0])
208 if __name__ == "__main__":
209 # i.e. main does not depend on name of the binary