use of org.batfish.datamodel.answers.Answer in project batfish by batfish.
the class Batfish method reducedReachability.
@Override
public AnswerElement reducedReachability(ReachabilitySettings reachabilitySettings) {
Settings settings = getSettings();
checkDifferentialDataPlaneQuestionDependencies();
String tag = getDifferentialFlowTag();
// load base configurations and generate base data plane
pushBaseEnvironment();
Map<String, Configuration> baseConfigurations = loadConfigurations();
Synthesizer baseDataPlaneSynthesizer = synthesizeDataPlane();
popEnvironment();
// load diff configurations and generate diff data plane
pushDeltaEnvironment();
Map<String, Configuration> diffConfigurations = loadConfigurations();
Synthesizer diffDataPlaneSynthesizer = synthesizeDataPlane();
popEnvironment();
Set<String> ingressNodes;
try {
ingressNodes = ImmutableSet.copyOf(Sets.intersection(reachabilitySettings.computeActiveIngressNodes(baseConfigurations), reachabilitySettings.computeActiveIngressNodes(diffConfigurations)));
} catch (InvalidReachabilitySettingsException e) {
return e.getInvalidSettingsAnswer();
}
pushDeltaEnvironment();
SortedSet<String> blacklistNodes = getNodeBlacklist();
Set<NodeInterfacePair> blacklistInterfaces = getInterfaceBlacklist();
SortedSet<Edge> blacklistEdges = getEdgeBlacklist();
popEnvironment();
BlacklistDstIpQuerySynthesizer blacklistQuery = new BlacklistDstIpQuerySynthesizer(null, blacklistNodes, blacklistInterfaces, blacklistEdges, baseConfigurations);
// compute composite program and flows
List<Synthesizer> synthesizers = ImmutableList.of(baseDataPlaneSynthesizer, diffDataPlaneSynthesizer, baseDataPlaneSynthesizer);
// generate base reachability and diff blackhole and blacklist queries
List<CompositeNodJob> jobs = ingressNodes.stream().flatMap(node -> baseConfigurations.get(node).getVrfs().keySet().stream().map(vrf -> {
Map<String, Set<String>> ingressNodeVrfs = ImmutableMap.of(node, ImmutableSet.of(vrf));
StandardReachabilityQuerySynthesizer acceptQuery = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT, ForwardingAction.NEIGHBOR_UNREACHABLE_OR_EXITS_NETWORK)).setHeaderSpace(reachabilitySettings.getHeaderSpace()).setIngressNodeVrfs(ingressNodeVrfs).setFinalNodes(ImmutableSet.of()).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).setSrcNatted(reachabilitySettings.getSrcNatted()).build();
StandardReachabilityQuerySynthesizer notAcceptQuery = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT, ForwardingAction.NEIGHBOR_UNREACHABLE_OR_EXITS_NETWORK)).setHeaderSpace(new HeaderSpace()).setIngressNodeVrfs(ingressNodeVrfs).setFinalNodes(ImmutableSet.of()).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).build();
notAcceptQuery.setNegate(true);
SortedSet<Pair<String, String>> nodes = ImmutableSortedSet.of(new Pair<>(node, vrf));
List<QuerySynthesizer> queries = ImmutableList.of(acceptQuery, notAcceptQuery, blacklistQuery);
return new CompositeNodJob(settings, synthesizers, queries, nodes, tag);
})).collect(Collectors.toList());
// TODO: maybe do something with nod answer element
Set<Flow> flows = computeCompositeNodOutput(jobs, new NodAnswerElement());
pushBaseEnvironment();
getDataPlanePlugin().processFlows(flows, loadDataPlane());
popEnvironment();
pushDeltaEnvironment();
getDataPlanePlugin().processFlows(flows, loadDataPlane());
popEnvironment();
AnswerElement answerElement = getHistory();
return answerElement;
}
use of org.batfish.datamodel.answers.Answer in project batfish by batfish.
the class Batfish method outputAnswer.
private void outputAnswer(Answer answer, boolean writeLog) {
try {
String answerString = BatfishObjectMapper.writePrettyString(answer) + '\n';
_logger.debug(answerString);
@Nullable String logString = writeLog ? answerString : null;
writeJsonAnswerWithLog(logString, answerString);
} catch (Exception e) {
BatfishException be = new BatfishException("Error in sending answer", e);
try {
Answer failureAnswer = Answer.failureAnswer(e.toString(), answer.getQuestion());
failureAnswer.addAnswerElement(be.getBatfishStackTrace());
String answerString = BatfishObjectMapper.writePrettyString(failureAnswer) + '\n';
_logger.error(answerString);
@Nullable String logString = writeLog ? answerString : null;
writeJsonAnswerWithLog(logString, answerString);
} catch (Exception e1) {
_logger.errorf("Could not serialize failure answer. %s", ExceptionUtils.getStackTrace(e1));
}
throw be;
}
}
use of org.batfish.datamodel.answers.Answer in project batfish by batfish.
the class Batfish method serializeIndependentConfigs.
private Answer serializeIndependentConfigs(Path vendorConfigPath) {
Answer answer = new Answer();
ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
answerElement.setVersion(Version.getVersion());
if (_settings.getVerboseParse()) {
answer.addAnswerElement(answerElement);
}
Map<String, Configuration> configurations = getConfigurations(vendorConfigPath, answerElement);
Topology testrigTopology = computeTestrigTopology(_testrigSettings.getTestRigPath(), configurations);
serializeAsJson(_testrigSettings.getTopologyPath(), testrigTopology, "testrig topology");
checkTopology(configurations, testrigTopology);
org.batfish.datamodel.pojo.Topology pojoTopology = org.batfish.datamodel.pojo.Topology.create(_testrigSettings.getName(), configurations, testrigTopology);
serializeAsJson(_testrigSettings.getPojoTopologyPath(), pojoTopology, "testrig pojo topology");
_storage.storeConfigurations(configurations, answerElement, _testrigSettings.getName());
applyEnvironment(configurations);
Topology envTopology = computeEnvironmentTopology(configurations);
serializeAsJson(_testrigSettings.getEnvironmentSettings().getSerializedTopologyPath(), envTopology, "environment topology");
NodeRoleSpecifier roleSpecifier = inferNodeRoles(configurations);
serializeAsJson(_testrigSettings.getInferredNodeRolesPath(), roleSpecifier, "inferred node roles");
return answer;
}
use of org.batfish.datamodel.answers.Answer in project batfish by batfish.
the class Batfish method compileEnvironmentConfigurations.
private Answer compileEnvironmentConfigurations(TestrigSettings testrigSettings) {
Answer answer = new Answer();
EnvironmentSettings envSettings = testrigSettings.getEnvironmentSettings();
Path deltaConfigurationsDir = envSettings.getDeltaConfigurationsDir();
Path vendorConfigsDir = envSettings.getDeltaVendorConfigurationsDir();
if (deltaConfigurationsDir != null) {
if (Files.exists(deltaConfigurationsDir)) {
answer.append(serializeVendorConfigs(envSettings.getEnvPath(), vendorConfigsDir));
answer.append(serializeIndependentConfigs(vendorConfigsDir));
}
return answer;
} else {
throw new BatfishException("Delta configurations directory cannot be null");
}
}
use of org.batfish.datamodel.answers.Answer in project batfish by batfish.
the class Client method getAnswer.
private boolean getAnswer(@Nullable FileWriter outWriter, List<String> options, List<String> parameters, boolean delta, boolean differential) {
Command command = differential ? Command.GET_ANSWER_DIFFERENTIAL : delta ? Command.GET_ANSWER_DELTA : Command.GET_ANSWER;
if (!isValidArgument(options, parameters, 0, 1, 1, command)) {
return false;
}
if (!isSetTestrig() || !isSetContainer(true)) {
return false;
}
String questionName = parameters.get(0);
String baseTestrig;
String baseEnvironment;
String deltaTestrig;
String deltaEnvironment;
if (differential) {
baseTestrig = _currTestrig;
baseEnvironment = _currEnv;
deltaTestrig = _currDeltaTestrig;
deltaEnvironment = _currDeltaEnv;
} else if (delta) {
baseTestrig = _currDeltaTestrig;
baseEnvironment = _currDeltaEnv;
deltaTestrig = null;
deltaEnvironment = null;
} else {
baseTestrig = _currTestrig;
baseEnvironment = _currEnv;
deltaTestrig = null;
deltaEnvironment = null;
}
String answerString = _workHelper.getAnswer(_currContainerName, baseTestrig, baseEnvironment, deltaTestrig, deltaEnvironment, questionName);
String answerStringToPrint = answerString;
if (outWriter == null && _settings.getPrettyPrintAnswers()) {
Answer answer;
try {
answer = BatfishObjectMapper.mapper().readValue(answerString, Answer.class);
} catch (IOException e) {
throw new BatfishException("Response does not appear to be valid JSON representation of " + Answer.class.getSimpleName());
}
answerStringToPrint = answer.prettyPrint();
}
logOutput(outWriter, answerStringToPrint + "\n");
return true;
}
Aggregations