Fix a 'memeber' typo
[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 = countnodes({"jsonblob": pobject, "category": category, "node": node})
139     return number_nodes
140
141
142 def get_id_by_name(args):
143     """Get an ID by the Name field.
144
145         Go through the json given, and pull out all ids that are identified
146         by the corresponding name argument.
147
148     Args:
149         :param jsonblob: a smattering of JSON code to work through
150         :param name: a name to look up in the database of json
151         :param head: will be one of roles, users, domains
152         :param typeval: literal value of either user, role or domain
153         :param size: a count on the number of records to search
154
155     Returns:
156         :returns nodelist: return the first id that has same corresponding name
157     """
158     try:
159         jsonobj = json.loads(str(args["jsonblob"]))
160     except KeyError:
161         print("get_id_by_name: json blob not specified:")
162         raise
163
164     try:
165         name = args["name"]
166     except KeyError:
167         print("get_id_by_name: name [usr, domain, role] not specified in args")
168         raise
169
170     if "head" in args:
171         blobkey = args["head"]
172     else:
173         # use an empty key when the arg is not specified.  deals with simpler
174         # form
175         blobkey = ""
176
177     try:
178         datatype = args["typeval"]
179     except KeyError:
180         print("get_id_by_name: need a type arg to process correct name for id")
181         raise
182
183     try:
184         ncount = args["size"]
185     except KeyError:
186         raise
187
188     nodelist = []
189
190     # Loop through the records looking for the specified name.  When found,
191     # return the corresponding attribute value
192     if ncount > 0:
193         for i in range(ncount):
194             # build up some 'lookup' keys, call jsonpath with that key
195             bkey1 = "$." + blobkey + "[" + str(i) + "].name"
196             typename = datatype + "id"
197             bkey2 = "$." + blobkey + "[" + str(i) + "]." + typename
198
199             # find records with same name
200             name_record = jsonobj[blobkey][i]["name"]
201             # find corresponding node info, for that name
202             node_record = jsonobj[blobkey][i][typename]
203
204             try:
205                 if name == name_record:
206                     return node_record
207             except Exception:
208                 raise
209
210
211 def get_attribute_by_id(args):
212     """Get an attribute by the id field.
213
214         Each json record in the json blob has a unique ID, return
215         the corresponding attribute field from that record.  Could be
216         description, name, email, password, or any field in available
217         in that record.
218
219     Args:
220         :param jsonblob: a smattering of JSON code to work through
221         :param id: the ID to look up in the database of json
222         :param head: will be one of roles, users, domains
223         :param typeval: literal value of either user, role or domain
224         :param size: a count on the number of records to search
225
226     Returns:
227         :returns name_record: the name attribute value that corresponds
228         to the provided id
229     """
230     try:
231         jsonobj = json.loads(args["jsonblob"])
232     except KeyError:
233         print("get_attribute_by_id: json blob not specified:")
234         raise
235
236     try:
237         nodeid = args["id"]
238     except KeyError:
239         print("get_attribute_by_id: id to look for not specified in parameters")
240         raise
241
242     if "attr" in args:
243         attr = args["attr"]
244     else:
245         # If caller does not specify a record attribute to return, then
246         # simply default to giving the description of the id you are
247         # searching on
248         attr = "description"
249
250     if "head" in args:
251         # will be one of roles, users, domains, or empty to process more
252         # specific grouping of json data
253         blobkey = args["head"]
254     else:
255         # use an empty key when the arg is not specified, allows us to
256         # process chunk of JSON without the outer layer defining roles,
257         # users, domains. (simpler format)
258         blobkey = ""
259
260     try:
261         datatype = args["typeval"]
262     except KeyError:
263         print("get_attribute_by_id: need type arg to process name for id")
264         raise
265
266     try:
267         size = args["size"]
268     except KeyError:
269         print("get_attribute_by_id: specify number of records we need")
270         raise
271
272     typename = datatype + "id"
273
274     # Loop through the records looking for the nodeid, when found, return
275     # the corresponding attribute value
276
277     ncount = size
278     if ncount > 0:
279         for i in range(ncount):
280
281             try:
282                 name_record = jsonobj[blobkey][i]["name"]
283                 node_record = jsonobj[blobkey][i][typename]
284             except Exception:
285                 name_record = jsonobj["name"]
286                 node_record = jsonobj[typename]
287
288             if nodeid == node_record:
289                 return name_record
290
291
292 def get_role_id_by_rolename(pobject, rolename, number_nodes):
293     """Helper-func - use get_id_by_name to obtain role-ids for a role-name
294
295         sample record...
296         "roles": [ {
297               "description": "a role for admins",
298               "name": "admin",
299               "roleid": 1
300           }
301           {
302               ...
303           } ]
304
305     Args:
306         :param pobject: JSON blob to work through
307         :param rolename: the name element to search for
308         :param number_nodes: number of records to process
309
310     Returns:
311         :returns roleid:  a list of one or more roleid's that match
312         the rolename given
313     """
314     roleid = get_id_by_name(
315         {
316             "jsonblob": pobject,
317             "name": rolename,
318             "head": "roles",
319             "size": number_nodes,
320             "typeval": "role",
321         }
322     )
323     try:
324         roleid
325     except Exception:
326         raise
327     else:
328         return roleid
329
330
331 def get_role_name_by_roleid(pobject, roleid, number_nodes):
332     """Helper-func - use get_attribute_by_id to get role-name for a role-id
333
334         sample record...
335         "roles": [ {
336               "description": "a role for admins",
337               "name": "admin",
338               "roleid": 1
339           }
340           {
341               ...
342           } ]
343
344     Args:
345         :param pobject: JSON blob to work through
346         :param roleid: the identifier element to search for
347         :param number_nodes: number of records to process
348
349     Returns:
350         :returns rolename:  the role name that corresponds to the record
351         identified by the role-id
352     """
353     rolename = get_attribute_by_id(
354         {
355             "jsonblob": pobject,
356             "head": "roles",
357             "id": roleid,
358             "attr": "name",
359             "size": number_nodes,
360             "typeval": "role",
361         }
362     )
363     try:
364         rolename
365     except Exception:
366         raise
367     else:
368         return rolename
369
370
371 def get_role_description_by_roleid(pobject, roleid, number_nodes):
372     """Helper-func - get role-description for a role-id
373
374         sample record...
375         "roles": [ {
376               "description": "a role for admins",
377               "name": "admin",
378               "roleid": 1
379           }
380           {
381               ...
382           } ]
383
384     Args:
385         :param pobject: JSON blob to work through
386         :param roleid: the identifier element to search for
387         :param number_nodes: number of records to process
388
389     Returns:
390         :returns roledesc:  the role description that corresponds to the record
391         identified by the role-id
392     """
393     roledesc = get_attribute_by_id(
394         {
395             "jsonblob": pobject,
396             "head": "roles",
397             "id": roleid,
398             "attr": "description",
399             "size": number_nodes,
400             "typeval": "role",
401         }
402     )
403     try:
404         roledesc
405     except Exception:
406         raise
407     else:
408         return roledesc
409
410
411 def get_domain_id_by_domainname(pobject, domainname, number_nodes):
412     """Helper-func - get all domain-ids corresponding to domain-name
413
414         sample record...
415         "domains": [ {
416               "description": "default odl sdn domain",
417               "domainid": 1,
418               "enabled": true,
419               "name": "admin"
420           }
421           {
422               ...
423           } ]
424
425     Args:
426         :param pobject: JSON blob to work through
427         :param domainname: the name element to search for
428         :param number_nodes: number of records to process
429
430     Returns:
431         :returns domainid:  a list of one or more domain-id's that match
432         the domain-name given
433     """
434     domainid = get_id_by_name(
435         {
436             "jsonblob": pobject,
437             "head": "domains",
438             "name": domainname,
439             "size": number_nodes,
440             "typeval": "domain",
441         }
442     )
443
444     try:
445         domainid
446     except Exception:
447         raise
448     else:
449         return domainid
450
451
452 def get_domain_name_by_domainid(pobject, domainid, number_nodes):
453     """Helper-func - get domain-name for a particular domainid
454
455         sample record...
456         "domains": [ {
457               "description": "default odl sdn domain",
458               "domainid": 1,
459               "enabled": true,
460               "name": "admin"
461           }
462           {
463               ...
464           } ]
465
466     Args:
467         :param pobject: JSON blob to work through
468         :param domainid: the identifier element to search for
469         :param number_nodes: number of records to process
470
471     Returns:
472         :returns domainname:  the domain name that corresponds to the record
473         identified by the domainid
474     """
475     domainname = get_attribute_by_id(
476         {
477             "jsonblob": pobject,
478             "head": "domains",
479             "id": domainid,
480             "attr": "name",
481             "size": number_nodes,
482             "typeval": "domain",
483         }
484     )
485     try:
486         domainname
487     except Exception:
488         raise
489     else:
490         return domainname
491
492
493 def get_domain_description_by_domainid(pobject, domainid, number_nodes):
494     """Helper-func - get the domaind descripton for a particular domainid
495
496         sample record...
497         "domains": [ {
498               "description": "default odl sdn domain",
499               "domainid": 1,
500               "enabled": true,
501               "name": "admin"
502           }
503           {
504               ...
505           } ]
506
507     Args:
508         :param pobject: JSON blob to work through
509         :param domainid: the identifier element to search for
510         :param number_nodes: number of records to process
511
512     Returns:
513         :returns domainname:  the domain description field that corresponds
514         to the record identified by the domainid
515     """
516     domaindesc = get_attribute_by_id(
517         {
518             "jsonblob": pobject,
519             "head": "domains",
520             "id": domainid,
521             "attr": "description",
522             "size": number_nodes,
523             "typeval": "domain",
524         }
525     )
526     try:
527         domaindesc
528     except Exception:
529         raise
530     else:
531         return domaindesc
532
533
534 def get_domain_state_by_domainid(pobject, domainid, number_nodes):
535     """Helper-func - get domain state field  for a particular domainid
536
537         sample record...
538         "domains": [ {
539               "description": "default odl sdn domain",
540               "domainid": 1,
541               "enabled": true,
542               "name": "admin"
543           }
544           {
545               ...
546           } ]
547
548     Args:
549         :param pobject: JSON blob to work through
550         :param domainid: the identifier element to search for
551         :param number_nodes: number of records to process
552
553     Returns:
554         :returns domainstate:  the domain state (enabled) field that
555         corresponds to the record identified by the domainid
556     """
557     domainstate = get_attribute_by_id(
558         {
559             "jsonblob": pobject,
560             "head": "domains",
561             "id": domainid,
562             "attr": "enabled",
563             "size": number_nodes,
564             "typeval": "domain",
565         }
566     )
567     try:
568         domainstate
569     except Exception:
570         raise
571     else:
572         return domainstate
573
574
575 def get_user_id_by_username(pobject, username, number_nodes):
576     """Helper-func - get user-ids corresponding to username
577
578         sample record...
579         "users": [ {
580               "description": "admin user",
581               "email": "admin@anydomain.com",
582               "enabled": true,
583               "userid": 1,
584               "name": "admin",
585               "password": "**********",
586               "userid": 1
587           }
588           {
589               ...
590           } ]
591
592     Args:
593         :param pobject: JSON blob to work through
594         :param username: the name element to search for
595         :param number_nodes: number of records to process
596
597     Returns:
598         :returns userid:  a list of one or more user-id's that match
599         the username given
600     """
601     userid = get_id_by_name(
602         {
603             "jsonblob": pobject,
604             "name": username,
605             "head": "users",
606             "size": number_nodes,
607             "typeval": "user",
608         }
609     )
610     try:
611         userid
612     except Exception:
613         raise
614     else:
615         return userid
616
617
618 def get_user_password_by_userid(pobject, userid, number_nodes):
619     """Helper-func - get user password field for a particular userid
620
621         sample record...
622         "users": [ {
623               "description": "admin user",
624               "email": "admin@anydomain.com",
625               "enabled": true,
626               "userid": 1,
627               "name": "admin",
628               "password": "**********",
629               "userid": 1
630           }
631           {
632               ...
633           } ]
634
635     Args:
636         :param pobject: JSON blob to work through
637         :param userid: the identifier element to search for
638         :param number_nodes: number of records to process
639
640     Returns:
641         :returns userpassword:  the raw password field that corresponds to
642          the record identified by the userid
643     """
644     userpassword = get_attribute_by_id(
645         {
646             "jsonblob": pobject,
647             "head": "users",
648             "id": userid,
649             "attr": "password",
650             "size": number_nodes,
651             "typeval": "user",
652         }
653     )
654     try:
655         userpassword
656     except Exception:
657         raise
658     else:
659         return userpassword
660
661
662 def get_user_name_by_userid(pobject, userid, number_nodes):
663     """Helper-func - get the username field for a particular userid
664
665         sample record...
666         "users": [ {
667               "description": "admin user",
668               "email": "admin@anydomain.com",
669               "enabled": true,
670               "userid": 1,
671               "name": "admin",
672               "password": "**********",
673               "userid": 1
674           }
675           {
676               ...
677           } ]
678
679     Args:
680         :param pobject: JSON blob to work through
681         :param userid: the identifier element to search for
682         :param number_nodes: number of records to process
683
684     Returns:
685         :returns username:  the name field that corresponds to the record
686         identified by the userid
687     """
688     username = get_attribute_by_id(
689         {
690             "jsonblob": pobject,
691             "head": "users",
692             "id": userid,
693             "attr": "name",
694             "size": number_nodes,
695             "typeval": "user",
696         }
697     )
698     try:
699         username
700     except Exception:
701         raise
702     else:
703         return username
704
705
706 def get_user_state_by_userid(pobject, userid, number_nodes):
707     """Helper-func - get user state field for a particular userid
708
709         sample record...
710         "users": [ {
711               "description": "admin user",
712               "email": "admin@anydomain.com",
713               "enabled": true,
714               "userid": 1,
715               "name": "admin",
716               "password": "**********",
717               "userid": 1
718           }
719           {
720               ...
721           } ]
722
723     Args:
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
727
728     Returns:
729         :returns userstate:  the enabled field that corresponds to the record
730         identified by the userid
731     """
732     userstate = get_attribute_by_id(
733         {
734             "jsonblob": pobject,
735             "head": "users",
736             "id": userid,
737             "attr": "enabled",
738             "size": number_nodes,
739             "typeval": "user",
740         }
741     )
742     try:
743         userstate
744     except Exception:
745         raise
746     else:
747         return userstate
748
749
750 def get_user_email_by_userid(pobject, userid, number_nodes):
751     """Helper-func - get user email field for a particular userid
752
753         sample record...
754         "users": [ {
755               "description": "admin user",
756               "email": "admin@anydomain.com",
757               "enabled": true,
758               "userid": 1,
759               "name": "admin",
760               "password": "**********",
761               "userid": 1
762           }
763           {
764               ...
765           } ]
766
767     Args:
768         :param pobject: JSON blob to work through
769         :param userid: the identifier element to search for
770         :param number_nodes: number of records to process
771
772     Returns:
773         :returns useremail:  the email field that corresponds to the record
774         identified by the userid
775     """
776     useremail = get_attribute_by_id(
777         {
778             "jsonblob": pobject,
779             "head": "users",
780             "id": userid,
781             "attr": "email",
782             "size": number_nodes,
783             "typeval": "user",
784         }
785     )
786     try:
787         useremail
788     except Exception:
789         raise
790     else:
791         return useremail
792
793
794 def get_user_description_by_userid(pobject, userid, number_nodes):
795     """Helper-func - get user description field for a particular userid
796
797         sample record...
798         "users": [ {
799               "description": "admin user",
800               "email": "admin@anydomain.com",
801               "enabled": true,
802               "userid": 1,
803               "name": "admin",
804               "password": "**********",
805               "userid": 1
806           }
807           {
808               ...
809           } ]
810
811     Args:
812         :param pobject: JSON blob to work through
813         :param userid: the identifier element to search for
814         :param number_nodes: number of records to process
815
816     Returns:
817         :returns userdesc:  the description field that corresponds to the
818         record identified by the userid
819     """
820     userdesc = get_attribute_by_id(
821         {
822             "jsonblob": pobject,
823             "head": "users",
824             "id": userid,
825             "attr": "description",
826             "size": number_nodes,
827             "typeval": "user",
828         }
829     )
830     try:
831         userdesc
832     except Exception:
833         raise
834     else:
835         return userdesc