Give some life to AAA CSIT suite
[integration/test.git] / csit / libraries / AAAJsonUtils.py
1 """
2 AAAJSonUtils library for looking up relevant information in a json record
3
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
7 """
8
9 import json
10 import jsonpath
11
12
13 def countnodes(args):
14     """ Count the number of nodes in a chunk of JSON.
15
16         Because json blobs come in multiple forms, use node, subnode or
17         category to assist in defining what to count.
18
19     Args:
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
24
25     Returns:
26         :returns ctr: the correct number of records have in the json
27     """
28     ctr = 0
29
30     try:
31         jsonobj = json.loads(args['jsonblob'])
32     except KeyError:
33         print "countnodes: json blob to parse not found"
34         raise
35
36     if 'subnode' in args:
37         ctr = len(jsonobj)
38     elif 'category' in args:
39         category_ = args['category'].encode('ascii', 'ignore')
40         ctr = len(jsonobj[category_])
41     else:
42         # working with a single record, short-cut and return count of 1
43         return 1
44     return ctr
45
46
47 def fieldcount(pobject, field):
48     """ Helper-func - use countnodes to count the occurences of field in pobject
49
50         example
51         count the occurences of domainid in this single record...
52
53         [ {
54               "description": "default odl sdn domain",
55               "domainid": 1,
56               "enabled": "true",
57               "name": "MasterTest Domain"
58           } ]
59
60     Args:
61         :param pobject: JSON code to work through
62         :param field: an element to search for and count
63
64     Returns:
65         :returns number_nodes: the correct number of fields you counted
66         in the json
67     """
68     number_nodes = countnodes({'jsonblob': pobject, 'field': field})
69     return number_nodes
70
71
72 def subnodecount(pobject, subnode):
73     """ Helper-func - use countnodes to count subnode in pobject
74
75         example
76         count the occurences of domainid in this json.
77         this chunk lacks nested dictionary keywords (users, domains, roles)...
78
79         {
80               "description": "odl master domain",
81               "domainid": 1,
82               "enabled": "true",
83               "name": "Master Test Domain"
84         }
85         {
86               "description": "sdn user domain",
87               "domainid": 2,
88               "enabled": "true",
89               "name": "User Domain"
90         }
91         ...
92
93     Args:
94         :param pobject: JSON code to work through
95         :param subnode: a subnode, such as domainid, to search for and count
96
97     Returns:
98         :returns number_nodes: the correct number of fields you counted
99         in the json
100     """
101     number_nodes = countnodes({'jsonblob': pobject, 'subnode': subnode})
102     return number_nodes
103
104
105 def nodecount(pobject, category, node):
106     """ Helper-func - use countnodes function to count node of a category type
107
108         example
109         count the domainid in these properly formatted json blobs...
110
111         "domains: [
112             {
113               "description": "odl master domain",
114               "domainid": 1,
115               "enabled": "true",
116               "name": "Master Test Domain"
117             }
118             {
119               "description": "sdn user domain",
120               "domainid": 2,
121               "enabled": "true",
122               "name": "User Domain"
123             }
124             ...
125         ]
126         "users": [
127             ...
128         ]
129
130     Args:
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
134
135     Returns:
136         :returns number_nodes: the correct number of fields you counted
137         in the json
138     """
139     number_nodes = \
140         countnodes({'jsonblob': pobject, 'category': category, 'node': node})
141     return number_nodes
142
143
144 def get_id_by_name(args):
145     """ Get an ID by the Name field.
146
147         Go through the json given, and pull out all ids that are identified
148         by the corresponding name argument.
149
150     Args:
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
156
157     Returns:
158         :returns nodelist: return the first id that has same corresponding name
159     """
160     try:
161         jsonobj = json.loads(str(args['jsonblob']))
162     except KeyError:
163         print "get_id_by_name: json blob not specified:"
164         raise
165
166     try:
167         name = args['name']
168     except KeyError:
169         print "get_id_by_name: name [usr, domain, role] not specified in args"
170         raise
171
172     if 'head' in args:
173         blobkey = args['head']
174     else:
175         # use an empty key when the arg is not specified.  deals with simpler
176         # form
177         blobkey = ''
178
179     try:
180         datatype = args['typeval']
181     except KeyError:
182         print "get_id_by_name: need a type arg to process correct name for id"
183         raise
184
185     try:
186         ncount = args['size']
187     except KeyError:
188         raise
189
190     nodelist = []
191
192     # Loop through the records looking for the specified name.  When found,
193     # return the corresponding attribute value
194     if ncount > 0:
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
200
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]
205
206             try:
207                 if name == name_record:
208                     return node_record
209             except:
210                 raise
211
212
213 def get_attribute_by_id(args):
214     """ Get an attribute by the id field.
215
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
219         in that record.
220
221     Args:
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
227
228     Returns:
229         :returns name_record: the name attribute value that corresponds
230         to the provided id
231     """
232     try:
233         jsonobj = json.loads(args['jsonblob'])
234     except KeyError:
235         print "get_attribute_by_id: json blob not specified:"
236         raise
237
238     try:
239         nodeid = args['id']
240     except KeyError:
241         print "get_attribute_by_id: id to look for not specified in parameters"
242         raise
243
244     if 'attr' in args:
245         attr = args['attr']
246     else:
247         # If caller does not specify a record attribute to return, then
248         # simply default to giving the description of the id you are
249         # searching on
250         attr = 'description'
251
252     if 'head' in args:
253         # will be one of roles, users, domains, or empty to process more
254         # specific grouping of json data
255         blobkey = args['head']
256     else:
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)
260         blobkey = ''
261
262     try:
263         datatype = args['typeval']
264     except KeyError:
265         print "get_attribute_by_id: need type arg to process name for id"
266         raise
267
268     try:
269         size = args['size']
270     except KeyError:
271         print "get_attribute_by_id: specify number of records we need"
272         raise
273
274     typename = datatype + 'id'
275
276     # Loop through the records looking for the nodeid, when found, return
277     # the corresponding attribute value
278
279     ncount = size
280     if ncount > 0:
281         for i in range(ncount):
282
283             try:
284                 name_record = jsonobj[blobkey][i]['name']
285                 node_record = jsonobj[blobkey][i][typename]
286             except:
287                 name_record = jsonobj['name']
288                 node_record = jsonobj[typename]
289
290             if nodeid == node_record:
291                 return name_record
292
293
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
296
297         sample record...
298         "roles": [ {
299               "description": "a role for admins",
300               "name": "admin",
301               "roleid": 1
302           }
303           {
304               ...
305           } ]
306
307     Args:
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
311
312     Returns:
313         :returns roleid:  a list of one or more roleid's that match
314         the rolename given
315     """
316     roleid = get_id_by_name({'jsonblob': pobject,
317                              'name': rolename,
318                              'head': 'roles',
319                              'size': number_nodes,
320                              'typeval': 'role'})
321     try:
322         roleid
323     except:
324         raise
325     else:
326         return roleid
327
328
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
331
332         sample record...
333         "roles": [ {
334               "description": "a role for admins",
335               "name": "admin",
336               "roleid": 1
337           }
338           {
339               ...
340           } ]
341
342     Args:
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
346
347     Returns:
348         :returns rolename:  the role name that corresponds to the record
349         identified by the role-id
350     """
351     rolename = get_attribute_by_id({'jsonblob': pobject,
352                                     'head': 'roles',
353                                     'id': roleid,
354                                     'attr': 'name',
355                                     'size': number_nodes,
356                                     'typeval': 'role'})
357     try:
358         rolename
359     except:
360         raise
361     else:
362         return rolename
363
364
365 def get_role_description_by_roleid(pobject, roleid, number_nodes):
366     """ Helper-func - get role-description for a role-id
367
368         sample record...
369         "roles": [ {
370               "description": "a role for admins",
371               "name": "admin",
372               "roleid": 1
373           }
374           {
375               ...
376           } ]
377
378     Args:
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
382
383     Returns:
384         :returns roledesc:  the role description that corresponds to the record
385         identified by the role-id
386     """
387     roledesc = get_attribute_by_id({'jsonblob': pobject,
388                                     'head': 'roles',
389                                     'id': roleid,
390                                     'attr': 'description',
391                                     'size': number_nodes,
392                                     'typeval': 'role'})
393     try:
394         roledesc
395     except:
396         raise
397     else:
398         return roledesc
399
400
401 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
402     """ Helper-func - get all domain-ids corresponding to domain-name
403
404         sample record...
405         "domains": [ {
406               "description": "default odl sdn domain",
407               "domainid": 1,
408               "enabled": true,
409               "name": "admin"
410           }
411           {
412               ...
413           } ]
414
415     Args:
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
419
420     Returns:
421         :returns domainid:  a list of one or more domain-id's that match
422         the domain-name given
423     """
424     domainid = get_id_by_name({'jsonblob': pobject,
425                                'head': 'domains',
426                                'name': domainname,
427                                'size': number_nodes,
428                                'typeval': 'domain'})
429
430     try:
431         domainid
432     except:
433         raise
434     else:
435         return domainid
436
437
438 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
439     """ Helper-func - get domain-name for a particular domainid
440
441         sample record...
442         "domains": [ {
443               "description": "default odl sdn domain",
444               "domainid": 1,
445               "enabled": true,
446               "name": "admin"
447           }
448           {
449               ...
450           } ]
451
452     Args:
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
456
457     Returns:
458         :returns domainname:  the domain name that corresponds to the record
459         identified by the domainid
460     """
461     domainname = get_attribute_by_id({'jsonblob': pobject,
462                                       'head': 'domains',
463                                       'id': domainid,
464                                       'attr': 'name',
465                                       'size': number_nodes,
466                                       'typeval': 'domain'})
467     try:
468         domainname
469     except:
470         raise
471     else:
472         return domainname
473
474
475 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
476     """ Helper-func - get the domaind descripton for a particular domainid
477
478         sample record...
479         "domains": [ {
480               "description": "default odl sdn domain",
481               "domainid": 1,
482               "enabled": true,
483               "name": "admin"
484           }
485           {
486               ...
487           } ]
488
489     Args:
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
493
494     Returns:
495         :returns domainname:  the domain description field that corresponds
496         to the record identified by the domainid
497     """
498     domaindesc = get_attribute_by_id({'jsonblob': pobject,
499                                       'head': 'domains',
500                                       'id': domainid,
501                                       'attr': 'description',
502                                       'size': number_nodes,
503                                       'typeval': 'domain'})
504     try:
505         domaindesc
506     except:
507         raise
508     else:
509         return domaindesc
510
511
512 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
513     """ Helper-func - get domain state field  for a particular domainid
514
515         sample record...
516         "domains": [ {
517               "description": "default odl sdn domain",
518               "domainid": 1,
519               "enabled": true,
520               "name": "admin"
521           }
522           {
523               ...
524           } ]
525
526     Args:
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
530
531     Returns:
532         :returns domainstate:  the domain state (enabled) field that
533         corresponds to the record identified by the domainid
534     """
535     domainstate = get_attribute_by_id({'jsonblob': pobject,
536                                        'head': 'domains',
537                                        'id': domainid,
538                                        'attr': 'enabled',
539                                        'size': number_nodes,
540                                        'typeval': 'domain'})
541     try:
542         domainstate
543     except:
544         raise
545     else:
546         return domainstate
547
548
549 def get_user_id_by_username(pobject, username, number_nodes):
550     """ Helper-func - get user-ids corresponding to username
551
552         sample record...
553         "users": [ {
554               "description": "admin user",
555               "email": "admin@anydomain.com",
556               "enabled": true,
557               "userid": 1,
558               "name": "admin",
559               "password": "**********",
560               "userid": 1
561           }
562           {
563               ...
564           } ]
565
566     Args:
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
570
571     Returns:
572         :returns userid:  a list of one or more user-id's that match
573         the username given
574     """
575     userid = get_id_by_name({'jsonblob': pobject,
576                              'name': username,
577                              'head': 'users',
578                              'size': number_nodes,
579                              'typeval': 'user'})
580     try:
581         userid
582     except:
583         raise
584     else:
585         return userid
586
587
588 def get_user_password_by_userid(pobject, userid, number_nodes):
589     """ Helper-func - get user password field for a particular userid
590
591         sample record...
592         "users": [ {
593               "description": "admin user",
594               "email": "admin@anydomain.com",
595               "enabled": true,
596               "userid": 1,
597               "name": "admin",
598               "password": "**********",
599               "userid": 1
600           }
601           {
602               ...
603           } ]
604
605     Args:
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
609
610     Returns:
611         :returns userpassword:  the raw password field that corresponds to
612          the record identified by the userid
613     """
614     userpassword = get_attribute_by_id({'jsonblob': pobject,
615                                         'head': 'users',
616                                         'id': userid,
617                                         'attr': 'password',
618                                         'size': number_nodes,
619                                         'typeval': 'user'})
620     try:
621         userpassword
622     except:
623         raise
624     else:
625         return userpassword
626
627
628 def get_user_name_by_userid(pobject, userid, number_nodes):
629     """ Helper-func - get the username field for a particular userid
630
631         sample record...
632         "users": [ {
633               "description": "admin user",
634               "email": "admin@anydomain.com",
635               "enabled": true,
636               "userid": 1,
637               "name": "admin",
638               "password": "**********",
639               "userid": 1
640           }
641           {
642               ...
643           } ]
644
645     Args:
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
649
650     Returns:
651         :returns username:  the name field that corresponds to the record
652         identified by the userid
653     """
654     username = get_attribute_by_id({'jsonblob': pobject,
655                                     'head': 'users',
656                                     'id': userid,
657                                     'attr': 'name',
658                                     'size': number_nodes,
659                                     'typeval': 'user'})
660     try:
661         username
662     except:
663         raise
664     else:
665         return username
666
667
668 def get_user_state_by_userid(pobject, userid, number_nodes):
669     """ Helper-func - get user state field for a particular userid
670
671         sample record...
672         "users": [ {
673               "description": "admin user",
674               "email": "admin@anydomain.com",
675               "enabled": true,
676               "userid": 1,
677               "name": "admin",
678               "password": "**********",
679               "userid": 1
680           }
681           {
682               ...
683           } ]
684
685     Args:
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
689
690     Returns:
691         :returns userstate:  the enabled field that corresponds to the record
692         identified by the userid
693     """
694     userstate = get_attribute_by_id({'jsonblob': pobject,
695                                      'head': 'users',
696                                      'id': userid,
697                                      'attr': 'enabled',
698                                      'size': number_nodes,
699                                      'typeval': 'user'})
700     try:
701         userstate
702     except:
703         raise
704     else:
705         return userstate
706
707
708 def get_user_email_by_userid(pobject, userid, number_nodes):
709     """ Helper-func - get user email field for a particular userid
710
711         sample record...
712         "users": [ {
713               "description": "admin user",
714               "email": "admin@anydomain.com",
715               "enabled": true,
716               "userid": 1,
717               "name": "admin",
718               "password": "**********",
719               "userid": 1
720           }
721           {
722               ...
723           } ]
724
725     Args:
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
729
730     Returns:
731         :returns useremail:  the email field that corresponds to the record
732         identified by the userid
733     """
734     useremail = get_attribute_by_id({'jsonblob': pobject,
735                                      'head': 'users',
736                                      'id': userid,
737                                      'attr': 'email',
738                                      'size': number_nodes,
739                                      'typeval': 'user'})
740     try:
741         useremail
742     except:
743         raise
744     else:
745         return useremail
746
747
748 def get_user_description_by_userid(pobject, userid, number_nodes):
749     """ Helper-func - get user description field for a particular userid
750
751         sample record...
752         "users": [ {
753               "description": "admin user",
754               "email": "admin@anydomain.com",
755               "enabled": true,
756               "userid": 1,
757               "name": "admin",
758               "password": "**********",
759               "userid": 1
760           }
761           {
762               ...
763           } ]
764
765     Args:
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
769
770     Returns:
771         :returns userdesc:  the description field that corresponds to the
772         record identified by the userid
773     """
774     userdesc = get_attribute_by_id({'jsonblob': pobject,
775                                     'head': 'users',
776                                     'id': userid,
777                                     'attr': 'description',
778                                     'size': number_nodes,
779                                     'typeval': 'user'})
780     try:
781         userdesc
782     except:
783         raise
784     else:
785         return userdesc