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