Search in sources :

Example 46 with InterfaceAddress

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

the class NodJobTest method setupConfigs.

private void setupConfigs() {
    NetworkFactory nf = new NetworkFactory();
    Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    Interface.Builder ib = nf.interfaceBuilder().setActive(true).setBandwidth(1E9d);
    IpAccessList.Builder aclb = nf.aclBuilder();
    IpAccessListLine.Builder acllb = IpAccessListLine.builder();
    SourceNat.Builder snb = SourceNat.builder();
    Vrf.Builder vb = nf.vrfBuilder();
    _srcNode = cb.build();
    _dstNode = cb.build();
    _srcVrf = vb.setOwner(_srcNode).build();
    _originateVrf = new OriginateVrf(_srcNode.getHostname(), _srcVrf.getName());
    Vrf dstVrf = vb.setOwner(_dstNode).build();
    Prefix p1 = Prefix.parse("1.0.0.0/31");
    Ip poolIp1 = new Ip("1.0.0.10");
    // apply NAT to all packets
    IpAccessList sourceNat1Acl = aclb.setLines(ImmutableList.of(acllb.setSrcIps(ImmutableList.of(new IpWildcard("3.0.0.0/32"))).setAction(LineAction.ACCEPT).build())).setOwner(_srcNode).build();
    SourceNat sourceNat1 = // Would be easier to understand, and Nuno says it will likely be more efficient.
    snb.setPoolIpFirst(poolIp1).setPoolIpLast(poolIp1).setAcl(sourceNat1Acl).build();
    ib.setOwner(_srcNode).setVrf(_srcVrf).setAddress(new InterfaceAddress(p1.getStartIp(), p1.getPrefixLength())).setSourceNats(ImmutableList.of(sourceNat1)).build();
    ib.setOwner(_dstNode).setVrf(dstVrf).setAddress(new InterfaceAddress(p1.getEndIp(), p1.getPrefixLength())).setSourceNats(ImmutableList.of()).build();
    // For the destination
    Prefix pDest = Prefix.parse("2.0.0.0/32");
    ib.setOwner(_dstNode).setVrf(dstVrf).setAddress(new InterfaceAddress(pDest.getEndIp(), pDest.getPrefixLength())).build();
    StaticRoute.Builder bld = StaticRoute.builder().setNetwork(pDest);
    _srcVrf.getStaticRoutes().add(bld.setNextHopIp(p1.getEndIp()).build());
    _configs = ImmutableSortedMap.of(_srcNode.getName(), _srcNode, _dstNode.getName(), _dstNode);
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) Vrf(org.batfish.datamodel.Vrf) OriginateVrf(org.batfish.z3.state.OriginateVrf) Prefix(org.batfish.datamodel.Prefix) OriginateVrf(org.batfish.z3.state.OriginateVrf) SourceNat(org.batfish.datamodel.SourceNat) IpWildcard(org.batfish.datamodel.IpWildcard) NetworkFactory(org.batfish.datamodel.NetworkFactory) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) IpAccessList(org.batfish.datamodel.IpAccessList) Interface(org.batfish.datamodel.Interface)

Example 47 with InterfaceAddress

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

the class BatfishCompressionTest method diamondNetwork.

/**
 * This network should be compressed from: A --> B --> D, A --> C --> D to A --> {B,C} --> D.
 * i.e., B and C should be merged into one node.
 *
 * @return Configurations for the original (uncompressed) network.
 */
private SortedMap<String, Configuration> diamondNetwork() {
    NetworkFactory nf = new NetworkFactory();
    Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    Configuration cA = cb.setHostname("A").build();
    Configuration cB = cb.setHostname("B").build();
    Configuration cC = cb.setHostname("C").build();
    Configuration cD = cb.setHostname("D").build();
    Vrf.Builder vb = nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
    Vrf vA = vb.setOwner(cA).build();
    Vrf vB = vb.setOwner(cB).build();
    Vrf vC = vb.setOwner(cC).build();
    Vrf vD = vb.setOwner(cD).build();
    Prefix pAB = Prefix.parse("10.12.0.0/31");
    Prefix pAC = Prefix.parse("10.13.0.0/31");
    Prefix pBD = Prefix.parse("10.24.0.0/31");
    Prefix pCD = Prefix.parse("10.34.0.0/31");
    Interface.Builder ib = nf.interfaceBuilder().setActive(true);
    // Add a route from A --> B
    ib.setOwner(cA).setVrf(vA).setAddress(new InterfaceAddress(pAB.getStartIp(), pAB.getPrefixLength())).build();
    // Interface iBA
    ib.setOwner(cB).setVrf(vA).setAddress(new InterfaceAddress(pAB.getEndIp(), pAB.getPrefixLength())).build();
    // Interface iAC
    ib.setOwner(cA).setVrf(vA).setAddress(new InterfaceAddress(pAC.getStartIp(), pAC.getPrefixLength())).build();
    // Interface iCA
    ib.setOwner(cC).setVrf(vC).setAddress(new InterfaceAddress(pAC.getEndIp(), pAC.getPrefixLength())).build();
    // Interface iBD
    ib.setOwner(cB).setVrf(vB).setAddress(new InterfaceAddress(pBD.getStartIp(), pBD.getPrefixLength())).build();
    // Interface iDB
    ib.setOwner(cD).setVrf(vD).setAddress(new InterfaceAddress(pBD.getEndIp(), pBD.getPrefixLength())).build();
    // Interface iCD
    ib.setOwner(cC).setVrf(vC).setAddress(new InterfaceAddress(pCD.getStartIp(), pCD.getPrefixLength())).build();
    // Interface iDC
    ib.setOwner(cD).setVrf(vD).setAddress(new InterfaceAddress(pCD.getEndIp(), pCD.getPrefixLength())).build();
    // For the destination
    Prefix pD = Prefix.parse("4.4.4.4/32");
    // Interface iD
    ib.setOwner(cD).setVrf(vD).setAddress(new InterfaceAddress(pD.getEndIp(), pD.getPrefixLength())).build();
    StaticRoute.Builder bld = StaticRoute.builder().setNetwork(pD);
    vA.getStaticRoutes().add(bld.setNextHopIp(pAB.getEndIp()).build());
    vA.getStaticRoutes().add(bld.setNextHopIp(pAC.getEndIp()).build());
    vB.getStaticRoutes().add(bld.setNextHopIp(pBD.getEndIp()).build());
    vC.getStaticRoutes().add(bld.setNextHopIp(pCD.getEndIp()).build());
    return new TreeMap<>(ImmutableSortedMap.of(cA.getName(), cA, cB.getName(), cB, cC.getName(), cC, cD.getName(), cD));
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) NetworkFactory(org.batfish.datamodel.NetworkFactory) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) Interface(org.batfish.datamodel.Interface)

Example 48 with InterfaceAddress

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

the class BatfishCompressionTest method compressibleNetwork.

/**
 * Build a network that can be easily compressed.
 */
private SortedMap<String, Configuration> compressibleNetwork() {
    NetworkFactory nf = new NetworkFactory();
    Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    _compressedNode1 = cb.build();
    _compressedNode2 = cb.build();
    _compressedNode3 = cb.build();
    Vrf.Builder vb = nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
    Vrf v1 = vb.setOwner(_compressedNode1).build();
    Vrf v3 = vb.setOwner(_compressedNode3).build();
    // add a vrf to c2 too
    vb.setOwner(_compressedNode2).build();
    Prefix p = Prefix.parse("10.23.0.0/31");
    Interface.Builder ib = nf.interfaceBuilder().setActive(true);
    ib.setOwner(_compressedNode1).setVrf(v1).setAddress(new InterfaceAddress(p.getStartIp(), p.getPrefixLength())).build();
    ib.setOwner(_compressedNode3).setVrf(v3).setAddress(new InterfaceAddress(p.getStartIp(), p.getPrefixLength())).build();
    StaticRoute staticRoute = StaticRoute.builder().setNetwork(p).setNextHopIp(p.getEndIp()).build();
    v1.getStaticRoutes().add(staticRoute);
    v3.getStaticRoutes().add(staticRoute);
    return new TreeMap<>(ImmutableSortedMap.of(_compressedNode1.getName(), _compressedNode1, _compressedNode2.getName(), _compressedNode2, _compressedNode3.getName(), _compressedNode3));
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) NetworkFactory(org.batfish.datamodel.NetworkFactory) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) Interface(org.batfish.datamodel.Interface)

Example 49 with InterfaceAddress

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

the class BatfishCompressionTest method simpleNetwork.

private SortedMap<String, Configuration> simpleNetwork() {
    NetworkFactory nf = new NetworkFactory();
    Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    Configuration c1 = cb.build();
    Configuration c2 = cb.build();
    Configuration c3 = cb.build();
    Vrf.Builder vb = nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
    Vrf v1 = vb.setOwner(c1).build();
    Vrf v2 = vb.setOwner(c2).build();
    Vrf v3 = vb.setOwner(c3).build();
    Prefix p12 = Prefix.parse("10.12.0.0/31");
    Prefix p23 = Prefix.parse("10.23.0.0/31");
    Interface.Builder ib = nf.interfaceBuilder().setActive(true);
    ib.setOwner(c1).setVrf(v1).setAddress(new InterfaceAddress(p12.getStartIp(), p12.getPrefixLength())).build();
    ib.setOwner(c2).setVrf(v2).setAddress(new InterfaceAddress(p12.getEndIp(), p12.getPrefixLength())).build();
    ib.setAddress(new InterfaceAddress(p23.getStartIp(), p23.getPrefixLength())).build();
    ib.setOwner(c3).setVrf(v3).setAddress(new InterfaceAddress(p23.getEndIp(), p23.getPrefixLength())).build();
    StaticRoute s13 = StaticRoute.builder().setNetwork(p23).setNextHopIp(p12.getEndIp()).build();
    v1.getStaticRoutes().add(s13);
    StaticRoute s31 = StaticRoute.builder().setNetwork(p12).setNextHopIp(p23.getStartIp()).build();
    v3.getStaticRoutes().add(s31);
    return new TreeMap<>(ImmutableSortedMap.of(c1.getName(), c1, c2.getName(), c2, c3.getName(), c3));
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) NetworkFactory(org.batfish.datamodel.NetworkFactory) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) Interface(org.batfish.datamodel.Interface)

Example 50 with InterfaceAddress

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

the class CommonUtil method computeIpOwners.

public static Map<Ip, Set<String>> computeIpOwners(boolean excludeInactive, Map<String, Map<String, Interface>> enabledInterfaces) {
    // TODO: confirm VRFs are handled correctly
    Map<Ip, Set<String>> ipOwners = new HashMap<>();
    Map<Pair<InterfaceAddress, Integer>, Set<Interface>> vrrpGroups = new HashMap<>();
    enabledInterfaces.forEach((hostname, currentEnabledInterfaces) -> {
        for (Interface i : currentEnabledInterfaces.values()) {
            if (!i.getActive() && (excludeInactive || !i.getBlacklisted())) {
                continue;
            }
            // collect vrrp info
            i.getVrrpGroups().forEach((groupNum, vrrpGroup) -> {
                InterfaceAddress address = vrrpGroup.getVirtualAddress();
                if (address == null) {
                    // never win the election, so is not a candidate.
                    return;
                }
                Pair<InterfaceAddress, Integer> key = new Pair<>(address, groupNum);
                Set<Interface> candidates = vrrpGroups.computeIfAbsent(key, k -> Collections.newSetFromMap(new IdentityHashMap<>()));
                candidates.add(i);
            });
            // collect prefixes
            i.getAllAddresses().stream().map(InterfaceAddress::getIp).forEach(ip -> {
                Set<String> owners = ipOwners.computeIfAbsent(ip, k -> new HashSet<>());
                owners.add(hostname);
            });
        }
    });
    vrrpGroups.forEach((p, candidates) -> {
        int groupNum = p.getSecond();
        InterfaceAddress address = p.getFirst();
        Ip ip = address.getIp();
        int lowestPriority = Integer.MAX_VALUE;
        String bestCandidate = null;
        SortedSet<String> bestCandidates = new TreeSet<>();
        for (Interface candidate : candidates) {
            VrrpGroup group = candidate.getVrrpGroups().get(groupNum);
            int currentPriority = group.getPriority();
            if (currentPriority < lowestPriority) {
                lowestPriority = currentPriority;
                bestCandidates.clear();
                bestCandidate = candidate.getOwner().getHostname();
            }
            if (currentPriority == lowestPriority) {
                bestCandidates.add(candidate.getOwner().getHostname());
            }
        }
        if (bestCandidates.size() != 1) {
            String deterministicBestCandidate = bestCandidates.first();
            bestCandidate = deterministicBestCandidate;
        // _logger.redflag(
        // "Arbitrarily choosing best vrrp candidate: '"
        // + deterministicBestCandidate
        // + " for prefix/groupNumber: '"
        // + p.toString()
        // + "' among multiple best candidates: "
        // + bestCandidates);
        }
        Set<String> owners = ipOwners.computeIfAbsent(ip, k -> new HashSet<>());
        owners.add(bestCandidate);
    });
    return ipOwners;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) VrrpGroup(org.batfish.datamodel.VrrpGroup) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) IdentityHashMap(java.util.IdentityHashMap) TreeSet(java.util.TreeSet) Interface(org.batfish.datamodel.Interface) Pair(org.batfish.common.Pair) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair)

Aggregations

InterfaceAddress (org.batfish.datamodel.InterfaceAddress)51 Ip (org.batfish.datamodel.Ip)34 Configuration (org.batfish.datamodel.Configuration)26 Prefix (org.batfish.datamodel.Prefix)26 Interface (org.batfish.datamodel.Interface)23 StaticRoute (org.batfish.datamodel.StaticRoute)19 Vrf (org.batfish.datamodel.Vrf)17 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)12 BatfishException (org.batfish.common.BatfishException)9 BgpProcess (org.batfish.datamodel.BgpProcess)9 HashSet (java.util.HashSet)8 TreeSet (java.util.TreeSet)8 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)8 Topology (org.batfish.datamodel.Topology)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 Set (java.util.Set)7 SortedSet (java.util.SortedSet)7 TreeMap (java.util.TreeMap)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6