Skip to content

Commit

Permalink
general: cleanup asn grammar (#8011)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalperi committed Feb 7, 2022
1 parent 7d65ea6 commit ffd8079
Show file tree
Hide file tree
Showing 23 changed files with 256 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public ExplicitAs(long as) {
_as = as;
}

/** Use {@link #ExplicitAs(long)}. */
@Deprecated
public ExplicitAs(int as) {
this((long) as);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testExcludeSingletonAsSet() {

@Test
public void testExcludeWithIntermediateAttributes() {
List<AsExpr> exclude = Lists.newArrayList(new ExplicitAs(3));
List<AsExpr> exclude = Lists.newArrayList(new ExplicitAs(3L));
ExcludeAsPath operation = new ExcludeAsPath(new LiteralAsList(exclude));
Bgpv4Route.Builder outputRoute = Bgpv4Route.testBuilder();
outputRoute.setAsPath(ofSingletonAsSets(3L, 4L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static Environment newTestEnvironment(Bgpv4Route.Builder outputRoute) {

@Test
public void testPrepend() {
List<AsExpr> prepend = Lists.newArrayList(new ExplicitAs(1), new ExplicitAs(2));
List<AsExpr> prepend = Lists.newArrayList(new ExplicitAs(1L), new ExplicitAs(2L));
PrependAsPath operation = new PrependAsPath(new LiteralAsList(prepend));
Bgpv4Route.Builder builder = Bgpv4Route.testBuilder();
builder.setAsPath(ofSingletonAsSets(3L, 4L));
Expand All @@ -39,7 +39,7 @@ public void testPrepend() {

@Test
public void testPrependWithIntermediateAttributes() {
List<AsExpr> prepend = Lists.newArrayList(new ExplicitAs(1), new ExplicitAs(2));
List<AsExpr> prepend = Lists.newArrayList(new ExplicitAs(1L), new ExplicitAs(2L));
PrependAsPath operation = new PrependAsPath(new LiteralAsList(prepend));
Bgpv4Route.Builder outputRoute = Bgpv4Route.testBuilder();
outputRoute.setAsPath(ofSingletonAsSets(3L, 4L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ options {

as_expr
:
dec
asn = bgp_asn
| AUTO
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ address_family_footer

bgp_asn
:
asn = dec
asn = uint32
| asn4b = FLOAT // dec.dec , but this lexes as FLOAT
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ options {

as_expr
:
dec
asn = bgp_asn
| AUTO
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ options {

as_expr
:
dec
asn = bgp_asn
| AUTO
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ address_family_footer

bgp_asn
:
asn = dec
asn = uint32
| asn4b = FLOAT // dec.dec , but this lexes as FLOAT
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ address_family_footer

asn_dotted
:
uint_legacy PERIOD uint_legacy
uint16 PERIOD uint16
;

bgp_asn
:
asn = uint_legacy
asn = uint32
| asn4b = asn_dotted
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply_rp_stanza

as_expr
:
uint_legacy
asn = bgp_asn
| AUTO
| RP_VARIABLE
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3425,9 +3425,19 @@ M_AsPath_WS
mode M_AsPathPath;
M_AsPathPath_DEC
M_AsPathPath_UINT8
:
F_Digit+ -> type ( DEC ) , popMode
F_Uint8 -> type ( UINT8 ) , popMode
;
M_AsPathPath_UINT16
:
F_Uint16 -> type ( UINT16 ) , popMode
;
M_AsPathPath_UINT32
:
F_Uint32 -> type ( UINT32 ) , popMode
;
M_AsPathPath_DOUBLE_QUOTE
Expand All @@ -3442,9 +3452,19 @@ M_AsPathPath_WS
mode M_AsPathExpr;
M_AsPathExpr_DEC
M_AsPathExpr_UINT8
:
F_Digit+ -> type ( DEC )
F_Uint8 -> type ( UINT8 )
;
M_AsPathExpr_UINT16
:
F_Uint16 -> type ( UINT16 )
;
M_AsPathExpr_UINT32
:
F_Uint32 -> type ( UINT32 )
;
M_AsPathExpr_PERIOD
Expand Down Expand Up @@ -3530,9 +3550,19 @@ M_AsPathGroup3_NAME
mode M_AsPathPrepend;
M_AsPathPrepend_DEC
M_AsPathPrepend_UINT8
:
F_Digit+ -> type ( DEC ) , popMode
F_Uint8 -> type ( UINT8 ) , popMode
;
M_AsPathPrepend_UINT16
:
F_Uint16 -> type ( UINT16 ) , popMode
;
M_AsPathPrepend_UINT32
:
F_Uint32 -> type ( UINT32 ) , popMode
;
M_AsPathPrepend_DOUBLE_QUOTE
Expand All @@ -3547,9 +3577,19 @@ M_AsPathPrepend_WS
mode M_AsPathPrepend_Inner;
M_AsPathPrepend_Inner_DEC
M_AsPathPrepend_Inner_UINT8
:
F_Digit+ -> type ( DEC )
F_Uint8 -> type ( UINT8 )
;
M_AsPathPrepend_Inner_UINT16
:
F_Uint16 -> type ( UINT16 )
;
M_AsPathPrepend_Inner_UINT32
:
F_Uint32 -> type ( UINT32 )
;
M_AsPathPrepend_Inner_DOUBLE_QUOTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ as_unit

bgp_asn
:
asn = dec
| asn4hi = dec PERIOD asn4lo = dec
asn = uint32
| asn4hi = uint16 PERIOD asn4lo = uint16
;

dec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7301,8 +7301,8 @@ public void processParseTree(NetworkSnapshot snapshot, ParserRuleContext tree) {
}

private AsExpr toAsExpr(As_exprContext ctx) {
if (ctx.dec() != null) {
int as = toInteger(ctx.dec());
if (ctx.asn != null) {
long as = toAsNum(ctx.asn);
return new ExplicitAs(as);
} else if (ctx.AUTO() != null) {
return AutoAs.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9314,8 +9314,8 @@ private void resetPeerGroups() {
}

private AsExpr toAsExpr(As_exprContext ctx) {
if (ctx.dec() != null) {
int as = toInteger(ctx.dec());
if (ctx.asn != null) {
long as = toAsNum(ctx.asn);
return new ExplicitAs(as);
} else if (ctx.AUTO() != null) {
return AutoAs.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9188,8 +9188,8 @@ private void resetPeerGroups() {
}

private AsExpr toAsExpr(As_exprContext ctx) {
if (ctx.dec() != null) {
int as = toInteger(ctx.dec());
if (ctx.asn != null) {
long as = toAsNum(ctx.asn);
return new ExplicitAs(as);
} else if (ctx.AUTO() != null) {
return AutoAs.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6674,8 +6674,8 @@ private void resetPeerGroups() {
}

private AsExpr toAsExpr(As_exprContext ctx) {
if (ctx.uint_legacy() != null) {
int as = toInteger(ctx.uint_legacy());
if (ctx.asn != null) {
long as = toAsNum(ctx.asn);
return new ExplicitAs(as);
} else if (ctx.AUTO() != null) {
return AutoAs.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
import static org.batfish.representation.juniper.Nat.Type.SOURCE;
import static org.batfish.representation.juniper.RoutingInformationBase.RIB_IPV4_UNICAST;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
Expand Down Expand Up @@ -650,6 +649,7 @@
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Tcp_flags_alternativeContext;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Tcp_flags_atomContext;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Tcp_flags_literalContext;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Uint16Context;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Uint32Context;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.VariableContext;
import org.batfish.grammar.flatjuniper.FlatJuniperParser.Vlt_interfaceContext;
Expand Down Expand Up @@ -1783,32 +1783,13 @@ private OspfInterfaceType toOspfInterfaceType(Ospf_interface_typeContext ctx) {
return null;
}

@VisibleForTesting
long sanitizeAsn(long asn, int bytes, ParserRuleContext ctx) {
long mask;
if (bytes == 2) {
mask = 0xFFFF;
} else {
assert bytes == 4;
mask = 0xFFFFFFFF;
}
if ((asn & mask) != asn) {
_w.addWarning(
ctx,
ctx.getText(),
_parser,
String.format("AS number %s is out of the legal %s-byte AS range", asn, bytes));
return 0;
}
return asn;
}

private long toAsNum(Bgp_asnContext ctx) {
if (ctx.asn != null) {
return sanitizeAsn(toLong(ctx.asn), 4, ctx);
return toLong(ctx.asn);
} else {
long hi = sanitizeAsn(toLong(ctx.asn4hi), 2, ctx);
long lo = sanitizeAsn(toLong(ctx.asn4lo), 2, ctx);
assert ctx.asn4hi != null;
long hi = toLong(ctx.asn4hi);
long lo = toLong(ctx.asn4lo);
return (hi << 16) + lo;
}
}
Expand Down Expand Up @@ -1844,6 +1825,10 @@ private static long toLong(Uint32Context ctx) {
return Long.parseLong(ctx.getText());
}

private static long toLong(Uint16Context ctx) {
return Long.parseLong(ctx.getText());
}

private static IpProtocol toIpProtocol(Ip_protocolContext ctx) {
if (ctx.dec() != null) {
int protocolNum = toInt(ctx.dec());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void testToStatements() {
contains(
new PrependAsPath(
new LiteralAsList(
ImmutableList.of(new ExplicitAs(1), new ExplicitAs(2), new ExplicitAs(3))))));
ImmutableList.of(
new ExplicitAs(1L), new ExplicitAs(2L), new ExplicitAs(3L))))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void testToStatements() {
contains(
new ExcludeAsPath(
new LiteralAsList(
ImmutableList.of(new ExplicitAs(1), new ExplicitAs(2), new ExplicitAs(3))))));
ImmutableList.of(
new ExplicitAs(1L), new ExplicitAs(2L), new ExplicitAs(3L))))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void testToStatements() {
contains(
new PrependAsPath(
new LiteralAsList(
ImmutableList.of(new ExplicitAs(1), new ExplicitAs(2), new ExplicitAs(3))))));
ImmutableList.of(
new ExplicitAs(1L), new ExplicitAs(2L), new ExplicitAs(3L))))));
}
}

0 comments on commit ffd8079

Please sign in to comment.