예제 #1
0
def _update_port_loop_protect(interface_name, action="", vlan_list=[], **kwargs):
    """
    Perform GET and PUT calls to apply Loop-protect options on an interface.  Note that Loop-protect requires that
    the interface is L2, so this function will also update the interface to reflect that.

    :param interface_name: Alphanumeric String that is the name of the interface that will apply loop-protect options
    :param action: Alphanumeric String that will specify the actions for the Loop-protect interface.  The options are
        "do-not-disable", "tx-disable", "tx-rx-disable", or None.
    :param vlan_list: List of VLANs that will be configured for Loop-protect on the interface
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    int_name_percents = common_ops._replace_special_characters(interface_name)
    int_data = interface.get_interface(int_name_percents, depth=1, selector="writable", **kwargs)

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(int_data['interfaces'])

    if int_data['vlan_trunks']:
        int_data['vlan_trunks'] = common_ops._dictionary_to_list_values(int_data['vlan_trunks'])
    if int_data['loop_protect_vlan']:
        int_data['loop_protect_vlan'] = common_ops._dictionary_to_list_values(int_data['loop_protect_vlan'])

    int_data['loop_protect_enable'] = True
    # make interface L2
    int_data['routing'] = False

    # strings appended to output prints for status
    action_output = ""
    vlan_output = ""

    if action not in ['do-not-disable', 'tx-disable', 'tx-rx-disable', None]:
        raise Exception("ERROR: Action should be 'do-not-disable', 'tx-disable', 'tx-rx-disable' or None")
    elif action:
        int_data['loop_protect_action'] = action
        action_output = " with Action %s " % action

    if vlan_list:
        vlan_output = " with VLAN(s) ["
        for vlan in vlan_list:
            vlan_url = "/rest/v10.04/system/vlans/%s" % vlan
            if vlan_url not in int_data['loop_protect_vlan']:
                int_data['loop_protect_vlan'].append(vlan_url)
                vlan_output += (str(vlan) + " ")
        vlan_output += "] "

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_data, sort_keys=True, indent=4)

    response = kwargs["s"].put(target_url, data=put_data, verify=False)

    if not common_ops._response_ok(response, "PUT"):
        print("FAIL: Applying Loop-protect to Interface '%s'%s%s failed with status code %d"
              % (interface_name, action_output, vlan_output, response.status_code))
    else:
        print("SUCCESS: Applying Loop-protect to Interface '%s'%s%s succeeded"
              % (interface_name, action_output, vlan_output))
예제 #2
0
def _clear_port_acl_in(port_name, list_type, **kwargs):
    """
    Perform GET and PUT calls to clear a Port's Ingress ACL

    :param port_name: Alphanumeric name of the Port
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    port_name_percents = common_ops._replace_special_characters(port_name)

    port_data = interface.get_interface(port_name,
                                        depth=1,
                                        selector="writable",
                                        **kwargs)
    if list_type is "ipv6":
        port_data.pop('aclv6_in_cfg', None)
        port_data.pop('aclv6_in_cfg_version', None)
    elif list_type is "ipv4":
        port_data.pop('aclv4_in_cfg', None)
        port_data.pop('aclv4_in_cfg_version', None)

    target_url = kwargs["url"] + "system/interface/%s" % port_name_percents
    put_data = json.dumps(port_data, sort_keys=True, indent=4)
    response = kwargs["s"].put(target_url, data=put_data, verify=False)

    if not common_ops._response_ok(response, "PUT"):
        print(
            "FAIL: Clearing %s Ingress ACL on Port '%s' failed with status code %d"
            % (list_type, port_name, response.status_code))
    else:
        print("SUCCESS: Clearing %s Ingress ACL on Port '%s' succeeded" %
              (list_type, port_name))
예제 #3
0
def _update_vsx_interface_vlan(vlan_id, active_forwarding, vsx_sync,
                               act_gw_mac, act_gw_ip, **kwargs):
    """
    Perform PUT calls on a VLAN interface to configure VSX IPv4 settings
    :param vlan_id: Numeric ID of VLAN to that will be configured
    :param active_forwarding: True or False Boolean to set VSX active forwarding
    :param vsx_sync: Set of alphanumeric values to enable VSX configuration synchronization.  The options are
        any combination of 'active-gateways', 'irdp', and 'policies'
    :param act_gw_mac: Alphanumeric value of the Virtual MAC address for the interface active gateway
    :param act_gw_ip: Alphanumeric value of the Virtual IP address for the interface active gateway
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    ints_list = interface.get_all_interfaces(**kwargs)
    vlan_name = "vlan" + str(vlan_id)

    if vlan_name not in ints_list:
        print(
            "FAIL: Adding VSX information to VLAN Interface '%d' failed because "
            "VLAN Interface doesn't exist" % vlan_id)
    else:
        interface_vsx_data = interface.get_interface(vlan_name,
                                                     depth=2,
                                                     selector="writable",
                                                     **kwargs)

        vsx_sync_set = []
        if "active-gateways" in vsx_sync:
            vsx_sync_set.append("^vsx_virtual.*")
        if "irdp" in vsx_sync:
            vsx_sync_set.append(".irdp.*")
        if "policies" in vsx_sync:
            vsx_sync_set.append("^policy.*")

        interface_vsx_data["vsx_active_forwarding_enable"] = active_forwarding
        interface_vsx_data["vsx_sync"] = vsx_sync_set
        interface_vsx_data["vsx_virtual_gw_mac_v4"] = act_gw_mac
        interface_vsx_data["vsx_virtual_ip4"] = [act_gw_ip]

        target_url = kwargs["url"] + "system/interfaces/" + vlan_name
        put_data = json.dumps(interface_vsx_data, sort_keys=True, indent=4)
        response = kwargs["s"].put(target_url, data=put_data, verify=False)

        if not common_ops._response_ok(response, "PUT"):
            print(
                "FAIL: Adding VSX information to VLAN Interface '%d' failed with status code %d"
                % (vlan_id, response.status_code))
        else:
            print(
                "SUCCESS: Adding VSX information to VLAN Interface '%d' succeeded"
                % vlan_id)
예제 #4
0
def _update_port_acl_in(interface_name, acl_name, list_type, **kwargs):
    """
    Perform GET and PUT calls to apply ACL on an interface. This function specifically applies an ACL
    to Ingress traffic of the interface.  This function's minimum supported version is v10.04 and later

    :param interface_name: Alphanumeric name of the interface on which the ACL is applied to
    :param acl_name: Alphanumeric name of the ACL
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    int_name_percents = common_ops._replace_special_characters(interface_name)
    int_data = interface.get_interface(int_name_percents,
                                       depth=1,
                                       selector="writable",
                                       **kwargs)

    acl_key = "{},{}".format(acl_name, list_type)
    acl_value = kwargs["url"] + "system/acls/" + acl_key

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(
                int_data['interfaces'])

    if list_type is "ipv6":
        int_data['aclv6_in_cfg'] = {acl_key: acl_value}
        int_data['aclv6_in_cfg_version'] = random.randint(
            -9007199254740991, 9007199254740991)
    elif list_type is "ipv4":
        int_data['aclv4_in_cfg'] = {acl_key: acl_value}
        int_data['aclv4_in_cfg_version'] = random.randint(
            -9007199254740991, 9007199254740991)

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_data, sort_keys=True, indent=4)

    response = kwargs["s"].put(target_url, data=put_data, verify=False)

    if not common_ops._response_ok(response, "PUT"):
        print(
            "FAIL: Updating ACL %s on Ingress for Port '%s' failed with status code %d"
            % (acl_name, interface_name, response.status_code))
    else:
        print("SUCCESS: Updating ACL %s on Ingress for Port '%s' succeeded" %
              (acl_name, interface_name))
예제 #5
0
def _delete_vsx_interface_vlan(vlan_id, **kwargs):
    """
    Perform PUT calls on a VLAN interface to remove VSX IPv4 settings
    :param vlan_id: Numeric ID of VLAN to that will be configured
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    ints_list = interface.get_all_interfaces(**kwargs)
    vlan_name = "vlan" + str(vlan_id)

    if vlan_name not in ints_list:
        print(
            "FAIL: Deleting VSX information to VLAN Interface '%d' failed because "
            "VLAN Interface doesn't exist" % vlan_id)
    else:
        interface_vsx_data = interface.get_interface(vlan_name,
                                                     depth=2,
                                                     selector="writable",
                                                     **kwargs)

        interface_vsx_data["vsx_active_forwarding_enable"] = None
        interface_vsx_data["vsx_sync"] = None
        interface_vsx_data["vsx_virtual_gw_mac_v4"] = None
        interface_vsx_data["vsx_virtual_ip4"] = []

        target_url = kwargs["url"] + "system/interfaces/" + vlan_name
        put_data = json.dumps(interface_vsx_data, sort_keys=True, indent=4)
        response = kwargs["s"].put(target_url, data=put_data, verify=False)

        if not common_ops._response_ok(response, "PUT"):
            print(
                "FAIL: Deleting VSX information from VLAN Interface '%d' failed with status code %d"
                % (vlan_id, response.status_code))
        else:
            print(
                "SUCCESS: Deleting VSX information from VLAN Interface '%d' succeeded"
                % vlan_id)
예제 #6
0
def _update_port_acl_out(interface_name, acl_name, **kwargs):
    """
    Perform GET and PUT calls to apply ACL on an interface. This function specifically applies an ACL
    to Egress traffic of the interface, which must be a routing interface.  This function will set the interface
    to enable routing.

    :param interface_name: Alphanumeric name of the interface on which the ACL is applied to
    :param acl_name: Alphanumeric name of the ACL
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    port_name_percents = common_ops._replace_special_characters(interface_name)
    port_data = interface.get_interface(port_name_percents,
                                        depth=1,
                                        selector="writable",
                                        **kwargs)

    acl_key = "{},ipv4".format(acl_name)
    acl_value = kwargs["url"] + "system/acls/" + acl_key
    port_data['aclv4_out_cfg'] = {acl_key: acl_value}
    port_data['aclv4_out_cfg_version'] = random.randint(
        -9007199254740991, 9007199254740991)
    port_data['routing'] = True

    target_url = kwargs["url"] + "system/interfaces/%s" % port_name_percents
    put_data = json.dumps(port_data, sort_keys=True, indent=4)

    response = kwargs["s"].put(target_url, data=put_data, verify=False)

    if not common_ops._response_ok(response, "PUT"):
        print(
            "FAIL: Applying ACL '%s' to Egress on Interface '%s' failed with status code %d"
            % (acl_name, interface_name, response.status_code))
    else:
        print(
            "SUCCESS: Applying ACL '%s' to Egress on Interface '%s' succeeded"
            % (acl_name, interface_name))
예제 #7
0
def _clear_port_loop_protect(interface_name, **kwargs):
    """
    Perform GET and PUT calls to clear an Interface's Loop-protect settings

    :param interface_name: Alphanumeric name of the Interface
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: Nothing
    """
    int_name_percents = common_ops._replace_special_characters(interface_name)

    int_data = interface.get_interface(interface_name, depth=1, selector="writable", **kwargs)

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(int_data['interfaces'])

    if int_data['vlan_trunks']:
        int_data['vlan_trunks'] = common_ops._dictionary_to_list_values(int_data['vlan_trunks'])

    int_data['loop_protect_enable'] = None
    int_data['loop_protect_action'] = "tx-disable"
    int_data['loop_protect_vlan'] = []

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_data, sort_keys=True, indent=4)

    response = kwargs["s"].put(target_url, data=put_data, verify=False)

    if not common_ops._response_ok(response, "PUT"):
        print("FAIL: Clearing Loop-protect options on Interface '%s' failed with status code %d"
              % (interface_name, response.status_code))
    else:
        print("SUCCESS: Clearing the Loop-protect options on Interface '%s' succeeded"
              % (interface_name))