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);
}
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();
}
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)));
}
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));
}
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())));
}
Aggregations