use of org.batfish.datamodel.Interface 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;
}
use of org.batfish.datamodel.Interface in project batfish by batfish.
the class CommonUtil method initPrivateIpsByPublicIp.
@VisibleForTesting
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.getInterfaces().values();
Set<InterfaceAddress> nonNattedInterfaceAddresses = interfaces.stream().filter(i -> i.getSourceNats().isEmpty()).flatMap(i -> i.getAllAddresses().stream()).collect(ImmutableSet.toImmutableSet());
Set<IpWildcard> blacklist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getIp(), Ip.ZERO)).collect(ImmutableSet.toImmutableSet());
Set<IpWildcard> whitelist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getPrefix())).collect(ImmutableSet.toImmutableSet());
IpWildcardSetIpSpace ipSpace = IpWildcardSetIpSpace.builder().including(whitelist).excluding(blacklist).build();
interfaces.stream().flatMap(i -> i.getSourceNats().stream()).forEach(sourceNat -> {
for (long ipAsLong = sourceNat.getPoolIpFirst().asLong(); ipAsLong <= sourceNat.getPoolIpLast().asLong(); ipAsLong++) {
Ip currentPoolIp = new Ip(ipAsLong);
builder.put(currentPoolIp, ipSpace);
}
});
}
return builder.build();
}
use of org.batfish.datamodel.Interface in project batfish by batfish.
the class CommonUtil method initRemoteOspfNeighbors.
public static void initRemoteOspfNeighbors(Map<String, Configuration> configurations, Map<Ip, Set<String>> ipOwners, Topology topology) {
for (Entry<String, Configuration> e : configurations.entrySet()) {
String hostname = e.getKey();
Configuration c = e.getValue();
for (Entry<String, Vrf> e2 : c.getVrfs().entrySet()) {
Vrf vrf = e2.getValue();
OspfProcess proc = vrf.getOspfProcess();
if (proc != null) {
proc.setOspfNeighbors(new TreeMap<>());
String vrfName = e2.getKey();
for (Entry<Long, OspfArea> e3 : proc.getAreas().entrySet()) {
long areaNum = e3.getKey();
OspfArea area = e3.getValue();
for (String ifaceName : area.getInterfaces()) {
Interface iface = c.getInterfaces().get(ifaceName);
if (iface.getOspfPassive()) {
continue;
}
SortedSet<Edge> ifaceEdges = topology.getInterfaceEdges().get(new NodeInterfacePair(hostname, ifaceName));
boolean hasNeighbor = false;
Ip localIp = iface.getAddress().getIp();
if (ifaceEdges != null) {
for (Edge edge : ifaceEdges) {
if (edge.getNode1().equals(hostname)) {
String remoteHostname = edge.getNode2();
String remoteIfaceName = edge.getInt2();
Configuration remoteNode = configurations.get(remoteHostname);
Interface remoteIface = remoteNode.getInterfaces().get(remoteIfaceName);
if (remoteIface.getOspfPassive()) {
continue;
}
Vrf remoteVrf = remoteIface.getVrf();
String remoteVrfName = remoteVrf.getName();
OspfProcess remoteProc = remoteVrf.getOspfProcess();
if (remoteProc != null) {
if (remoteProc.getOspfNeighbors() == null) {
remoteProc.setOspfNeighbors(new TreeMap<>());
}
OspfArea remoteArea = remoteProc.getAreas().get(areaNum);
if (remoteArea != null && remoteArea.getInterfaces().contains(remoteIfaceName)) {
Ip remoteIp = remoteIface.getAddress().getIp();
IpLink localKey = new IpLink(localIp, remoteIp);
OspfNeighbor neighbor = proc.getOspfNeighbors().get(localKey);
if (neighbor == null) {
hasNeighbor = true;
// initialize local neighbor
neighbor = new OspfNeighbor(localKey);
neighbor.setArea(areaNum);
neighbor.setVrf(vrfName);
neighbor.setOwner(c);
neighbor.setInterface(iface);
proc.getOspfNeighbors().put(localKey, neighbor);
// initialize remote neighbor
IpLink remoteKey = new IpLink(remoteIp, localIp);
OspfNeighbor remoteNeighbor = new OspfNeighbor(remoteKey);
remoteNeighbor.setArea(areaNum);
remoteNeighbor.setVrf(remoteVrfName);
remoteNeighbor.setOwner(remoteNode);
remoteNeighbor.setInterface(remoteIface);
remoteProc.getOspfNeighbors().put(remoteKey, remoteNeighbor);
// link neighbors
neighbor.setRemoteOspfNeighbor(remoteNeighbor);
remoteNeighbor.setRemoteOspfNeighbor(neighbor);
}
}
}
}
}
}
if (!hasNeighbor) {
IpLink key = new IpLink(localIp, Ip.ZERO);
OspfNeighbor neighbor = new OspfNeighbor(key);
neighbor.setArea(areaNum);
neighbor.setVrf(vrfName);
neighbor.setOwner(c);
neighbor.setInterface(iface);
proc.getOspfNeighbors().put(key, neighbor);
}
}
}
}
}
}
}
Aggregations