Skip to content

Commit

Permalink
FRR: introduce ospf area intermediate representation (#6655)
Browse files Browse the repository at this point in the history
Better than sticking per-area configs in the Vrf itself.
  • Loading branch information
dhalperi committed Feb 24, 2021
1 parent 5121889 commit c7d00a4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
import org.batfish.representation.cumulus.IpCommunityListStandardLine;
import org.batfish.representation.cumulus.IpPrefixList;
import org.batfish.representation.cumulus.IpPrefixListLine;
import org.batfish.representation.cumulus.OspfArea;
import org.batfish.representation.cumulus.OspfAreaRange;
import org.batfish.representation.cumulus.OspfNetworkArea;
import org.batfish.representation.cumulus.OspfNetworkType;
Expand Down Expand Up @@ -251,7 +252,7 @@ public class CumulusFrrConfigurationBuilder extends CumulusFrrParserBaseListener
private @Nullable BgpNeighborIpv4UnicastAddressFamily _currentBgpNeighborIpv4UnicastAddressFamily;
private @Nullable BgpNeighborL2vpnEvpnAddressFamily _currentBgpNeighborL2vpnEvpnAddressFamily;
private @Nullable FrrInterface _currentInterface;
private Long _currentOspfArea;
private OspfArea _currentOspfArea;
private OspfVrf _currentOspfVrf;

public CumulusFrrConfigurationBuilder(
Expand Down Expand Up @@ -854,7 +855,8 @@ public void exitS_router_ospf(S_router_ospfContext ctx) {

@Override
public void enterRo_area(Ro_areaContext ctx) {
_currentOspfArea = toLong(ctx.area);
long area = toLong(ctx.area);
_currentOspfArea = _currentOspfVrf.getOrCreateArea(area);
}

@Override
Expand Down Expand Up @@ -935,9 +937,8 @@ public void handleToOspfRedistribution(

@Override
public void exitRoa_range(Roa_rangeContext ctx) {
assert _currentOspfArea != null;
Prefix pfx = toPrefix(ctx.pfx);
OspfAreaRange range = _currentOspfVrf.getOrCreateAreaRange(_currentOspfArea, pfx);
OspfAreaRange range = _currentOspfArea.getOrCreateRange(pfx);
if (ctx.cost != null) {
toInteger(ctx.getParent(), ctx.cost).ifPresent(range::setCost);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.batfish.representation.cumulus;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.batfish.datamodel.Prefix;

/** OSPF configuration for an area in a vrf. */
public class OspfArea implements Serializable {

private final long _area;
private final @Nonnull Map<Prefix, OspfAreaRange> _ranges;

public OspfArea(long area) {
_area = area;
_ranges = new HashMap<>();
}

public long getArea() {
return _area;
}

public @Nonnull OspfAreaRange getOrCreateRange(@Nonnull Prefix range) {
return _ranges.computeIfAbsent(range, OspfAreaRange::new);
}

public @Nullable OspfAreaRange getRange(@Nonnull Prefix range) {
return _ranges.get(range);
}

public @Nonnull Map<Prefix, OspfAreaRange> getRanges() {
return Collections.unmodifiableMap(_ranges);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
/** OSPF configuration for {@code area A range A.B.C.D/M [cost C]}. */
public class OspfAreaRange implements Serializable {

private final long _area;
private final @Nonnull Prefix _range;
private @Nullable Integer _cost;

public OspfAreaRange(long area, @Nonnull Prefix range) {
_area = area;
public OspfAreaRange(@Nonnull Prefix range) {
_range = range;
}

public long getArea() {
return _area;
}

public @Nonnull Prefix getRange() {
return _range;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
package org.batfish.representation.cumulus;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.batfish.datamodel.Ip;
import org.batfish.datamodel.Prefix;

/** OSPF configuration for a particular VRF. */
public class OspfVrf implements Serializable {

private final @Nonnull String _vrfName;
private @Nullable Ip _routerId;
private final @Nonnull Table<Long, Prefix, OspfAreaRange> _ospfAreaRange;
private final @Nonnull Map<Long, OspfArea> _areas;

public OspfVrf(String name) {
_vrfName = name;
_ospfAreaRange = HashBasedTable.create();
_areas = new HashMap<>();
}

public @Nonnull OspfAreaRange getOrCreateAreaRange(long area, @Nonnull Prefix prefix) {
return _ospfAreaRange.row(area).computeIfAbsent(prefix, p -> new OspfAreaRange(area, p));
public @Nonnull OspfArea getOrCreateArea(long area) {
return _areas.computeIfAbsent(area, OspfArea::new);
}

public @Nullable OspfAreaRange getAreaRange(long area, @Nonnull Prefix prefix) {
return _ospfAreaRange.get(area, prefix);
}

public @Nonnull Collection<OspfAreaRange> getAreaRanges() {
return _ospfAreaRange.values();
public @Nullable OspfArea getArea(long area) {
return _areas.get(area);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.batfish.representation.cumulus.IpCommunityListExpandedLine;
import org.batfish.representation.cumulus.IpPrefixList;
import org.batfish.representation.cumulus.IpPrefixListLine;
import org.batfish.representation.cumulus.OspfArea;
import org.batfish.representation.cumulus.OspfNetworkArea;
import org.batfish.representation.cumulus.OspfNetworkType;
import org.batfish.representation.cumulus.OspfVrf;
Expand Down Expand Up @@ -1841,11 +1842,14 @@ public void testRouterOspfAreaRange() {
+ " area 5 range 1.255.0.0/17\n");
Prefix prefix = Prefix.parse("1.255.0.0/17");
OspfVrf vrf = _frr.getOspfProcess().getDefaultVrf();
assertThat(vrf.getAreaRanges(), hasSize(2));
assertThat(vrf.getAreaRange(Ip.parse("1.1.1.0").asLong(), prefix), notNullValue());
assertThat(vrf.getAreaRange(Ip.parse("1.1.1.0").asLong(), prefix).getCost(), equalTo(10));
assertThat(vrf.getAreaRange(5L, prefix), notNullValue());
assertThat(vrf.getAreaRange(5L, prefix).getCost(), nullValue());
OspfArea area1110 = vrf.getArea(Ip.parse("1.1.1.0").asLong());
assertThat(area1110, notNullValue());
assertThat(area1110.getRanges(), hasKeys(prefix));
assertThat(area1110.getRange(prefix).getCost(), equalTo(10));
OspfArea area5 = vrf.getArea(5L);
assertThat(area5, notNullValue());
assertThat(area5.getRanges(), hasKeys(prefix));
assertThat(area5.getRange(prefix).getCost(), nullValue());
}

@Test
Expand Down

0 comments on commit c7d00a4

Please sign in to comment.