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
139 countnodes({'jsonblob': pobject, 'category': category, 'node': node})
143 def get_id_by_name(args):
144 """ Get an ID by the Name field.
146 Go through the json given, and pull out all ids that are identified
147 by the corresponding name argument.
150 :param jsonblob: a smattering of JSON code to work through
151 :param name: a name to look up in the database of json
152 :param head: will be one of roles, users, domains
153 :param typeval: literal value of either user, role or domain
154 :param size: a count on the number of records to search
157 :returns nodelist: return the first id that has same corresponding name
160 jsonobj = json.loads(str(args['jsonblob']))
162 print "get_id_by_name: json blob not specified:"
168 print "get_id_by_name: name [usr, domain, role] not specified in args"
172 blobkey = args['head']
174 # use an empty key when the arg is not specified. deals with simpler
179 datatype = args['typeval']
181 print "get_id_by_name: need a type arg to process correct name for id"
185 ncount = args['size']
191 # Loop through the records looking for the specified name. When found,
192 # return the corresponding attribute value
194 for i in range(ncount):
195 # build up some 'lookup' keys, call jsonpath with that key
196 bkey1 = '$.' + blobkey + '[' + str(i) + '].name'
197 typename = datatype + 'id'
198 bkey2 = '$.' + blobkey + '[' + str(i) + '].' + typename
200 # find records with same name
201 name_record = jsonobj[blobkey][i]['name']
202 # find corresponding node info, for that name
203 node_record = jsonobj[blobkey][i][typename]
206 if name == name_record:
212 def get_attribute_by_id(args):
213 """ Get an attribute by the id field.
215 Each json record in the json blob has a unique ID, return
216 the corresponding attribute field from that record. Could be
217 description, name, email, password, or any field in available
221 :param jsonblob: a smattering of JSON code to work through
222 :param id: the ID to look up in the database of json
223 :param head: will be one of roles, users, domains
224 :param typeval: literal value of either user, role or domain
225 :param size: a count on the number of records to search
228 :returns name_record: the name attribute value that corresponds
232 jsonobj = json.loads(args['jsonblob'])
234 print "get_attribute_by_id: json blob not specified:"
240 print "get_attribute_by_id: id to look for not specified in parameters"
246 # If caller does not specify a record attribute to return, then
247 # simply default to giving the description of the id you are
252 # will be one of roles, users, domains, or empty to process more
253 # specific grouping of json data
254 blobkey = args['head']
256 # use an empty key when the arg is not specified, allows us to
257 # process chunk of JSON without the outer layer defining roles,
258 # users, domains. (simpler format)
262 datatype = args['typeval']
264 print "get_attribute_by_id: need type arg to process name for id"
270 print "get_attribute_by_id: specify number of records we need"
273 typename = datatype + 'id'
275 # Loop through the records looking for the nodeid, when found, return
276 # the corresponding attribute value
280 for i in range(ncount):
283 name_record = jsonobj[blobkey][i]['name']
284 node_record = jsonobj[blobkey][i][typename]
286 name_record = jsonobj['name']
287 node_record = jsonobj[typename]
289 if nodeid == node_record:
293 def get_role_id_by_rolename(pobject, rolename, number_nodes):
294 """ Helper-func - use get_id_by_name to obtain role-ids for a role-name
298 "description": "a role for admins",
307 :param pobject: JSON blob to work through
308 :param rolename: the name element to search for
309 :param number_nodes: number of records to process
312 :returns roleid: a list of one or more roleid's that match
315 roleid = get_id_by_name({'jsonblob': pobject,
318 'size': number_nodes,
328 def get_role_name_by_roleid(pobject, roleid, number_nodes):
329 """ Helper-func - use get_attribute_by_id to get role-name for a role-id
333 "description": "a role for admins",
342 :param pobject: JSON blob to work through
343 :param roleid: the identifier element to search for
344 :param number_nodes: number of records to process
347 :returns rolename: the role name that corresponds to the record
348 identified by the role-id
350 rolename = get_attribute_by_id({'jsonblob': pobject,
354 'size': number_nodes,
364 def get_role_description_by_roleid(pobject, roleid, number_nodes):
365 """ Helper-func - get role-description for a role-id
369 "description": "a role for admins",
378 :param pobject: JSON blob to work through
379 :param roleid: the identifier element to search for
380 :param number_nodes: number of records to process
383 :returns roledesc: the role description that corresponds to the record
384 identified by the role-id
386 roledesc = get_attribute_by_id({'jsonblob': pobject,
389 'attr': 'description',
390 'size': number_nodes,
400 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
401 """ Helper-func - get all domain-ids corresponding to domain-name
405 "description": "default odl sdn domain",
415 :param pobject: JSON blob to work through
416 :param domainname: the name element to search for
417 :param number_nodes: number of records to process
420 :returns domainid: a list of one or more domain-id's that match
421 the domain-name given
423 domainid = get_id_by_name({'jsonblob': pobject,
426 'size': number_nodes,
427 'typeval': 'domain'})
437 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
438 """ Helper-func - get domain-name for a particular domainid
442 "description": "default odl sdn domain",
452 :param pobject: JSON blob to work through
453 :param domainid: the identifier element to search for
454 :param number_nodes: number of records to process
457 :returns domainname: the domain name that corresponds to the record
458 identified by the domainid
460 domainname = get_attribute_by_id({'jsonblob': pobject,
464 'size': number_nodes,
465 'typeval': 'domain'})
474 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
475 """ Helper-func - get the domaind descripton for a particular domainid
479 "description": "default odl sdn domain",
489 :param pobject: JSON blob to work through
490 :param domainid: the identifier element to search for
491 :param number_nodes: number of records to process
494 :returns domainname: the domain description field that corresponds
495 to the record identified by the domainid
497 domaindesc = get_attribute_by_id({'jsonblob': pobject,
500 'attr': 'description',
501 'size': number_nodes,
502 'typeval': 'domain'})
511 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
512 """ Helper-func - get domain state field for a particular domainid
516 "description": "default odl sdn domain",
526 :param pobject: JSON blob to work through
527 :param domainid: the identifier element to search for
528 :param number_nodes: number of records to process
531 :returns domainstate: the domain state (enabled) field that
532 corresponds to the record identified by the domainid
534 domainstate = get_attribute_by_id({'jsonblob': pobject,
538 'size': number_nodes,
539 'typeval': 'domain'})
548 def get_user_id_by_username(pobject, username, number_nodes):
549 """ Helper-func - get user-ids corresponding to username
553 "description": "admin user",
554 "email": "admin@anydomain.com",
558 "password": "**********",
566 :param pobject: JSON blob to work through
567 :param username: the name element to search for
568 :param number_nodes: number of records to process
571 :returns userid: a list of one or more user-id's that match
574 userid = get_id_by_name({'jsonblob': pobject,
577 'size': number_nodes,
587 def get_user_password_by_userid(pobject, userid, number_nodes):
588 """ Helper-func - get user password field for a particular userid
592 "description": "admin user",
593 "email": "admin@anydomain.com",
597 "password": "**********",
605 :param pobject: JSON blob to work through
606 :param userid: the identifier element to search for
607 :param number_nodes: number of records to process
610 :returns userpassword: the raw password field that corresponds to
611 the record identified by the userid
613 userpassword = get_attribute_by_id({'jsonblob': pobject,
617 'size': number_nodes,
627 def get_user_name_by_userid(pobject, userid, number_nodes):
628 """ Helper-func - get the username field for a particular userid
632 "description": "admin user",
633 "email": "admin@anydomain.com",
637 "password": "**********",
645 :param pobject: JSON blob to work through
646 :param userid: the identifier element to search for
647 :param number_nodes: number of records to process
650 :returns username: the name field that corresponds to the record
651 identified by the userid
653 username = get_attribute_by_id({'jsonblob': pobject,
657 'size': number_nodes,
667 def get_user_state_by_userid(pobject, userid, number_nodes):
668 """ Helper-func - get user state field for a particular userid
672 "description": "admin user",
673 "email": "admin@anydomain.com",
677 "password": "**********",
685 :param pobject: JSON blob to work through
686 :param userid: the identifier element to search for
687 :param number_nodes: number of records to process
690 :returns userstate: the enabled field that corresponds to the record
691 identified by the userid
693 userstate = get_attribute_by_id({'jsonblob': pobject,
697 'size': number_nodes,
707 def get_user_email_by_userid(pobject, userid, number_nodes):
708 """ Helper-func - get user email field for a particular userid
712 "description": "admin user",
713 "email": "admin@anydomain.com",
717 "password": "**********",
725 :param pobject: JSON blob to work through
726 :param userid: the identifier element to search for
727 :param number_nodes: number of records to process
730 :returns useremail: the email field that corresponds to the record
731 identified by the userid
733 useremail = get_attribute_by_id({'jsonblob': pobject,
737 'size': number_nodes,
747 def get_user_description_by_userid(pobject, userid, number_nodes):
748 """ Helper-func - get user description field for a particular userid
752 "description": "admin user",
753 "email": "admin@anydomain.com",
757 "password": "**********",
765 :param pobject: JSON blob to work through
766 :param userid: the identifier element to search for
767 :param number_nodes: number of records to process
770 :returns userdesc: the description field that corresponds to the
771 record identified by the userid
773 userdesc = get_attribute_by_id({'jsonblob': pobject,
776 'attr': 'description',
777 'size': number_nodes,