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