Skip to content

Commit

Permalink
test: extend exclude as path tests
Browse files Browse the repository at this point in the history
- add test cases
- remove TODOs
- fix small bug where the intermediate representation used the as path of the output route
  • Loading branch information
lukaskoenen committed Aug 26, 2021
1 parent 09ed57b commit 99e2db4
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Result execute(Environment environment) {
AsPath.of(
ImmutableList.<AsSet>builder()
.addAll(
outputRoute.getAsPath().getAsSets().stream()
ir.getAsPath().getAsSets().stream()
.filter(currentAsSet -> !excludeAsPath.contains(currentAsSet))
.collect(Collectors.toSet()))
.build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.batfish.datamodel.routing_policy.statement.BufferedStatement;
import org.batfish.datamodel.routing_policy.statement.CallStatement;
import org.batfish.datamodel.routing_policy.statement.Comment;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.PrependAsPath;
import org.batfish.datamodel.routing_policy.statement.SetAdministrativeCost;
Expand Down Expand Up @@ -159,6 +160,8 @@ public void testStatementVerifierUnrelated() {
assertNull(new Comment("a").accept(STATEMENT_VERIFIER, ctx));
assertNull(
new PrependAsPath(new LiteralAsList(ImmutableList.of())).accept(STATEMENT_VERIFIER, ctx));
assertNull(
new ExcludeAsPath(new LiteralAsList(ImmutableList.of())).accept(STATEMENT_VERIFIER, ctx));
assertNull(new SetAdministrativeCost(new LiteralInt(1)).accept(STATEMENT_VERIFIER, ctx));
assertNull(new SetDefaultPolicy("a").accept(STATEMENT_VERIFIER, ctx));
assertNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.batfish.datamodel.routing_policy.statement.BufferedStatement;
import org.batfish.datamodel.routing_policy.statement.CallStatement;
import org.batfish.datamodel.routing_policy.statement.Comment;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.PrependAsPath;
import org.batfish.datamodel.routing_policy.statement.SetAdministrativeCost;
Expand Down Expand Up @@ -150,6 +151,8 @@ public void testStatementVerifierUnrelated() {
assertNull(new Comment("a").accept(STATEMENT_VERIFIER, ctx));
assertNull(
new PrependAsPath(new LiteralAsList(ImmutableList.of())).accept(STATEMENT_VERIFIER, ctx));
assertNull(
new ExcludeAsPath(new LiteralAsList(ImmutableList.of())).accept(STATEMENT_VERIFIER, ctx));
assertNull(new SetAdministrativeCost(new LiteralInt(1)).accept(STATEMENT_VERIFIER, ctx));
assertNull(new SetCommunities(InputCommunities.instance()).accept(STATEMENT_VERIFIER, ctx));
assertNull(new SetDefaultPolicy("a").accept(STATEMENT_VERIFIER, ctx));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.batfish.datamodel.routing_policy.statement;

import static org.batfish.datamodel.AsPath.ofSingletonAsSets;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import com.google.common.collect.Lists;
import java.util.List;
import org.batfish.datamodel.Bgpv4Route;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.ConfigurationFormat;
import org.batfish.datamodel.routing_policy.Environment;
import org.batfish.datamodel.routing_policy.expr.AsExpr;
import org.batfish.datamodel.routing_policy.expr.ExplicitAs;
import org.batfish.datamodel.routing_policy.expr.LiteralAsList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ExcludeAsPathTest {

private static Environment newTestEnvironment(Bgpv4Route.Builder outputRoute) {
Configuration c = new Configuration("host", ConfigurationFormat.CISCO_IOS);
return Environment.builder(c).setOutputRoute(outputRoute).build();
}

@Test
public void testExclude() {
List<AsExpr> exclude = Lists.newArrayList(new ExplicitAs(3));
ExcludeAsPath operation = new ExcludeAsPath(new LiteralAsList(exclude));
Bgpv4Route.Builder builder = Bgpv4Route.testBuilder();
builder.setAsPath(ofSingletonAsSets(3L, 4L));
Environment env = newTestEnvironment(builder);

operation.execute(env);
assertThat(builder.getAsPath(), equalTo(ofSingletonAsSets(4L)));
}

@Test
public void testExcludeWithIntermediateAttributes() {
List<AsExpr> exclude = Lists.newArrayList(new ExplicitAs(3));
ExcludeAsPath operation = new ExcludeAsPath(new LiteralAsList(exclude));
Bgpv4Route.Builder outputRoute = Bgpv4Route.testBuilder();
outputRoute.setAsPath(ofSingletonAsSets(3L, 4L));

Bgpv4Route.Builder intermediateAttributes = Bgpv4Route.testBuilder();
intermediateAttributes.setAsPath(ofSingletonAsSets(3L, 5L));
Environment env = newTestEnvironment(outputRoute);
env.setIntermediateBgpAttributes(intermediateAttributes);
env.setWriteToIntermediateBgpAttributes(true);

operation.execute(env);
assertThat(outputRoute.getAsPath(), equalTo(ofSingletonAsSets(4L)));
assertThat(intermediateAttributes.getAsPath(), equalTo(ofSingletonAsSets(5L)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ public void setDescription(@Nullable String description) {
_description = description;
}

// TODO: check usage
public void setSetAsPath(@Nullable RouteMapSetPrependAsPath setAsPath) {
_setAsPath = setAsPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.batfish.datamodel.routing_policy.statement.Statement;

/** Clause of set as-path prepend in route map. */
// TODO rename RouteMapSetPrependAsPath
@ParametersAreNonnullByDefault
public class RouteMapSetPrependAsPath implements RouteMapSet {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.batfish.datamodel.routing_policy.expr.LiteralInt;
import org.batfish.datamodel.routing_policy.expr.LiteralLong;
import org.batfish.datamodel.routing_policy.expr.LiteralOrigin;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.PrependAsPath;
import org.batfish.datamodel.routing_policy.statement.SetLocalPreference;
import org.batfish.datamodel.routing_policy.statement.SetOrigin;
Expand Down Expand Up @@ -159,7 +160,9 @@ public void testActivateAndSetBgpProperties() {
new LiteralCommunitySet(CommunitySet.of(StandardCommunity.of(2L)))),
new PrependAsPath(
new LiteralAsList(
ImmutableList.of(new ExplicitAs(1L), new ExplicitAs(65100L)))),
ImmutableList.of(
new ExplicitAs(1L), new ExplicitAs(2L), new ExplicitAs(65100L)))),
new ExcludeAsPath(new LiteralAsList(ImmutableList.of(new ExplicitAs(2L)))),
new SetOrigin(new LiteralOrigin(IGP, null)),
Statements.RemovePrivateAs.toStaticStatement(),
new SetLocalPreference(new LiteralLong(123L)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,14 @@ public void testCumulusFrrVrfRouteMapSetAsPath() {
assertThat(entry.getSetAsPath().getAsns(), contains(11111L, 22222L, 33333L));
}

@Test
public void testCumulusFrrVrfRouteMapSetExcludeAsPath() {
String name = "ROUTE-MAP-NAME";
parse(String.format("route-map %s permit 10\nset as-path exclude 11111 22222 33333\n", name));
RouteMapEntry entry = _frr.getRouteMaps().get(name).getEntries().get(10);
assertThat(entry.getSetExcludeAsPath().getAsns(), contains(11111L, 22222L, 33333L));
}

@Test
public void testCumulusFrrVrfRouteMapSetMetric() {
String name = "ROUTE-MAP-NAME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public Set<SymbolicAsPathRegex> visitPrependAsPath(
@Override
public Set<SymbolicAsPathRegex> visitExcludeAsPath(
ExcludeAsPath excludeAsPath, Configuration arg) {
// if/when we update TransferBDD to support AS-path prepending, we will need to update this as
// well
return ImmutableSet.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import org.batfish.datamodel.ConfigurationFormat;
import org.batfish.datamodel.NetworkFactory;
import org.batfish.datamodel.TraceElement;
import org.batfish.datamodel.routing_policy.expr.ExplicitAs;
import org.batfish.datamodel.routing_policy.expr.ExplicitAsPathSet;
import org.batfish.datamodel.routing_policy.expr.LegacyMatchAsPath;
import org.batfish.datamodel.routing_policy.expr.LiteralAsList;
import org.batfish.datamodel.routing_policy.expr.RegexAsPathSetElem;
import org.batfish.datamodel.routing_policy.statement.BufferedStatement;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.Statements;
import org.batfish.datamodel.routing_policy.statement.TraceableStatement;
Expand Down Expand Up @@ -101,4 +104,13 @@ public void testVisitTraceableStatement() {
ImmutableSet.of(new SymbolicAsPathRegex(ASPATH1), new SymbolicAsPathRegex(ASPATH2)),
result);
}

@Test
public void testVisitExcludeAsPath() {
ExcludeAsPath excludeAsPath =
new ExcludeAsPath(new LiteralAsList(ImmutableList.of(new ExplicitAs(1L))));

assertEquals(
ImmutableSet.of(), _asPathCollector.visitExcludeAsPath(excludeAsPath, _baseConfig));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.batfish.datamodel.routing_policy.communities.LiteralCommunitySet;
import org.batfish.datamodel.routing_policy.communities.MatchCommunities;
import org.batfish.datamodel.routing_policy.communities.SetCommunities;
import org.batfish.datamodel.routing_policy.expr.ExplicitAs;
import org.batfish.datamodel.routing_policy.expr.LiteralAsList;
import org.batfish.datamodel.routing_policy.statement.BufferedStatement;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.TraceableStatement;
import org.batfish.minesweeper.CommunityVar;
Expand Down Expand Up @@ -98,4 +101,12 @@ public void testVisitTraceableStatement() {
_varCollector.visitTraceableStatement(traceableStatement, _baseConfig),
ImmutableSet.of(CommunityVar.from(COMM1)));
}

@Test
public void testVisitExcludeAsPath() {
ExcludeAsPath excludeAsPath =
new ExcludeAsPath(new LiteralAsList(ImmutableList.of(new ExplicitAs(1L))));

assertEquals(ImmutableSet.of(), _varCollector.visitExcludeAsPath(excludeAsPath, _baseConfig));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.google.common.collect.ImmutableList;
import java.util.List;
import org.batfish.datamodel.TraceElement;
import org.batfish.datamodel.routing_policy.expr.ExplicitAs;
import org.batfish.datamodel.routing_policy.expr.LiteralAsList;
import org.batfish.datamodel.routing_policy.statement.ExcludeAsPath;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.Statement;
import org.batfish.datamodel.routing_policy.statement.TraceableStatement;
Expand Down Expand Up @@ -62,4 +65,13 @@ public void testVisitTraceableStatement() {
new TraceableStatement(
TraceElement.of(STRIP_TOKEN), ImmutableList.of(new If()))))));
}

@Test
public void testVisitExcludeAsPath() {
ExcludeAsPath excludeAsPath =
new ExcludeAsPath(new LiteralAsList(ImmutableList.of(new ExplicitAs(1L))));

assertThat(
excludeAsPath, equalTo((ExcludeAsPath) excludeAsPath.accept(TRACING_HINTS_STRIPPER, null)));
}
}

0 comments on commit 99e2db4

Please sign in to comment.