Exemple #1
0
def is_interface_present_running_config(device, interface):
    """ Verify if interface is present in running-config
        Args:
            device ('obj')      : Device object
            interface ('str')   : Interface

        Raises:
            SubCommandFailure
            Exception
        Returns
            True
            False
    """

    try:
        output = get_interface_running_config(
            device=device, interface=interface
        )

    except SubCommandFailure as e:
        raise SubCommandFailure(str(e))
    except Exception as e:
        raise Exception(str(e))

    return bool(output)
Exemple #2
0
def get_policy_map_configurational_policy_map(device, interfaces):
    """ Get policy-map running configuration

        Args:
            device (`obj`): Device object
            interfaces (`list`): List of interfaces

        Returns:
            policy-map configurational dictionary
    """

    out = {}
    policy_out = {}
    for interface in interfaces:
        out[interface] = get_interface_running_config(device, interface)

    service_policies = []

    for item in out[interface]:
        if interface in item:
            for service_policy_item in out[interface][item]:
                if "service-policy" in service_policy_item:
                    service_policies.append(service_policy_item[21:].strip())

    for service_policy in service_policies:
        if "in" in service_policy:
            direction = "input"
        else:
            direction = "output"
        output = get_policy_map_running_policy_map(device, service_policy)
        if not output:
            continue
        policy_out.setdefault(direction, {})
        policy_out[direction] = output
        for class_name in policy_out[direction]["policy_map"][service_policy][
                "class"]:
            for item in policy_out[direction]["policy_map"][service_policy][
                    "class"][class_name]:
                if "service_policy" in item:
                    nested = policy_out[direction]["policy_map"][
                        service_policy]["class"][class_name][item]
                    nested_policy_out = get_policy_map_running_policy_map(
                        device, nested)
                    if not nested_policy_out:
                        continue
                    new_nested_policy_out = {}
                    new_nested_policy_out.setdefault(
                        "policy_map",
                        {}).setdefault(service_policy,
                                       {}).setdefault("child_policy_name", {})
                    new_nested_policy_out["policy_map"][service_policy][
                        "child_policy_name"] = nested_policy_out["policy_map"]
                    merge_dict(policy_out[direction], new_nested_policy_out)

    return policy_out
Exemple #3
0
def remove_interface_configured_service_policy(device, interface, out=None):
    """ Remove any service policy configured under interface

        Args:
            device (`obj`): Device object
            interface (`str`): Interface to remove service policy from
            out (`dict`): Show run interface <interface> output

        Returns:
            None

        Raises:
            SubCommandFailure
    """

    configs = []

    if not out:
        out = get_interface_running_config(device, interface)

    for item in out:
        if "interface" in item:
            for serv_policy in out[item]:
                if "service-policy input" in serv_policy:
                    configs.append(
                        "no {service_policy_input}".format(
                            service_policy_input=serv_policy
                        )
                    )
                elif "service-policy output" in serv_policy:
                    configs.append(
                        "no {service_policy_output}".format(
                            service_policy_output=serv_policy
                        )
                    )

    if len(configs) >= 1:
        configs.insert(0, "interface {interface}".format(interface=interface))
        try:
            device.configure(configs)
        except SubCommandFailure as e:
            raise SubCommandFailure(
                "Failed to unconfigure service policy"
                " in/out under interface {interface}. Error:\n{error}".format(
                    interface=interface, error=e
                )
            )
    else:
        log.info(
            "No configured service policy found under interface {interface}".format(
                interface=interface
            )
        )
 def test_get_interface_running_config(self):
     result = get_interface_running_config(self.device, 'GigabitEthernet1')
     expected_output = {
         'Building configuration...': {},
         'Current configuration : 147 bytes': {},
         'end': {},
         'interface GigabitEthernet1': {
             'ip address 172.16.1.211 255.255.255.0': {},
             'negotiation auto': {},
             'no mop enabled': {},
             'no mop sysid': {},
             'vrf forwarding Mgmt-intf': {}
         }
     }
     self.assertEqual(result, expected_output)