Search in sources :

Example 36 with Configuration

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

the class Graph method initStaticRoutes.

/*
   * Collect all static routes after inferring which interface they indicate
   * should be used for the next-hop.
   */
private void initStaticRoutes() {
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        Map<String, List<StaticRoute>> map = new HashMap<>();
        _staticRoutes.put(router, map);
        for (StaticRoute sr : conf.getDefaultVrf().getStaticRoutes()) {
            boolean someIface = false;
            for (GraphEdge ge : _edgeMap.get(router)) {
                Interface here = ge.getStart();
                Interface there = ge.getEnd();
                // Check if next-hop interface is specified
                String hereName = here.getName();
                someIface = true;
                if (hereName.equals(sr.getNextHopInterface())) {
                    List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
                    srs.add(sr);
                    map.put(hereName, srs);
                }
                // Check if next-hop ip corresponds to direct interface
                Ip nhIp = sr.getNextHopIp();
                boolean isNextHop = there != null && there.getAddress() != null && there.getAddress().getIp().equals(nhIp);
                if (isNextHop) {
                    someIface = true;
                    List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
                    srs.add(sr);
                    map.put(here.getName(), srs);
                }
            }
            if (Graph.isNullRouted(sr)) {
                List<StaticRoute> nulls = _nullStaticRoutes.computeIfAbsent(router, k -> new ArrayList<>());
                nulls.add(sr);
            }
            if (!someIface && !Graph.isNullRouted(sr)) {
                _hasStaticRouteWithDynamicNextHop = true;
            }
        }
    }
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) Ip(org.batfish.datamodel.Ip) List(java.util.List) ArrayList(java.util.ArrayList) CommunityList(org.batfish.datamodel.CommunityList) Interface(org.batfish.datamodel.Interface)

Example 37 with Configuration

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

the class Graph method findExportRoutingPolicy.

/*
   * Find the export routing policy for a given edge
   */
@Nullable
public RoutingPolicy findExportRoutingPolicy(String router, Protocol proto, GraphEdge ge) {
    Configuration conf = _configurations.get(router);
    if (proto.isConnected()) {
        return null;
    }
    if (proto.isStatic()) {
        return null;
    }
    if (proto.isOspf()) {
        OspfProcess p = conf.getDefaultVrf().getOspfProcess();
        if (p == null) {
            return null;
        }
        String exp = p.getExportPolicy();
        return conf.getRoutingPolicies().get(exp);
    }
    if (proto.isBgp()) {
        BgpNeighbor n = findBgpNeighbor(ge);
        // if no neighbor (e.g., loopback), or no export policy
        if (n == null || n.getExportPolicy() == null) {
            return null;
        }
        return conf.getRoutingPolicies().get(n.getExportPolicy());
    }
    throw new BatfishException("TODO: findExportRoutingPolicy for " + proto.name());
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) BatfishException(org.batfish.common.BatfishException) Configuration(org.batfish.datamodel.Configuration) OspfProcess(org.batfish.datamodel.OspfProcess) Nullable(javax.annotation.Nullable)

Example 38 with Configuration

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

the class AbstractionBuilder method createAbstractConfig.

/*
   * Creates a new Configuration from an old one for an abstract router
   * by copying the old configuration, but removing any concrete interfaces,
   * neighbors etc that do not correpond to any abstract neighbors.
   */
private Configuration createAbstractConfig(Set<String> abstractRouters, Configuration conf) {
    Configuration abstractConf = new Configuration(conf.getHostname(), conf.getConfigurationFormat());
    abstractConf.setDnsServers(conf.getDnsServers());
    abstractConf.setDnsSourceInterface(conf.getDnsSourceInterface());
    abstractConf.setDomainName(conf.getDomainName());
    abstractConf.setAuthenticationKeyChains(conf.getAuthenticationKeyChains());
    abstractConf.setIkeGateways(conf.getIkeGateways());
    abstractConf.setDefaultCrossZoneAction(conf.getDefaultCrossZoneAction());
    abstractConf.setIkePolicies(conf.getIkePolicies());
    abstractConf.setIkeProposals(conf.getIkeProposals());
    abstractConf.setDefaultInboundAction(conf.getDefaultInboundAction());
    abstractConf.setIpAccessLists(conf.getIpAccessLists());
    abstractConf.setIp6AccessLists(conf.getIp6AccessLists());
    abstractConf.setRouteFilterLists(conf.getRouteFilterLists());
    abstractConf.setRoute6FilterLists(conf.getRoute6FilterLists());
    abstractConf.setIpsecPolicies(conf.getIpsecPolicies());
    abstractConf.setIpsecProposals(conf.getIpsecProposals());
    abstractConf.setIpsecVpns(conf.getIpsecVpns());
    abstractConf.setLoggingServers(conf.getLoggingServers());
    abstractConf.setLoggingSourceInterface(conf.getLoggingSourceInterface());
    abstractConf.setNormalVlanRange(conf.getNormalVlanRange());
    abstractConf.setNtpServers(conf.getNtpServers());
    abstractConf.setNtpSourceInterface(conf.getNtpSourceInterface());
    abstractConf.setRoles(conf.getRoles());
    abstractConf.setSnmpSourceInterface(conf.getSnmpSourceInterface());
    abstractConf.setSnmpTrapServers(conf.getSnmpTrapServers());
    abstractConf.setTacacsServers(conf.getTacacsServers());
    abstractConf.setTacacsSourceInterface(conf.getTacacsSourceInterface());
    abstractConf.setVendorFamily(conf.getVendorFamily());
    abstractConf.setZones(conf.getZones());
    abstractConf.setCommunityLists(conf.getCommunityLists());
    abstractConf.setRoutingPolicies(conf.getRoutingPolicies());
    abstractConf.setRoute6FilterLists(conf.getRoute6FilterLists());
    SortedSet<Interface> toRetain = new TreeSet<>();
    SortedSet<IpLink> ipNeighbors = new TreeSet<>();
    SortedSet<BgpNeighbor> bgpNeighbors = new TreeSet<>();
    List<GraphEdge> edges = _graph.getEdgeMap().get(conf.getName());
    for (GraphEdge ge : edges) {
        boolean leavesNetwork = (ge.getPeer() == null);
        if (leavesNetwork || (abstractRouters.contains(ge.getRouter()) && abstractRouters.contains(ge.getPeer()))) {
            toRetain.add(ge.getStart());
            Ip start = ge.getStart().getAddress().getIp();
            if (!leavesNetwork) {
                Ip end = ge.getEnd().getAddress().getIp();
                ipNeighbors.add(new IpLink(start, end));
            }
            BgpNeighbor n = _graph.getEbgpNeighbors().get(ge);
            if (n != null) {
                bgpNeighbors.add(n);
            }
        }
    }
    // Update interfaces
    NavigableMap<String, Interface> abstractInterfaces = new TreeMap<>();
    for (Entry<String, Interface> entry : conf.getInterfaces().entrySet()) {
        String name = entry.getKey();
        Interface iface = entry.getValue();
        if (toRetain.contains(iface)) {
            abstractInterfaces.put(name, iface);
        }
    }
    abstractConf.setInterfaces(abstractInterfaces);
    // Update VRFs
    Map<String, Vrf> abstractVrfs = new HashMap<>();
    for (Entry<String, Vrf> entry : conf.getVrfs().entrySet()) {
        String name = entry.getKey();
        Vrf vrf = entry.getValue();
        Vrf abstractVrf = new Vrf(name);
        abstractVrf.setStaticRoutes(vrf.getStaticRoutes());
        abstractVrf.setIsisProcess(vrf.getIsisProcess());
        abstractVrf.setRipProcess(vrf.getRipProcess());
        abstractVrf.setSnmpServer(vrf.getSnmpServer());
        NavigableMap<String, Interface> abstractVrfInterfaces = new TreeMap<>();
        for (Entry<String, Interface> entry2 : vrf.getInterfaces().entrySet()) {
            String iname = entry2.getKey();
            Interface iface = entry2.getValue();
            if (toRetain.contains(iface)) {
                abstractVrfInterfaces.put(iname, iface);
            }
        }
        abstractVrf.setInterfaces(abstractVrfInterfaces);
        abstractVrf.setInterfaceNames(new TreeSet<>(abstractVrfInterfaces.keySet()));
        OspfProcess ospf = vrf.getOspfProcess();
        if (ospf != null) {
            OspfProcess abstractOspf = new OspfProcess();
            abstractOspf.setAreas(ospf.getAreas());
            abstractOspf.setExportPolicy(ospf.getExportPolicy());
            abstractOspf.setReferenceBandwidth(ospf.getReferenceBandwidth());
            abstractOspf.setRouterId(ospf.getRouterId());
            // Copy over neighbors
            Map<IpLink, OspfNeighbor> abstractNeighbors = new HashMap<>();
            if (ospf.getOspfNeighbors() != null) {
                for (Entry<IpLink, OspfNeighbor> entry2 : ospf.getOspfNeighbors().entrySet()) {
                    IpLink link = entry2.getKey();
                    OspfNeighbor neighbor = entry2.getValue();
                    if (ipNeighbors.contains(link)) {
                        abstractNeighbors.put(link, neighbor);
                    }
                }
            }
            abstractOspf.setOspfNeighbors(abstractNeighbors);
            abstractVrf.setOspfProcess(abstractOspf);
        }
        BgpProcess bgp = vrf.getBgpProcess();
        if (bgp != null) {
            BgpProcess abstractBgp = new BgpProcess();
            abstractBgp.setMultipathEbgp(bgp.getMultipathEbgp());
            abstractBgp.setMultipathIbgp(bgp.getMultipathIbgp());
            abstractBgp.setRouterId(bgp.getRouterId());
            abstractBgp.setOriginationSpace(bgp.getOriginationSpace());
            // TODO: set bgp neighbors accordingly
            // Copy over neighbors
            SortedMap<Prefix, BgpNeighbor> abstractBgpNeighbors = new TreeMap<>();
            if (bgp.getNeighbors() != null) {
                for (Entry<Prefix, BgpNeighbor> entry2 : bgp.getNeighbors().entrySet()) {
                    Prefix prefix = entry2.getKey();
                    BgpNeighbor neighbor = entry2.getValue();
                    if (bgpNeighbors.contains(neighbor)) {
                        abstractBgpNeighbors.put(prefix, neighbor);
                    }
                }
            }
            abstractBgp.setNeighbors(abstractBgpNeighbors);
            abstractVrf.setBgpProcess(abstractBgp);
        }
        abstractVrfs.put(name, abstractVrf);
    }
    abstractConf.setVrfs(abstractVrfs);
    return abstractConf;
}
Also used : IpLink(org.batfish.datamodel.IpLink) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) BgpProcess(org.batfish.datamodel.BgpProcess) Ip(org.batfish.datamodel.Ip) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) TreeSet(java.util.TreeSet) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) OspfProcess(org.batfish.datamodel.OspfProcess) TreeMap(java.util.TreeMap) GraphEdge(org.batfish.symbolic.GraphEdge) Interface(org.batfish.datamodel.Interface)

Example 39 with Configuration

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

the class BatfishCompressor method applyFilters.

/**
 * Update RoutingPolicies to filter traffic according to filtersByRouter. This mutates the
 * _graph's configurations.
 *
 * @param filtersByRouter Filters for each router/graph edge.
 * @return A new network with the updated configs.
 */
private Map<String, Configuration> applyFilters(Table2<String, GraphEdge, EquivalenceClassFilter> filtersByRouter) {
    Map<String, Configuration> newConfigs = new HashMap<>();
    for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
        String router = entry.getKey();
        Map<GraphEdge, EquivalenceClassFilter> filters = filtersByRouter.get(router);
        if (filters != null) {
            Configuration config = entry.getValue();
            // Include this config in the compressed network.
            newConfigs.put(router, config);
            // Mutate the config by adding import/export filters
            for (GraphEdge ge : _graph.getEdgeMap().get(router)) {
                EquivalenceClassFilter tup = filters.get(ge);
                RoutingPolicy ipol = _graph.findImportRoutingPolicy(router, Protocol.BGP, ge);
                if (ipol != null) {
                    RoutingPolicy newIpol = new RoutingPolicy(ipol.getName(), config);
                    newIpol.setStatements(applyFilters(ipol.getStatements(), tup));
                    config.getRoutingPolicies().put(newIpol.getName(), newIpol);
                }
                RoutingPolicy epol = _graph.findExportRoutingPolicy(router, Protocol.BGP, ge);
                if (epol != null) {
                    RoutingPolicy newEpol = new RoutingPolicy(epol.getName(), config);
                    newEpol.setStatements(applyFilters(epol.getStatements(), tup));
                    config.getRoutingPolicies().put(newEpol.getName(), newEpol);
                }
            }
        }
    }
    return newConfigs;
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 40 with Configuration

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

the class DestinationClasses method buildProtocolMap.

/*
   * Initialize a mapping from router to collection of protocol
   */
private Map<String, List<Protocol>> buildProtocolMap() {
    // Figure out which protocols are running on which devices
    Map<String, List<Protocol>> protocols = new HashMap<>();
    for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        List<Protocol> protos = new ArrayList<>();
        protocols.put(router, protos);
        if (conf.getDefaultVrf().getOspfProcess() != null) {
            protos.add(Protocol.OSPF);
        }
        if (conf.getDefaultVrf().getBgpProcess() != null) {
            protos.add(Protocol.BGP);
        }
        if (!conf.getDefaultVrf().getStaticRoutes().isEmpty()) {
            protos.add(Protocol.STATIC);
        }
        if (!conf.getInterfaces().isEmpty()) {
            protos.add(Protocol.CONNECTED);
        }
    }
    return protocols;
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(org.batfish.symbolic.Protocol)

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