use of org.batfish.common.Pair 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.common.Pair in project batfish by batfish.
the class BfCoordWorkHelper method getWorkStatus.
@Nullable
public Pair<WorkStatusCode, String> getWorkStatus(UUID workId) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_WORKSTATUS);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_WORKID, workId.toString());
JSONObject jObj = postData(webTarget, multiPart);
if (jObj == null) {
return null;
}
if (!jObj.has(CoordConsts.SVC_KEY_WORKSTATUS)) {
_logger.errorf("workstatus key not found in: %s\n", jObj);
return null;
}
WorkStatusCode workStatus = WorkStatusCode.valueOf(jObj.getString(CoordConsts.SVC_KEY_WORKSTATUS));
if (!jObj.has(CoordConsts.SVC_KEY_TASKSTATUS)) {
_logger.errorf("taskstatus key not found in: %s\n", jObj);
}
String taskStr = jObj.getString(CoordConsts.SVC_KEY_TASKSTATUS);
return new Pair<>(workStatus, taskStr);
} catch (Exception e) {
_logger.errorf("exception: ");
_logger.error(ExceptionUtils.getStackTrace(e) + "\n");
return null;
}
}
Aggregations