Search in sources :

Example 16 with Pair

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

Example 17 with Pair

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;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) JSONObject(org.codehaus.jettison.json.JSONObject) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Pair(org.batfish.common.Pair) Nullable(javax.annotation.Nullable)

Aggregations

Pair (org.batfish.common.Pair)17 BatfishException (org.batfish.common.BatfishException)8 Configuration (org.batfish.datamodel.Configuration)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6 HeaderSpace (org.batfish.datamodel.HeaderSpace)6 Interface (org.batfish.datamodel.Interface)6 Ip (org.batfish.datamodel.Ip)6 NodeInterfacePair (org.batfish.datamodel.collections.NodeInterfacePair)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)5 ImmutableConfiguration (org.apache.commons.configuration2.ImmutableConfiguration)5 Settings (org.batfish.config.Settings)5 AwsConfiguration (org.batfish.representation.aws.AwsConfiguration)5 HostConfiguration (org.batfish.representation.host.HostConfiguration)5 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)5