Search in sources :

Example 1 with ConcreteInterfaceAddress

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

the class ConfigDb method createInterfaces.

/**
 * Converts configdb's interface multi-level key encoding for interfaces, where keys are
 * "Ethernet", "Ethernet|1.1.1.1", to a map of interfaces.
 *
 * <p>In this encoding, the same interface may appear as key multiple times: by itself, with a v4
 * address, or with a v6 address.
 */
@VisibleForTesting
static Map<String, L3Interface> createInterfaces(Set<String> interfaceKeys) {
    Map<String, L3Interface> interfaces = new HashMap<>();
    for (String key : interfaceKeys) {
        String[] parts = key.split("\\|", 2);
        interfaces.computeIfAbsent(parts[0], i -> new L3Interface(null));
        if (parts.length == 2) {
            try {
                // if the interface appears with a v4 address, overwrite with that version
                ConcreteInterfaceAddress v4Address = ConcreteInterfaceAddress.parse(parts[1]);
                interfaces.put(parts[0], new L3Interface(v4Address));
            } catch (IllegalArgumentException e) {
                // try to parse as v6; Will throw an exception upon failure
                Prefix6.parse(parts[1]);
            }
        }
    }
    return ImmutableMap.copyOf(interfaces);
}
Also used : HashMap(java.util.HashMap) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ConcreteInterfaceAddress

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

the class IpsecUtil method initPrivateIpsByPublicIp.

private static SetMultimap<Ip, IpWildcardSetIpSpace> initPrivateIpsByPublicIp(Map<String, Configuration> configurations) {
    /*
     * Very hacky mapping from public IP to set of spaces of possible natted private IPs.
     * Does not currently support source-nat acl.
     *
     * The current implementation just considers every IP in every prefix on a non-masquerading
     * interface (except the local address in each such prefix) to be a possible private IP
     * match for every public IP referred to by every source-nat pool on a masquerading interface.
     */
    ImmutableSetMultimap.Builder<Ip, IpWildcardSetIpSpace> builder = ImmutableSetMultimap.builder();
    for (Configuration c : configurations.values()) {
        Collection<Interface> interfaces = c.getAllInterfaces().values();
        Set<ConcreteInterfaceAddress> nonNattedInterfaceAddresses = interfaces.stream().filter(i -> !hasSourceNat(i.getOutgoingTransformation())).flatMap(i -> i.getAllConcreteAddresses().stream()).collect(ImmutableSet.toImmutableSet());
        Set<IpWildcard> blacklist = nonNattedInterfaceAddresses.stream().map(address -> IpWildcard.create(address.getIp())).collect(ImmutableSet.toImmutableSet());
        Set<IpWildcard> whitelist = nonNattedInterfaceAddresses.stream().map(address -> IpWildcard.create(address.getPrefix())).collect(ImmutableSet.toImmutableSet());
        IpWildcardSetIpSpace ipSpace = IpWildcardSetIpSpace.create(blacklist, whitelist);
        interfaces.stream().flatMap(i -> sourceNatPoolIps(i.getOutgoingTransformation())).forEach(currentPoolIp -> builder.put(currentPoolIp, ipSpace));
    }
    return builder.build();
}
Also used : IpProtocol(org.batfish.datamodel.IpProtocol) Hop(org.batfish.datamodel.flow.Hop) IpsecStaticPeerConfig(org.batfish.datamodel.IpsecStaticPeerConfig) EndpointPair(com.google.common.graph.EndpointPair) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) Edge(org.batfish.datamodel.Edge) Trace(org.batfish.datamodel.flow.Trace) Interface(org.batfish.datamodel.Interface) ValueGraphBuilder(com.google.common.graph.ValueGraphBuilder) Flow(org.batfish.datamodel.Flow) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) IkePhase1Key(org.batfish.datamodel.IkePhase1Key) NetworkConfigurations(org.batfish.datamodel.NetworkConfigurations) Map(java.util.Map) NamedPort(org.batfish.datamodel.NamedPort) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) FlowDisposition(org.batfish.datamodel.FlowDisposition) Set(java.util.Set) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) IpsecPeerConfigId(org.batfish.datamodel.IpsecPeerConfigId) TraceAndReverseFlow(org.batfish.datamodel.flow.TraceAndReverseFlow) Objects(java.util.Objects) IpsecProtocol(org.batfish.datamodel.IpsecProtocol) List(java.util.List) TransformationUtil.sourceNatPoolIps(org.batfish.datamodel.transformation.TransformationUtil.sourceNatPoolIps) ValueGraph(com.google.common.graph.ValueGraph) Entry(java.util.Map.Entry) IpWildcard(org.batfish.datamodel.IpWildcard) Ip(org.batfish.datamodel.Ip) IkePhase1Policy(org.batfish.datamodel.IkePhase1Policy) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) TransformationUtil.hasSourceNat(org.batfish.datamodel.transformation.TransformationUtil.hasSourceNat) TracerouteEngine(org.batfish.common.plugin.TracerouteEngine) HashMap(java.util.HashMap) IkePhase1Proposal(org.batfish.datamodel.IkePhase1Proposal) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Configuration(org.batfish.datamodel.Configuration) IpsecDynamicPeerConfig(org.batfish.datamodel.IpsecDynamicPeerConfig) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) IpsecTopology(org.batfish.datamodel.ipsec.IpsecTopology) IpsecPeerConfig(org.batfish.datamodel.IpsecPeerConfig) IkeKeyType(org.batfish.datamodel.IkeKeyType) IpsecSession(org.batfish.datamodel.IpsecSession) SetMultimap(com.google.common.collect.SetMultimap) IpsecPhase2Policy(org.batfish.datamodel.IpsecPhase2Policy) IpsecPhase2Proposal(org.batfish.datamodel.IpsecPhase2Proposal) MutableValueGraph(com.google.common.graph.MutableValueGraph) VisibleForTesting(com.google.common.annotations.VisibleForTesting) IpWildcard(org.batfish.datamodel.IpWildcard) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) Configuration(org.batfish.datamodel.Configuration) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Ip(org.batfish.datamodel.Ip) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) Interface(org.batfish.datamodel.Interface)

Example 3 with ConcreteInterfaceAddress

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

the class IspModelingUtilsTest method testInferSnapshotBgpIfaceAddress.

/**
 * Test the preference order of inferSnapshotBgpIfaceAddress
 */
@Test
public void testInferSnapshotBgpIfaceAddress() {
    Ip localIp = Ip.parse("1.1.1.1");
    ConcreteInterfaceAddress addr22 = ConcreteInterfaceAddress.create(localIp, 22);
    ConcreteInterfaceAddress addr23 = ConcreteInterfaceAddress.create(localIp, 23);
    ConcreteInterfaceAddress addr24 = ConcreteInterfaceAddress.create(localIp, 24);
    ConcreteInterfaceAddress addr25 = ConcreteInterfaceAddress.create(localIp, 25);
    Interface active1 = _nf.interfaceBuilder().setAdminUp(true).setName("active1").setAddresses(addr24, addr25).build();
    Interface inactive1 = _nf.interfaceBuilder().setAdminUp(false).setName("inactive1").setAddresses(addr22, addr23).build();
    // lower address is picked
    assertThat(inferSnapshotBgpIfaceAddress(ImmutableList.of(active1), localIp), equalTo(Optional.of(addr24)));
    // active is picked even if a lower inactive is present
    assertThat(inferSnapshotBgpIfaceAddress(ImmutableList.of(active1, inactive1), localIp), equalTo(Optional.of(addr24)));
    // lower inactive is picked when nothing is active
    assertThat(inferSnapshotBgpIfaceAddress(ImmutableList.of(inactive1), localIp), equalTo(Optional.of(addr22)));
}
Also used : Ip(org.batfish.datamodel.Ip) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) Interface(org.batfish.datamodel.Interface) ConfigurationMatchers.hasInterface(org.batfish.datamodel.matchers.ConfigurationMatchers.hasInterface) IspModelingUtils.getSnapshotConnectionsForBorderInterface(org.batfish.common.util.isp.IspModelingUtils.getSnapshotConnectionsForBorderInterface) Test(org.junit.Test)

Example 4 with ConcreteInterfaceAddress

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

the class CompletionMetadataUtilsTest method testGetIps.

@Test
public void testGetIps() {
    String nodeName = "nodeName";
    String int1 = "int1";
    String int2 = "int2";
    Ip ip1 = Ip.parse("10.1.3.1");
    Ip ip2 = Ip.parse("128.212.155.30");
    Ip ip3 = Ip.parse("124.51.32.2");
    String address1 = ip1 + "/30";
    String address2 = ip2 + "/24";
    String address3 = ip3 + "/20";
    ConcreteInterfaceAddress interfaceAddress1 = ConcreteInterfaceAddress.parse(address1);
    ConcreteInterfaceAddress interfaceAddress2 = ConcreteInterfaceAddress.parse(address2);
    ConcreteInterfaceAddress interfaceAddress3 = ConcreteInterfaceAddress.parse(address3);
    Map<String, Configuration> configs = new HashMap<>();
    Configuration config = createTestConfiguration(nodeName, ConfigurationFormat.HOST, int1, int2);
    Interface iface1 = config.getAllInterfaces().get(int1);
    iface1.setAllAddresses(ImmutableSet.of(interfaceAddress1, interfaceAddress2));
    Interface iface2 = config.getAllInterfaces().get(int2);
    iface2.setAllAddresses(ImmutableSet.of(interfaceAddress2, interfaceAddress3));
    configs.put(nodeName, config);
    RangeSet<Ip> ownedIps = ImmutableRangeSet.<Ip>builder().add(Range.singleton(interfaceAddress1.getIp())).add(Range.singleton(interfaceAddress2.getIp())).add(Range.singleton(interfaceAddress3.getIp())).build();
    PrefixTrieMultiMap<IpCompletionMetadata> trie = new PrefixTrieMultiMap<>();
    trie.put(ip1.toPrefix(), new IpCompletionMetadata(new IpCompletionRelevance(interfaceDisplayString(iface1), config.getHostname(), iface1.getName())));
    trie.put(ip2.toPrefix(), new IpCompletionMetadata(ImmutableList.of(new IpCompletionRelevance(interfaceDisplayString(iface1), config.getHostname(), iface1.getName()), new IpCompletionRelevance(interfaceDisplayString(iface2), config.getHostname(), iface2.getName()))));
    trie.put(interfaceAddress2.getPrefix(), new IpCompletionMetadata(unownedSubnetHostIps(interfaceAddress2.getPrefix(), ownedIps), ImmutableList.of(new IpCompletionRelevance(interfaceLinkDisplayString(iface1), config.getHostname(), iface1.getName()), new IpCompletionRelevance(interfaceLinkDisplayString(iface2), config.getHostname(), iface2.getName()))));
    trie.put(ip3.toPrefix(), new IpCompletionMetadata(new IpCompletionRelevance(interfaceDisplayString(iface2), config.getHostname(), iface2.getName())));
    trie.put(interfaceAddress3.getPrefix(), new IpCompletionMetadata(unownedSubnetHostIps(interfaceAddress3.getPrefix(), ownedIps), ImmutableList.of(new IpCompletionRelevance(interfaceLinkDisplayString(iface2), config.getHostname(), iface2.getName()))));
    createWellKnownIpCompletion(WELL_KNOWN_IPS).forEach((ip, metadata) -> trie.put(ip.toPrefix(), metadata));
    assertThat(CompletionMetadataUtils.getIps(configs, ownedIps), equalTo(trie));
}
Also used : IpCompletionRelevance(org.batfish.common.autocomplete.IpCompletionRelevance) Configuration(org.batfish.datamodel.Configuration) IpCompletionMetadata(org.batfish.common.autocomplete.IpCompletionMetadata) HashMap(java.util.HashMap) PrefixTrieMultiMap(org.batfish.datamodel.PrefixTrieMultiMap) Ip(org.batfish.datamodel.Ip) CompletionMetadataUtils.addressGroupDisplayString(org.batfish.common.util.CompletionMetadataUtils.addressGroupDisplayString) CompletionMetadataUtils.interfaceDisplayString(org.batfish.common.util.CompletionMetadataUtils.interfaceDisplayString) CompletionMetadataUtils.interfaceLinkDisplayString(org.batfish.common.util.CompletionMetadataUtils.interfaceLinkDisplayString) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) Interface(org.batfish.datamodel.Interface) Test(org.junit.Test)

Example 5 with ConcreteInterfaceAddress

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

the class CompletionMetadataUtilsTest method testGetPrefixes.

@Test
public void testGetPrefixes() {
    String nodeName = "nodeName";
    String int1 = "int1";
    String int2 = "int2";
    String address1 = "10.1.3.1/30";
    String address2 = "128.212.155.30/24";
    String address3 = "124.51.32.2/20";
    ConcreteInterfaceAddress interfaceAddress1 = ConcreteInterfaceAddress.parse(address1);
    ConcreteInterfaceAddress interfaceAddress2 = ConcreteInterfaceAddress.parse(address2);
    ConcreteInterfaceAddress interfaceAddress3 = ConcreteInterfaceAddress.parse(address3);
    Map<String, Configuration> configs = new HashMap<>();
    Configuration config = createTestConfiguration(nodeName, ConfigurationFormat.HOST, int1, int2);
    config.getAllInterfaces().get(int1).setAllAddresses(ImmutableSet.of(interfaceAddress1, interfaceAddress2));
    config.getAllInterfaces().get(int2).setAllAddresses(ImmutableSet.of(interfaceAddress2, interfaceAddress3));
    configs.put(nodeName, config);
    assertThat(getPrefixes(configs), equalTo(ImmutableSet.of(interfaceAddress1.getPrefix().toString(), interfaceAddress2.getPrefix().toString(), interfaceAddress3.getPrefix().toString())));
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) CompletionMetadataUtils.addressGroupDisplayString(org.batfish.common.util.CompletionMetadataUtils.addressGroupDisplayString) CompletionMetadataUtils.interfaceDisplayString(org.batfish.common.util.CompletionMetadataUtils.interfaceDisplayString) CompletionMetadataUtils.interfaceLinkDisplayString(org.batfish.common.util.CompletionMetadataUtils.interfaceLinkDisplayString) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) Test(org.junit.Test)

Aggregations

ConcreteInterfaceAddress (org.batfish.datamodel.ConcreteInterfaceAddress)132 Ip (org.batfish.datamodel.Ip)77 Configuration (org.batfish.datamodel.Configuration)54 Prefix (org.batfish.datamodel.Prefix)44 Interface (org.batfish.datamodel.Interface)38 Test (org.junit.Test)37 Nullable (javax.annotation.Nullable)32 ImmutableList (com.google.common.collect.ImmutableList)30 HashMap (java.util.HashMap)29 Vrf (org.batfish.datamodel.Vrf)29 VisibleForTesting (com.google.common.annotations.VisibleForTesting)28 ImmutableMap (com.google.common.collect.ImmutableMap)28 List (java.util.List)28 Map (java.util.Map)28 ImmutableSet (com.google.common.collect.ImmutableSet)27 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)27 Nonnull (javax.annotation.Nonnull)27 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)24 Optional (java.util.Optional)22 Set (java.util.Set)22