@Test public void testRouteDiffs() { BgpRoute route1; BgpRoute route2; route1 = builder().setAsPath(AsPath.ofSingletonAsSets(1L, 2L)).build(); route2 = builder().setAsPath(AsPath.ofSingletonAsSets(2L, 3L)).build(); assertThat( routeDiffs(route1, route2), contains(new BgpRouteDiff(PROP_AS_PATH, "[1, 2]", "[2, 3]"))); route1 = builder() .setCommunities(ImmutableSet.of(StandardCommunity.of(1L), StandardCommunity.of(2L))) .build(); route2 = builder() .setCommunities(ImmutableSet.of(StandardCommunity.of(2L), StandardCommunity.of(3L))) .build(); assertThat( routeDiffs(route1, route2), contains(new BgpRouteDiff(PROP_COMMUNITIES, "[0:1, 0:2]", "[0:2, 0:3]"))); route1 = builder().setLocalPreference(1).build(); route2 = builder().setLocalPreference(2).build(); assertThat( routeDiffs(route1, route2), contains(new BgpRouteDiff(PROP_LOCAL_PREFERENCE, "1", "2"))); route1 = builder().setMetric(1).build(); route2 = builder().setMetric(2).build(); assertThat(routeDiffs(route1, route2), contains(new BgpRouteDiff(PROP_METRIC, "1", "2"))); route1 = builder() .setAsPath(AsPath.ofSingletonAsSets(1L, 2L)) .setCommunities(ImmutableSet.of(StandardCommunity.of(1L), StandardCommunity.of(2L))) .setLocalPreference(1) .setMetric(1) .build(); route2 = builder() .setAsPath(AsPath.ofSingletonAsSets(2L, 3L)) .setCommunities(ImmutableSet.of(StandardCommunity.of(2L), StandardCommunity.of(3L))) .setLocalPreference(2) .setMetric(2) .build(); assertThat( routeDiffs(route1, route2), containsInAnyOrder( new BgpRouteDiff(PROP_AS_PATH, "[1, 2]", "[2, 3]"), new BgpRouteDiff(PROP_COMMUNITIES, "[0:1, 0:2]", "[0:2, 0:3]"), new BgpRouteDiff(PROP_LOCAL_PREFERENCE, "1", "2"), new BgpRouteDiff(PROP_METRIC, "1", "2"))); }
public static SortedSet<BgpRouteDiff> routeDiffs( @Nullable BgpRoute route1, @Nullable BgpRoute route2) { if (route1 == null || route2 == null || route1.equals(route2)) { return ImmutableSortedSet.of(); } checkArgument( route1 .toBuilder() .setAsPath(route2.getAsPath()) .setCommunities(route2.getCommunities()) .setLocalPreference(route2.getLocalPreference()) .setMetric(route2.getMetric()) .build() .equals(route2), "routeDiffs only supports differences of fields: " + ROUTE_DIFF_FIELD_NAMES); return Stream.of( routeDiff(route1, route2, PROP_AS_PATH, BgpRoute::getAsPath), routeDiff(route1, route2, PROP_COMMUNITIES, BgpRoute::getCommunities), routeDiff(route1, route2, PROP_LOCAL_PREFERENCE, BgpRoute::getLocalPreference), routeDiff(route1, route2, PROP_METRIC, BgpRoute::getMetric)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSortedSet.toImmutableSortedSet(natural())); }
BgpRouteDiff implements Comparable<BgpRouteDiff> { public static SortedSet<BgpRouteDiff> routeDiffs( @Nullable BgpRoute route1, @Nullable BgpRoute route2) { if (route1 == null || route2 == null || route1.equals(route2)) { return ImmutableSortedSet.of(); } checkArgument( route1 .toBuilder() .setAsPath(route2.getAsPath()) .setCommunities(route2.getCommunities()) .setLocalPreference(route2.getLocalPreference()) .setMetric(route2.getMetric()) .build() .equals(route2), "routeDiffs only supports differences of fields: " + ROUTE_DIFF_FIELD_NAMES); return Stream.of( routeDiff(route1, route2, PROP_AS_PATH, BgpRoute::getAsPath), routeDiff(route1, route2, PROP_COMMUNITIES, BgpRoute::getCommunities), routeDiff(route1, route2, PROP_LOCAL_PREFERENCE, BgpRoute::getLocalPreference), routeDiff(route1, route2, PROP_METRIC, BgpRoute::getMetric)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSortedSet.toImmutableSortedSet(natural())); } }
BgpRouteDiff implements Comparable<BgpRouteDiff> { public static SortedSet<BgpRouteDiff> routeDiffs( @Nullable BgpRoute route1, @Nullable BgpRoute route2) { if (route1 == null || route2 == null || route1.equals(route2)) { return ImmutableSortedSet.of(); } checkArgument( route1 .toBuilder() .setAsPath(route2.getAsPath()) .setCommunities(route2.getCommunities()) .setLocalPreference(route2.getLocalPreference()) .setMetric(route2.getMetric()) .build() .equals(route2), "routeDiffs only supports differences of fields: " + ROUTE_DIFF_FIELD_NAMES); return Stream.of( routeDiff(route1, route2, PROP_AS_PATH, BgpRoute::getAsPath), routeDiff(route1, route2, PROP_COMMUNITIES, BgpRoute::getCommunities), routeDiff(route1, route2, PROP_LOCAL_PREFERENCE, BgpRoute::getLocalPreference), routeDiff(route1, route2, PROP_METRIC, BgpRoute::getMetric)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSortedSet.toImmutableSortedSet(natural())); } BgpRouteDiff(String fieldName, String oldValue, String newValue); }
BgpRouteDiff implements Comparable<BgpRouteDiff> { public static SortedSet<BgpRouteDiff> routeDiffs( @Nullable BgpRoute route1, @Nullable BgpRoute route2) { if (route1 == null || route2 == null || route1.equals(route2)) { return ImmutableSortedSet.of(); } checkArgument( route1 .toBuilder() .setAsPath(route2.getAsPath()) .setCommunities(route2.getCommunities()) .setLocalPreference(route2.getLocalPreference()) .setMetric(route2.getMetric()) .build() .equals(route2), "routeDiffs only supports differences of fields: " + ROUTE_DIFF_FIELD_NAMES); return Stream.of( routeDiff(route1, route2, PROP_AS_PATH, BgpRoute::getAsPath), routeDiff(route1, route2, PROP_COMMUNITIES, BgpRoute::getCommunities), routeDiff(route1, route2, PROP_LOCAL_PREFERENCE, BgpRoute::getLocalPreference), routeDiff(route1, route2, PROP_METRIC, BgpRoute::getMetric)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSortedSet.toImmutableSortedSet(natural())); } BgpRouteDiff(String fieldName, String oldValue, String newValue); @Override boolean equals(Object o); @JsonProperty(PROP_FIELD_NAME) String getFieldName(); @JsonProperty(PROP_OLD_VALUE) String getOldValue(); @Override int hashCode(); @JsonProperty(PROP_NEW_VALUE) String getNewValue(); static SortedSet<BgpRouteDiff> routeDiffs(
      @Nullable BgpRoute route1, @Nullable BgpRoute route2); @Override int compareTo(BgpRouteDiff that); }
BgpRouteDiff implements Comparable<BgpRouteDiff> { public static SortedSet<BgpRouteDiff> routeDiffs( @Nullable BgpRoute route1, @Nullable BgpRoute route2) { if (route1 == null || route2 == null || route1.equals(route2)) { return ImmutableSortedSet.of(); } checkArgument( route1 .toBuilder() .setAsPath(route2.getAsPath()) .setCommunities(route2.getCommunities()) .setLocalPreference(route2.getLocalPreference()) .setMetric(route2.getMetric()) .build() .equals(route2), "routeDiffs only supports differences of fields: " + ROUTE_DIFF_FIELD_NAMES); return Stream.of( routeDiff(route1, route2, PROP_AS_PATH, BgpRoute::getAsPath), routeDiff(route1, route2, PROP_COMMUNITIES, BgpRoute::getCommunities), routeDiff(route1, route2, PROP_LOCAL_PREFERENCE, BgpRoute::getLocalPreference), routeDiff(route1, route2, PROP_METRIC, BgpRoute::getMetric)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSortedSet.toImmutableSortedSet(natural())); } BgpRouteDiff(String fieldName, String oldValue, String newValue); @Override boolean equals(Object o); @JsonProperty(PROP_FIELD_NAME) String getFieldName(); @JsonProperty(PROP_OLD_VALUE) String getOldValue(); @Override int hashCode(); @JsonProperty(PROP_NEW_VALUE) String getNewValue(); static SortedSet<BgpRouteDiff> routeDiffs(
      @Nullable BgpRoute route1, @Nullable BgpRoute route2); @Override int compareTo(BgpRouteDiff that);  }