当前位置: 首页>>代码示例>>Python>>正文


Python AutoNetkit.nodes_by_as方法代码示例

本文整理汇总了Python中AutoNetkit.nodes_by_as方法的典型用法代码示例。如果您正苦于以下问题:Python AutoNetkit.nodes_by_as方法的具体用法?Python AutoNetkit.nodes_by_as怎么用?Python AutoNetkit.nodes_by_as使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AutoNetkit的用法示例。


在下文中一共展示了AutoNetkit.nodes_by_as方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: graph_to_ank

# 需要导入模块: import AutoNetkit [as 别名]
# 或者: from AutoNetkit import nodes_by_as [as 别名]
def graph_to_ank(network, graph, asn=None, include_ext_nodes=True):
    """ Converts a GML graph from the Zoo into AutoNetkit compatible graph"""
    # Default label
    if not 'Network' in graph.graph:
        graph.graph['Network'] = "Network"

    # if no asn set, use next available
    #TODO: put this into a function which returns a graph for combining at the
    # end
    LOG.debug("passed in asn of %s " % asn)
    if graph.is_multigraph():
        graph = nx.DiGraph(graph)
        if 'Network' in graph.graph:
            LOG.info(("Converting {0} to single-edge graph").format(
                graph.graph['Network']))
        else:
            LOG.info("Converting to single-edge graph")
    # ANK uses directed graphs
    graph = graph.to_directed()
    #TODO: see if ASN set in the graph
    asn_list = ank.nodes_by_as(network).keys()

    LOG.debug("current ASN list %s " % asn_list)

    # And append any ASNs manually specified
    manual_asn = {}
    for node, data in graph.nodes(data=True):
        if ('type' in data and data['type'].startswith("AS") and
            data['type'][2:].isdigit()):
            # This node has a valid manually specified ASN
            node_asn = int(data['type'][2:])
            manual_asn[node] = node_asn

    # Unique
    manual_asn_unique = list(set(manual_asn.keys()))
    for node_asn in manual_asn_unique:
        if node_asn in asn_list:
            LOG.warn("Manually specified ASN %i already in use" % asn)
    # Record these as in use
    asn_list += manual_asn_unique
    LOG.debug("asn list after adding manual uniques %s " % asn_list)

    # Allocate asns
    # Find next free asn
    def next_unallocated_asn():
        LOG.debug("nua fn asn list %s " % asn_list)
        if len(asn_list) > 0:
            nua = max(asn_list)
            while nua in asn_list:
                try:
                    nua += 1
                except TypeError as e:
                    LOG.warn("Unable to set Next Unallocated ASN %i to ASN list"
                             " %s " % (nua, asn_list))

                asn_list.append(nua)
                return nua
        else:
            # No asn in use
            nua = 1
            asn_list.append(nua)
            return nua

    #TODO: clean up this logic
    if asn and asn in asn_list:
        # User specified asn already in use
        LOG.warn("ASN %s already in use" % asn)
    elif asn:
        # Record as being used
        asn_list.append(int(asn))
        # Record name for DNS
        network.as_names[asn] = graph.graph['Network']
    else:
        # No asn set, use next available
        asn = next_unallocated_asn()
        # and record
        network.as_names[asn] = graph.graph['Network']

    #TODO: check that writing using names doesn't overwrite the  same folder
    # eg may have 5 AARNET nodes, but all overwrite same folder in nk lab
    # If multiple external nodes with same name, merge into single node
    # TODO: make this behaviour able to be turned on/off
    # eg either merge, keep, make unique, or remove, depending on how
    # external names combined
    #TODO: use include_external_nodes
    external_nodes = [node for node in graph.nodes()
                        if ('Internal' in
                            graph.node[node] and
                            graph.node[node]['Internal'] == 0)]
    # Group external nodes by their name
    ext_node_dict = defaultdict(list)
    for node in external_nodes:
        if not include_ext_nodes:
            graph.remove_node(node)
        else:
            label = graph.node[node]['label']
            ext_node_dict[label].append(node)
            # Now merge nodes
            for label, nodes in ext_node_dict.items():
                if len(nodes) > 1:
#.........这里部分代码省略.........
开发者ID:coaj,项目名称:autonetkit,代码行数:103,代码来源:zoo.py


注:本文中的AutoNetkit.nodes_by_as方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。