def interface_properties_http(content_type, device_name, interface_name=None):
    '''Return the HTTP request and response, for interface properties, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        return odl_http_get(_properties_url_template, {'node-id' : device_name}, content_type)
    else:
        url_params = {'node-id' : device_name, 'interface-id' : interface_name}
        return odl_http_get(_properties_uni_url_template, url_params, content_type)
def interface_configuration_http(content_type, device_name, interface_name=None):
    '''Return the HTTP request and response, for interface configuration, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        return odl_http_get(_configuration_multi_url_template, {'node-id' : device_name}, content_type)
    else:
        url_params = {'node-id' : device_name, 'active' : 'act', 'interface-id' : interface_name}
        return odl_http_get(_configuration_uni_url_template, url_params, content_type)
def dismount_device(
    device_name
):
    'Dismount a network device that has been mounted on the ODL server.'
#     request_content = _request_content_template % (device_name, device_address, device_port, device_username, device_password)
#     request_content = _request_content_template % (quote_plus(device_name), 'dummy_address', 'dummy_port', 'dummy_username', 'dummy_password')
    dismount_url_suffix = _dismount_url_suffix_template % device_name
    print odl_http_get(dismount_url_suffix, 'application/xml', expected_status_code=200).text
    odl_http_delete(dismount_url_suffix, 'application/xml', expected_status_code=200)
    print odl_http_get(dismount_url_suffix, 'application/xml', expected_status_code=200).text
def interface_properties_http(content_type, device_name, interface_name=None):
    '''Return the HTTP request and response, for interface properties, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        return odl_http_get(_properties_url_template, {'node-id': device_name},
                            content_type)
    else:
        url_params = {'node-id': device_name, 'interface-id': interface_name}
        return odl_http_get(_properties_uni_url_template, url_params,
                            content_type)
def get_bytes(device_name, interface_name):
    url = 'operational/network-topology:network-topology/topology/topology-netconf/'
    data = odl_http_get(url_suffix=url, accept='application/json')
    topo = data.json()["topology"][0]
    topo_id = topo["topology-id"]
    node = topo["node"]

    url2 = 'operational/network-topology:network-topology/topology/topology-netconf/node/' + device_name + '/yang-ext:mount/Cisco-IOS-XR-drivers-media-eth-oper:ethernet-interface/statistics/statistic/' + interface_name
    data2 = odl_http_get(url_suffix=url2, accept='application/json')

    static = data2.json()["statistic"][0]

    total = static["total-good-bytes-transmitted"]

    return total
def cdp_neighbors_json(device_name):
    response = odl_http_get(
        url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404)
    )
    j = json.loads(response.text)
    container = j["cdp"]["nodes"]["node"][0]
    mgmt_name = management_interface(device_name)
    neighbor_set = set()
    if "neighbors" in container:
        neighbors = container["neighbors"]
        #       print(json.dumps(neighbors, indent=2))
        if "summaries" in neighbors:
            summary = neighbors["summaries"]["summary"]
            for device_info in summary:
                neighbor_id = device_info["device-id"]
                neighbor_interface = device_info["cdp-neighbor"][0]["port-id"]
                device_interface = device_info["interface-name"]
                if device_interface in mgmt_name:
                    continue
                neighbor_info = (
                    device_name,
                    device_interface.encode("utf8"),
                    neighbor_id.encode("utf8"),
                    neighbor_interface.encode("utf8"),
                )
                neighbor_set.add(neighbor_info)
            return neighbor_set
示例#7
0
def management_interface(device_name):
    'Return the name of the interface that is used to manage the specified network device.'
    control = device_control(device_name)
    if (not control):
        return None
    url_suffix = _configuration_multi_url_template % quote_plus(device_name)
    response = odl_http_get(url_suffix,
                            'application/xml',
                            expected_status_code=[200, 400])
    if response.status_code == 400:
        return None  # The specified device does not have interfaces.
    if device_name in _network_device_config.keys():
        device_address = _network_device_config[device_name]['address']
    else:
        device_address = None  # Discovered or configured independently.
    tree = etree.parse(StringIO(response.text))
    for ifc in tree.xpath(
            "/ifc:interface-configurations/ifc:interface-configuration",
            namespaces=_interface_namespaces):
        for ipv4 in ifc.xpath('ipv4c:ipv4-network/ipv4c:addresses/*',
                              namespaces=_interface_namespaces):
            address = ipv4.findtext('ipv4c:address',
                                    namespaces=_interface_namespaces)
            netmask = ipv4.findtext('ipv4c:netmask',
                                    namespaces=_interface_namespaces)
            if address == control.address:
                return ifc.findtext('ifc:interface-name',
                                    namespaces=_interface_namespaces)
    return None
def resolveDataFromJosn():
    data = odl_http_get(
        url_suffix=
        'operational/network-topology:network-topology/topology/flow:1',
        accept='application/json')
    topo = data.json()["topology"][0]
    topo_id = topo["topology-id"]
    node = topo["node"]
    link = topo["link"]

    for nd in node:
        node_id = nd["node-id"]
        if (node_id.find("openflow") == -1):
            continue
        switches[node_id] = OdlNode(node_id)

    for lk in link:
        if (lk["source"]["source-node"].find("openflow") == -1
                or lk["destination"]["dest-node"].find("openflow") == -1):
            continue
        srcNode = switches[lk["source"]["source-node"]]
        dstNode = switches[lk["destination"]["dest-node"]]
        src = lk["source"]["source-tp"]
        dst = lk["destination"]["dest-tp"]
        addLink(src, dst, srcNode, dstNode)
def port_stati(delay, switchId, portId):
    count = 0
    #     print(portId)
    # portId = str("openflow:3:1")

    while (count < 2):
        response = odl_http_get(
            url_suffix=
            "operational/opendaylight-inventory:nodes/node/{switchId}/node-connector/{portId}/opendaylight-port-statistics:flow-capable-node-connector-statistics/bytes/",
            url_params={
                "switchId": switchId,
                "portId": portId
            },
            accept='application/json')
        time.sleep(delay)
        count += 1
        #         print count
        statistics = response.json()
        if count == 1:
            old_received = statistics["opendaylight-port-statistics:bytes"][
                "received"]
        else:
            new_received = statistics["opendaylight-port-statistics:bytes"][
                "received"]

    static = (new_received - old_received) * 2.0
    speeds[portId][0] = new_received
    speeds[portId][1] = static

    if (static > 0):
        print(portId + "    " + str(speeds[portId][1]))
def interface_properties(
    device_name,
    interface_name=None
):
    """
    Obtain network interface properties for one specific network device.
    
    Parameters:
    - device_name
        Identifies the network device.
    - interface_name
        Either None or a specific name.
        - Unspecified
            Return properties for all network interfaces on the network device.
            Return type is a list of type InterfaceProperties, which may be empty.
        - Specified
            Return an instance of InterfaceProperties or None.
    """
    url_params = {'node-id' : device_name, 'data-node-id' : '0/0/CPU0'} 
    if interface_name:
        url_params['interface-id'] = interface_name
        url_template = _url_template_specific
    else:
        url_template = _url_template_general
    response = odl_http_get(url_template, url_params, 'application/xml')
    tree = etree.parse(StringIO(response.text))
    return interface_properties_tuple_from_xml(tree) if interface_name \
        else [interface_properties_tuple_from_xml(config) for config in tree.iterfind('{*}interface')]
示例#11
0
def acl_exists(
    device_name,
    acl_name
):
    """ Determine whether the specified ACL exists on the specified device. """
    response = odl_http_get(_url_named_acl, {'node-id' : device_name, 'access-id' : acl_name}, 'application/json', expected_status_code=[200, 404])
    return response.status_code == 200
示例#12
0
def mounted(device_name):
    '''Determine whether a single device is mounted on the Controller.
    
    Return True if mounted.
    '''
    response = odl_http_get(_url_connector, {'node-id' : device_name}, 'application/xml', expected_status_code=[200, 404])
    return response.status_code == 200
示例#13
0
def acl_exists(
    device_name,
    acl_name
):
    """ Determine whether the specified ACL exists on the specified device. """
    response = odl_http_get(_url_named_acl, {'node-id' : device_name, 'access-id' : acl_name}, 'application/json', expected_status_code=[200, 404])
    return response.status_code == 200
def port_stati( delay,switchId,portId):
    count = 0
#     print(portId)
   # portId = str("openflow:3:1")

    while (count < 2):
        response = odl_http_get(
            url_suffix = "operational/opendaylight-inventory:nodes/node/{switchId}/node-connector/{portId}/opendaylight-port-statistics:flow-capable-node-connector-statistics/bytes/",
            url_params={"switchId":switchId,"portId":portId},
            accept='application/json'
        )
        time.sleep(delay)
        count += 1
#         print count
        statistics = response.json()
        if count == 1:
            old_received = statistics["opendaylight-port-statistics:bytes"]["received"]
        else :
            new_received = statistics["opendaylight-port-statistics:bytes"]["received"]
        
    static = (new_received-old_received)*2.0
    speeds[portId][0] = new_received
    speeds[portId][1] = static

    if(static > 0):
        print(portId  + "    "+ str(speeds[portId][1]))
def interface_configuration_http(content_type,
                                 device_name,
                                 interface_name=None):
    '''Return the HTTP request and response, for interface configuration, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        return odl_http_get(_configuration_multi_url_template,
                            {'node-id': device_name}, content_type)
    else:
        url_params = {
            'node-id': device_name,
            'active': 'act',
            'interface-id': interface_name
        }
        return odl_http_get(_configuration_uni_url_template, url_params,
                            content_type)
示例#16
0
def link_info():
    #def main():
    ii = 0
    link_info_dic = {}
    for device_name in inventory_connected():

        response = odl_http_get(url_neighbors, {'node-id': device_name},
                                'application/json',
                                expected_status_code=(200, 404))
        l = json.loads(response.text)
        container_l = l["cdp"]["nodes"]["node"][0]
        mgmt_name = management_interface(device_name)

        if "neighbors" in container_l:
            neighbors = container_l["neighbors"]
            #           print(json.dumps(neighbors, indent=2))
            if "summaries" in neighbors:
                summary = neighbors["summaries"]["summary"]
                for device_info in summary:
                    neighbor_id = device_info["device-id"]
                    if neighbor_id == "kcy":
                        neighbor_id = 'iosxrv-1'
                    elif neighbor_id == "lax":
                        neighbor_id = 'iosxrv-2'
                    elif neighbor_id == "min":
                        neighbor_id = 'iosxrv-3'
                    elif neighbor_id == "por":
                        neighbor_id = 'iosxrv-4'
                    elif neighbor_id == "san":
                        neighbor_id = 'iosxrv-5'
                    elif neighbor_id == "sea":
                        neighbor_id = 'iosxrv-6'
                    elif neighbor_id == "sfc":
                        neighbor_id = 'iosxrv-7'
                    else:
                        neighbor_id = 'iosxrv-8'
                    device_interface = device_info["interface-name"]
                    if device_interface in mgmt_name:
                        continue
                    link_info = (device_name, neighbor_id.encode('utf8'))
                    link_info_re = (neighbor_id.encode('utf8'), device_name)
                    jj = 0
                    flag = 1
                    while ii > jj:
                        if link_info_dic[jj] == link_info or link_info_dic[
                                jj] == link_info_re:
                            flag = 0
                            jj = jj + 1
                        else:
                            jj = jj + 1
                    if flag == 1:
                        link_info_dic[ii] = link_info
                        ii = ii + 1
                    else:
                        jj = 0


#                   return link_info_set
    return link_info_dic
示例#17
0
def acl_exists(
    device_name,
    acl_name
):
    """ Determine whether the specified ACL exists on the specified device. """
    url_suffix = _url_named_acl % (quote_plus(device_name), quote_plus(acl_name))
    response = odl_http_get(url_suffix, 'application/json', expected_status_code=[200, 404])
    return response.status_code == 200
示例#18
0
def mounted(device_name):
    '''Determine whether a single device is mounted on the Controller.
    
    Return True if mounted.
    '''
    url_suffix = _url_connector % device_name
    response = odl_http_get(url_suffix, 'application/xml', expected_status_code=[200, 404])
    return response.status_code == 200
示例#19
0
def interface_configuration_http(content_type, device_name, interface_name=None):
    '''Return the HTTP request and response, for interface configuration, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        url_suffix = _configuration_multi_url_template % (device_name)
    else:
        url_suffix = _configuration_uni_url_template % (device_name, 'act', quote_plus(interface_name))
    return odl_http_get(url_suffix, content_type)
示例#20
0
def acl_exists(device_name, acl_name):
    """ Determine whether the specified ACL exists on the specified device. """
    url_suffix = _url_named_acl % (quote_plus(device_name),
                                   quote_plus(acl_name))
    response = odl_http_get(url_suffix,
                            'application/json',
                            expected_status_code=[200, 404])
    return response.status_code == 200
示例#21
0
def get_bytes(device_name, interface_name):
    url = 'operational/network-topology:network-topology/topology/topology-netconf/'
    data=odl_http_get(url_suffix=url, accept= 'application/json')
    topo= data.json()["topology"][0]
    topo_id=topo["topology-id"]
    node=topo["node"]

    
    url2='operational/network-topology:network-topology/topology/topology-netconf/node/' + device_name + '/yang-ext:mount/Cisco-IOS-XR-drivers-media-eth-oper:ethernet-interface/statistics/statistic/' + interface_name
    data2= odl_http_get(url_suffix=url2, accept= 'application/json')
    
    static=data2.json()["statistic"][0]

    total=static["total-good-bytes-transmitted"]

    
    return total
示例#22
0
def static_route_json_all(device_name):
    '''All static routes on the specified network device.'''
    url_suffix = _static_route_url_template % device_name
    response = odl_http_get(url_suffix, 'application/json', expected_status_code=[200, 404])
    if response.status_code == 404:
        return []
    else:
        return response.json()['vrf-prefixes']['vrf-prefix']
def capability(device_name):
    'Return a list of capability names, given the name of a mounted, connected device.'
    response = odl_http_get(_url_template, {'node-id' : device_name}, 'application/xml')
    tree = etree.parse(StringIO(response.text))
    return [
        _short_name.match(long_name).group(1) 
        for long_name 
        in tree.xpath(".//nni:initial-capability/text()", namespaces=ns)]
    
示例#24
0
def capability(device_name):
    'Return a list of capability names for the specified device.'
    response = odl_http_get(_url_inventory_node, {'node-id' : device_name}, 'application/xml')
    tree = etree.parse(StringIO(response.text))
    initial_capability_list = tree.xpath(".//nni:initial-capability/text()", namespaces=_inventory_namespaces)
    if initial_capability_list:
        return initial_capability_list
    else:
        return tree.xpath(".//fi:switch-features/fi:capabilities/text()", namespaces=_inventory_namespaces)
def interface_names(device_name):
    'Return a list of interface names, given the name of a mounted, connected device.'
    response = odl_http_get(_properties_url_template, {'node-id' : device_name}, 'application/xml', expected_status_code=[200, 400])
    if response.status_code == 400:
        return []  # The inventory item does not have interfaces.
    tree = etree.parse(StringIO(response.text))
    namespace = tree.getroot().tag[1:].split("}")[0]
    ns = {'n':namespace}
    return tree.xpath(".//n:system-view//n:interface[n:encapsulation/text()='ether']/n:interface-name/text()", namespaces=ns)
def link_info():
    # def main():
    ii = 0
    link_info_dic = {}
    for device_name in inventory_connected():

        response = odl_http_get(
            url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404)
        )
        l = json.loads(response.text)
        container_l = l["cdp"]["nodes"]["node"][0]
        mgmt_name = management_interface(device_name)

        if "neighbors" in container_l:
            neighbors = container_l["neighbors"]
            #           print(json.dumps(neighbors, indent=2))
            if "summaries" in neighbors:
                summary = neighbors["summaries"]["summary"]
                for device_info in summary:
                    neighbor_id = device_info["device-id"]
                    if neighbor_id == "kcy":
                        neighbor_id = "iosxrv-1"
                    elif neighbor_id == "lax":
                        neighbor_id = "iosxrv-2"
                    elif neighbor_id == "min":
                        neighbor_id = "iosxrv-3"
                    elif neighbor_id == "por":
                        neighbor_id = "iosxrv-4"
                    elif neighbor_id == "san":
                        neighbor_id = "iosxrv-5"
                    elif neighbor_id == "sea":
                        neighbor_id = "iosxrv-6"
                    elif neighbor_id == "sfc":
                        neighbor_id = "iosxrv-7"
                    else:
                        neighbor_id = "iosxrv-8"
                    device_interface = device_info["interface-name"]
                    if device_interface in mgmt_name:
                        continue
                    link_info = (device_name, neighbor_id.encode("utf8"))
                    link_info_re = (neighbor_id.encode("utf8"), device_name)
                    jj = 0
                    flag = 1
                    while ii > jj:
                        if link_info_dic[jj] == link_info or link_info_dic[jj] == link_info_re:
                            flag = 0
                            jj = jj + 1
                        else:
                            jj = jj + 1
                    if flag == 1:
                        link_info_dic[ii] = link_info
                        ii = ii + 1
                    else:
                        jj = 0

    #                   return link_info_set
    return link_info_dic
def management_interface_name(node_id):
    """Retrieve the management interface name for a particular XRV node."""
    request_url = _url_interface_configurations.format(**{'config': 'config', 
                                               'topology-id': 'topology-netconf',
                                               'node-id': node_id,
                                               })
    response = odl_http_get(request_url, 'application/json', expected_status_code=[200, 400])
    interfaces = response.json()['interface-configurations']['interface-configuration']
    return next(interface['interface-name'] for interface in interfaces if interface['Cisco-IOS-XR-ipv4-io-cfg:ipv4-network']['addresses']['primary']['address'] == settings.config['network_device'][node_id]['address'])    
def get_loopback(device_name):
    _url_node_loopback_2 = _url_node_loopback_address % device_name
    device_address_response=odl_http_get(_url_node_loopback_2,
                        {},
                        'application/json',
                        )                    
    ttt1 = json.loads(device_address_response.text)
    container = ttt1["Cisco-IOS-XR-ipv4-io-cfg:primary"]["address"]
    return container
示例#29
0
def interface_properties_http(content_type, device_name, interface_name=None):
    '''Return the HTTP request and response, for interface properties, for the specified, mounted device 
    and (optionally) the specified interface.'''
    if interface_name is None:
        url_suffix = _properties_url_template % (device_name)
    else:
        url_suffix = _properties_uni_url_template % (
            device_name, quote_plus(interface_name))
    return odl_http_get(url_suffix, content_type)
def interface_properties(node_id, interface_name):
    """Retrieve the interface properties for a particular XRV node."""
    request_url = _url_interface_properties.format(**{'config': 'operational', 
                                                      'topology-id': 'topology-netconf',
                                                      'node-id': node_id,
                                                      'interface-name': quote_plus(interface_name),
                                                      })
    response = odl_http_get(request_url, 'application/json', expected_status_code=[200, 400])
    return response.json()
def interface_properties(node_id, interface_name):
    """Retrieve the interface properties for a particular XRV node."""
    url_params = {
        "config": "operational",
        "topology-id": "topology-netconf",
        "node-id": node_id,
        "interface-name": interface_name,
    }
    response = odl_http_get(_url_interface_properties, url_params, "application/json", expected_status_code=[200, 400])
    return response.json()
示例#32
0
def capability(item_name):
    'Return a list of capability names for the specified device.'
    url_suffix = _url_inventory_node % quote_plus(item_name)
    response = odl_http_get(url_suffix, 'application/xml')
    tree = etree.parse(StringIO(response.text))
    initial_capability_list = tree.xpath(".//nni:initial-capability/text()", namespaces=_inventory_namespaces)
    if initial_capability_list:
        return initial_capability_list
    else:
        return tree.xpath(".//fi:switch-features/fi:capabilities/text()", namespaces=_inventory_namespaces)
示例#33
0
def get_loopback(device_name):
    _url_node_loopback_2 = _url_node_loopback_address % device_name
    device_address_response = odl_http_get(
        _url_node_loopback_2,
        {},
        'application/json',
    )
    ttt1 = json.loads(device_address_response.text)
    container = ttt1["Cisco-IOS-XR-ipv4-io-cfg:primary"]["address"]
    return container
示例#34
0
def acl_json_all(device_name):
    """ List of ACLs on one network device.

        Return a list where each element is a JSON representation of an ACL.
    """
    response = odl_http_get(_url_acl_all_template, {'node-id' : device_name}, 'application/json', [200, 404])
    if response.status_code == 404:
        return []
    else:
        return response.json()['accesses']['access']
def interface_names(device_name):
    'Return a list of interface names, given the name of a mounted, connected device.'
    url_suffix = _url_template % quote_plus(device_name)
    response = odl_http_get(url_suffix, 'application/xml', expected_status_code=[200, 400])
    if response.status_code == 400:
        return []  # The inventory item does not have interfaces.
    tree = etree.parse(StringIO(response.text))
    namespace = tree.getroot().tag[1:].split("}")[0]
    ns = {'n':namespace}
    return tree.xpath(".//n:system-view//n:interface[n:encapsulation/text()='ether']/n:interface-name/text()", namespaces=ns)
示例#36
0
def acl_json_all(device_name):
    """ List of ACLs on one network device.

        Return a list where each element is a JSON representation of an ACL.
    """
    response = odl_http_get(_url_acl_all_template, {'node-id' : device_name}, 'application/json', [200, 404])
    if response.status_code == 404:
        return []
    else:
        return response.json()['accesses']['access']
def interface_names(node_id):
    """Retrieve the interface names for a particular XRV node."""
    request_url = _url_interfaces.format(**{'config': 'operational', 
                                            'topology-id': 'topology-netconf',
                                            'node-id': node_id,
                                            })
    response = odl_http_get(request_url, 'application/json', expected_status_code=[200, 400])
    all_names = [ interface['interface-name'].encode('utf-8') for interface in response.json()['interfaces']['interface'] if interface['type'].lower().find('ethernet') >= 0 ] 
    management_name= management_interface_name(node_id)
    return sorted( [ name for name in all_names if name != management_name ] )
def mounted(node_id):
    """Determine whether a single device is mounted on the Controller.
    
    Return True if mounted, False otherwise.
    """
    request_url = _url_connector.format(**{'config': 'config', 
                                          'topology-id': 'topology-netconf',
                                          'node-id': quote_plus(node_id),
                                          })
    response = odl_http_get(request_url, 'application/json', expected_status_code=[200, 404])
    return response.status_code == 200
示例#39
0
def mounted(node_id):
    """Determine whether a single device is mounted on the Controller.
    
    Return True if mounted, False otherwise.
    """
    request_url = _url_connector.format(**{'config': 'config', 
                                          'topology-id': 'topology-netconf',
                                          'node-id': quote_plus(node_id),
                                          })
    response = odl_http_get(request_url, 'application/json', expected_status_code=[200, 404])
    return response.status_code == 200
def interface_names(node_id):
    """Retrieve the interface names for a particular XRV node."""
    url_params = {"config": "operational", "topology-id": "topology-netconf", "node-id": node_id}
    response = odl_http_get(_url_interfaces, url_params, "application/json", expected_status_code=[200, 400])
    all_names = [
        interface["interface-name"].encode("utf-8")
        for interface in response.json()["interfaces"]["interface"]
        if interface["type"].lower().find("ethernet") >= 0
    ]
    management_name = management_interface_name(node_id)
    return sorted([name for name in all_names if name != management_name])
示例#41
0
def cdp_neighbors_json(device_name):
    response = odl_http_get(url_neighbours, {'node-id': device_name},
                            'application/json',
                            expected_status_code=(200, 404))
    j = json_loads(response.text)
    container = j["cdp"]["nodes"]["node"][0]
    print(json_dumps(container, indent=2))
    if "neighbors" in container:
        neighbors = container["neighbors"]
        print(len(neighbors))
        print(json_dumps(neighbors, indent=2))
def connected(device_name):
    '''Determine whether a single device is connected to the Controller.
    
    Return True if connected.
    '''
    response = odl_http_get(_url_inventory_node, {'node-id' : device_name}, 'application/xml', expected_status_code=(200, 404))
    if response.status_code == 404:
        return False
    tree = etree.parse(StringIO(response.text))
    names = tree.xpath("/i:node[nni:connected/text()='true']/i:id/text()", namespaces=_inventory_namespaces)
    return len(names) > 0
示例#43
0
def cdp_neighbors_json(device_name):
    response = odl_http_get(url_neighbours.format(**{'node-id': quote_plus(device_name), }),
                            'application/json',
                            expected_status_code=(200, 404)
                            )
    j = json_loads(response.text)
    container = j["cdp"]["nodes"]["node"][0]
    print(json_dumps(container, indent=2))
    if "neighbors" in container:
        neighbors = container["neighbors"]
        print(len(neighbors))
        print(json_dumps(neighbors, indent=2))
示例#44
0
def static_route_json(device_name, destination_network):
    """ JSON representation of the specified 'static route' on the specified network device.
    
        Return None if not found.
    """
    assert isinstance(destination_network, _BaseNetwork)
    url_suffix = _static_route_uni_url_template % (device_name, destination_network)
    response = odl_http_get(url_suffix, 'application/json', expected_status_code=[200, 404])
    if response.status_code == 404:
        return None
    else:
        return response.json()['vrf-prefix'][0]
示例#45
0
def static_route_exists(device_name, destination_network):
    """ Determine whether the specified 'static route' exists on the specified device. """
    assert isinstance(destination_network, _BaseNetwork)
    url_params = {
        "node-id": device_name,
        "ip-address": destination_network.network_address,
        "prefix-length": destination_network.prefixlen,
    }
    response = odl_http_get(
        _static_route_uni_url_template, url_params, "application/json", expected_status_code=[200, 404]
    )
    return response.status_code == 200
示例#46
0
def connected(device_name):
    '''Determine whether a single device is connected to the Controller.
    
    Return True if connected.
    '''
    url_suffix = _url_inventory_node % quote_plus(device_name)
    response = odl_http_get(url_suffix, 'application/xml', expected_status_code=(200, 404))
    if response.status_code == 404:
        return False
    tree = etree.parse(StringIO(response.text))
    names = tree.xpath("/i:node[nni:connected/text()='true']/i:id/text()", namespaces=_inventory_namespaces)
    return len(names) > 0
示例#47
0
def interface_properties(node_id, interface_name):
    """Retrieve the interface properties for a particular XRV node."""
    url_params = {
        'config': 'operational',
        'topology-id': 'topology-netconf',
        'node-id': node_id,
        'interface-name': interface_name,
    }
    response = odl_http_get(_url_interface_properties,
                            url_params,
                            'application/json',
                            expected_status_code=[200, 400])
    return response.json()
def management_interface_name(node_id):
    """Retrieve the management interface name for a particular XRV node."""
    url_params = {"config": "config", "topology-id": "topology-netconf", "node-id": node_id}
    response = odl_http_get(
        _url_interface_configurations, url_params, "application/json", expected_status_code=[200, 400]
    )
    interfaces = response.json()["interface-configurations"]["interface-configuration"]
    return next(
        interface["interface-name"]
        for interface in interfaces
        if interface["Cisco-IOS-XR-ipv4-io-cfg:ipv4-network"]["addresses"]["primary"]["address"]
        == settings.config["network_device"][node_id]["address"]
    )
示例#49
0
def static_route_exists(device_name, destination_network):
    """ Determine whether the specified 'static route' exists on the specified device. """
    assert isinstance(destination_network, _BaseNetwork)
    url_params = {
        'node-id': device_name,
        'ip-address': destination_network.network_address,
        'prefix-length': destination_network.prefixlen
    }
    response = odl_http_get(_static_route_uni_url_template,
                            url_params,
                            'application/json',
                            expected_status_code=[200, 404])
    return response.status_code == 200
示例#50
0
def device_control(device_name):
    """A DeviceControl if the specified device is mounted, otherwise None."""
    response = odl_http_get(_url_connector, {'node-id':device_name}, 'application/xml', expected_status_code=[200, 404])
    if response.status_code == 404:
        return None
    tree = etree.parse(BytesIO(response.content))
    # print(etree.tostring(tree, pretty_print=True, xml_declaration=True))
    module_name = tree.findtext('/m:name', namespaces=_inventory_namespaces)
    assert module_name == device_name
    address = tree.findtext('/c:address', namespaces=_inventory_namespaces)
    port = tree.findtext('/c:port', namespaces=_inventory_namespaces)
    username = tree.findtext('/c:username', namespaces=_inventory_namespaces)
    password = tree.findtext('/c:password', namespaces=_inventory_namespaces)
    return DeviceControl(device_name, address=address, port=int(port), username=username, password=password)
示例#51
0
def device_control(device_name):
    """A DeviceControl if the specified device is mounted, otherwise None"""
    url_suffix = _url_connector % device_name
    response = odl_http_get(url_suffix, 'application/xml', expected_status_code=[200, 404])
    if response.status_code == 404:
        return None
    tree = etree.parse(BytesIO(response.content))
    module_name = tree.findtext('/m:name', namespaces=_inventory_namespaces)
    assert module_name == device_name
    address = tree.findtext('/c:address', namespaces=_inventory_namespaces)
    port = tree.findtext('/c:port', namespaces=_inventory_namespaces)
    username = tree.findtext('/c:username', namespaces=_inventory_namespaces)
    password = tree.findtext('/c:password', namespaces=_inventory_namespaces)
    return DeviceControl(address=address, port=port, username=username, password=password)
示例#52
0
def node(config, node_id):
    """Retrieve JSON for a single node based on node-id.
    
    Should be the only node in the list.
    """
    response = odl_http_get(_url_topology_node.format(**{'config': config, 
                                                         'topology-id': 'topology-netconf',
                                                         'node-id': quote_plus(node_id),
                                                         }), 
                            'application/json',
                            expected_status_code=(200, 404)
                            )
    if response.status_code == 404:
        return None
    return response.json()['node'][0]
示例#53
0
def topology(config):
    """Return the NETCONF topology JSON format.

    The topology should be the only topology in the list.
    The topology includes all mounted devices.
    The topology excludes all unmounted devices.
    The topology can contain devices that are neither mounted nor unmounted.
    The topology includes all connected devices.
    The topology can contain unconnected devices.
    """
    response = odl_http_get(_url_topology, {
        'config': config,
        'topology-id': 'topology-netconf',
    }, 'application/json')
    return response.json()['topology'][0]
示例#54
0
def acl_json(device_name, acl_name):
    ''' JSON representation of one ACL.
    
        Return JSON data structure if exists; otherwise None.
    '''
    url_suffix = _url_named_acl % (quote_plus(device_name),
                                   quote_plus(acl_name))
    response = odl_http_get(url_suffix,
                            'application/json',
                            expected_status_code=[200, 404])
    if response.status_code != 200:
        return None
    else:
        acl_list = response.json()
        return acl_list['access'][0]
示例#55
0
def acl_json(device_name, acl_name):
    ''' JSON representation of one ACL.
    
        Return JSON data structure if exists; otherwise None.
    '''
    url_params = {'node-id': device_name, 'access-id': acl_name}
    response = odl_http_get(_url_named_acl,
                            url_params,
                            'application/json',
                            expected_status_code=[200, 404])
    if response.status_code != 200:
        return None
    else:
        acl_list = response.json()
        return acl_list['access'][0]