Search in sources :

Example 1 with NodeInterfacePair

use of org.batfish.datamodel.collections.NodeInterfacePair in project batfish by batfish.

the class Client method initEnvironment.

private boolean initEnvironment(String paramsLine, FileWriter outWriter) {
    InitEnvironmentParams params = parseInitEnvironmentParams(paramsLine);
    String newEnvName;
    String paramsLocation = params.getSourcePath();
    String paramsName = params.getNewEnvironmentName();
    String paramsPrefix = params.getNewEnvironmentPrefix();
    String testrigName = params.getDoDelta() ? _currDeltaTestrig : _currTestrig;
    if (paramsName != null) {
        newEnvName = paramsName;
    } else if (paramsPrefix != null) {
        newEnvName = paramsPrefix + UUID.randomUUID();
    } else {
        newEnvName = DEFAULT_DELTA_ENV_PREFIX + UUID.randomUUID();
    }
    String paramsBaseEnv = params.getSourceEnvironmentName();
    String baseEnvName = paramsBaseEnv != null ? paramsBaseEnv : BfConsts.RELPATH_DEFAULT_ENVIRONMENT_NAME;
    String fileToSend;
    SortedSet<String> paramsNodeBlacklist = params.getNodeBlacklist();
    SortedSet<NodeInterfacePair> paramsInterfaceBlacklist = params.getInterfaceBlacklist();
    SortedSet<Edge> paramsEdgeBlacklist = params.getEdgeBlacklist();
    if (paramsLocation == null || Files.isDirectory(Paths.get(paramsLocation)) || !paramsNodeBlacklist.isEmpty() || !paramsInterfaceBlacklist.isEmpty() || !paramsEdgeBlacklist.isEmpty()) {
        Path tempFile = CommonUtil.createTempFile("batfish_client_tmp_env_", ".zip");
        fileToSend = tempFile.toString();
        if (paramsLocation != null && Files.isDirectory(Paths.get(paramsLocation)) && paramsNodeBlacklist.isEmpty() && paramsInterfaceBlacklist.isEmpty() && paramsEdgeBlacklist.isEmpty()) {
            ZipUtility.zipFiles(Paths.get(paramsLocation), tempFile);
        } else {
            Path tempDir = CommonUtil.createTempDirectory("batfish_client_tmp_env_");
            if (paramsLocation != null) {
                if (Files.isDirectory(Paths.get(paramsLocation))) {
                    CommonUtil.copyDirectory(Paths.get(paramsLocation), tempDir);
                } else if (Files.isRegularFile(Paths.get(paramsLocation))) {
                    UnzipUtility.unzip(Paths.get(paramsLocation), tempDir);
                } else {
                    throw new BatfishException("Invalid environment directory or zip: '" + paramsLocation + "'");
                }
            }
            if (!paramsNodeBlacklist.isEmpty()) {
                String nodeBlacklistText;
                try {
                    nodeBlacklistText = BatfishObjectMapper.writePrettyString(paramsNodeBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write node blacklist to string", e);
                }
                Path nodeBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_NODE_BLACKLIST_FILE);
                CommonUtil.writeFile(nodeBlacklistFilePath, nodeBlacklistText);
            }
            if (!paramsInterfaceBlacklist.isEmpty()) {
                String interfaceBlacklistText;
                try {
                    interfaceBlacklistText = BatfishObjectMapper.writePrettyString(paramsInterfaceBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write interface blacklist to string", e);
                }
                Path interfaceBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_INTERFACE_BLACKLIST_FILE);
                CommonUtil.writeFile(interfaceBlacklistFilePath, interfaceBlacklistText);
            }
            if (!paramsEdgeBlacklist.isEmpty()) {
                String edgeBlacklistText;
                try {
                    edgeBlacklistText = BatfishObjectMapper.writePrettyString(paramsEdgeBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write edge blacklist to string", e);
                }
                Path edgeBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_EDGE_BLACKLIST_FILE);
                CommonUtil.writeFile(edgeBlacklistFilePath, edgeBlacklistText);
            }
            ZipUtility.zipFiles(tempDir, tempFile);
        }
    } else if (Files.isRegularFile(Paths.get(paramsLocation))) {
        fileToSend = paramsLocation;
    } else {
        throw new BatfishException("Invalid environment directory or zip: '" + paramsLocation + "'");
    }
    if (!uploadEnv(fileToSend, testrigName, newEnvName, baseEnvName)) {
        return false;
    }
    _currDeltaEnv = newEnvName;
    _currDeltaTestrig = _currTestrig;
    _logger.output("Active delta testrig->environment is set");
    _logger.infof("to %s->%s\n", _currDeltaTestrig, _currDeltaEnv);
    _logger.output("\n");
    WorkItem wItemProcessEnv = WorkItemBuilder.getWorkItemProcessEnvironment(_currContainerName, _currDeltaTestrig, _currDeltaEnv);
    if (!execute(wItemProcessEnv, outWriter)) {
        return false;
    }
    return true;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) InitEnvironmentParams(org.batfish.client.params.InitEnvironmentParams) Edge(org.batfish.datamodel.Edge) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WorkItem(org.batfish.common.WorkItem)

Example 2 with NodeInterfacePair

use of org.batfish.datamodel.collections.NodeInterfacePair in project batfish by batfish.

the class CommonUtil method synthesizeTopology.

public static Topology synthesizeTopology(Map<String, Configuration> configurations) {
    SortedSet<Edge> edges = new TreeSet<>();
    Map<Prefix, Set<NodeInterfacePair>> prefixInterfaces = new HashMap<>();
    configurations.forEach((nodeName, node) -> {
        for (Entry<String, Interface> e : node.getInterfaces().entrySet()) {
            String ifaceName = e.getKey();
            Interface iface = e.getValue();
            if (!iface.isLoopback(node.getConfigurationFormat()) && iface.getActive()) {
                for (InterfaceAddress address : iface.getAllAddresses()) {
                    if (address.getNetworkBits() < Prefix.MAX_PREFIX_LENGTH) {
                        Prefix prefix = address.getPrefix();
                        NodeInterfacePair pair = new NodeInterfacePair(nodeName, ifaceName);
                        Set<NodeInterfacePair> interfaceBucket = prefixInterfaces.computeIfAbsent(prefix, k -> new HashSet<>());
                        interfaceBucket.add(pair);
                    }
                }
            }
        }
    });
    for (Set<NodeInterfacePair> bucket : prefixInterfaces.values()) {
        for (NodeInterfacePair p1 : bucket) {
            for (NodeInterfacePair p2 : bucket) {
                if (!p1.equals(p2)) {
                    Edge edge = new Edge(p1, p2);
                    edges.add(edge);
                }
            }
        }
    }
    return new Topology(edges);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) Prefix(org.batfish.datamodel.Prefix) Topology(org.batfish.datamodel.Topology) TreeSet(java.util.TreeSet) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface)

Example 3 with NodeInterfacePair

use of org.batfish.datamodel.collections.NodeInterfacePair in project batfish by batfish.

the class ForwardingAnalysisImpl method computeRoutesWithNextHopIpArpTrue.

@VisibleForTesting
Map<Edge, Set<AbstractRoute>> computeRoutesWithNextHopIpArpTrue(Map<String, Map<String, Fib>> fibs, Topology topology) {
    ImmutableMap.Builder<Edge, Set<AbstractRoute>> routesByEdgeBuilder = ImmutableMap.builder();
    _routesWithNextHop.forEach((hostname, routesWithNextHopByVrf) -> routesWithNextHopByVrf.forEach((vrf, routesWithNextHopByInterface) -> routesWithNextHopByInterface.forEach((outInterface, candidateRoutes) -> {
        Fib fib = fibs.get(hostname).get(vrf);
        NodeInterfacePair out = new NodeInterfacePair(hostname, outInterface);
        Set<NodeInterfacePair> receivers = topology.getNeighbors(out);
        receivers.forEach(receiver -> {
            String recvNode = receiver.getHostname();
            String recvInterface = receiver.getInterface();
            IpSpace recvReplies = _arpReplies.get(recvNode).get(recvInterface);
            Edge edge = new Edge(out, receiver);
            Set<AbstractRoute> routes = candidateRoutes.stream().filter(route -> fib.getNextHopInterfaces().get(route).get(outInterface).keySet().stream().filter(ip -> !ip.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)).anyMatch(recvReplies::containsIp)).collect(ImmutableSet.toImmutableSet());
            routesByEdgeBuilder.put(edge, routes);
        });
    })));
    return routesByEdgeBuilder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) HashMap(java.util.HashMap) Function(java.util.function.Function) Sets(com.google.common.collect.Sets) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Entry(java.util.Map.Entry) Predicates(com.google.common.base.Predicates) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SortedMap(java.util.SortedMap) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with NodeInterfacePair

use of org.batfish.datamodel.collections.NodeInterfacePair in project batfish by batfish.

the class Topology method prune.

/**
 * Removes the specified blacklists from the topology
 */
public void prune(Set<Edge> blacklistEdges, Set<String> blacklistNodes, Set<NodeInterfacePair> blacklistInterfaces) {
    if (blacklistEdges != null) {
        SortedSet<Edge> edges = getEdges();
        edges.removeAll(blacklistEdges);
    }
    if (blacklistNodes != null) {
        for (String blacklistNode : blacklistNodes) {
            removeNode(blacklistNode);
        }
    }
    if (blacklistInterfaces != null) {
        for (NodeInterfacePair blacklistInterface : blacklistInterfaces) {
            removeInterface(blacklistInterface);
        }
    }
    rebuildFromEdges();
}
Also used : NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair)

Example 5 with NodeInterfacePair

use of org.batfish.datamodel.collections.NodeInterfacePair in project batfish by batfish.

the class Topology method rebuildFromEdges.

private void rebuildFromEdges() {
    _nodeEdges.clear();
    _interfaceEdges.clear();
    for (Edge edge : getEdges()) {
        String node1 = edge.getNode1();
        String node2 = edge.getNode2();
        NodeInterfacePair int1 = edge.getInterface1();
        NodeInterfacePair int2 = edge.getInterface2();
        SortedSet<Edge> node1Edges = _nodeEdges.computeIfAbsent(node1, k -> new TreeSet<>());
        node1Edges.add(edge);
        SortedSet<Edge> node2Edges = _nodeEdges.computeIfAbsent(node2, k -> new TreeSet<>());
        node2Edges.add(edge);
        SortedSet<Edge> interface1Edges = _interfaceEdges.computeIfAbsent(int1, k -> new TreeSet<>());
        interface1Edges.add(edge);
        SortedSet<Edge> interface2Edges = _interfaceEdges.computeIfAbsent(int2, k -> new TreeSet<>());
        interface2Edges.add(edge);
    }
}
Also used : NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair)

Aggregations

NodeInterfacePair (org.batfish.datamodel.collections.NodeInterfacePair)19 Edge (org.batfish.datamodel.Edge)13 Configuration (org.batfish.datamodel.Configuration)8 HashMap (java.util.HashMap)7 Set (java.util.Set)7 Interface (org.batfish.datamodel.Interface)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 SortedMap (java.util.SortedMap)5 BatfishException (org.batfish.common.BatfishException)5 FlowTrace (org.batfish.datamodel.FlowTrace)5 Ip (org.batfish.datamodel.Ip)5 Topology (org.batfish.datamodel.Topology)5 ImmutableList (com.google.common.collect.ImmutableList)4 Sets (com.google.common.collect.Sets)4 Path (java.nio.file.Path)4 Entry (java.util.Map.Entry)4 SortedSet (java.util.SortedSet)4