Search in sources :

Example 31 with Configuration

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

the class Graph method initEbgpNeighbors.

/*
   * Initialize external eBGP neighbors by looking for BGP neighbors
   * where their is no neighbor in the configurations, and the IPs align.
   */
private void initEbgpNeighbors() {
    Map<String, List<Ip>> ips = new HashMap<>();
    Map<String, List<BgpNeighbor>> neighbors = new HashMap<>();
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        List<Ip> ipList = new ArrayList<>();
        List<BgpNeighbor> ns = new ArrayList<>();
        ips.put(router, ipList);
        neighbors.put(router, ns);
        if (conf.getDefaultVrf().getBgpProcess() != null) {
            BgpProcess bgp = conf.getDefaultVrf().getBgpProcess();
            for (BgpNeighbor neighbor : bgp.getNeighbors().values()) {
                ipList.add(neighbor.getAddress());
                ns.add(neighbor);
            }
        }
    }
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        List<Ip> ipList = ips.get(router);
        List<BgpNeighbor> ns = neighbors.get(router);
        if (conf.getDefaultVrf().getBgpProcess() != null) {
            List<GraphEdge> edges = _edgeMap.get(router);
            for (GraphEdge ge : edges) {
                for (int i = 0; i < ipList.size(); i++) {
                    Ip ip = ipList.get(i);
                    BgpNeighbor n = ns.get(i);
                    Interface iface = ge.getStart();
                    if (ip != null && iface.getAddress().getPrefix().containsIp(ip)) {
                        _ebgpNeighbors.put(ge, n);
                    }
                }
            }
        }
    }
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) BgpProcess(org.batfish.datamodel.BgpProcess) Ip(org.batfish.datamodel.Ip) ArrayList(java.util.ArrayList) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) List(java.util.List) ArrayList(java.util.ArrayList) CommunityList(org.batfish.datamodel.CommunityList) Interface(org.batfish.datamodel.Interface)

Example 32 with Configuration

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

the class Graph method findAllCommunities.

public Set<CommunityVar> findAllCommunities(String router) {
    Set<CommunityVar> comms = new HashSet<>();
    Configuration conf = getConfigurations().get(router);
    for (RoutingPolicy pol : conf.getRoutingPolicies().values()) {
        AstVisitor v = new AstVisitor();
        v.visit(conf, pol.getStatements(), stmt -> {
            if (stmt instanceof SetCommunity) {
                SetCommunity sc = (SetCommunity) stmt;
                comms.addAll(findAllCommunities(conf, sc.getExpr()));
            }
            if (stmt instanceof AddCommunity) {
                AddCommunity ac = (AddCommunity) stmt;
                comms.addAll(findAllCommunities(conf, ac.getExpr()));
            }
            if (stmt instanceof DeleteCommunity) {
                DeleteCommunity dc = (DeleteCommunity) stmt;
                comms.addAll(findAllCommunities(conf, dc.getExpr()));
            }
            if (stmt instanceof RetainCommunity) {
                RetainCommunity rc = (RetainCommunity) stmt;
                comms.addAll(findAllCommunities(conf, rc.getExpr()));
            }
        }, expr -> {
            if (expr instanceof MatchCommunitySet) {
                MatchCommunitySet m = (MatchCommunitySet) expr;
                CommunitySetExpr ce = m.getExpr();
                comms.addAll(findAllCommunities(conf, ce));
            }
        });
    }
    return comms;
}
Also used : AddCommunity(org.batfish.datamodel.routing_policy.statement.AddCommunity) Configuration(org.batfish.datamodel.Configuration) SetCommunity(org.batfish.datamodel.routing_policy.statement.SetCommunity) DeleteCommunity(org.batfish.datamodel.routing_policy.statement.DeleteCommunity) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) RetainCommunity(org.batfish.datamodel.routing_policy.statement.RetainCommunity) CommunitySetExpr(org.batfish.datamodel.routing_policy.expr.CommunitySetExpr) HashSet(java.util.HashSet)

Example 33 with Configuration

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

the class Graph method findRouterId.

/*
   * Find the router Id for the neighbor corresponding to a logical edge.
   */
public long findRouterId(GraphEdge ge, Protocol proto) {
    GraphEdge eOther = _otherEnd.get(ge);
    if (proto.isOspf() || proto.isConnected() || proto.isStatic()) {
        return 0L;
    }
    if (eOther != null) {
        String peer = eOther.getRouter();
        Configuration peerConf = getConfigurations().get(peer);
        return routerId(peerConf, proto);
    }
    BgpNeighbor n = findBgpNeighbor(ge);
    if (n != null && n.getAddress() != null) {
        return n.getAddress().asLong();
    }
    throw new BatfishException("Unable to find router id for " + ge + "," + proto.name());
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) BatfishException(org.batfish.common.BatfishException) Configuration(org.batfish.datamodel.Configuration)

Example 34 with Configuration

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

the class Graph method isHost.

/*
   * Determine if an edge is potentially attached to a host
   */
public boolean isHost(String router) {
    Configuration peerConf = _configurations.get(router);
    String vendor = peerConf.getConfigurationFormat().getVendorString();
    return "host".equals(vendor);
}
Also used : Configuration(org.batfish.datamodel.Configuration)

Example 35 with Configuration

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

the class Graph method initGraph.

/*
   * Initialize the topology by inferring interface pairs and
   * create the opposite edge mapping.
   */
private void initGraph(Topology topology) {
    Map<NodeInterfacePair, Interface> ifaceMap = new HashMap<>();
    Map<String, Set<NodeInterfacePair>> routerIfaceMap = new HashMap<>();
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        Set<NodeInterfacePair> ifacePairs = new HashSet<>();
        for (Entry<String, Interface> entry2 : conf.getInterfaces().entrySet()) {
            String name = entry2.getKey();
            Interface iface = entry2.getValue();
            NodeInterfacePair nip = new NodeInterfacePair(router, name);
            ifacePairs.add(nip);
            ifaceMap.put(nip, iface);
        }
        routerIfaceMap.put(router, ifacePairs);
    }
    Map<NodeInterfacePair, SortedSet<Edge>> ifaceEdges = topology.getInterfaceEdges();
    _neighbors = new HashMap<>();
    for (Entry<String, Set<NodeInterfacePair>> entry : routerIfaceMap.entrySet()) {
        String router = entry.getKey();
        Set<NodeInterfacePair> nips = entry.getValue();
        Set<GraphEdge> graphEdges = new HashSet<>();
        Set<String> neighs = new HashSet<>();
        for (NodeInterfacePair nip : nips) {
            SortedSet<Edge> es = ifaceEdges.get(nip);
            Interface i1 = ifaceMap.get(nip);
            boolean hasNoOtherEnd = (es == null && i1.getAddress() != null);
            if (hasNoOtherEnd) {
                GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                graphEdges.add(ge);
            }
            if (es != null) {
                boolean hasMultipleEnds = (es.size() > 2);
                if (hasMultipleEnds) {
                    GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                    graphEdges.add(ge);
                } else {
                    for (Edge e : es) {
                        // Weird inference behavior from Batfish here with a self-loop
                        if (router.equals(e.getNode1()) && router.equals(e.getNode2())) {
                            GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                            graphEdges.add(ge);
                        }
                        // Only look at the first pair
                        if (!router.equals(e.getNode2())) {
                            Interface i2 = ifaceMap.get(e.getInterface2());
                            String neighbor = e.getNode2();
                            GraphEdge ge1 = new GraphEdge(i1, i2, router, neighbor, false, false);
                            GraphEdge ge2 = new GraphEdge(i2, i1, neighbor, router, false, false);
                            _otherEnd.put(ge1, ge2);
                            graphEdges.add(ge1);
                            neighs.add(neighbor);
                        }
                    }
                }
            }
        }
        _allRealEdges.addAll(graphEdges);
        _allEdges.addAll(graphEdges);
        _edgeMap.put(router, new ArrayList<>(graphEdges));
        _neighbors.put(router, neighs);
    }
}
Also used : SortedSet(java.util.SortedSet) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) InlineCommunitySet(org.batfish.datamodel.routing_policy.expr.InlineCommunitySet) Set(java.util.Set) HashSet(java.util.HashSet) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) NamedCommunitySet(org.batfish.datamodel.routing_policy.expr.NamedCommunitySet) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) SortedSet(java.util.SortedSet) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface) HashSet(java.util.HashSet)

Aggregations

Configuration (org.batfish.datamodel.Configuration)170 Test (org.junit.Test)69 Interface (org.batfish.datamodel.Interface)55 Ip (org.batfish.datamodel.Ip)49 Vrf (org.batfish.datamodel.Vrf)45 HashMap (java.util.HashMap)44 Topology (org.batfish.datamodel.Topology)38 VendorConfiguration (org.batfish.vendor.VendorConfiguration)35 Prefix (org.batfish.datamodel.Prefix)33 Edge (org.batfish.datamodel.Edge)32 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)30 Map (java.util.Map)29 Set (java.util.Set)29 TreeMap (java.util.TreeMap)29 BatfishException (org.batfish.common.BatfishException)28 IpAccessList (org.batfish.datamodel.IpAccessList)26 ArrayList (java.util.ArrayList)25 HashSet (java.util.HashSet)25 List (java.util.List)25 SortedSet (java.util.SortedSet)24