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(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 = jsonpath.jsonpath(jsonobj, bkey1)
203 # find corresponding node info, for that name
204 node_record = jsonpath.jsonpath(jsonobj, bkey2)
206 # build up an alternative set of keys. This lets you deal with
207 # other format of json
208 bkey3 = '$.' + blobkey + '.name'
209 typename2 = datatype + 'id'
210 bkey4 = '$.' + blobkey + '.' + typename2
212 # find records with same name
213 altname_record = jsonpath.jsonpath(jsonobj, bkey3)
214 # find corresponding record node info, for that name
215 altnode_record = jsonpath.jsonpath(jsonobj, bkey4)
217 if name in list(name_record):
218 nodelist.append(node_record.pop())
221 if name in list(altname_record):
222 nodelist.append(altnode_record.pop())
227 return nodelist.pop()
232 def get_attribute_by_id(args):
233 """ Get an attribute by the id field.
235 Each json record in the json blob has a unique ID, return
236 the corresponding attribute field from that record. Could be
237 description, name, email, password, or any field in available
241 :param jsonblob: a smattering of JSON code to work through
242 :param id: the ID to look up in the database of json
243 :param head: will be one of roles, users, domains
244 :param typeval: literal value of either user, role or domain
245 :param size: a count on the number of records to search
248 :returns name_record: the name attribute value that corresponds
252 jsonobj = json.loads(args['jsonblob'])
254 print "get_attribute_by_id: json blob not specified:"
260 print "get_attribute_by_id: id to look for not specified in parameters"
266 # If caller does not specify a record attribute to return, then
267 # simply default to giving the description of the id you are
272 # will be one of roles, users, domains, or empty to process more
273 # specific grouping of json data
274 blobkey = args['head']
276 # use an empty key when the arg is not specified, allows us to
277 # process chunk of JSON without the outer layer defining roles,
278 # users, domains. (simpler format)
282 datatype = args['typeval']
284 print "get_attribute_by_id: need type arg to process name for id"
290 print "get_attribute_by_id: specify number of records we need"
293 # Loop through the records looking for the nodeid, when found, return
294 # the corresponding attribute value
298 for i in range(ncount):
299 bkey1 = '$.' + blobkey + '[' + str(i) + '].' + attr
300 bkey2 = '$.' + blobkey + '[' + str(i) + '].' + datatype + 'id'
302 bkey3 = '$.' + blobkey + '.' + attr
303 bkey4 = '$.' + blobkey + '.' + datatype + 'id'
305 name_record = jsonpath.jsonpath(jsonobj, bkey1)
306 node_record = jsonpath.jsonpath(jsonobj, bkey2)
307 altname_record = jsonpath.jsonpath(jsonobj, bkey3)
308 altnode_record = jsonpath.jsonpath(jsonobj, bkey4)
310 if type(node_record) is list:
311 if nodeid in list(node_record):
312 return name_record.pop()
321 if type(altnode_record) is list:
322 if nodeid in list(altnode_record):
323 return altname_record.pop()
330 return altname_record
333 def get_role_id_by_rolename(pobject, rolename, number_nodes):
334 """ Helper-func - use get_id_by_name to obtain role-ids for a role-name
338 "description": "a role for admins",
347 :param pobject: JSON blob to work through
348 :param rolename: the name element to search for
349 :param number_nodes: number of records to process
352 :returns roleid: a list of one or more roleid's that match
355 roleid = get_id_by_name({'jsonblob': pobject,
358 'size': number_nodes,
368 def get_role_name_by_roleid(pobject, roleid, number_nodes):
369 """ Helper-func - use get_attribute_by_id to get role-name for a role-id
373 "description": "a role for admins",
382 :param pobject: JSON blob to work through
383 :param roleid: the identifier element to search for
384 :param number_nodes: number of records to process
387 :returns rolename: the role name that corresponds to the record
388 identified by the role-id
390 rolename = get_attribute_by_id({'jsonblob': pobject,
394 'size': number_nodes,
404 def get_role_description_by_roleid(pobject, roleid, number_nodes):
405 """ Helper-func - get role-description for a role-id
409 "description": "a role for admins",
418 :param pobject: JSON blob to work through
419 :param roleid: the identifier element to search for
420 :param number_nodes: number of records to process
423 :returns roledesc: the role description that corresponds to the record
424 identified by the role-id
426 roledesc = get_attribute_by_id({'jsonblob': pobject,
429 'attr': 'description',
430 'size': number_nodes,
440 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
441 """ Helper-func - get all domain-ids corresponding to domain-name
445 "description": "default odl sdn domain",
455 :param pobject: JSON blob to work through
456 :param domainname: the name element to search for
457 :param number_nodes: number of records to process
460 :returns domainid: a list of one or more domain-id's that match
461 the domain-name given
463 domainid = get_id_by_name({'jsonblob': pobject,
465 'size': number_nodes,
466 'typeval': 'domain'})
476 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
477 """ Helper-func - get domain-name for a particular domainid
481 "description": "default odl sdn domain",
491 :param pobject: JSON blob to work through
492 :param domainid: the identifier element to search for
493 :param number_nodes: number of records to process
496 :returns domainname: the domain name that corresponds to the record
497 identified by the domainid
499 domainname = get_attribute_by_id({'jsonblob': pobject,
503 'size': number_nodes,
504 'typeval': 'domain'})
513 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
514 """ Helper-func - get the domaind descripton for a particular domainid
518 "description": "default odl sdn domain",
528 :param pobject: JSON blob to work through
529 :param domainid: the identifier element to search for
530 :param number_nodes: number of records to process
533 :returns domainname: the domain description field that corresponds
534 to the record identified by the domainid
536 domaindesc = get_attribute_by_id({'jsonblob': pobject,
539 'attr': 'description',
540 'size': number_nodes,
541 'typeval': 'domain'})
550 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
551 """ Helper-func - get domain state field for a particular domainid
555 "description": "default odl sdn domain",
565 :param pobject: JSON blob to work through
566 :param domainid: the identifier element to search for
567 :param number_nodes: number of records to process
570 :returns domainstate: the domain state (enabled) field that
571 corresponds to the record identified by the domainid
573 domainstate = get_attribute_by_id({'jsonblob': pobject,
577 'size': number_nodes,
578 'typeval': 'domain'})
587 def get_user_id_by_username(pobject, username, number_nodes):
588 """ Helper-func - get user-ids corresponding to username
592 "description": "admin user",
593 "email": "admin@anydomain.com",
597 "password": "**********",
605 :param pobject: JSON blob to work through
606 :param username: the name element to search for
607 :param number_nodes: number of records to process
610 :returns userid: a list of one or more user-id's that match
613 userid = get_id_by_name({'jsonblob': pobject,
616 'size': number_nodes,
626 def get_user_password_by_userid(pobject, userid, number_nodes):
627 """ Helper-func - get user password field for a particular userid
631 "description": "admin user",
632 "email": "admin@anydomain.com",
636 "password": "**********",
644 :param pobject: JSON blob to work through
645 :param userid: the identifier element to search for
646 :param number_nodes: number of records to process
649 :returns userpassword: the raw password field that corresponds to
650 the record identified by the userid
652 userpassword = get_attribute_by_id({'jsonblob': pobject,
656 'size': number_nodes,
666 def get_user_name_by_userid(pobject, userid, number_nodes):
667 """ Helper-func - get the username field for a particular userid
671 "description": "admin user",
672 "email": "admin@anydomain.com",
676 "password": "**********",
684 :param pobject: JSON blob to work through
685 :param userid: the identifier element to search for
686 :param number_nodes: number of records to process
689 :returns username: the name field that corresponds to the record
690 identified by the userid
692 username = get_attribute_by_id({'jsonblob': pobject,
696 '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({'jsonblob': pobject,
736 'size': number_nodes,
746 def get_user_email_by_userid(pobject, userid, number_nodes):
747 """ Helper-func - get user email field for a particular userid
751 "description": "admin user",
752 "email": "admin@anydomain.com",
756 "password": "**********",
764 :param pobject: JSON blob to work through
765 :param userid: the identifier element to search for
766 :param number_nodes: number of records to process
769 :returns useremail: the email field that corresponds to the record
770 identified by the userid
772 useremail = get_attribute_by_id({'jsonblob': pobject,
776 'size': number_nodes,
786 def get_user_description_by_userid(pobject, userid, number_nodes):
787 """ Helper-func - get user description field for a particular userid
791 "description": "admin user",
792 "email": "admin@anydomain.com",
796 "password": "**********",
804 :param pobject: JSON blob to work through
805 :param userid: the identifier element to search for
806 :param number_nodes: number of records to process
809 :returns userdesc: the description field that corresponds to the
810 record identified by the userid
812 userdesc = get_attribute_by_id({'jsonblob': pobject,
815 'attr': 'description',
816 'size': number_nodes,