示例#1
0
def load(input_graph_string):
    # TODO: look at XML header for file type
    import autonetkit.load.graphml as graphml
    import autonetkit.load.load_json as load_json
    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
        try:
            input_graph = load_json.load_json(input_graph_string)
        except (ValueError, autonetkit.exception.AnkIncorrectFileFormat):
            # try a different reader
            try:
                from autonetkit_cisco import load as cisco_load
            except ImportError, error:
                log.debug("Unable to load autonetkit_cisco %s", error)
                return  # module not present (development module)
            else:
                input_graph = cisco_load.load(input_graph_string)
                # add local deployment host
                SETTINGS['General']['deploy'] = True
                SETTINGS['Deploy Hosts']['internal'] = {
                    'VIRL': {
                        'deploy': True,
                    },
                }
示例#2
0
def load(input_graph_string, defaults=True):

    # TODO: look at XML header for file type
    import autonetkit.load.graphml as graphml
    import autonetkit.load.load_json as load_json
    try:
        input_graph = graphml.load_graphml(input_graph_string,
                                           defaults=defaults)
    except autonetkit.exception.AnkIncorrectFileFormat:
        try:
            input_graph = load_json.load_json(input_graph_string,
                                              defaults=defaults)
        except (ValueError, autonetkit.exception.AnkIncorrectFileFormat):
            # try a different reader
            try:
                from autonetkit_cisco import load as cisco_load
            except ImportError, error:
                log.debug("Unable to load autonetkit_cisco %s", error)
                return  # module not present (development module)
            else:
                input_graph = cisco_load.load(input_graph_string)
                # add local deployment host
                SETTINGS['General']['deploy'] = True
                SETTINGS['Deploy Hosts']['internal'] = {
                    'VIRL': {
                        'deploy': True,
                    },
                }
    def Load(self, graphml_file_path):

        if not graphml_file_path:
            raise ValueError("graphml_file_path", properties.resources['value_null'])

        try:
            self.network_graph = graphml.load_graphml(graphml_file_path)
            return self.network_graph

        except Exception, e:
            raise exception.GraphLoaderException(properties.resources['graph_file_load_failed'], e)
示例#4
0
def build_anm(topology_name):
    print "Building anm for %s" % topology_name
    dirname, filename = os.path.split(os.path.abspath(__file__))
    input_filename = os.path.join(dirname, "%s.graphml" % topology_name)

    anm =  autonetkit.ANM()
    input_graph = graphml.load_graphml(input_filename)

    import autonetkit.build_network as build_network
    anm = build_network.initialise(input_graph)
    anm = build_network.apply_design_rules(anm)
    return anm
示例#5
0
def build_anm(topology_name):
    print "Building anm for %s" % topology_name
    dirname, filename = os.path.split(os.path.abspath(__file__))
    input_filename = os.path.join(dirname, "%s.graphml" % topology_name)

    anm = autonetkit.ANM()
    input_graph = graphml.load_graphml(input_filename)

    import autonetkit.build_network as build_network
    anm = build_network.initialise(input_graph)
    anm = build_network.apply_design_rules(anm)
    return anm
示例#6
0
def build_overlays(filename):

    anm = autonetkit.anm.AbstractNetworkModel()
    input_graph = graphml.load_graphml(filename)
    G_in = anm.add_overlay("input", graph=input_graph)

    G_graphics = anm.add_overlay("graphics")  # plotting data
    G_graphics.add_nodes_from(G_in, retain=['x', 'y', 'device_type', 'asn'])

    G_phy = anm['phy']
    G_phy.add_nodes_from(
        G_in,
        retain=['label', 'device_type', 'asn', 'platform', 'host', 'syntax'])
    G_phy.add_edges_from(G_in.edges(type="physical"))
    G_phy.update(G_phy, syntax="quagga")

    routers = list(G_in.routers())
    G_ospf = anm.add_overlay("ospf", G_in.routers())
    G_ospf.add_edges_from(e for e in G_in.edges() if e.src.asn == e.dst.asn)
    G_ospf.update(area=0)  # set defaults
    G_ospf.update_edges(area=0)

    G_ebgp = anm.add_overlay("ebgp", G_in.routers(), directed=True)
    G_ebgp.add_edges_from((e for e in G_in.edges() if e.src.asn != e.dst.asn),
                          bidirectional=True)

    G_ibgp = anm.add_overlay("ibgp", G_in.routers(), directed=True)
    G_ibgp.add_edges_from(
        ((s, t) for s in routers for t in routers if s.asn == t.asn),
        bidirectional=True)

    # hierarchical
    G_ibgp = anm.add_overlay("ibgp_rr", G_in.routers(), directed=True)

    graph_phy = ank_utils.unwrap_graph(G_phy)
    centrality = nx.degree_centrality(graph_phy)
    rrs = [n for n in centrality if centrality[n] > 0.13]

    for rr in rrs:
        G_ibgp.node(rr).route_reflector = True

    rrs = set(r for r in G_ibgp if r.route_reflector)
    clients = set(G_ibgp) - rrs
    G_ibgp.add_edges_from(((s, t) for s in clients for t in rrs),
                          direction="up")
    G_ibgp.add_edges_from(((s, t) for s in rrs for t in clients),
                          direction="down")
    G_ibgp.add_edges_from(((s, t) for s in rrs for t in rrs), direction="over")

    build_ip(anm)

    return anm
示例#7
0
def load(input_graph_string):
    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
        # try a different reader
        try:
            from autonetkit_cisco import load as cisco_load
        except ImportError:
            return  # module not present (development module)
        input_graph = cisco_load.load(input_graph_string)
        # add local deployment host
        SETTINGS["General"]["deploy"] = True
        SETTINGS["Deploy Hosts"]["internal"] = {"cisco": {"deploy": True}}

    return input_graph
示例#8
0
def build_overlays(filename):

    anm = autonetkit.anm.AbstractNetworkModel()
    input_graph = graphml.load_graphml(filename)
    G_in = anm.add_overlay("input", graph = input_graph)

    G_graphics = anm.add_overlay("graphics") # plotting data
    G_graphics.add_nodes_from(G_in, retain=['x', 'y', 'device_type', 'asn'])
    
    G_phy = anm['phy']
    G_phy.add_nodes_from(G_in, retain=['label', 'device_type', 'asn', 'platform', 'host', 'syntax'])
    G_phy.add_edges_from(G_in.edges(type="physical"))
    G_phy.update(G_phy, syntax="quagga")

    routers = list(G_in.routers())
    G_ospf = anm.add_overlay("ospf", G_in.routers())
    G_ospf.add_edges_from(e for e in G_in.edges() if e.src.asn == e.dst.asn)
    G_ospf.update(area=0) # set defaults
    G_ospf.update_edges(area=0)

    G_ebgp = anm.add_overlay("ebgp", G_in.routers(), directed = True)
    G_ebgp.add_edges_from((e for e in G_in.edges() if e.src.asn != e.dst.asn), bidirectional = True)

    G_ibgp = anm.add_overlay("ibgp", G_in.routers(), directed = True)
    G_ibgp.add_edges_from(((s, t) for s in routers for t in routers if s.asn == t.asn), bidirectional = True)

    # hierarchical
    G_ibgp = anm.add_overlay("ibgp_rr", G_in.routers(), directed = True)

    graph_phy = ank_utils.unwrap_graph(G_phy)
    centrality = nx.degree_centrality(graph_phy) 
    rrs = [n for n in centrality if centrality[n] > 0.13]

    for rr in rrs:
        G_ibgp.node(rr).route_reflector = True

    rrs = set(r for r in G_ibgp if r.route_reflector)
    clients = set(G_ibgp) - rrs
    G_ibgp.add_edges_from(((s, t) for s in clients for t in rrs), direction = "up")
    G_ibgp.add_edges_from(((s, t) for s in rrs for t in clients), direction = "down")
    G_ibgp.add_edges_from(((s, t) for s in rrs for t in rrs), direction = "over")

    build_ip(anm)

    return anm
示例#9
0
def load(input_graph_string):
    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
# try a different reader
        try:
            from autonetkit_cisco import load as cisco_load
        except ImportError, e:
            log.debug("Unable to load autonetkit_cisco %s" % e)
            return  # module not present (development module)
        input_graph = cisco_load.load(input_graph_string)
# add local deployment host
        SETTINGS['General']['deploy'] = True
        SETTINGS['Deploy Hosts']['internal'] = {
            'cisco': {
            'deploy': True,
            },
        }
示例#10
0
def load(input_graph_string):
    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
        # try a different reader
        try:
            from autonetkit_cisco import load as cisco_load
        except ImportError:
            return  # module not present (development module)
        input_graph = cisco_load.load(input_graph_string)
        # add local deployment host
        SETTINGS['General']['deploy'] = True
        SETTINGS['Deploy Hosts']['internal'] = {
            'cisco': {
                'deploy': True,
            },
        }

    return input_graph
示例#11
0
def test():


    automated = True # whether to open ksdiff, log to file...
    if __name__ == "__main__":
        automated = False

    dirname, filename = os.path.split(os.path.abspath(__file__))

    anm =  autonetkit.NetworkModel()
    input_file = os.path.join(dirname, "small_internet.graphml")
    input_graph = graphml.load_graphml(input_file)

    import autonetkit.build_network as build_network
    anm = build_network.initialise(input_graph)
    anm = build_network.apply_design_rules(anm)
    anm.save()
    anm_restored =  autonetkit.NetworkModel()
    anm_restored.restore_latest()
    g_phy_original = anm['phy']
    g_phy_restored = anm_restored['phy']
    assert(all(n in g_phy_restored for n in g_phy_original))

    #TODO: do more extensive deep check of parameters

    import autonetkit.workflow as workflow
    render_hostname = "localhost"

    nidb = workflow.create_nidb(anm)
    import autonetkit.compilers.platform.netkit as pl_netkit
    nk_compiler = pl_netkit.NetkitCompiler(nidb, anm, render_hostname)
    nk_compiler.compile()

    nidb.save()
    nidb_restored =  autonetkit.DeviceModel()
    nidb_restored.restore_latest()
    assert(all(n in nidb_restored for n in nidb))

    # cleanup
    shutil.rmtree("versions")
示例#12
0
def load(input_graph_string):
    # TODO: look at XML header for file type
    import autonetkit.load.graphml as graphml
    import autonetkit.load.load_json as load_json

    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
        try:
            input_graph = load_json.load_json(input_graph_string)
        except autonetkit.exception.AnkIncorrectFileFormat:
            # try a different reader
            try:
                from autonetkit_cisco import load as cisco_load
            except ImportError, e:
                log.debug("Unable to load autonetkit_cisco %s" % e)
                return  # module not present (development module)
            else:
                input_graph = cisco_load.load(input_graph_string)
                # add local deployment host
                SETTINGS["General"]["deploy"] = True
                SETTINGS["Deploy Hosts"]["internal"] = {"VIRL": {"deploy": True}}
示例#13
0
import autonetkit
import os
import autonetkit.load.graphml as graphml
import shutil

automated = True # whether to open ksdiff, log to file...
if __name__ == "__main__":
    automated = False

dirname, filename = os.path.split(os.path.abspath(__file__))

anm =  autonetkit.ANM()
input_file = os.path.join(dirname, "small_internet.graphml")
input_graph = graphml.load_graphml(input_file)

import autonetkit.build_network as build_network
anm = build_network.initialise(input_graph)
anm = build_network.apply_design_rules(anm)
anm.save()
anm_restored =  autonetkit.ANM()
anm_restored.restore_latest()
g_phy_original = anm['phy']
g_phy_restored = anm_restored['phy']
assert(all(n in g_phy_restored for n in g_phy_original))

#TODO: do more extensive deep check of parameters

import autonetkit.console_script as console_script
render_hostname = "localhost"

nidb = console_script.create_nidb(anm)
示例#14
0
def test():
	automated = True # whether to open ksdiff, log to file...
	if __name__ == "__main__":
	    automated = False

	dirname, filename = os.path.split(os.path.abspath(__file__))
	parent_dir = os.path.abspath(os.path.join(dirname, os.pardir))

	anm =  autonetkit.NetworkModel()
	input_file = os.path.join(parent_dir, "small_internet.graphml")
	input_graph = graphml.load_graphml(input_file)

	import autonetkit.build_network as build_network
	anm = build_network.initialise(input_graph)
	anm = build_network.apply_design_rules(anm)



	try:
		from websocket import create_connection
	except ImportError:
		print "websocket-client package not installed"
	else:
		autonetkit.update_http(anm)

		ws = create_connection("ws://localhost:8000/ws")
		ws.send("overlay_list")
		result =  ws.recv()
		expected = '{"overlay_list": ["bgp", "ebgp", "ebgp_v4", "ebgp_v6", "eigrp", graphics", "ibgp_v4", "ibgp_v6", "ibgp_vpn_v4", "input", "input_directed", "ip", "ipv4", "ipv6", "isis", "l3_conn", "ospf", "phy", "vrf"]}'
		assert(result == expected)

		overlay_id = "phy"
		ws.send("overlay_id=" + overlay_id)
		result =  ws.recv()
		with open(os.path.join(dirname, "expected_phy.json"), "r") as fh:
			expected = fh.read()

		assert(result == expected)
		ws.close()

		import autonetkit.console_script as console_script
		render_hostname = "localhost"

		nidb = console_script.create_nidb(anm)
		nidb._graph.graph['timestamp'] = "123456"

		import autonetkit.compilers.platform.netkit as pl_netkit
		nk_compiler = pl_netkit.NetkitCompiler(nidb, anm, render_hostname)
		nk_compiler.compile()
		autonetkit.update_http(anm, nidb)

		ws = create_connection("ws://localhost:8000/ws")
		ws.send("overlay_list")
		result =  ws.recv()
		expected = '{"overlay_list": ["bgp", "ebgp", "ebgp_v4", "ebgp_v6", "eigrp", graphics", "ibgp_v4", "ibgp_v6", "ibgp_vpn_v4", "input", "input_directed", "ip", "ipv4", "ipv6", "isis", "l3_conn", "nidb", "ospf", "phy", "vrf"]}'
		assert(result == expected)

		overlay_id = "nidb"
		ws.send("overlay_id=" + overlay_id)
		result =  ws.recv()
		with open(os.path.join(dirname, "expected_nidb.json"), "r") as fh:
			expected = fh.read()

		assert(result == expected)
		ws.close()
示例#15
0
def build(input_graph_string, timestamp):
    #TODO: move this out of main console wrapper
    anm = autonetkit.anm.AbstractNetworkModel()
    
    try:
        input_graph = graphml.load_graphml(input_graph_string)
    except autonetkit.exception.AnkIncorrectFileFormat:
# try a different reader
        try:
            from autonetkit_cisco import load as cisco_load
        except ImportError:
            return # module not present (development module)
        input_graph = cisco_load.load(input_graph_string)
# add local deployment host
        settings['General']['deploy'] = True
        settings['Deploy Hosts']['internal'] = {
                'cisco': {
                    'deploy': True,
                    },
                }

    #TODO: make this more explicit than overloading add_overlay - make it load_graph or something similar

#TODO: may need to revisit the collapse to a single directed graph: may need to consider link type, eg physical, when reducing directed to undirected graph
#Note: this may also require stripping edge specific information, such as IP addressing that only applies in one direction for a directed edge
    input_undirected = nx.Graph(input_graph)
    for node in input_graph:
        #del input_graph.node[node]['router config']
        #del input_graph.node[node]['device_subtype']
        pass
    #nx.write_graphml(input_graph, "output.graphml")
    G_in = anm.add_overlay("input", graph = input_undirected)
    G_in_directed = anm.add_overlay("input_directed", graph = input_graph, directed = True)

# set defaults
    if not G_in.data.specified_int_names:
        G_in.data.specified_int_names = False # if not specified then automatically assign interface names

    import autonetkit.plugins.graph_product as graph_product
    graph_product.expand(G_in) # apply graph products if relevant
    
    expand_fqdn = False #TODO: make this set from config and also in the input file
    if expand_fqdn and len(ank.unique_attr(G_in, "asn")) > 1:
        # Multiple ASNs set, use label format device.asn 
        anm.set_node_label(".",  ['label', 'pop', 'asn'])

#TODO: remove, used for demo on nectar
    #for node in G_in:
        #node.platform = "netkit"
        #node.host = "nectar1"
    #G_in.data.igp = "ospf"

# set syntax for routers according to platform
#TODO: make these defaults
    G_in.update(G_in.nodes("is_router", platform = "junosphere"), syntax="junos")
    G_in.update(G_in.nodes("is_router", platform = "dynagen"), syntax="ios")
    G_in.update(G_in.nodes("is_router", platform = "netkit"), syntax="quagga")
    #G_in.update(G_in.nodes("is_router", platform = "cisco"), syntax="ios2")

    G_graphics = anm.add_overlay("graphics") # plotting data
    G_graphics.add_nodes_from(G_in, retain=['x', 'y', 'device_type', 'device_subtype', 'pop', 'asn'])

    build_phy(anm)
    #update_messaging(anm)
    #build_conn(anm)
    build_ip(anm)
    build_ip6(anm)
    
    igp = G_in.data.igp or "ospf" #TODO: make default template driven
#TODO: make the global igp be set on each node - this way can also support different IGPs per router

# Add overlays even if not used: simplifies compiler where can check for presence in overlay (if blank not present, don't configure ospf etc)
    anm.add_overlay("ospf")
    anm.add_overlay("isis")

    G_phy = anm['phy']
    ank.copy_attr_from(G_in, G_phy, "include_csr") #TODO: find more elegant passing method from input to compiler

    if igp == "ospf":
        build_ospf(anm)
    if igp == "isis":
        build_isis(anm)
    build_bgp(anm)

    #TODO: provide an ANM wide function that allocates interfaces
    #TODO: work out why some interfaces in bgp graph in vis have node data....
    for node in G_phy:
        for interface in node:
            interface.speed = 102

    return anm
def test():
    automated = True # whether to open ksdiff, log to file...
    if __name__ == "__main__":
        automated = False

    dirname, filename = os.path.split(os.path.abspath(__file__))

    anm =  autonetkit.NetworkModel()
    input_file = os.path.join(dirname, "small_internet.graphml")
    input_graph = graphml.load_graphml(input_file)

    import autonetkit.build_network as build_network
    anm = build_network.initialise(input_graph)
    anm = build_network.apply_design_rules(anm)

    import autonetkit.console_script as console_script
    render_hostname = "localhost"

    nidb = console_script.create_nidb(anm)
    import autonetkit.compilers.platform.netkit as pl_netkit
    nk_compiler = pl_netkit.NetkitCompiler(nidb, anm, render_hostname)
    nk_compiler.compile()

    import autonetkit.render
    autonetkit.render.render(nidb)

    import os
    dst_folder = nidb.topology['localhost'].render_dst_folder

    # test folder structure
    dir_structure = {}
    for path, dirs, files in os.walk(dst_folder):
        dir_structure[path] = list(files)

    # record folder structure
    structure_filename = os.path.join(dirname, "dir_structure_expected.tar.gz")
    json_to_gzip(dir_structure, structure_filename)
    dir_structure_expected = gzip_to_json(structure_filename)
    assert dir_structure == dir_structure_expected

    routernames = ["as1r1", "as20r3"]
    config_files = ["bgpd.conf", "ospfd.conf", "zebra.conf"]

    for routername in routernames:
        router = nidb.node(routername)
        zebra_dir = os.path.join(router.render.base_dst_folder, "etc", "zebra")

        for conf_file in config_files:
            expected_filename = os.path.join(dirname, "%s_%s" % (routername, conf_file))
            with open(expected_filename, "r") as fh:
                expected_result = fh.read()

            actual_filename = os.path.join(zebra_dir, conf_file)
            with open(actual_filename, "r") as fh:
                actual_result = fh.read()

            if expected_result != actual_result:
                if automated:
                    #TODO: use difflib
                    print "Expected"
                    print expected_result
                    print "Actual"
                    print actual_result
                    raise AssertionError("Invalid result")
                else:
                    cmd = ["ksdiff", expected_filename, actual_filename]
                    child = subprocess.Popen(cmd)
                    answer = raw_input("Merge (answer yes to merge): ")
                    if answer == "yes":
                        print "Replacing expected with output"
                        shutil.move(actual_filename, expected_filename)