コード例 #1
0
 def test_add_peer_link(self):
     # init
     g = NetworkGraph()
     router1 = 'R1'
     router2 = 'R2'
     g.add_router(router1)
     g.add_peer(router2)
     # Action
     g.add_peer_edge(router1, router2)
     # Assert
     self.assertEqual(list(g.edges()), [(router1, router2)])
     self.assertEqual(g[router1][router2][EDGE_TYPE], EDGETYPE.PEER)
コード例 #2
0
 def test_add_peer(self):
     # init
     g = NetworkGraph()
     peer = 'PEER1'
     # Action
     g.add_peer(peer)
     # Asserts
     self.assertTrue(g.has_node(peer))
     self.assertTrue(g.is_peer(peer))
     self.assertTrue(g.is_router(peer))
     self.assertEqual(g.node[peer][VERTEX_TYPE], VERTEXTYPE.PEER)
     self.assertEqual(list(g.peers_iter()), [peer])
     self.assertFalse(g.is_local_router(peer))
     self.assertFalse(g.is_network(peer))
コード例 #3
0
def ibgp_net():
    graph = NetworkGraph()
    peer, r1, r2, r3, r4 = 'Ext', 'R1', 'R2', 'R3', 'R4'
    # Add routers
    graph.add_peer(peer)
    graph.add_router(r1)
    graph.add_router(r2)
    graph.add_router(r3)
    graph.add_router(r4)
    # Connect routers
    graph.add_peer_edge(peer, r1)
    graph.add_peer_edge(r1, peer)
    graph.add_router_edge(r1, r2)
    graph.add_router_edge(r2, r1)
    graph.add_router_edge(r1, r3)
    graph.add_router_edge(r3, r1)
    graph.add_router_edge(r4, r2)
    graph.add_router_edge(r2, r4)
    graph.add_router_edge(r4, r3)
    graph.add_router_edge(r3, r4)
    # Enable OSPF
    graph.enable_ospf(r1, 100)
    graph.enable_ospf(r2, 100)
    graph.enable_ospf(r3, 100)
    graph.enable_ospf(r4, 100)
    # Set BGP ASN
    graph.set_bgp_asnum(peer, 200)
    graph.set_bgp_asnum(r1, 100)
    graph.set_bgp_asnum(r2, 100)
    graph.set_bgp_asnum(r3, 100)
    graph.set_bgp_asnum(r4, 100)
    # Establish BGP peering
    graph.add_bgp_neighbor(peer, r1)
    graph.add_bgp_neighbor(r1, r2)
    graph.add_bgp_neighbor(r1, r3)
    graph.add_bgp_neighbor(r1, r4)
    graph.add_bgp_neighbor(r2, r3)
    graph.add_bgp_neighbor(r2, r4)
    graph.add_bgp_neighbor(r3, r4)
    # Assign interfaces
    graph.set_iface_names()
    # Some internal to be announced network
    net = ip_network(u'128.0.0.0/24')
    prefix = '128_0_0_0'
    lo0 = 'lo0'
    prefix_map = {prefix: net}
    loaddr = ip_interface("%s/%d" % (net.hosts().next(), net.prefixlen))
    graph.set_loopback_addr(peer, lo0, loaddr)
    graph.add_bgp_announces(peer, lo0)

    # Synthesize connectivity
    syn = ConnectedSyn(graph, full=True, default_ibgp_lo='lo10')
    assert syn.synthesize()

    # Some route map
    actions = [ActionSetLocalPref(200)]
    line = RouteMapLine(matches=None,
                        actions=actions,
                        access=Access.permit,
                        lineno=100)
    route_map = RouteMap(name="ImpPolicy", lines=[line])
    graph.add_route_map(r1, route_map)
    graph.add_bgp_import_route_map(r1, peer, route_map.name)
    # Add networks
    # Note, some of the loop back interfaces where generated due to
    # ibgp peering in ConnectedSyn
    for router in graph.local_routers_iter():
        for iface in graph.get_ifaces(router):
            graph.add_ospf_network(router, iface, 0)
        for lo in graph.get_loopback_interfaces(router):
            graph.add_ospf_network(router, lo, 0)

    # Write the configs
    gns3 = GNS3Topo(graph, prefix_map=prefix_map)
    gns3.write_configs('out-configs/ibgp')