2 AAAJSonUtils library for looking up relevant information in a json record
4 This library can be used to take a chunk of json results and mine out
5 needed information for testing.
6 Author: Carmen Kelling - HP Enterprise
13 """Count the number of nodes in a chunk of JSON.
15 Because json blobs come in multiple forms, use node, subnode or
16 category to assist in defining what to count.
19 :param jsonblob: a smattering of JSON data to work through
20 :param node: a node to look for such as users, groups, domains
21 :param subnode: a sub-item to look for, such as domainid
22 :param category: from a simple json record, a field to look for
25 :returns ctr: the correct number of records have in the json
30 jsonobj = json.loads(args["jsonblob"])
32 print("countnodes: json blob to parse not found")
37 elif "category" in args:
38 category_ = args["category"].encode("ascii", "ignore")
39 ctr = len(jsonobj[category_])
41 # working with a single record, short-cut and return count of 1
46 def fieldcount(pobject, field):
47 """Helper-func - use countnodes to count the occurences of field in pobject
50 count the occurences of domainid in this single record...
53 "description": "default odl sdn domain",
56 "name": "MasterTest Domain"
60 :param pobject: JSON code to work through
61 :param field: an element to search for and count
64 :returns number_nodes: the correct number of fields you counted
67 number_nodes = countnodes({"jsonblob": pobject, "field": field})
71 def subnodecount(pobject, subnode):
72 """Helper-func - use countnodes to count subnode in pobject
75 count the occurences of domainid in this json.
76 this chunk lacks nested dictionary keywords (users, domains, roles)...
79 "description": "odl master domain",
82 "name": "Master Test Domain"
85 "description": "sdn user domain",
93 :param pobject: JSON code to work through
94 :param subnode: a subnode, such as domainid, to search for and count
97 :returns number_nodes: the correct number of fields you counted
100 number_nodes = countnodes({"jsonblob": pobject, "subnode": subnode})
104 def nodecount(pobject, category, node):
105 """Helper-func - use countnodes function to count node of a category type
108 count the domainid in these properly formatted json blobs...
112 "description": "odl master domain",
115 "name": "Master Test Domain"
118 "description": "sdn user domain",
121 "name": "User Domain"
130 :param pobject: JSON code to work through
131 :param node: a node, such as domainid, to search for in a properly
132 formatted json object, and count
135 :returns number_nodes: the correct number of fields you counted
138 number_nodes = countnodes({"jsonblob": pobject, "category": category, "node": node})
142 def get_id_by_name(args):
143 """Get an ID by the Name field.
145 Go through the json given, and pull out all ids that are identified
146 by the corresponding name argument.
149 :param jsonblob: a smattering of JSON code to work through
150 :param name: a name to look up in the database of json
151 :param head: will be one of roles, users, domains
152 :param typeval: literal value of either user, role or domain
153 :param size: a count on the number of records to search
156 :returns nodelist: return the first id that has same corresponding name
159 jsonobj = json.loads(str(args["jsonblob"]))
161 print("get_id_by_name: json blob not specified:")
167 print("get_id_by_name: name [usr, domain, role] not specified in args")
171 blobkey = args["head"]
173 # use an empty key when the arg is not specified. deals with simpler
178 datatype = args["typeval"]
180 print("get_id_by_name: need a type arg to process correct name for id")
184 ncount = args["size"]
190 # Loop through the records looking for the specified name. When found,
191 # return the corresponding attribute value
193 for i in range(ncount):
194 # build up some 'lookup' keys, call jsonpath with that key
195 bkey1 = "$." + blobkey + "[" + str(i) + "].name"
196 typename = datatype + "id"
197 bkey2 = "$." + blobkey + "[" + str(i) + "]." + typename
199 # find records with same name
200 name_record = jsonobj[blobkey][i]["name"]
201 # find corresponding node info, for that name
202 node_record = jsonobj[blobkey][i][typename]
205 if name == name_record:
211 def get_attribute_by_id(args):
212 """Get an attribute by the id field.
214 Each json record in the json blob has a unique ID, return
215 the corresponding attribute field from that record. Could be
216 description, name, email, password, or any field in available
220 :param jsonblob: a smattering of JSON code to work through
221 :param id: the ID to look up in the database of json
222 :param head: will be one of roles, users, domains
223 :param typeval: literal value of either user, role or domain
224 :param size: a count on the number of records to search
227 :returns name_record: the name attribute value that corresponds
231 jsonobj = json.loads(args["jsonblob"])
233 print("get_attribute_by_id: json blob not specified:")
239 print("get_attribute_by_id: id to look for not specified in parameters")
245 # If caller does not specify a record attribute to return, then
246 # simply default to giving the description of the id you are
251 # will be one of roles, users, domains, or empty to process more
252 # specific grouping of json data
253 blobkey = args["head"]
255 # use an empty key when the arg is not specified, allows us to
256 # process chunk of JSON without the outer layer defining roles,
257 # users, domains. (simpler format)
261 datatype = args["typeval"]
263 print("get_attribute_by_id: need type arg to process name for id")
269 print("get_attribute_by_id: specify number of records we need")
272 typename = datatype + "id"
274 # Loop through the records looking for the nodeid, when found, return
275 # the corresponding attribute value
279 for i in range(ncount):
282 name_record = jsonobj[blobkey][i]["name"]
283 node_record = jsonobj[blobkey][i][typename]
285 name_record = jsonobj["name"]
286 node_record = jsonobj[typename]
288 if nodeid == node_record:
292 def get_role_id_by_rolename(pobject, rolename, number_nodes):
293 """Helper-func - use get_id_by_name to obtain role-ids for a role-name
297 "description": "a role for admins",
306 :param pobject: JSON blob to work through
307 :param rolename: the name element to search for
308 :param number_nodes: number of records to process
311 :returns roleid: a list of one or more roleid's that match
314 roleid = get_id_by_name(
319 "size": number_nodes,
331 def get_role_name_by_roleid(pobject, roleid, number_nodes):
332 """Helper-func - use get_attribute_by_id to get role-name for a role-id
336 "description": "a role for admins",
345 :param pobject: JSON blob to work through
346 :param roleid: the identifier element to search for
347 :param number_nodes: number of records to process
350 :returns rolename: the role name that corresponds to the record
351 identified by the role-id
353 rolename = get_attribute_by_id(
359 "size": number_nodes,
371 def get_role_description_by_roleid(pobject, roleid, number_nodes):
372 """Helper-func - get role-description for a role-id
376 "description": "a role for admins",
385 :param pobject: JSON blob to work through
386 :param roleid: the identifier element to search for
387 :param number_nodes: number of records to process
390 :returns roledesc: the role description that corresponds to the record
391 identified by the role-id
393 roledesc = get_attribute_by_id(
398 "attr": "description",
399 "size": number_nodes,
411 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
412 """Helper-func - get all domain-ids corresponding to domain-name
416 "description": "default odl sdn domain",
426 :param pobject: JSON blob to work through
427 :param domainname: the name element to search for
428 :param number_nodes: number of records to process
431 :returns domainid: a list of one or more domain-id's that match
432 the domain-name given
434 domainid = get_id_by_name(
439 "size": number_nodes,
452 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
453 """Helper-func - get domain-name for a particular domainid
457 "description": "default odl sdn domain",
467 :param pobject: JSON blob to work through
468 :param domainid: the identifier element to search for
469 :param number_nodes: number of records to process
472 :returns domainname: the domain name that corresponds to the record
473 identified by the domainid
475 domainname = get_attribute_by_id(
481 "size": number_nodes,
493 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
494 """Helper-func - get the domaind descripton for a particular domainid
498 "description": "default odl sdn domain",
508 :param pobject: JSON blob to work through
509 :param domainid: the identifier element to search for
510 :param number_nodes: number of records to process
513 :returns domainname: the domain description field that corresponds
514 to the record identified by the domainid
516 domaindesc = get_attribute_by_id(
521 "attr": "description",
522 "size": number_nodes,
534 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
535 """Helper-func - get domain state field for a particular domainid
539 "description": "default odl sdn domain",
549 :param pobject: JSON blob to work through
550 :param domainid: the identifier element to search for
551 :param number_nodes: number of records to process
554 :returns domainstate: the domain state (enabled) field that
555 corresponds to the record identified by the domainid
557 domainstate = get_attribute_by_id(
563 "size": number_nodes,
575 def get_user_id_by_username(pobject, username, number_nodes):
576 """Helper-func - get user-ids corresponding to username
580 "description": "admin user",
581 "email": "admin@anydomain.com",
585 "password": "**********",
593 :param pobject: JSON blob to work through
594 :param username: the name element to search for
595 :param number_nodes: number of records to process
598 :returns userid: a list of one or more user-id's that match
601 userid = get_id_by_name(
606 "size": number_nodes,
618 def get_user_password_by_userid(pobject, userid, number_nodes):
619 """Helper-func - get user password field for a particular userid
623 "description": "admin user",
624 "email": "admin@anydomain.com",
628 "password": "**********",
636 :param pobject: JSON blob to work through
637 :param userid: the identifier element to search for
638 :param number_nodes: number of records to process
641 :returns userpassword: the raw password field that corresponds to
642 the record identified by the userid
644 userpassword = get_attribute_by_id(
650 "size": number_nodes,
662 def get_user_name_by_userid(pobject, userid, number_nodes):
663 """Helper-func - get the username field for a particular userid
667 "description": "admin user",
668 "email": "admin@anydomain.com",
672 "password": "**********",
680 :param pobject: JSON blob to work through
681 :param userid: the identifier element to search for
682 :param number_nodes: number of records to process
685 :returns username: the name field that corresponds to the record
686 identified by the userid
688 username = get_attribute_by_id(
694 "size": number_nodes,
706 def get_user_state_by_userid(pobject, userid, number_nodes):
707 """Helper-func - get user state field for a particular userid
711 "description": "admin user",
712 "email": "admin@anydomain.com",
716 "password": "**********",
724 :param pobject: JSON blob to work through
725 :param userid: the identifier element to search for
726 :param number_nodes: number of records to process
729 :returns userstate: the enabled field that corresponds to the record
730 identified by the userid
732 userstate = get_attribute_by_id(
738 "size": number_nodes,
750 def get_user_email_by_userid(pobject, userid, number_nodes):
751 """Helper-func - get user email field for a particular userid
755 "description": "admin user",
756 "email": "admin@anydomain.com",
760 "password": "**********",
768 :param pobject: JSON blob to work through
769 :param userid: the identifier element to search for
770 :param number_nodes: number of records to process
773 :returns useremail: the email field that corresponds to the record
774 identified by the userid
776 useremail = get_attribute_by_id(
782 "size": number_nodes,
794 def get_user_description_by_userid(pobject, userid, number_nodes):
795 """Helper-func - get user description field for a particular userid
799 "description": "admin user",
800 "email": "admin@anydomain.com",
804 "password": "**********",
812 :param pobject: JSON blob to work through
813 :param userid: the identifier element to search for
814 :param number_nodes: number of records to process
817 :returns userdesc: the description field that corresponds to the
818 record identified by the userid
820 userdesc = get_attribute_by_id(
825 "attr": "description",
826 "size": number_nodes,