Skip to content

Commit

Permalink
Fix checkstyle and format errors
Browse files Browse the repository at this point in the history
  • Loading branch information
samarthdhingra committed Aug 11, 2023
1 parent ca9cf8e commit 6b49010
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 192 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package org.batfish.representation.juniper;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.batfish.datamodel.routing_policy.as_path.AsPathMatchExpr;
import org.batfish.datamodel.routing_policy.as_path.AsPathMatchRegex;
import org.batfish.datamodel.routing_policy.as_path.AsSetsMatchingRanges;
import org.batfish.representation.juniper.parboiled.AsPathRegex;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkArgument;

/**
* A class that converts a Juniper AS Path regex to an instance of {@link AsPathMatchExpr}.
*
Expand All @@ -22,93 +20,121 @@
*/
public final class AsPathMatchExprParser {

// "N" : "AS Path matches single ASN number"
private static final Pattern AS_PATH_EXACT_MATCH_ASN = Pattern.compile("(\\d+)");
// "N" : "AS Path matches single ASN number"
private static final Pattern AS_PATH_EXACT_MATCH_ASN = Pattern.compile("(\\d+)");

// ".* N .*" : "AS Path contains N"
private static final Pattern AS_PATH_CONTAINS_ASN = Pattern.compile("\\.\\*\\s?(\\d+)\\s?\\.\\*");
// ".* N .*" : "AS Path contains N"
private static final Pattern AS_PATH_CONTAINS_ASN = Pattern.compile("\\.\\*\\s?(\\d+)\\s?\\.\\*");

// ".* N" or ".* N$" : "AS Path ends with N"
private static final Pattern AS_PATH_ENDS_WITH_ASN = Pattern.compile("\\.\\* (\\d+)\\$?");
// ".* N" or ".* N$" : "AS Path ends with N"
private static final Pattern AS_PATH_ENDS_WITH_ASN = Pattern.compile("\\.\\* (\\d+)\\$?");

// "N .*" or "^N .*" : "AS Path starts with N".
private static final Pattern AS_PATH_STARTS_WITH_ASN = Pattern.compile("\\^?(\\d+) \\.\\*");
// "N .*" or "^N .*" : "AS Path starts with N".
private static final Pattern AS_PATH_STARTS_WITH_ASN = Pattern.compile("\\^?(\\d+) \\.\\*");

// ".* [start-end] .*" : "AS Path contains an ASN in range between start and end included
private static final Pattern AS_PATH_CONTAINS_ASN_RANGE_PATTERN_1 = Pattern.compile("\\.\\* \\[(\\d+)-(\\d+)\\] \\.\\*");
// ".* [start-end] .*" : "AS Path contains an ASN in range between start and end included
private static final Pattern AS_PATH_CONTAINS_ASN_RANGE_PATTERN_1 =
Pattern.compile("\\.\\* \\[(\\d+)-(\\d+)\\] \\.\\*");

// ".* start-end .*" : "AS Path contains an ASN in range between start and end included
private static final Pattern AS_PATH_CONTAINS_ASN_RANGE_PATTERN_2 = Pattern.compile("\\.\\* (\\d+)-(\\d+) \\.\\*");
// ".* start-end .*" : "AS Path contains an ASN in range between start and end included
private static final Pattern AS_PATH_CONTAINS_ASN_RANGE_PATTERN_2 =
Pattern.compile("\\.\\* (\\d+)-(\\d+) \\.\\*");

// "[start-end]" : "AS Path matches single ASN number in range between start and end included
private static final Pattern AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_1 = Pattern.compile("\\[(\\d+)-(\\d+)\\]");
// "[start-end]" : "AS Path matches single ASN number in range between start and end included
private static final Pattern AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_1 =
Pattern.compile("\\[(\\d+)-(\\d+)\\]");

// "start-end" : "AS Path matches single ASN number in range between start and end included
private static final Pattern AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_2 = Pattern.compile("(\\d+)-(\\d+)");
// "start-end" : "AS Path matches single ASN number in range between start and end included
private static final Pattern AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_2 =
Pattern.compile("(\\d+)-(\\d+)");

/**
* Converts the given Juniper AS Path regular expression to an instance of {@link AsPathMatchExpr}.
* First match the AS path input regex against predefined AS path regexes and return VI construct {@link AsSetsMatchingRanges}
* Else convert to java regex and return {@link AsPathMatchRegex}
*/
public static AsPathMatchExpr convertToAsPathMatchExpr(String asPathRegex) {
/**
* Converts the given Juniper AS Path regular expression to an instance of {@link
* AsPathMatchExpr}. First match the AS path input regex against predefined AS path regexes and
* return VI construct {@link AsSetsMatchingRanges} Else convert to java regex and return {@link
* AsPathMatchRegex}
*/
public static AsPathMatchExpr convertToAsPathMatchExpr(String asPathRegex) {

Matcher asPathExactMatchAsn = AS_PATH_EXACT_MATCH_ASN.matcher(asPathRegex);
if (asPathExactMatchAsn.matches()) {
return getAsSetsMatchingRanges(asPathExactMatchAsn.group(1), true, true);
}

Matcher asPathContainsAsn = AS_PATH_CONTAINS_ASN.matcher(asPathRegex);
if (asPathContainsAsn.matches()) {
return getAsSetsMatchingRanges(asPathContainsAsn.group(1), false, false);
}

Matcher asPathStartsWithAsn = AS_PATH_STARTS_WITH_ASN.matcher(asPathRegex);
if (asPathStartsWithAsn.matches()) {
return getAsSetsMatchingRanges(asPathStartsWithAsn.group(1), false, true);
}

Matcher asPathEndsWithAsn = AS_PATH_ENDS_WITH_ASN.matcher(asPathRegex);
if (asPathEndsWithAsn.matches()) {
return getAsSetsMatchingRanges(asPathEndsWithAsn.group(1), true, false);
}

Matcher asPathContainsAsnRangeBrackets = AS_PATH_CONTAINS_ASN_RANGE_PATTERN_1.matcher(asPathRegex);
if (asPathContainsAsnRangeBrackets.matches()) {
return getAsSetsMatchingRanges(asPathContainsAsnRangeBrackets.group(1), asPathContainsAsnRangeBrackets.group(2), false, false);
}
Matcher asPathExactMatchAsn = AS_PATH_EXACT_MATCH_ASN.matcher(asPathRegex);
if (asPathExactMatchAsn.matches()) {
return getAsSetsMatchingRanges(asPathExactMatchAsn.group(1), true, true);
}

Matcher asPathContainsAsnRangeNoBrackets = AS_PATH_CONTAINS_ASN_RANGE_PATTERN_2.matcher(asPathRegex);
if (asPathContainsAsnRangeNoBrackets.matches()) {
return getAsSetsMatchingRanges(asPathContainsAsnRangeNoBrackets.group(1), asPathContainsAsnRangeNoBrackets.group(2), false, false);
}
Matcher asPathContainsAsn = AS_PATH_CONTAINS_ASN.matcher(asPathRegex);
if (asPathContainsAsn.matches()) {
return getAsSetsMatchingRanges(asPathContainsAsn.group(1), false, false);
}

Matcher asPathExactMatchAsnRangeBrackets = AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_1.matcher(asPathRegex);
if (asPathExactMatchAsnRangeBrackets.matches()) {
return getAsSetsMatchingRanges(asPathExactMatchAsnRangeBrackets.group(1), asPathExactMatchAsnRangeBrackets.group(2), true, true);
}
Matcher asPathStartsWithAsn = AS_PATH_STARTS_WITH_ASN.matcher(asPathRegex);
if (asPathStartsWithAsn.matches()) {
return getAsSetsMatchingRanges(asPathStartsWithAsn.group(1), false, true);
}

Matcher asPathExactMatchAsnRangeNoBrackets = AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_2.matcher(asPathRegex);
if (asPathExactMatchAsnRangeNoBrackets.matches()) {
return getAsSetsMatchingRanges(asPathExactMatchAsnRangeNoBrackets.group(1), asPathExactMatchAsnRangeNoBrackets.group(2), true, true);
}
Matcher asPathEndsWithAsn = AS_PATH_ENDS_WITH_ASN.matcher(asPathRegex);
if (asPathEndsWithAsn.matches()) {
return getAsSetsMatchingRanges(asPathEndsWithAsn.group(1), true, false);
}

String javaRegex = AsPathRegex.convertToJavaRegex(asPathRegex);
return AsPathMatchRegex.of(javaRegex);
Matcher asPathContainsAsnRangeBrackets =
AS_PATH_CONTAINS_ASN_RANGE_PATTERN_1.matcher(asPathRegex);
if (asPathContainsAsnRangeBrackets.matches()) {
return getAsSetsMatchingRanges(
asPathContainsAsnRangeBrackets.group(1),
asPathContainsAsnRangeBrackets.group(2),
false,
false);
}

private static AsSetsMatchingRanges getAsSetsMatchingRanges(String asn, boolean anchorEnd, boolean anchorStart) {
long asnLong = Long.parseLong(asn);
return AsSetsMatchingRanges.of(anchorEnd, anchorStart, ImmutableList.of(Range.singleton(asnLong)));
Matcher asPathContainsAsnRangeNoBrackets =
AS_PATH_CONTAINS_ASN_RANGE_PATTERN_2.matcher(asPathRegex);
if (asPathContainsAsnRangeNoBrackets.matches()) {
return getAsSetsMatchingRanges(
asPathContainsAsnRangeNoBrackets.group(1),
asPathContainsAsnRangeNoBrackets.group(2),
false,
false);
}

private static AsSetsMatchingRanges getAsSetsMatchingRanges(String asnLowerRange, String asnUpperRange, boolean anchorEnd, boolean anchorStart) {
long start = Long.parseLong(asnLowerRange);
long end = Long.parseLong(asnUpperRange);
checkArgument(start <= end, "Invalid range %s-%s", start, end);
return AsSetsMatchingRanges.of(anchorEnd, anchorStart, ImmutableList.of(Range.closed(start, end)));
Matcher asPathExactMatchAsnRangeBrackets =
AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_1.matcher(asPathRegex);
if (asPathExactMatchAsnRangeBrackets.matches()) {
return getAsSetsMatchingRanges(
asPathExactMatchAsnRangeBrackets.group(1),
asPathExactMatchAsnRangeBrackets.group(2),
true,
true);
}

Matcher asPathExactMatchAsnRangeNoBrackets =
AS_PATH_EXACT_MATCH_ASN_RANGE_PATTERN_2.matcher(asPathRegex);
if (asPathExactMatchAsnRangeNoBrackets.matches()) {
return getAsSetsMatchingRanges(
asPathExactMatchAsnRangeNoBrackets.group(1),
asPathExactMatchAsnRangeNoBrackets.group(2),
true,
true);
}

private AsPathMatchExprParser() {} // prevent instantiation of utility class.
}
String javaRegex = AsPathRegex.convertToJavaRegex(asPathRegex);
return AsPathMatchRegex.of(javaRegex);
}

private static AsSetsMatchingRanges getAsSetsMatchingRanges(
String asn, boolean anchorEnd, boolean anchorStart) {
long asnLong = Long.parseLong(asn);
return AsSetsMatchingRanges.of(
anchorEnd, anchorStart, ImmutableList.of(Range.singleton(asnLong)));
}

private static AsSetsMatchingRanges getAsSetsMatchingRanges(
String asnLowerRange, String asnUpperRange, boolean anchorEnd, boolean anchorStart) {
long start = Long.parseLong(asnLowerRange);
long end = Long.parseLong(asnUpperRange);
checkArgument(start <= end, "Invalid range %s-%s", start, end);
return AsSetsMatchingRanges.of(
anchorEnd, anchorStart, ImmutableList.of(Range.closed(start, end)));
}

private AsPathMatchExprParser() {} // prevent instantiation of utility class.
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static BooleanExpr toBooleanExpr(@Nullable AsPath asPath, Warnings w) {
return BooleanExprs.FALSE;
}
try {
AsPathMatchExpr asPathMatchExpr = AsPathMatchExprParser.convertToAsPathMatchExpr(asPath.getRegex());
AsPathMatchExpr asPathMatchExpr =
AsPathMatchExprParser.convertToAsPathMatchExpr(asPath.getRegex());
return MatchAsPath.of(InputAsPath.instance(), asPathMatchExpr);
} catch (Exception e) {
w.redFlag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ static BooleanExpr toBooleanExpr(@Nullable AsPathGroup asPathGroup, Warnings w)
List<BooleanExpr> asPaths = new ArrayList<>();
for (NamedAsPath namedAsPath : asPathGroup.getAsPaths().values()) {
try {
AsPathMatchExpr asPathMatchExpr = AsPathMatchExprParser.convertToAsPathMatchExpr(namedAsPath.getRegex());
AsPathMatchExpr asPathMatchExpr =
AsPathMatchExprParser.convertToAsPathMatchExpr(namedAsPath.getRegex());
asPaths.add(MatchAsPath.of(InputAsPath.instance(), asPathMatchExpr));
} catch (Exception e) {
w.redFlag(
Expand Down

0 comments on commit 6b49010

Please sign in to comment.