Skip to content

Commit

Permalink
cumulus frr parse and convert route-map set metric-type (#6656)
Browse files Browse the repository at this point in the history
  • Loading branch information
arifogel committed Feb 24, 2021
1 parent c7d00a4 commit 13d9e3f
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,9 @@ TRADITIONAL
'traditional'
;

TYPE_1: 'type-1';
TYPE_2: 'type-2';

WARNINGS
:
'warnings'
Expand All @@ -774,6 +777,8 @@ METRIC
'metric'
;

METRIC_TYPE: 'metric-type';

NEWLINE
:
F_Newline+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ rm_set
| rms_ip
| rms_local_preference
| rms_metric
| rms_metric_type
| rms_tag
| rms_weight
)
Expand All @@ -108,6 +109,15 @@ rms_metric
METRIC metric = int_expr NEWLINE
;

rms_metric_type
:
METRIC_TYPE
(
TYPE_1
| TYPE_2
) NEWLINE
;

rmm_ip
:
IP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_communityContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_local_preferenceContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_metricContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_metric_typeContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_tagContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_weightContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rmsipnh_literalContext;
Expand Down Expand Up @@ -221,12 +222,14 @@
import org.batfish.representation.cumulus.RouteMapMatchSourceProtocol;
import org.batfish.representation.cumulus.RouteMapMatchSourceProtocol.Protocol;
import org.batfish.representation.cumulus.RouteMapMatchTag;
import org.batfish.representation.cumulus.RouteMapMetricType;
import org.batfish.representation.cumulus.RouteMapSetAsPath;
import org.batfish.representation.cumulus.RouteMapSetCommListDelete;
import org.batfish.representation.cumulus.RouteMapSetCommunity;
import org.batfish.representation.cumulus.RouteMapSetIpNextHopLiteral;
import org.batfish.representation.cumulus.RouteMapSetLocalPreference;
import org.batfish.representation.cumulus.RouteMapSetMetric;
import org.batfish.representation.cumulus.RouteMapSetMetricType;
import org.batfish.representation.cumulus.RouteMapSetTag;
import org.batfish.representation.cumulus.RouteMapSetWeight;
import org.batfish.representation.cumulus.StaticRoute;
Expand Down Expand Up @@ -1470,6 +1473,21 @@ public void exitRms_metric(Rms_metricContext ctx) {
_currentRouteMapEntry.setSetMetric(new RouteMapSetMetric(val));
}

@Override
public void exitRms_metric_type(Rms_metric_typeContext ctx) {
RouteMapMetricType type;
if (ctx.TYPE_1() != null) {
type = RouteMapMetricType.TYPE_1;
} else if (ctx.TYPE_2() != null) {
type = RouteMapMetricType.TYPE_2;
} else {
// assume valid but unsupported
todo(ctx);
return;
}
_currentRouteMapEntry.setSetMetricType(new RouteMapSetMetricType(type));
}

@Override
public void exitRms_weight(Rms_weightContext ctx) {
Integer val = Integer.parseInt(ctx.weight.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class RouteMapEntry implements Serializable {

private @Nullable RouteMapSetAsPath _setAsPath;
private @Nullable RouteMapSetMetric _setMetric;
private @Nullable RouteMapSetMetricType _setMetricType;
private @Nullable RouteMapSetIpNextHopLiteral _setIpNextHop;
private @Nullable RouteMapSetCommListDelete _setCommListDelete;
private @Nullable RouteMapSetCommunity _setCommunity;
Expand Down Expand Up @@ -121,6 +122,7 @@ public int getNumber() {
_setAsPath,
_setCommListDelete,
_setMetric,
_setMetricType,
_setIpNextHop,
_setCommunity,
_setLocalPreference,
Expand All @@ -137,6 +139,10 @@ public int getNumber() {
return _setMetric;
}

public @Nullable RouteMapSetMetricType getSetMetricType() {
return _setMetricType;
}

public @Nullable RouteMapSetIpNextHopLiteral getSetIpNextHop() {
return _setIpNextHop;
}
Expand Down Expand Up @@ -186,6 +192,10 @@ public void setSetMetric(@Nullable RouteMapSetMetric setMetric) {
_setMetric = setMetric;
}

public void setSetMetricType(RouteMapSetMetricType setMetricType) {
_setMetricType = setMetricType;
}

public void setSetWeight(@Nullable RouteMapSetWeight setWeight) {
_setWeight = setWeight;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.batfish.representation.cumulus;

public enum RouteMapMetricType {
/** OSPF type-1 */
TYPE_1,
/** OSPF type-2 */
TYPE_2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.batfish.representation.cumulus;

import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.batfish.common.Warnings;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.ospf.OspfMetricType;
import org.batfish.datamodel.routing_policy.statement.SetOspfMetricType;
import org.batfish.datamodel.routing_policy.statement.Statement;

/** A {@link RouteMapSet} that sets the metric-type for an OSPF route. */
public final class RouteMapSetMetricType implements RouteMapSet {

public RouteMapSetMetricType(RouteMapMetricType metricType) {
_metricType = metricType;
}

public @Nonnull RouteMapMetricType getMetricType() {
return _metricType;
}

@Nonnull
@Override
public Stream<Statement> toStatements(
Configuration c, CumulusConcatenatedConfiguration vc, Warnings w) {
switch (_metricType) {
case TYPE_1:
return Stream.of(new SetOspfMetricType(OspfMetricType.E1));

case TYPE_2:
return Stream.of(new SetOspfMetricType(OspfMetricType.E2));

default:
// should not happen
return Stream.empty();
}
}

private final @Nonnull RouteMapMetricType _metricType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import org.batfish.representation.cumulus.RouteMap;
import org.batfish.representation.cumulus.RouteMapEntry;
import org.batfish.representation.cumulus.RouteMapMatchSourceProtocol.Protocol;
import org.batfish.representation.cumulus.RouteMapMetricType;
import org.batfish.representation.cumulus.StaticRoute;
import org.batfish.representation.cumulus.Vrf;
import org.junit.Before;
Expand Down Expand Up @@ -1259,6 +1260,25 @@ public void testCumulusFrrVrfRouteMapSetMetric() {
assertThat(entry.getSetMetric().getMetric(), equalTo(new LiteralLong(30)));
}

@Test
public void testCumulusFrrRouteMapSetMetricType() {
String name = "ROUTE-MAP-NAME";

parse(
String.format(
"route-map %s permit 10\nset metric-type type-1\n"
+ "route-map %s permit 20\nset metric-type type-2\n",
name, name));
{
RouteMapEntry entry = _frr.getRouteMaps().get(name).getEntries().get(10);
assertThat(entry.getSetMetricType().getMetricType(), equalTo(RouteMapMetricType.TYPE_1));
}
{
RouteMapEntry entry = _frr.getRouteMaps().get(name).getEntries().get(20);
assertThat(entry.getSetMetricType().getMetricType(), equalTo(RouteMapMetricType.TYPE_2));
}
}

@Test
public void testCumulusFrrVrfRouteMapSetWeight() {
String name = "ROUTE-MAP-NAME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public void testGetSets_Metric() {
assertThat(sets, contains(isA(RouteMapSetMetric.class)));
}

@Test
public void testGetSets_MetricType() {
RouteMapEntry entry = new RouteMapEntry(10, LineAction.DENY);
entry.setSetMetricType(new RouteMapSetMetricType(RouteMapMetricType.TYPE_1));

ImmutableList<RouteMapSet> sets = entry.getSets().collect(ImmutableList.toImmutableList());

assertThat(sets, contains(isA(RouteMapSetMetricType.class)));
}

@Test
public void testGetSets_NextHop() {
RouteMapEntry entry = new RouteMapEntry(10, LineAction.DENY);
Expand Down

0 comments on commit 13d9e3f

Please sign in to comment.