X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=csit%2Flibraries%2FAAAJsonUtils.py;h=9d4a93a7338165d59c4e027d2eb93b096bd33e34;hb=97e57d489bfc88db0a70fcb04f145c4737d76b75;hp=35994589febc893243696e942374ad620b7b8712;hpb=59e81c38620fa1b61e15771191e35771450b9499;p=integration%2Ftest.git diff --git a/csit/libraries/AAAJsonUtils.py b/csit/libraries/AAAJsonUtils.py index 35994589fe..9d4a93a733 100644 --- a/csit/libraries/AAAJsonUtils.py +++ b/csit/libraries/AAAJsonUtils.py @@ -7,11 +7,10 @@ Author: Carmen Kelling - HP Enterprise """ import json -import jsonpath def countnodes(args): - """ Count the number of nodes in a chunk of JSON. + """Count the number of nodes in a chunk of JSON. Because json blobs come in multiple forms, use node, subnode or category to assist in defining what to count. @@ -28,15 +27,15 @@ def countnodes(args): ctr = 0 try: - jsonobj = json.loads(args['jsonblob']) + jsonobj = json.loads(args["jsonblob"]) except KeyError: - print "countnodes: json blob to parse not found" + print("countnodes: json blob to parse not found") raise - if 'subnode' in args: + if "subnode" in args: ctr = len(jsonobj) - elif 'category' in args: - category_ = args['category'].encode('ascii', 'ignore') + elif "category" in args: + category_ = args["category"].encode("ascii", "ignore") ctr = len(jsonobj[category_]) else: # working with a single record, short-cut and return count of 1 @@ -45,7 +44,7 @@ def countnodes(args): def fieldcount(pobject, field): - """ Helper-func - use countnodes to count the occurences of field in pobject + """Helper-func - use countnodes to count the occurences of field in pobject example count the occurences of domainid in this single record... @@ -65,12 +64,12 @@ def fieldcount(pobject, field): :returns number_nodes: the correct number of fields you counted in the json """ - number_nodes = countnodes({'jsonblob': pobject, 'field': field}) + number_nodes = countnodes({"jsonblob": pobject, "field": field}) return number_nodes def subnodecount(pobject, subnode): - """ Helper-func - use countnodes to count subnode in pobject + """Helper-func - use countnodes to count subnode in pobject example count the occurences of domainid in this json. @@ -98,12 +97,12 @@ def subnodecount(pobject, subnode): :returns number_nodes: the correct number of fields you counted in the json """ - number_nodes = countnodes({'jsonblob': pobject, 'subnode': subnode}) + number_nodes = countnodes({"jsonblob": pobject, "subnode": subnode}) return number_nodes def nodecount(pobject, category, node): - """ Helper-func - use countnodes function to count node of a category type + """Helper-func - use countnodes function to count node of a category type example count the domainid in these properly formatted json blobs... @@ -136,13 +135,12 @@ def nodecount(pobject, category, node): :returns number_nodes: the correct number of fields you counted in the json """ - number_nodes = \ - countnodes({'jsonblob': pobject, 'category': category, 'node': node}) + number_nodes = countnodes({"jsonblob": pobject, "category": category, "node": node}) return number_nodes def get_id_by_name(args): - """ Get an ID by the Name field. + """Get an ID by the Name field. Go through the json given, and pull out all ids that are identified by the corresponding name argument. @@ -158,32 +156,32 @@ def get_id_by_name(args): :returns nodelist: return the first id that has same corresponding name """ try: - jsonobj = json.loads(args['jsonblob']) + jsonobj = json.loads(str(args["jsonblob"])) except KeyError: - print "get_id_by_name: json blob not specified:" + print("get_id_by_name: json blob not specified:") raise try: - name = args['name'] + name = args["name"] except KeyError: - print "get_id_by_name: name [usr, domain, role] not specified in args" + print("get_id_by_name: name [usr, domain, role] not specified in args") raise - if 'head' in args: - blobkey = args['head'] + if "head" in args: + blobkey = args["head"] else: # use an empty key when the arg is not specified. deals with simpler # form - blobkey = '' + blobkey = "" try: - datatype = args['typeval'] + datatype = args["typeval"] except KeyError: - print "get_id_by_name: need a type arg to process correct name for id" + print("get_id_by_name: need a type arg to process correct name for id") raise try: - ncount = args['size'] + ncount = args["size"] except KeyError: raise @@ -194,43 +192,24 @@ def get_id_by_name(args): if ncount > 0: for i in range(ncount): # build up some 'lookup' keys, call jsonpath with that key - bkey1 = '$.' + blobkey + '[' + str(i) + '].name' - typename = datatype + 'id' - bkey2 = '$.' + blobkey + '[' + str(i) + '].' + typename + bkey1 = "$." + blobkey + "[" + str(i) + "].name" + typename = datatype + "id" + bkey2 = "$." + blobkey + "[" + str(i) + "]." + typename # find records with same name - name_record = jsonpath.jsonpath(jsonobj, bkey1) + name_record = jsonobj[blobkey][i]["name"] # find corresponding node info, for that name - node_record = jsonpath.jsonpath(jsonobj, bkey2) + node_record = jsonobj[blobkey][i][typename] - # build up an alternative set of keys. This lets you deal with - # other format of json - bkey3 = '$.' + blobkey + '.name' - typename2 = datatype + 'id' - bkey4 = '$.' + blobkey + '.' + typename2 - - # find records with same name - altname_record = jsonpath.jsonpath(jsonobj, bkey3) - # find corresponding record node info, for that name - altnode_record = jsonpath.jsonpath(jsonobj, bkey4) try: - if name in list(name_record): - nodelist.append(node_record.pop()) - except: - try: - if name in list(altname_record): - nodelist.append(altnode_record.pop()) - except: - raise - - try: - return nodelist.pop() - except LookupError: - raise + if name == name_record: + return node_record + except Exception: + raise def get_attribute_by_id(args): - """ Get an attribute by the id field. + """Get an attribute by the id field. Each json record in the json blob has a unique ID, return the corresponding attribute field from that record. Could be @@ -249,89 +228,69 @@ def get_attribute_by_id(args): to the provided id """ try: - jsonobj = json.loads(args['jsonblob']) + jsonobj = json.loads(args["jsonblob"]) except KeyError: - print "get_attribute_by_id: json blob not specified:" + print("get_attribute_by_id: json blob not specified:") raise try: - nodeid = args['id'] + nodeid = args["id"] except KeyError: - print "get_attribute_by_id: id to look for not specified in parameters" + print("get_attribute_by_id: id to look for not specified in parameters") raise - if 'attr' in args: - attr = args['attr'] + if "attr" in args: + attr = args["attr"] else: # If caller does not specify a record attribute to return, then # simply default to giving the description of the id you are # searching on - attr = 'description' + attr = "description" - if 'head' in args: + if "head" in args: # will be one of roles, users, domains, or empty to process more # specific grouping of json data - blobkey = args['head'] + blobkey = args["head"] else: # use an empty key when the arg is not specified, allows us to # process chunk of JSON without the outer layer defining roles, # users, domains. (simpler format) - blobkey = '' + blobkey = "" try: - datatype = args['typeval'] + datatype = args["typeval"] except KeyError: - print "get_attribute_by_id: need type arg to process name for id" + print("get_attribute_by_id: need type arg to process name for id") raise try: - size = args['size'] + size = args["size"] except KeyError: - print "get_attribute_by_id: specify number of records we need" + print("get_attribute_by_id: specify number of records we need") raise + typename = datatype + "id" + # Loop through the records looking for the nodeid, when found, return # the corresponding attribute value ncount = size if ncount > 0: for i in range(ncount): - bkey1 = '$.' + blobkey + '[' + str(i) + '].' + attr - bkey2 = '$.' + blobkey + '[' + str(i) + '].' + datatype + 'id' - - bkey3 = '$.' + blobkey + '.' + attr - bkey4 = '$.' + blobkey + '.' + datatype + 'id' - - name_record = jsonpath.jsonpath(jsonobj, bkey1) - node_record = jsonpath.jsonpath(jsonobj, bkey2) - altname_record = jsonpath.jsonpath(jsonobj, bkey3) - altnode_record = jsonpath.jsonpath(jsonobj, bkey4) - - if type(node_record) is list: - if nodeid in list(node_record): - return name_record.pop() - else: - try: - node_record - except: - print "not in list" - else: - return name_record - - if type(altnode_record) is list: - if nodeid in list(altnode_record): - return altname_record.pop() - else: - try: - altnode_record - except: - print "not in list" - else: - return altname_record + + try: + name_record = jsonobj[blobkey][i]["name"] + node_record = jsonobj[blobkey][i][typename] + except Exception: + name_record = jsonobj["name"] + node_record = jsonobj[typename] + + if nodeid == node_record: + return name_record def get_role_id_by_rolename(pobject, rolename, number_nodes): - """ Helper-func - use get_id_by_name to obtain role-ids for a role-name + """Helper-func - use get_id_by_name to obtain role-ids for a role-name sample record... "roles": [ { @@ -352,21 +311,25 @@ def get_role_id_by_rolename(pobject, rolename, number_nodes): :returns roleid: a list of one or more roleid's that match the rolename given """ - roleid = get_id_by_name({'jsonblob': pobject, - 'name': rolename, - 'head': 'roles', - 'size': number_nodes, - 'typeval': 'role'}) + roleid = get_id_by_name( + { + "jsonblob": pobject, + "name": rolename, + "head": "roles", + "size": number_nodes, + "typeval": "role", + } + ) try: roleid - except: + except Exception: raise else: return roleid def get_role_name_by_roleid(pobject, roleid, number_nodes): - """ Helper-func - use get_attribute_by_id to get role-name for a role-id + """Helper-func - use get_attribute_by_id to get role-name for a role-id sample record... "roles": [ { @@ -387,22 +350,26 @@ def get_role_name_by_roleid(pobject, roleid, number_nodes): :returns rolename: the role name that corresponds to the record identified by the role-id """ - rolename = get_attribute_by_id({'jsonblob': pobject, - 'head': 'roles', - 'id': roleid, - 'attr': 'name', - 'size': number_nodes, - 'typeval': 'role'}) + rolename = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "roles", + "id": roleid, + "attr": "name", + "size": number_nodes, + "typeval": "role", + } + ) try: rolename - except: + except Exception: raise else: return rolename def get_role_description_by_roleid(pobject, roleid, number_nodes): - """ Helper-func - get role-description for a role-id + """Helper-func - get role-description for a role-id sample record... "roles": [ { @@ -423,22 +390,26 @@ def get_role_description_by_roleid(pobject, roleid, number_nodes): :returns roledesc: the role description that corresponds to the record identified by the role-id """ - roledesc = get_attribute_by_id({'jsonblob': pobject, - 'head': 'roles', - 'id': roleid, - 'attr': 'description', - 'size': number_nodes, - 'typeval': 'role'}) + roledesc = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "roles", + "id": roleid, + "attr": "description", + "size": number_nodes, + "typeval": "role", + } + ) try: roledesc - except: + except Exception: raise else: return roledesc def get_domain_id_by_domainname(pobject, domainname, number_nodes): - """ Helper-func - get all domain-ids corresponding to domain-name + """Helper-func - get all domain-ids corresponding to domain-name sample record... "domains": [ { @@ -460,21 +431,26 @@ def get_domain_id_by_domainname(pobject, domainname, number_nodes): :returns domainid: a list of one or more domain-id's that match the domain-name given """ - domainid = get_id_by_name({'jsonblob': pobject, - 'name': domainname, - 'size': number_nodes, - 'typeval': 'domain'}) + domainid = get_id_by_name( + { + "jsonblob": pobject, + "head": "domains", + "name": domainname, + "size": number_nodes, + "typeval": "domain", + } + ) try: domainid - except: + except Exception: raise else: return domainid def get_domain_name_by_domainid(pobject, domainid, number_nodes): - """ Helper-func - get domain-name for a particular domainid + """Helper-func - get domain-name for a particular domainid sample record... "domains": [ { @@ -496,22 +472,26 @@ def get_domain_name_by_domainid(pobject, domainid, number_nodes): :returns domainname: the domain name that corresponds to the record identified by the domainid """ - domainname = get_attribute_by_id({'jsonblob': pobject, - 'head': 'domains', - 'id': domainid, - 'attr': 'name', - 'size': number_nodes, - 'typeval': 'domain'}) + domainname = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "domains", + "id": domainid, + "attr": "name", + "size": number_nodes, + "typeval": "domain", + } + ) try: domainname - except: + except Exception: raise else: return domainname def get_domain_description_by_domainid(pobject, domainid, number_nodes): - """ Helper-func - get the domaind descripton for a particular domainid + """Helper-func - get the domaind descripton for a particular domainid sample record... "domains": [ { @@ -533,22 +513,26 @@ def get_domain_description_by_domainid(pobject, domainid, number_nodes): :returns domainname: the domain description field that corresponds to the record identified by the domainid """ - domaindesc = get_attribute_by_id({'jsonblob': pobject, - 'head': 'domains', - 'id': domainid, - 'attr': 'description', - 'size': number_nodes, - 'typeval': 'domain'}) + domaindesc = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "domains", + "id": domainid, + "attr": "description", + "size": number_nodes, + "typeval": "domain", + } + ) try: domaindesc - except: + except Exception: raise else: return domaindesc def get_domain_state_by_domainid(pobject, domainid, number_nodes): - """ Helper-func - get domain state field for a particular domainid + """Helper-func - get domain state field for a particular domainid sample record... "domains": [ { @@ -570,22 +554,26 @@ def get_domain_state_by_domainid(pobject, domainid, number_nodes): :returns domainstate: the domain state (enabled) field that corresponds to the record identified by the domainid """ - domainstate = get_attribute_by_id({'jsonblob': pobject, - 'head': 'domains', - 'id': domainid, - 'attr': 'enabled', - 'size': number_nodes, - 'typeval': 'domain'}) + domainstate = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "domains", + "id": domainid, + "attr": "enabled", + "size": number_nodes, + "typeval": "domain", + } + ) try: domainstate - except: + except Exception: raise else: return domainstate def get_user_id_by_username(pobject, username, number_nodes): - """ Helper-func - get user-ids corresponding to username + """Helper-func - get user-ids corresponding to username sample record... "users": [ { @@ -610,21 +598,25 @@ def get_user_id_by_username(pobject, username, number_nodes): :returns userid: a list of one or more user-id's that match the username given """ - userid = get_id_by_name({'jsonblob': pobject, - 'name': username, - 'head': 'users', - 'size': number_nodes, - 'typeval': 'user'}) + userid = get_id_by_name( + { + "jsonblob": pobject, + "name": username, + "head": "users", + "size": number_nodes, + "typeval": "user", + } + ) try: userid - except: + except Exception: raise else: return userid def get_user_password_by_userid(pobject, userid, number_nodes): - """ Helper-func - get user password field for a particular userid + """Helper-func - get user password field for a particular userid sample record... "users": [ { @@ -649,22 +641,26 @@ def get_user_password_by_userid(pobject, userid, number_nodes): :returns userpassword: the raw password field that corresponds to the record identified by the userid """ - userpassword = get_attribute_by_id({'jsonblob': pobject, - 'head': 'users', - 'id': userid, - 'attr': 'password', - 'size': number_nodes, - 'typeval': 'user'}) + userpassword = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "users", + "id": userid, + "attr": "password", + "size": number_nodes, + "typeval": "user", + } + ) try: userpassword - except: + except Exception: raise else: return userpassword def get_user_name_by_userid(pobject, userid, number_nodes): - """ Helper-func - get the username field for a particular userid + """Helper-func - get the username field for a particular userid sample record... "users": [ { @@ -689,22 +685,26 @@ def get_user_name_by_userid(pobject, userid, number_nodes): :returns username: the name field that corresponds to the record identified by the userid """ - username = get_attribute_by_id({'jsonblob': pobject, - 'head': 'users', - 'id': userid, - 'attr': 'name', - 'size': number_nodes, - 'typeval': 'user'}) + username = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "users", + "id": userid, + "attr": "name", + "size": number_nodes, + "typeval": "user", + } + ) try: username - except: + except Exception: raise else: return username def get_user_state_by_userid(pobject, userid, number_nodes): - """ Helper-func - get user state field for a particular userid + """Helper-func - get user state field for a particular userid sample record... "users": [ { @@ -729,22 +729,26 @@ def get_user_state_by_userid(pobject, userid, number_nodes): :returns userstate: the enabled field that corresponds to the record identified by the userid """ - userstate = get_attribute_by_id({'jsonblob': pobject, - 'head': 'users', - 'id': userid, - 'attr': 'enabled', - 'size': number_nodes, - 'typeval': 'user'}) + userstate = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "users", + "id": userid, + "attr": "enabled", + "size": number_nodes, + "typeval": "user", + } + ) try: userstate - except: + except Exception: raise else: return userstate def get_user_email_by_userid(pobject, userid, number_nodes): - """ Helper-func - get user email field for a particular userid + """Helper-func - get user email field for a particular userid sample record... "users": [ { @@ -769,22 +773,26 @@ def get_user_email_by_userid(pobject, userid, number_nodes): :returns useremail: the email field that corresponds to the record identified by the userid """ - useremail = get_attribute_by_id({'jsonblob': pobject, - 'head': 'users', - 'id': userid, - 'attr': 'email', - 'size': number_nodes, - 'typeval': 'user'}) + useremail = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "users", + "id": userid, + "attr": "email", + "size": number_nodes, + "typeval": "user", + } + ) try: useremail - except: + except Exception: raise else: return useremail def get_user_description_by_userid(pobject, userid, number_nodes): - """ Helper-func - get user description field for a particular userid + """Helper-func - get user description field for a particular userid sample record... "users": [ { @@ -809,15 +817,19 @@ def get_user_description_by_userid(pobject, userid, number_nodes): :returns userdesc: the description field that corresponds to the record identified by the userid """ - userdesc = get_attribute_by_id({'jsonblob': pobject, - 'head': 'users', - 'id': userid, - 'attr': 'description', - 'size': number_nodes, - 'typeval': 'user'}) + userdesc = get_attribute_by_id( + { + "jsonblob": pobject, + "head": "users", + "id": userid, + "attr": "description", + "size": number_nodes, + "typeval": "user", + } + ) try: userdesc - except: + except Exception: raise else: return userdesc