Search in sources :

Example 21 with BgpNeighbor

use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.

the class EncoderSlice method addEnvironmentVariables.

/*
   * Initialize all environment symbolic records for BGP.
   */
private void addEnvironmentVariables() {
    // If not the main slice, just use the main slice
    if (!isMainSlice()) {
        Map<LogicalEdge, SymbolicRoute> envs = _logicalGraph.getEnvironmentVars();
        EncoderSlice main = _encoder.getMainSlice();
        LogicalGraph lg = main.getLogicalGraph();
        Map<LogicalEdge, SymbolicRoute> existing = lg.getEnvironmentVars();
        envs.putAll(existing);
        return;
    }
    // Otherwise create it anew
    for (String router : getGraph().getRouters()) {
        for (Protocol proto : getProtocols().get(router)) {
            if (proto.isBgp()) {
                List<ArrayList<LogicalEdge>> les = _logicalGraph.getLogicalEdges().get(router, proto);
                assert (les != null);
                for (ArrayList<LogicalEdge> eList : les) {
                    for (LogicalEdge e : eList) {
                        if (e.getEdgeType() == EdgeType.IMPORT) {
                            GraphEdge ge = e.getEdge();
                            BgpNeighbor n = getGraph().getEbgpNeighbors().get(ge);
                            if (n != null && ge.getEnd() == null) {
                                if (!isMainSlice()) {
                                    LogicalGraph lg = _encoder.getMainSlice().getLogicalGraph();
                                    SymbolicRoute r = lg.getEnvironmentVars().get(e);
                                    _logicalGraph.getEnvironmentVars().put(e, r);
                                } else {
                                    String address;
                                    if (n.getAddress() == null) {
                                        address = "null";
                                    } else {
                                        address = n.getAddress().toString();
                                    }
                                    String ifaceName = "ENV-" + address;
                                    String name = String.format("%d_%s%s_%s_%s_%s", _encoder.getId(), _sliceName, router, proto.name(), "EXPORT", ifaceName);
                                    SymbolicRoute vars = new SymbolicRoute(this, name, router, proto, _optimizations, null, ge.isAbstract());
                                    getAllSymbolicRecords().add(vars);
                                    _logicalGraph.getEnvironmentVars().put(e, vars);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) ArrayList(java.util.ArrayList) IpProtocol(org.batfish.datamodel.IpProtocol) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) Protocol(org.batfish.symbolic.Protocol) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 22 with BgpNeighbor

use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.

the class Graph method initIbgpNeighbors.

// TODO: very inefficient
/*
   * Initialize iBGP neighbors by looking for nieghbors
   * with the same AS number.
   */
private void initIbgpNeighbors() {
    Map<String, Ip> ips = new HashMap<>();
    Table2<String, String, BgpNeighbor> neighbors = new Table2<>();
    // Match iBGP sessions with pairs of routers and BgpNeighbor
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        BgpProcess p = conf.getDefaultVrf().getBgpProcess();
        if (p != null) {
            for (BgpNeighbor n : p.getNeighbors().values()) {
                if (n.getLocalAs().equals(n.getRemoteAs())) {
                    ips.put(router, n.getLocalIp());
                }
            }
        }
    }
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        BgpProcess p = conf.getDefaultVrf().getBgpProcess();
        if (p != null) {
            for (Entry<Prefix, BgpNeighbor> entry2 : p.getNeighbors().entrySet()) {
                Prefix pfx = entry2.getKey();
                BgpNeighbor n = entry2.getValue();
                if (n.getLocalAs().equals(n.getRemoteAs())) {
                    for (Entry<String, Ip> ipEntry : ips.entrySet()) {
                        String r = ipEntry.getKey();
                        Ip ip = ipEntry.getValue();
                        if (!router.equals(r) && pfx.containsIp(ip)) {
                            neighbors.put(router, r, n);
                        }
                    }
                }
            }
        }
    }
    // Add abstract graph edges for iBGP sessions
    Table2<String, String, GraphEdge> reverse = new Table2<>();
    neighbors.forEach((r1, r2, n1) -> {
        Interface iface1 = createIbgpInterface(n1, r2);
        BgpNeighbor n2 = neighbors.get(r2, r1);
        GraphEdge ge;
        if (n2 != null) {
            Interface iface2 = createIbgpInterface(n2, r1);
            ge = new GraphEdge(iface1, iface2, r1, r2, true, false);
        } else {
            ge = new GraphEdge(iface1, null, r1, null, true, false);
        }
        _allEdges.add(ge);
        _ibgpNeighbors.put(ge, n1);
        reverse.put(r1, r2, ge);
        List<GraphEdge> edges = _edgeMap.get(r1);
        if (edges != null) {
            edges.add(ge);
        } else {
            edges = new ArrayList<>();
            edges.add(ge);
            _edgeMap.put(r1, edges);
        }
    });
    // Add other end to ibgp edges
    reverse.forEach((r1, r2, ge1) -> {
        GraphEdge ge2 = reverse.get(r2, r1);
        _otherEnd.put(ge1, ge2);
    });
    // Configure Route Reflector information
    Integer[] id = new Integer[1];
    id[0] = 1;
    neighbors.forEach((r1, ns) -> {
        if (!_originatorId.containsKey(r1)) {
            _originatorId.put(r1, id[0]);
            id[0]++;
        }
        Set<String> clients = new HashSet<>();
        ns.forEach((r2, n) -> {
            if (n.getRouteReflectorClient()) {
                clients.add(r2);
                _routeReflectorParent.put(r2, r1);
            }
        });
        _routeReflectorClients.put(r1, clients);
    });
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) BgpProcess(org.batfish.datamodel.BgpProcess) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) Table2(org.batfish.symbolic.collections.Table2) Interface(org.batfish.datamodel.Interface) HashSet(java.util.HashSet)

Example 23 with BgpNeighbor

use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.

the class FlatJuniperGrammarTest method testBgpClusterId.

@Test
public void testBgpClusterId() throws IOException {
    String testrigName = "rr";
    String configName = "rr";
    Ip neighbor1Ip = new Ip("2.2.2.2");
    Ip neighbor2Ip = new Ip("4.4.4.4");
    List<String> configurationNames = ImmutableList.of(configName);
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(TESTRIGS_PREFIX + testrigName, configurationNames).build(), _folder);
    Map<String, Configuration> configurations = batfish.loadConfigurations();
    Configuration rr = configurations.get(configName);
    BgpProcess proc = rr.getDefaultVrf().getBgpProcess();
    BgpNeighbor neighbor1 = proc.getNeighbors().get(new Prefix(neighbor1Ip, Prefix.MAX_PREFIX_LENGTH));
    BgpNeighbor neighbor2 = proc.getNeighbors().get(new Prefix(neighbor2Ip, Prefix.MAX_PREFIX_LENGTH));
    assertThat(neighbor1, hasClusterId(new Ip("3.3.3.3").asLong()));
    assertThat(neighbor2, hasClusterId(new Ip("1.1.1.1").asLong()));
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) Configuration(org.batfish.datamodel.Configuration) BgpProcess(org.batfish.datamodel.BgpProcess) VrfMatchers.hasBgpProcess(org.batfish.datamodel.matchers.VrfMatchers.hasBgpProcess) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Batfish(org.batfish.main.Batfish) Test(org.junit.Test)

Aggregations

BgpNeighbor (org.batfish.datamodel.BgpNeighbor)23 Ip (org.batfish.datamodel.Ip)15 Prefix (org.batfish.datamodel.Prefix)13 Configuration (org.batfish.datamodel.Configuration)10 BatfishException (org.batfish.common.BatfishException)9 BgpProcess (org.batfish.datamodel.BgpProcess)9 TreeSet (java.util.TreeSet)7 Interface (org.batfish.datamodel.Interface)7 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)5 GraphEdge (org.batfish.symbolic.GraphEdge)5 AsPath (org.batfish.datamodel.AsPath)4 BgpAdvertisement (org.batfish.datamodel.BgpAdvertisement)4 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)4 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)4 BgpAdvertisementType (org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType)3 BgpRoute (org.batfish.datamodel.BgpRoute)3