Skip to content

Commit

Permalink
Small cleanups to searchfilters (batfish#2341)
Browse files Browse the repository at this point in the history
* Small cleanups to searchfilters

* update ref
  • Loading branch information
ratulm authored Sep 21, 2018
1 parent 3a1bee6 commit 80ece1d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.batfish.common.Answerer;
import org.batfish.common.BatfishException;
import org.batfish.common.Pair;
Expand All @@ -39,6 +40,7 @@
import org.batfish.datamodel.questions.Question;
import org.batfish.datamodel.table.ColumnMetadata;
import org.batfish.datamodel.table.Row;
import org.batfish.datamodel.table.Row.RowBuilder;
import org.batfish.datamodel.table.TableAnswerElement;
import org.batfish.datamodel.table.TableDiff;
import org.batfish.datamodel.table.TableMetadata;
Expand All @@ -50,7 +52,7 @@

/** Answerer for SearchFiltersQuestion */
public final class SearchFiltersAnswerer extends Answerer {
private static final String HEADERSPACE = "HeaderSpace";
private static final String COL_HEADER_SPACE = "Header_Space";
private TableAnswerElement _tableAnswerElement;

public SearchFiltersAnswerer(Question question, IBatfish batfish) {
Expand All @@ -60,11 +62,7 @@ public SearchFiltersAnswerer(Question question, IBatfish batfish) {
@Override
public AnswerElement answer() {
SearchFiltersQuestion question = (SearchFiltersQuestion) _question;
if (question.getDifferential()) {
differentialAnswer(question);
} else {
nonDifferentialAnswer(question);
}
nonDifferentialAnswer(question);
return _tableAnswerElement;
}

Expand All @@ -89,10 +87,12 @@ private void differentialAnswer(SearchFiltersQuestion question) {

TableAnswerElement baseTable =
toReachFilterTable(
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)));
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)),
question.getGenerateExplanations());
TableAnswerElement deltaTable =
toReachFilterTable(
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)));
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)),
question.getGenerateExplanations());

Set<String> commonNodes = Sets.intersection(baseAcls.keySet(), deltaAcls.keySet());
for (String node : commonNodes) {
Expand Down Expand Up @@ -140,11 +140,14 @@ private void differentialAnswer(SearchFiltersQuestion question) {
AclLineMatchExpr description = result.getHeaderSpaceDescription().orElse(null);
baseTable.addRow(
toReachFilterRow(
description, testFiltersRow(true, node, baseAcl.get().getName(), flow)));
description,
testFiltersRow(true, node, baseAcl.get().getName(), flow),
question.getGenerateExplanations()));
deltaTable.addRow(
toReachFilterRow(
description,
testFiltersRow(false, node, deltaAcl.get().getName(), flow)));
testFiltersRow(false, node, deltaAcl.get().getName(), flow),
question.getGenerateExplanations()));
});
}
}
Expand All @@ -162,14 +165,17 @@ private void differentialAnswer(SearchFiltersQuestion question) {
_tableAnswerElement.postProcessAnswer(question, diffTable.getRows().getData());
}

private static TableAnswerElement toReachFilterTable(TableAnswerElement tableAnswerElement) {
private static TableAnswerElement toReachFilterTable(
TableAnswerElement tableAnswerElement, boolean generateExplanation) {
Map<String, ColumnMetadata> columnMap = tableAnswerElement.getMetadata().toColumnMap();

List<ColumnMetadata> columnMetadata = new ArrayList<>();
columnMetadata.add(columnMap.get(TestFiltersAnswerer.COL_NODE));
columnMetadata.add(columnMap.get(TestFiltersAnswerer.COL_FILTER_NAME));
columnMetadata.add(
new ColumnMetadata(HEADERSPACE, Schema.STRING, "Description of HeaderSpace"));
if (generateExplanation) {
columnMetadata.add(
new ColumnMetadata(COL_HEADER_SPACE, Schema.STRING, "Description of HeaderSpace"));
}
columnMetadata.add(columnMap.get(TestFiltersAnswerer.COL_FLOW));
columnMetadata.add(columnMap.get(TestFiltersAnswerer.COL_ACTION));
columnMetadata.add(columnMap.get(TestFiltersAnswerer.COL_LINE_CONTENT));
Expand All @@ -179,22 +185,27 @@ private static TableAnswerElement toReachFilterTable(TableAnswerElement tableAns
return new TableAnswerElement(metadata);
}

private static Row toReachFilterRow(AclLineMatchExpr description, Row row) {
/*
* Sending the explanation to the client as a JSON blob. TODO: do something better.
*/
String jsonDescription;
try {
jsonDescription = BatfishObjectMapper.writeString(description);
} catch (JsonProcessingException e) {
e.printStackTrace();
jsonDescription = e.getMessage();
private static Row toReachFilterRow(
@Nullable AclLineMatchExpr description, Row row, boolean generateExplanations) {

RowBuilder rowBuilder = Row.builder().putAll(row, row.getColumnNames());

if (generateExplanations) {
/*
* Sending the explanation to the client as a JSON blob. TODO: do something better.
*/
String jsonDescription;
try {
jsonDescription = BatfishObjectMapper.writeString(description);
} catch (JsonProcessingException e) {
e.printStackTrace();
jsonDescription = e.getMessage();
}

rowBuilder.put(COL_HEADER_SPACE, jsonDescription);
}

return Row.builder()
.putAll(row, row.getColumnNames())
.put(HEADERSPACE, jsonDescription)
.build();
return rowBuilder.build();
}

private void nonDifferentialAnswer(SearchFiltersQuestion question) {
Expand Down Expand Up @@ -225,12 +236,14 @@ private void nonDifferentialAnswer(SearchFiltersQuestion question) {
rows.add(
toReachFilterRow(
result.getHeaderSpaceDescription().orElse(null),
testFiltersRow(true, hostname, acl.getName(), result.getExampleFlow()))));
testFiltersRow(true, hostname, acl.getName(), result.getExampleFlow()),
question.getGenerateExplanations())));
}

_tableAnswerElement =
toReachFilterTable(
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)));
TestFiltersAnswerer.create(new TestFiltersQuestion(null, null, null, null)),
question.getGenerateExplanations());
_tableAnswerElement.postProcessAnswer(question, rows);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public boolean getComplementHeaderSpace() {
return _complementHeaderSpace;
}

@JsonProperty(PROP_GENERATE_EXPLANATIONS)
public boolean getGenerateExplanations() {
return _generateExplanations;
}

@Nullable
@JsonProperty(PROP_FILTERS)
private String getFilters() {
Expand Down
1 change: 1 addition & 0 deletions tests/questions/experimental/searchfilters.ref
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"class" : "org.batfish.question.searchfilters.SearchFiltersQuestion",
"explain" : false,
"filters" : ".*",
"headers" : {
"dstIps" : "2.2.2.2",
Expand Down

0 comments on commit 80ece1d

Please sign in to comment.