-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JunOS: Support As-Path-Group (#8613)
Add support for `as-path-group` definition and use in `policy-statement`.
- Loading branch information
Showing
11 changed files
with
259 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
projects/batfish/src/main/java/org/batfish/representation/juniper/PsFromAsPathGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.batfish.representation.juniper; | ||
|
||
import static org.batfish.representation.juniper.parboiled.AsPathRegex.convertToJavaRegex; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import org.batfish.common.Warnings; | ||
import org.batfish.datamodel.Configuration; | ||
import org.batfish.datamodel.routing_policy.as_path.AsPathMatchRegex; | ||
import org.batfish.datamodel.routing_policy.as_path.InputAsPath; | ||
import org.batfish.datamodel.routing_policy.as_path.MatchAsPath; | ||
import org.batfish.datamodel.routing_policy.expr.BooleanExpr; | ||
import org.batfish.datamodel.routing_policy.expr.BooleanExprs; | ||
import org.batfish.datamodel.routing_policy.expr.Disjunction; | ||
|
||
public final class PsFromAsPathGroup extends PsFrom { | ||
private final String _asPathGroupName; | ||
|
||
public PsFromAsPathGroup(String asPathGroupName) { | ||
_asPathGroupName = asPathGroupName; | ||
} | ||
|
||
@Override | ||
public BooleanExpr toBooleanExpr(JuniperConfiguration jc, Configuration c, Warnings warnings) { | ||
Map<String, AsPathGroup> asPathGroups = jc.getMasterLogicalSystem().getAsPathGroups(); | ||
return toBooleanExpr(asPathGroups.get(_asPathGroupName), warnings); | ||
} | ||
|
||
@VisibleForTesting | ||
static BooleanExpr toBooleanExpr(@Nullable AsPathGroup asPathGroup, Warnings w) { | ||
if (asPathGroup == null) { | ||
// Undefined reference, return false. | ||
return BooleanExprs.FALSE; | ||
} | ||
List<BooleanExpr> asPaths = new ArrayList<>(); | ||
for (NamedAsPath namedAsPath : asPathGroup.getAsPaths().values()) { | ||
try { | ||
String convertedVIRegex = convertToJavaRegex(namedAsPath.getRegex()); | ||
asPaths.add(MatchAsPath.of(InputAsPath.instance(), AsPathMatchRegex.of(convertedVIRegex))); | ||
} catch (Exception e) { | ||
w.redFlag( | ||
String.format( | ||
"Error converting Juniper as-path-group regex %s, will assume no paths match" | ||
+ " instead: %s.", | ||
asPathGroup.getName(), e.getMessage())); | ||
/* Handle error, add false to the list instead */ | ||
asPaths.add(BooleanExprs.FALSE); | ||
} | ||
} | ||
return new Disjunction(asPaths); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
projects/batfish/src/test/java/org/batfish/representation/juniper/PsFromAsPathGroupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.batfish.representation.juniper; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import com.google.common.collect.Iterables; | ||
import org.batfish.common.Warnings; | ||
import org.batfish.datamodel.routing_policy.expr.BooleanExpr; | ||
import org.batfish.datamodel.routing_policy.expr.BooleanExprs; | ||
import org.batfish.datamodel.routing_policy.expr.Disjunction; | ||
import org.junit.Test; | ||
|
||
/** Tests of {@link PsFromAsPathGroup}. */ | ||
public class PsFromAsPathGroupTest { | ||
|
||
@Test | ||
public void testUndefined() { | ||
Warnings w = new Warnings(true, true, true); | ||
BooleanExpr ret = PsFromAsPathGroup.toBooleanExpr(null, w); | ||
assertThat(ret, is(BooleanExprs.FALSE)); | ||
assertThat(w.getRedFlagWarnings(), empty()); // Prefer undefined reference elsewhere | ||
} | ||
|
||
@Test | ||
public void testConversionError() { | ||
AsPathGroup asPathGroup = new AsPathGroup("Group"); | ||
asPathGroup | ||
.getAsPaths() | ||
.put("not-aspath", new NamedAsPath("not-aspath", "this is not a valid as-path regex")); | ||
|
||
Warnings w = new Warnings(true, true, true); | ||
BooleanExpr ret = PsFromAsPathGroup.toBooleanExpr(asPathGroup, w); | ||
|
||
assertThat(ret, equalTo(new Disjunction(BooleanExprs.FALSE))); | ||
assertThat(w.getRedFlagWarnings(), hasSize(1)); | ||
assertThat( | ||
Iterables.getOnlyElement(w.getRedFlagWarnings()).getText(), | ||
containsString("Error converting Juniper as-path-group regex Group")); | ||
} | ||
|
||
@Test | ||
public void testConversionErrorWithValidRegex() { | ||
AsPathGroup asPathGroup = new AsPathGroup("Group"); | ||
asPathGroup.getAsPaths().put("aspath1", new NamedAsPath("aspath1", "1 2")); | ||
asPathGroup | ||
.getAsPaths() | ||
.put("not-aspath", new NamedAsPath("not-aspath", "this is not a valid as-path regex")); | ||
|
||
Warnings w = new Warnings(true, true, true); | ||
BooleanExpr ret = PsFromAsPathGroup.toBooleanExpr(asPathGroup, w); | ||
|
||
assertThat(ret, instanceOf(Disjunction.class)); | ||
assertThat(w.getRedFlagWarnings(), hasSize(1)); | ||
} | ||
|
||
@Test | ||
public void testConversion() { | ||
AsPathGroup asPathGroup = new AsPathGroup("Group"); | ||
asPathGroup.getAsPaths().put("aspath1", new NamedAsPath("aspath1", "1 2")); | ||
asPathGroup.getAsPaths().put("aspath2", new NamedAsPath("aspath2", "3 4")); | ||
|
||
Warnings w = new Warnings(true, true, true); | ||
BooleanExpr ret = PsFromAsPathGroup.toBooleanExpr(asPathGroup, w); | ||
|
||
assertThat(ret, instanceOf(Disjunction.class)); | ||
assertThat(w.getRedFlagWarnings(), empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.