@Test public void testRemoveRouteGetDelta() { StaticRoute.Builder srb = StaticRoute.builder().setNetwork(Prefix.parse("1.0.0.0/8")); StaticRoute r1 = srb.setAdministrativeCost(2).setTag(1L).build(); StaticRoute r2 = srb.setAdministrativeCost(2).setTag(2L).build(); StaticRoute r3 = srb.setAdministrativeCost(1).setTag(3L).build(); AbstractRib<StaticRoute> owner = new AbstractRib<StaticRoute>() { @Override public int comparePreference(StaticRoute lhs, StaticRoute rhs) { return rhs.getAdministrativeCost() - lhs.getAdministrativeCost(); } }; RibTree<StaticRoute> ribTree = new RibTree<>(owner); RibDelta<StaticRoute> addR1 = ribTree.mergeRoute(r1); assertThat(addR1, equalTo(RibDelta.adding(r1))); RibDelta<StaticRoute> addR2 = ribTree.mergeRoute(r2); assertThat(addR2, equalTo(RibDelta.adding(r2))); RibDelta<StaticRoute> addR3 = ribTree.mergeRoute(r3); assertThat( addR3, equalTo( RibDelta.builder() .remove(r1, Reason.REPLACE) .remove(r2, Reason.REPLACE) .add(r3) .build())); }
@Nonnull RibDelta<R> removeRouteGetDelta(R route, Reason reason) { assert reason != Reason.ADD : "cannot remove a route with reason ADD"; Prefix network = route.getNetwork(); boolean removed = _root.remove(network, route); if (!removed) { return RibDelta.empty(); } RouteAdvertisement<R> removeRoute = new RouteAdvertisement<>(route, reason); if (!_root.get(network).isEmpty()) { return RibDelta.of(removeRoute); } @Nullable Set<R> backups = _owner._backupRoutes == null ? null : _owner._backupRoutes.get(network); if (backups == null || backups.isEmpty()) { return RibDelta.of(removeRoute); } Collection<R> bestBackups = maxValues(backups, _owner::comparePreference); _root.putAll(network, bestBackups); return RibDelta.of( Streams.concat(Stream.of(removeRoute), bestBackups.stream().map(RouteAdvertisement::adding)) .collect(ImmutableList.toImmutableList())); }
RibTree implements Serializable { @Nonnull RibDelta<R> removeRouteGetDelta(R route, Reason reason) { assert reason != Reason.ADD : "cannot remove a route with reason ADD"; Prefix network = route.getNetwork(); boolean removed = _root.remove(network, route); if (!removed) { return RibDelta.empty(); } RouteAdvertisement<R> removeRoute = new RouteAdvertisement<>(route, reason); if (!_root.get(network).isEmpty()) { return RibDelta.of(removeRoute); } @Nullable Set<R> backups = _owner._backupRoutes == null ? null : _owner._backupRoutes.get(network); if (backups == null || backups.isEmpty()) { return RibDelta.of(removeRoute); } Collection<R> bestBackups = maxValues(backups, _owner::comparePreference); _root.putAll(network, bestBackups); return RibDelta.of( Streams.concat(Stream.of(removeRoute), bestBackups.stream().map(RouteAdvertisement::adding)) .collect(ImmutableList.toImmutableList())); } }
RibTree implements Serializable { @Nonnull RibDelta<R> removeRouteGetDelta(R route, Reason reason) { assert reason != Reason.ADD : "cannot remove a route with reason ADD"; Prefix network = route.getNetwork(); boolean removed = _root.remove(network, route); if (!removed) { return RibDelta.empty(); } RouteAdvertisement<R> removeRoute = new RouteAdvertisement<>(route, reason); if (!_root.get(network).isEmpty()) { return RibDelta.of(removeRoute); } @Nullable Set<R> backups = _owner._backupRoutes == null ? null : _owner._backupRoutes.get(network); if (backups == null || backups.isEmpty()) { return RibDelta.of(removeRoute); } Collection<R> bestBackups = maxValues(backups, _owner::comparePreference); _root.putAll(network, bestBackups); return RibDelta.of( Streams.concat(Stream.of(removeRoute), bestBackups.stream().map(RouteAdvertisement::adding)) .collect(ImmutableList.toImmutableList())); } RibTree(AbstractRib<R> owner); }
RibTree implements Serializable { @Nonnull RibDelta<R> removeRouteGetDelta(R route, Reason reason) { assert reason != Reason.ADD : "cannot remove a route with reason ADD"; Prefix network = route.getNetwork(); boolean removed = _root.remove(network, route); if (!removed) { return RibDelta.empty(); } RouteAdvertisement<R> removeRoute = new RouteAdvertisement<>(route, reason); if (!_root.get(network).isEmpty()) { return RibDelta.of(removeRoute); } @Nullable Set<R> backups = _owner._backupRoutes == null ? null : _owner._backupRoutes.get(network); if (backups == null || backups.isEmpty()) { return RibDelta.of(removeRoute); } Collection<R> bestBackups = maxValues(backups, _owner::comparePreference); _root.putAll(network, bestBackups); return RibDelta.of( Streams.concat(Stream.of(removeRoute), bestBackups.stream().map(RouteAdvertisement::adding)) .collect(ImmutableList.toImmutableList())); } RibTree(AbstractRib<R> owner); void clear(); Set<R> getRoutes(); Set<R> getRoutes(Prefix prefix); @Override int hashCode(); @Override boolean equals(@Nullable Object obj); }
RibTree implements Serializable { @Nonnull RibDelta<R> removeRouteGetDelta(R route, Reason reason) { assert reason != Reason.ADD : "cannot remove a route with reason ADD"; Prefix network = route.getNetwork(); boolean removed = _root.remove(network, route); if (!removed) { return RibDelta.empty(); } RouteAdvertisement<R> removeRoute = new RouteAdvertisement<>(route, reason); if (!_root.get(network).isEmpty()) { return RibDelta.of(removeRoute); } @Nullable Set<R> backups = _owner._backupRoutes == null ? null : _owner._backupRoutes.get(network); if (backups == null || backups.isEmpty()) { return RibDelta.of(removeRoute); } Collection<R> bestBackups = maxValues(backups, _owner::comparePreference); _root.putAll(network, bestBackups); return RibDelta.of( Streams.concat(Stream.of(removeRoute), bestBackups.stream().map(RouteAdvertisement::adding)) .collect(ImmutableList.toImmutableList())); } RibTree(AbstractRib<R> owner); void clear(); Set<R> getRoutes(); Set<R> getRoutes(Prefix prefix); @Override int hashCode(); @Override boolean equals(@Nullable Object obj);  }