コード例 #1
0
def static_route_create(device_name, destination_network, prefix_len, next_hop_address):
    
    route_list = static_route_json(device_name)
    for route in route_list:
        if route['prefix'] == destination_network:
            print('static_route_delete(%s, %s)' % (device_name, destination_network))
            static_route_delete(device_name, destination_network, prefix_len)
            break
        
    """ Create the specified 'static route' on the specified network device. """
    next_hop = {"next-hop-address" : str(next_hop_address)}
    request_content = {
        "Cisco-IOS-XR-ip-static-cfg:vrf-prefix": [
            {
                "prefix" : destination_network,
                "prefix-length": prefix_len,
                "vrf-route" : {
                    "vrf-next-hops" : {
                        "next-hop-address" : [next_hop]
                    }
                }
            }
        ]
    }
    print('static_route_create(%s, %s, %s)' % (device_name, destination_network, next_hop_address))
    response = odl_http_post(_static_route_url_template, {'node-id' : device_name}, 'application/json', request_content, expected_status_code=[204, 409])
    if response.status_code == 204:
        return True
    else:
        return False
        '''
コード例 #2
0
def demonstrate_all(device_name):
    """
    Apply function 'static_route_json' to the specified device without 
    specifying a static route destination.

    Return a list of static route destination networks (may be empty).
    """
    print('Request all static routes.')
    print('static_route_json(%s)' % device_name)
    route_list = static_route_json(device_name)
    if not route_list:
        print(None)
        print()
        return []

    print_table([
        OrderedDict([("device", device_name),
                     ("destination",
                      "%s/%s" % (route['prefix'], route['prefix-length'])),
                     ("next-hop", next_hop_address(route))])
        for route in route_list
    ])
    print()
    return [
        ip_network("%s/%s" % (route['prefix'], route['prefix-length']))
        for route in route_list
    ]
コード例 #3
0
def static_route_create(device_name, destination_network, prefix_len, next_hop_address):

    route_list = static_route_json(device_name)
    for route in route_list:
        if route["prefix"] == destination_network:
            print("static_route_delete(%s, %s)" % (device_name, destination_network))
            static_route_delete(device_name, destination_network, prefix_len)
            break

    """ Create the specified 'static route' on the specified network device. """
    next_hop = {"next-hop-address": str(next_hop_address)}
    request_content = {
        "Cisco-IOS-XR-ip-static-cfg:vrf-prefix": [
            {
                "prefix": destination_network,
                "prefix-length": prefix_len,
                "vrf-route": {"vrf-next-hops": {"next-hop-address": [next_hop]}},
            }
        ]
    }
    print("static_route_create(%s, %s, %s)" % (device_name, destination_network, next_hop_address))
    response = odl_http_post(
        _static_route_url_template,
        {"node-id": device_name},
        "application/json",
        request_content,
        expected_status_code=[204, 409],
    )
    if response.status_code == 204:
        return True
    else:
        return False
        """
コード例 #4
0
def demonstrate_one(device_name, destination_network):
    """
    Apply function 'static_route_json' to the specified device and destination.

    Return True if the static route is found.
    """
    print('Request a specific static route.')
    print('static_route_json(%s, %s)' % (device_name, destination_network))
    route = static_route_json(device_name, destination_network)
    print(json.dumps(route, indent=2, sort_keys=True))
    return True
コード例 #5
0
def demonstrate_one(device_name, destination_network):
    """
    Apply function 'static_route_json' to the specified device and destination.

    Return True if the static route is found.
    """
    print('Request a specific static route.')
    print('static_route_json(%s, %s)' % (device_name, destination_network))
    route = static_route_json(device_name, destination_network)
    print(json.dumps(route, indent=2, sort_keys=True))
    return True
コード例 #6
0
def demonstrate(device_name):
    ''' Apply function 'static_route_json' to the specified device for a sample destination.'''
    print()
    destination_network = static_route_fixture.sample_destination(device_name)
    print('static_route_json(' + device_name, destination_network, sep=', ', end=')\n')
    route = static_route_json(device_name, destination_network)
    if route:
        print(json.dumps(route, indent=2, sort_keys=True))
    else:
        print('\t', route)
    return route is not None
コード例 #7
0
def demonstrate(device_name):
    ''' Apply function 'static_route_json' to the specified device for a sample destination.'''
    print()
    destination_network = static_route_fixture.sample_destination(device_name)
    print('static_route_json(' + device_name,
          destination_network,
          sep=', ',
          end=')\n')
    route = static_route_json(device_name, destination_network)
    if route:
        print(json.dumps(route, indent=2, sort_keys=True))
    else:
        print('\t', route)
    return route is not None
コード例 #8
0
def static_route_delete(device_name, destination_network=None, prefixlen=0):
    """
    Delete zero, one or more static routes from the specified device.
    
    Parameters:
    - device_name
        Identifies the network device.
    - destination_network
        Either None or an instance of type ipaddress._BaseNetwork
        - Unspecified
            Delete all static routes on the device.
            An exception is raised if there are no routes found on the device.
        - Specified
            Delete the route with the specified destination.
            An exception is raised if the specified route is not found on the device.

    No value is returned.
    An exception is raised if the static route does not exist on the device.
    """
    
    if destination_network:
        route_list = static_route_json(device_name)
        for route in route_list:
            if route['prefix'] == destination_network:
                url_params = {
                'node-id' : device_name, 
                'ip-address' : destination_network, 
                'prefix-length' : prefixlen
                }
                url_template = _static_route_uni_url_template
                response = odl_http_delete(url_template, url_params, 'application/json', expected_status_code=[200, 404, 500])
                if response.status_code != 200:
                    return False
                    #raise Exception(_error_message(response.json()))
                return True
    else:
         url_params_all = {'node-id' : device_name} 
         url_template_all = _static_route_url_template
         response = odl_http_delete(url_template_all, url_params_all, 'application/json', expected_status_code=[200, 404, 500])
         return True
コード例 #9
0
def static_route_delete(device_name, destination_network=None, prefixlen=0):
    """
    Delete zero, one or more static routes from the specified device.
    
    Parameters:
    - device_name
        Identifies the network device.
    - destination_network
        Either None or an instance of type ipaddress._BaseNetwork
        - Unspecified
            Delete all static routes on the device.
            An exception is raised if there are no routes found on the device.
        - Specified
            Delete the route with the specified destination.
            An exception is raised if the specified route is not found on the device.

    No value is returned.
    An exception is raised if the static route does not exist on the device.
    """

    if destination_network:
        route_list = static_route_json(device_name)
        for route in route_list:
            if route["prefix"] == destination_network:
                url_params = {"node-id": device_name, "ip-address": destination_network, "prefix-length": prefixlen}
                url_template = _static_route_uni_url_template
                response = odl_http_delete(
                    url_template, url_params, "application/json", expected_status_code=[200, 404, 500]
                )
                if response.status_code != 200:
                    return False
                    # raise Exception(_error_message(response.json()))
                return True
    else:
        url_params_all = {"node-id": device_name}
        url_template_all = _static_route_url_template
        response = odl_http_delete(
            url_template_all, url_params_all, "application/json", expected_status_code=[200, 404, 500]
        )
        return True
コード例 #10
0
def demonstrate_all(device_name):
    """
    Apply function 'static_route_json' to the specified device without 
    specifying a static route destination.

    Return a list of static route destination networks (may be empty).
    """
    print('Request all static routes.')
    print('static_route_json(%s)' % device_name)
    route_list = static_route_json(device_name)
    if not route_list:
        print(None)
        print()
        return []
    
    print_table([OrderedDict([
            ("device", device_name),
            ("destination", "%s/%s" % (route['prefix'], route['prefix-length'])),
            ("next-hop", next_hop_address(route))
        ]) for route in route_list
    ])
    print()
    return [ip_network("%s/%s" % (route['prefix'], route['prefix-length'])) for route in route_list]