Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FRR Set Weight Support #6033

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ VTY
'vty'
;

WEIGHT
:
'weight'
;

WS
:
F_Whitespace+ -> channel ( HIDDEN ) // parser never sees tokens on hidden channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ rm_set
| rms_local_preference
| rms_metric
| rms_tag
| rms_weight
)
;

rms_weight
:
WEIGHT weight = uint32 NEWLINE
;

rms_metric
:
METRIC metric = int_expr NEWLINE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
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_tagContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rms_weightContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Rmsipnh_literalContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Ro_router_idContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Ronopi_defaultContext;
Expand Down Expand Up @@ -184,6 +185,7 @@
import org.batfish.representation.cumulus.RouteMapSetLocalPreference;
import org.batfish.representation.cumulus.RouteMapSetMetric;
import org.batfish.representation.cumulus.RouteMapSetTag;
import org.batfish.representation.cumulus.RouteMapSetWeight;
import org.batfish.representation.cumulus.StaticRoute;
import org.batfish.representation.cumulus.Vrf;

Expand Down Expand Up @@ -1067,6 +1069,12 @@ public void exitRms_metric(Rms_metricContext ctx) {
_currentRouteMapEntry.setSetMetric(new RouteMapSetMetric(val));
}

@Override
public void exitRms_weight(Rms_weightContext ctx) {
Integer val = Integer.parseInt(ctx.weight.getText());
_currentRouteMapEntry.setSetWeight(new RouteMapSetWeight(val));
}

@Override
public void exitRmsipnh_literal(Rmsipnh_literalContext ctx) {
Ip ip = Ip.parse(ctx.next_hop.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class RouteMapEntry implements Serializable {
private @Nullable RouteMapSetCommunity _setCommunity;
private @Nullable RouteMapSetLocalPreference _setLocalPreference;
private @Nullable RouteMapSetTag _setTag;
private @Nullable RouteMapSetWeight _setWeight;

public RouteMapEntry(int number, LineAction action) {
_number = number;
Expand Down Expand Up @@ -98,7 +99,8 @@ public int getNumber() {
_setIpNextHop,
_setCommunity,
_setLocalPreference,
_setTag)
_setTag,
_setWeight)
.filter(Objects::nonNull);
}

Expand All @@ -122,6 +124,10 @@ public int getNumber() {
return _setCommunity;
}

public @Nullable RouteMapSetWeight getSetWeight() {
return _setWeight;
}

public void setMatchAsPath(@Nullable RouteMapMatchAsPath matchAsPath) {
_matchAsPath = matchAsPath;
}
Expand Down Expand Up @@ -155,6 +161,10 @@ public void setSetMetric(@Nullable RouteMapSetMetric setMetric) {
_setMetric = setMetric;
}

public void setSetWeight(@Nullable RouteMapSetWeight setWeight) {
_setWeight = setWeight;
}

public void setSetCommListDelete(@Nullable RouteMapSetCommListDelete setCommListDelete) {
_setCommListDelete = setCommListDelete;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.routing_policy.expr.LiteralInt;
import org.batfish.datamodel.routing_policy.statement.SetWeight;
import org.batfish.datamodel.routing_policy.statement.Statement;

/** Clause of set weight in route map. */
public class RouteMapSetWeight implements RouteMapSet {

private int _weight;

public RouteMapSetWeight(int weight) {
_weight = weight;
}

public int getWeight() {
return _weight;
}

@Nonnull
@Override
public Stream<Statement> toStatements(Configuration c, CumulusNodeConfiguration vc, Warnings w) {
return Stream.of(new SetWeight(new LiteralInt(_weight)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,16 @@ public void testCumulusFrrVrfRouteMapSetMetric() {
assertThat(entry.getSetMetric().getMetric(), equalTo(new LiteralLong(30)));
}

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

parse(String.format("route-map %s permit 10\nset weight 30\n", name));

RouteMapEntry entry = _frr.getRouteMaps().get(name).getEntries().get(10);
assertThat(entry.getSetWeight().getWeight(), equalTo(30));
}

@Test
public void testCumulusFrrIpCommunityListExpanded() {
String name = "NAME";
Expand Down Expand Up @@ -1707,4 +1717,55 @@ public void testStaticRouteInterface_vrf_withDefinition() {
_frr.getVrfs().get("VRF").getStaticRoutes(),
contains(new StaticRoute(Prefix.parse("1.2.3.0/24"), null, "eth0", null)));
}

@Test
public void testBgpcommunityMatchSetTagAndWeight_behavior() throws IOException {
/*
There are two nodes in the topology arranged in a line.
- frr-originator -- frr-listener

Features tested:
1) Set comm
2) Match comm + set tag + set weight

frr-originator originates routes via network statements and applies a community outbound.

frr-listener matches on that community and sets a weight and tag.

*/

String snapshotName = "bgp-comm-match-set-tag-weight";
List<String> configurationNames = ImmutableList.of("frr-originator", "frr-listener");
Batfish batfish =
BatfishTestUtils.getBatfishFromTestrigText(
TestrigText.builder()
.setConfigurationFiles(SNAPSHOTS_PREFIX + snapshotName, configurationNames)
.build(),
_folder);

NetworkSnapshot snapshot = batfish.getSnapshot();
batfish.computeDataPlane(snapshot);
DataPlane dp = batfish.loadDataPlane(snapshot);

assertThat(
dp.getRibs().get("frr-listener").get(DEFAULT_VRF_NAME).getRoutes(),
hasItem(
equalTo(
Bgpv4Route.builder()
.setNetwork(Prefix.parse("10.2.2.2/32"))
.setNextHopIp(Ip.parse("10.1.1.1"))
.setReceivedFromIp(Ip.parse("10.1.1.1"))
.setOriginatorIp(Ip.parse("1.1.1.1"))
.setNextHopInterface("dynamic")
.setOriginType(OriginType.IGP)
.setProtocol(RoutingProtocol.BGP)
.setSrcProtocol(RoutingProtocol.BGP)
.setAsPath(AsPath.ofSingletonAsSets(1L))
.setCommunities(ImmutableSet.of(StandardCommunity.of(12345, 123)))
.setAdmin(20)
.setLocalPreference(100)
.setWeight(500)
.setTag(10000L)
.build())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.batfish.representation.cumulus;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import com.google.common.collect.ImmutableList;
import java.util.List;
import org.batfish.datamodel.routing_policy.expr.LiteralInt;
import org.batfish.datamodel.routing_policy.statement.SetWeight;
import org.batfish.datamodel.routing_policy.statement.Statement;
import org.junit.Test;

public class RouteMapSetWeightTest {

@Test
public void testGetWeight() {
RouteMapSetWeight set = new RouteMapSetWeight(10);
assertThat(set.getWeight(), equalTo(10));
}

@Test
public void testToStatements() {
RouteMapSetWeight set = new RouteMapSetWeight(10);
List<Statement> result =
set.toStatements(null, null, null).collect(ImmutableList.toImmutableList());
assertThat(result, equalTo(ImmutableList.of(new SetWeight(new LiteralInt(10)))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
frr-listener
# This file describes the network interfaces
auto lo
iface lo inet loopback

auto swp1
iface swp1

# ports.conf --
frr version
frr defaults datacenter
!
interface lo
ip address 5.5.5.5/32
!
interface swp1
ip address 10.1.1.5/24
!
router bgp 5
bgp router-id 5.5.5.5
neighbor 10.1.1.1 remote-as 1
neighbor 10.1.1.1 route-map MATCH_COMM_TAG_IN in
!
ip community-list standard TEST_COMM permit 12345:123
!
route-map MATCH_COMM_TAG_IN permit 10
match community TEST_COMM
set weight 500
set tag 10000
!
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
frr-originator
# This file describes the network interfaces
auto lo
iface lo inet loopback

auto swp1
iface swp1

# ports.conf --
frr version
frr defaults datacenter
!
interface lo
ip address 1.1.1.1/32
!
interface swp1
ip address 10.1.1.1/24
!
router bgp 1
bgp router-id 1.1.1.1
neighbor 10.1.1.5 remote-as 5
neighbor 10.1.1.5 route-map SET_COMM out
network 10.2.2.2/32
!
ip route 10.2.2.2/32 Null0
!
ip community-list standard TEST_COMM permit 12345:123
!
route-map SET_COMM permit 20
set community 12345:123
!
end