Exemple #1
0
def get_copp_config(dut, **kwargs):
    """
    Gets value of an attribute from a table
    Author : Chaitanya Lohith Bollapragada ([email protected])

    :param dut:
    :param table_name:
    :param attribute:
    :return value:
    """
    if 'table_name' not in kwargs:
        st.error("Mandatory parameter table_name not found")
        return False
    #command = "docker exec swss cat /etc/swss/config.d/00-copp.config.json"
    output = st.show(dut, "show copp config", skip_tmpl=True)
    reg_output = utils_obj.remove_last_line_from_string(output)
    if not reg_output:
        return False
    data = do_eval(reg_output)
    if not isinstance(data,dict):
        return False
    key = kwargs["table_name"]

    if key == "all":
        return data
    else:
        for each in data:
            if key in each:
                return data[each]['value']
    return False
def get_config_db(dut, table=None, object=None, attribute=None):
    """
    Get Config DB json value based on table, object, attribute
    Author: Rakesh Kumar Vooturi ([email protected])
    :param dut:
    :param table:
    :param object:
    :param attribute:
    :return:
    """
    command = "cat /etc/sonic/config_db.json"
    output = st.show(dut, command, skip_tmpl=True)
    reg_output = utils_obj.remove_last_line_from_string(output)
    try:
        data = do_eval(json.dumps(json.loads(reg_output)))
        if table is None and object is None and attribute is None:
            return data
        elif table is not None and object is None and attribute is None:
            return data[table]
        elif table is not None and object is not None and attribute is None:
            return data[table][object]
        elif table is not None and object is not None and attribute is not None:
            return data[table][object][attribute]
    except Exception as e:
        st.log(e)
        return None
Exemple #3
0
def set_copp_config(dut, *argv):
    """
    To set the config into copp
    Author : Chaitanya Lohith Bollapragada ([email protected])
    Expected input from user should be [[table_name,attribute,value],[table_name,attribute,value],...]

    :param dut:
    :param table_name:
    :param attribute:
    :param value:
    :return bool:

    Example : set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"])
              set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"],["COPP_TABLE:trap.group.lldp.dhcp.udld","trap_priority","6"])
    """
    command = "docker exec swss cat /etc/swss/config.d/00-copp.config.json"
    output = st.show(dut, command, skip_tmpl=True)
    reg_output = utils_obj.remove_last_line_from_string(output)
    try:
        data = do_eval(reg_output)
    except Exception as e:
        st.log(e)
        reg_output = "{} ]".format(reg_output)
        data = do_eval(reg_output)
    st.log("ARGV {}".format(argv))
    for eachli in argv:
        if len(eachli) != 3:
            st.error("Invalid input is provided  {}".format(eachli))
            return False
        table = eachli[0]
        attribute = eachli[1]
        value = eachli[2]
        for each in data:
            if table in each:
                each[table][attribute] = value
                break
            else:
                st.error("Table not found {}".format(table))
                return False
    file_path = utils_obj.write_to_json_file(data, "/tmp/00-copp.config.json")
    st.log("FILE PATH -- {}".format(file_path))
    st.upload_file_to_dut(dut, file_path, "/tmp/00-copp.config.json")
    command = "docker cp /tmp/00-copp.config.json swss:/etc/swss/config.d/00-copp.config.json"
    st.config(dut, command)
    command = "rm /tmp/00-copp.config.json"
    st.config(dut, command)
    return True
def get_running_config(dut,
                       table=None,
                       object=None,
                       attribute=None,
                       max_retry=3):
    """
    Get running config value based on table, object, attribute
    Author: Rakesh Kumar Vooturi ([email protected])
    :param dut:
    :param table:
    :param object:
    :param attribute:
    :return:
    """
    command = "sudo show runningconfiguration all"
    i = 1
    while True:
        try:
            output = st.show(dut, command, skip_tmpl=True)
            reg_output = utils_obj.remove_last_line_from_string(output)
            data = do_eval(json.dumps(json.loads(reg_output)))
            break
        except Exception as e:
            st.error("Exception occured in try-{} - {}".format(i, e))
            if i == max_retry:
                st.error("MAX retry {} reached..".format(i))
                return None
        i += 1
    try:
        if table is None and object is None and attribute is None:
            return data
        elif table is not None and object is None and attribute is None:
            return data[table]
        elif table is not None and object is not None and attribute is None:
            return data[table][object]
        elif table is not None and object is not None and attribute is not None:
            return data[table][object][attribute]
    except Exception as e:
        st.log(e)
        return None
Exemple #5
0
def get_jwt_token(dut, **kwargs):
    """
    To get JWT token using REST call.
    Author: Prudvi Mangadu ([email protected])
    :param dut:
    :param kwargs:
    :return:
    Usage:
    get_jwt_token(dut, username='******', password='******')
    """
    username = kwargs.get('username')
    password = kwargs.get('password')
    url = kwargs.get('url', 'authenticate')
    headers = kwargs.get('headers', {'Accept': 'application/json'})
    data = {'username': username, 'password': password}
    out = rest_call(dut, data=data, headers=headers, url=url, call_type='post')
    if not out:
        return None
    if out.get('status') not in [200]:
        return None
    token_dic = do_eval(out['output'])
    return token_dic.get('access_token', None)
def verify_config_db(dut,
                     table,
                     object,
                     attribute=None,
                     value=None,
                     max_retry=3):
    """
    Verify Config DB json value based on table, object, attribute
    Author: Rakesh Kumar Vooturi ([email protected])
    :param dut:
    :param table:
    :param object:
    :param attribute:
    :param value:
    :return:
    """
    command = "cat /etc/sonic/config_db.json"
    # Adding while loop and try/except to catch the truncated data issue.
    i = 1
    while True:
        try:
            output = st.show(dut, command, skip_tmpl=True)
            reg_output = utils_obj.remove_last_line_from_string(output)
            data = do_eval(json.dumps(json.loads(reg_output)))
            break
        except Exception as e:
            st.error("Exception occured in try-{} - {}".format(i, e))
            if i == max_retry:
                st.error("MAX retry {} reached..".format(i))
                return False
        i += 1

    st.log(
        "verifying for Table: {}, Object: {}, Attribute: {} and Value: {} in "
        "config DB".format(table, object, attribute, value))

    if table in data:
        if object in data[table]:
            if attribute is not None and value is not None:
                if attribute in data[table][object]:
                    if data[table][object][attribute] == value:
                        st.log("Found the data in config DB - {}".format(
                            data[table][object]))
                        return True
                    else:
                        st.log(
                            "Did not find the data in config DB - {}".format(
                                data[table][object]))
                        return False
                else:
                    st.log(
                        "Did not find the Table: {}, Object: {}, Attribute: {} "
                        "in config DB".format(table, object, attribute))
                    return False
            else:
                st.log("Found the data in config DB - {}".format(
                    data[table][object]))
                return True
        else:
            st.log(
                "Did not find the Table: {}, Object: {} in config DB".format(
                    table, object))
            return False
    else:
        st.log("Did not find the Table: {} in config DB".format(table))
        return False