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
14 """ Count the number of nodes in a chunk of JSON.
16 Because json blobs come in multiple forms, use node, subnode or
17 category to assist in defining what to count.
20 :param jsonblob: a smattering of JSON data to work through
21 :param node: a node to look for such as users, groups, domains
22 :param subnode: a sub-item to look for, such as domainid
23 :param category: from a simple json record, a field to look for
26 :returns ctr: the correct number of records have in the json
31 jsonobj = json.loads(args['jsonblob'])
33 print "countnodes: json blob to parse not found"
38 elif 'category' in args:
39 category_ = args['category'].encode('ascii', 'ignore')
40 ctr = len(jsonobj[category_])
42 # working with a single record, short-cut and return count of 1
47 def fieldcount(pobject, field):
48 """ Helper-func - use countnodes to count the occurences of field in pobject
51 count the occurences of domainid in this single record...
54 "description": "default odl sdn domain",
57 "name": "MasterTest Domain"
61 :param pobject: JSON code to work through
62 :param field: an element to search for and count
65 :returns number_nodes: the correct number of fields you counted
68 number_nodes = countnodes({'jsonblob': pobject, 'field': field})
72 def subnodecount(pobject, subnode):
73 """ Helper-func - use countnodes to count subnode in pobject
76 count the occurences of domainid in this json.
77 this chunk lacks nested dictionary keywords (users, domains, roles)...
80 "description": "odl master domain",
83 "name": "Master Test Domain"
86 "description": "sdn user domain",
94 :param pobject: JSON code to work through
95 :param subnode: a subnode, such as domainid, to search for and count
98 :returns number_nodes: the correct number of fields you counted
101 number_nodes = countnodes({'jsonblob': pobject, 'subnode': subnode})
105 def nodecount(pobject, category, node):
106 """ Helper-func - use countnodes function to count node of a category type
109 count the domainid in these properly formatted json blobs...
113 "description": "odl master domain",
116 "name": "Master Test Domain"
119 "description": "sdn user domain",
122 "name": "User Domain"
131 :param pobject: JSON code to work through
132 :param node: a node, such as domainid, to search for in a properly
133 formatted json object, and count
136 :returns number_nodes: the correct number of fields you counted
140 countnodes({'jsonblob': pobject, 'category': category, 'node': node})
144 def get_id_by_name(args):
145 """ Get an ID by the Name field.
147 Go through the json given, and pull out all ids that are identified
148 by the corresponding name argument.
151 :param jsonblob: a smattering of JSON code to work through
152 :param name: a name to look up in the database of json
153 :param head: will be one of roles, users, domains
154 :param typeval: literal value of either user, role or domain
155 :param size: a count on the number of records to search
158 :returns nodelist: return the first id that has same corresponding name
161 jsonobj = json.loads(str(args['jsonblob']))
163 print "get_id_by_name: json blob not specified:"
169 print "get_id_by_name: name [usr, domain, role] not specified in args"
173 blobkey = args['head']
175 # use an empty key when the arg is not specified. deals with simpler
180 datatype = args['typeval']
182 print "get_id_by_name: need a type arg to process correct name for id"
186 ncount = args['size']
192 # Loop through the records looking for the specified name. When found,
193 # return the corresponding attribute value
195 for i in range(ncount):
196 # build up some 'lookup' keys, call jsonpath with that key
197 bkey1 = '$.' + blobkey + '[' + str(i) + '].name'
198 typename = datatype + 'id'
199 bkey2 = '$.' + blobkey + '[' + str(i) + '].' + typename
201 # find records with same name
202 name_record = jsonobj[blobkey][i]['name']
203 # find corresponding node info, for that name
204 node_record = jsonobj[blobkey][i][typename]
207 if name == name_record:
213 def get_attribute_by_id(args):
214 """ Get an attribute by the id field.
216 Each json record in the json blob has a unique ID, return
217 the corresponding attribute field from that record. Could be
218 description, name, email, password, or any field in available
222 :param jsonblob: a smattering of JSON code to work through
223 :param id: the ID to look up in the database of json
224 :param head: will be one of roles, users, domains
225 :param typeval: literal value of either user, role or domain
226 :param size: a count on the number of records to search
229 :returns name_record: the name attribute value that corresponds
233 jsonobj = json.loads(args['jsonblob'])
235 print "get_attribute_by_id: json blob not specified:"
241 print "get_attribute_by_id: id to look for not specified in parameters"
247 # If caller does not specify a record attribute to return, then
248 # simply default to giving the description of the id you are
253 # will be one of roles, users, domains, or empty to process more
254 # specific grouping of json data
255 blobkey = args['head']
257 # use an empty key when the arg is not specified, allows us to
258 # process chunk of JSON without the outer layer defining roles,
259 # users, domains. (simpler format)
263 datatype = args['typeval']
265 print "get_attribute_by_id: need type arg to process name for id"
271 print "get_attribute_by_id: specify number of records we need"
274 typename = datatype + 'id'
276 # Loop through the records looking for the nodeid, when found, return
277 # the corresponding attribute value
281 for i in range(ncount):
284 name_record = jsonobj[blobkey][i]['name']
285 node_record = jsonobj[blobkey][i][typename]
287 name_record = jsonobj['name']
288 node_record = jsonobj[typename]
290 if nodeid == node_record:
294 def get_role_id_by_rolename(pobject, rolename, number_nodes):
295 """ Helper-func - use get_id_by_name to obtain role-ids for a role-name
299 "description": "a role for admins",
308 :param pobject: JSON blob to work through
309 :param rolename: the name element to search for
310 :param number_nodes: number of records to process
313 :returns roleid: a list of one or more roleid's that match
316 roleid = get_id_by_name({'jsonblob': pobject,
319 'size': number_nodes,
329 def get_role_name_by_roleid(pobject, roleid, number_nodes):
330 """ Helper-func - use get_attribute_by_id to get role-name for a role-id
334 "description": "a role for admins",
343 :param pobject: JSON blob to work through
344 :param roleid: the identifier element to search for
345 :param number_nodes: number of records to process
348 :returns rolename: the role name that corresponds to the record
349 identified by the role-id
351 rolename = get_attribute_by_id({'jsonblob': pobject,
355 'size': number_nodes,
365 def get_role_description_by_roleid(pobject, roleid, number_nodes):
366 """ Helper-func - get role-description for a role-id
370 "description": "a role for admins",
379 :param pobject: JSON blob to work through
380 :param roleid: the identifier element to search for
381 :param number_nodes: number of records to process
384 :returns roledesc: the role description that corresponds to the record
385 identified by the role-id
387 roledesc = get_attribute_by_id({'jsonblob': pobject,
390 'attr': 'description',
391 'size': number_nodes,
401 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
402 """ Helper-func - get all domain-ids corresponding to domain-name
406 "description": "default odl sdn domain",
416 :param pobject: JSON blob to work through
417 :param domainname: the name element to search for
418 :param number_nodes: number of records to process
421 :returns domainid: a list of one or more domain-id's that match
422 the domain-name given
424 domainid = get_id_by_name({'jsonblob': pobject,
427 'size': number_nodes,
428 'typeval': 'domain'})
438 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
439 """ Helper-func - get domain-name for a particular domainid
443 "description": "default odl sdn domain",
453 :param pobject: JSON blob to work through
454 :param domainid: the identifier element to search for
455 :param number_nodes: number of records to process
458 :returns domainname: the domain name that corresponds to the record
459 identified by the domainid
461 domainname = get_attribute_by_id({'jsonblob': pobject,
465 'size': number_nodes,
466 'typeval': 'domain'})
475 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
476 """ Helper-func - get the domaind descripton for a particular domainid
480 "description": "default odl sdn domain",
490 :param pobject: JSON blob to work through
491 :param domainid: the identifier element to search for
492 :param number_nodes: number of records to process
495 :returns domainname: the domain description field that corresponds
496 to the record identified by the domainid
498 domaindesc = get_attribute_by_id({'jsonblob': pobject,
501 'attr': 'description',
502 'size': number_nodes,
503 'typeval': 'domain'})
512 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
513 """ Helper-func - get domain state field for a particular domainid
517 "description": "default odl sdn domain",
527 :param pobject: JSON blob to work through
528 :param domainid: the identifier element to search for
529 :param number_nodes: number of records to process
532 :returns domainstate: the domain state (enabled) field that
533 corresponds to the record identified by the domainid
535 domainstate = get_attribute_by_id({'jsonblob': pobject,
539 'size': number_nodes,
540 'typeval': 'domain'})
549 def get_user_id_by_username(pobject, username, number_nodes):
550 """ Helper-func - get user-ids corresponding to username
554 "description": "admin user",
555 "email": "admin@anydomain.com",
559 "password": "**********",
567 :param pobject: JSON blob to work through
568 :param username: the name element to search for
569 :param number_nodes: number of records to process
572 :returns userid: a list of one or more user-id's that match
575 userid = get_id_by_name({'jsonblob': pobject,
578 'size': number_nodes,
588 def get_user_password_by_userid(pobject, userid, number_nodes):
589 """ Helper-func - get user password field for a particular userid
593 "description": "admin user",
594 "email": "admin@anydomain.com",
598 "password": "**********",
606 :param pobject: JSON blob to work through
607 :param userid: the identifier element to search for
608 :param number_nodes: number of records to process
611 :returns userpassword: the raw password field that corresponds to
612 the record identified by the userid
614 userpassword = get_attribute_by_id({'jsonblob': pobject,
618 'size': number_nodes,
628 def get_user_name_by_userid(pobject, userid, number_nodes):
629 """ Helper-func - get the username field for a particular userid
633 "description": "admin user",
634 "email": "admin@anydomain.com",
638 "password": "**********",
646 :param pobject: JSON blob to work through
647 :param userid: the identifier element to search for
648 :param number_nodes: number of records to process
651 :returns username: the name field that corresponds to the record
652 identified by the userid
654 username = get_attribute_by_id({'jsonblob': pobject,
658 'size': number_nodes,
668 def get_user_state_by_userid(pobject, userid, number_nodes):
669 """ Helper-func - get user state field for a particular userid
673 "description": "admin user",
674 "email": "admin@anydomain.com",
678 "password": "**********",
686 :param pobject: JSON blob to work through
687 :param userid: the identifier element to search for
688 :param number_nodes: number of records to process
691 :returns userstate: the enabled field that corresponds to the record
692 identified by the userid
694 userstate = get_attribute_by_id({'jsonblob': pobject,
698 'size': number_nodes,
708 def get_user_email_by_userid(pobject, userid, number_nodes):
709 """ Helper-func - get user email field for a particular userid
713 "description": "admin user",
714 "email": "admin@anydomain.com",
718 "password": "**********",
726 :param pobject: JSON blob to work through
727 :param userid: the identifier element to search for
728 :param number_nodes: number of records to process
731 :returns useremail: the email field that corresponds to the record
732 identified by the userid
734 useremail = get_attribute_by_id({'jsonblob': pobject,
738 'size': number_nodes,
748 def get_user_description_by_userid(pobject, userid, number_nodes):
749 """ Helper-func - get user description field for a particular userid
753 "description": "admin user",
754 "email": "admin@anydomain.com",
758 "password": "**********",
766 :param pobject: JSON blob to work through
767 :param userid: the identifier element to search for
768 :param number_nodes: number of records to process
771 :returns userdesc: the description field that corresponds to the
772 record identified by the userid
774 userdesc = get_attribute_by_id({'jsonblob': pobject,
777 'attr': 'description',
778 'size': number_nodes,