@Test public void testGroupMatchingRoutesByPrefix() { SortedMap<String, SortedMap<String, GenericRib<AbstractRoute>>> ribs = ImmutableSortedMap.of( "n1", ImmutableSortedMap.of( Configuration.DEFAULT_VRF_NAME, new MockRib<>( ImmutableSet.of( OspfExternalType2Route.builder() .setNetwork(Prefix.parse("1.1.1.0/24")) .setNextHopIp(Ip.parse("1.1.1.2")) .setAdmin(10) .setMetric(30L) .setLsaMetric(2) .setCostToAdvertiser(2) .setArea(1L) .setAdvertiser("n2") .setOspfMetricType(OspfMetricType.E2) .build(), OspfExternalType2Route.builder() .setNetwork(Prefix.parse("1.1.1.0/24")) .setNextHopIp(Ip.parse("1.1.1.3")) .setAdmin(10) .setMetric(20L) .setLsaMetric(2) .setCostToAdvertiser(2) .setArea(1L) .setAdvertiser("n2") .setOspfMetricType(OspfMetricType.E2) .build())))); Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> grouped = groupRoutes( ribs, ImmutableSet.of("n1"), null, ".*", RoutingProtocolSpecifier.ALL_PROTOCOLS_SPECIFIER, null); assertThat(grouped.keySet(), hasSize(1)); RouteRowKey expectedKey = new RouteRowKey("n1", Configuration.DEFAULT_VRF_NAME, Prefix.parse("1.1.1.0/24")); assertThat(grouped.keySet().iterator().next(), equalTo(expectedKey)); Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>> innerGroup = grouped.get(expectedKey); Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>> expectedInnerMap = ImmutableMap.of( new RouteRowSecondaryKey(Ip.parse("1.1.1.2"), "ospfE2"), ImmutableSortedSet.of( RouteRowAttribute.builder() .setAdminDistance(10) .setMetric(30L) .setNextHopInterface(Route.UNSET_NEXT_HOP_INTERFACE) .build()), new RouteRowSecondaryKey(Ip.parse("1.1.1.3"), "ospfE2"), ImmutableSortedSet.of( RouteRowAttribute.builder() .setAdminDistance(10) .setMetric(20L) .setNextHopInterface(Route.UNSET_NEXT_HOP_INTERFACE) .build())); assertThat(innerGroup, equalTo(expectedInnerMap)); }
public static <T extends AbstractRouteDecorator> Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes( SortedMap<String, SortedMap<String, GenericRib<T>>> ribs, Set<String> matchingNodes, @Nullable Prefix network, String vrfRegex, RoutingProtocolSpecifier protocolSpec, @Nullable Map<Ip, Set<String>> ipOwners) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); ribs.forEach( (node, vrfMap) -> { if (matchingNodes.contains(node)) { vrfMap.forEach( (vrfName, rib) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { rib.getRoutes().stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && protocolSpec.getProtocols().contains(route.getProtocol())) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(node, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setNextHop( computeNextHopNode(route.getNextHopIp(), ipOwners)) .setNextHopInterface(route.getNextHopInterface()) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setTag(route.getTag()) .build())); } }); } }); return routesGroups; }
RoutesAnswererUtil { public static <T extends AbstractRouteDecorator> Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes( SortedMap<String, SortedMap<String, GenericRib<T>>> ribs, Set<String> matchingNodes, @Nullable Prefix network, String vrfRegex, RoutingProtocolSpecifier protocolSpec, @Nullable Map<Ip, Set<String>> ipOwners) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); ribs.forEach( (node, vrfMap) -> { if (matchingNodes.contains(node)) { vrfMap.forEach( (vrfName, rib) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { rib.getRoutes().stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && protocolSpec.getProtocols().contains(route.getProtocol())) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(node, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setNextHop( computeNextHopNode(route.getNextHopIp(), ipOwners)) .setNextHopInterface(route.getNextHopInterface()) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setTag(route.getTag()) .build())); } }); } }); return routesGroups; } }
RoutesAnswererUtil { public static <T extends AbstractRouteDecorator> Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes( SortedMap<String, SortedMap<String, GenericRib<T>>> ribs, Set<String> matchingNodes, @Nullable Prefix network, String vrfRegex, RoutingProtocolSpecifier protocolSpec, @Nullable Map<Ip, Set<String>> ipOwners) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); ribs.forEach( (node, vrfMap) -> { if (matchingNodes.contains(node)) { vrfMap.forEach( (vrfName, rib) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { rib.getRoutes().stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && protocolSpec.getProtocols().contains(route.getProtocol())) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(node, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setNextHop( computeNextHopNode(route.getNextHopIp(), ipOwners)) .setNextHopInterface(route.getNextHopInterface()) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setTag(route.getTag()) .build())); } }); } }); return routesGroups; }  }
RoutesAnswererUtil { public static <T extends AbstractRouteDecorator> Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes( SortedMap<String, SortedMap<String, GenericRib<T>>> ribs, Set<String> matchingNodes, @Nullable Prefix network, String vrfRegex, RoutingProtocolSpecifier protocolSpec, @Nullable Map<Ip, Set<String>> ipOwners) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); ribs.forEach( (node, vrfMap) -> { if (matchingNodes.contains(node)) { vrfMap.forEach( (vrfName, rib) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { rib.getRoutes().stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && protocolSpec.getProtocols().contains(route.getProtocol())) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(node, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setNextHop( computeNextHopNode(route.getNextHopIp(), ipOwners)) .setNextHopInterface(route.getNextHopInterface()) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setTag(route.getTag()) .build())); } }); } }); return routesGroups; }  static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes(
          SortedMap<String, SortedMap<String, GenericRib<T>>> ribs,
          Set<String> matchingNodes,
          @Nullable Prefix network,
          String vrfRegex,
          RoutingProtocolSpecifier protocolSpec,
          @Nullable Map<Ip, Set<String>> ipOwners); static List<DiffRoutesOutput> getRoutesDiff(
      Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesInBase,
      Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesInRef); }
RoutesAnswererUtil { public static <T extends AbstractRouteDecorator> Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes( SortedMap<String, SortedMap<String, GenericRib<T>>> ribs, Set<String> matchingNodes, @Nullable Prefix network, String vrfRegex, RoutingProtocolSpecifier protocolSpec, @Nullable Map<Ip, Set<String>> ipOwners) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); ribs.forEach( (node, vrfMap) -> { if (matchingNodes.contains(node)) { vrfMap.forEach( (vrfName, rib) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { rib.getRoutes().stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && protocolSpec.getProtocols().contains(route.getProtocol())) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(node, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setNextHop( computeNextHopNode(route.getNextHopIp(), ipOwners)) .setNextHopInterface(route.getNextHopInterface()) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setTag(route.getTag()) .build())); } }); } }); return routesGroups; }  static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupRoutes(
          SortedMap<String, SortedMap<String, GenericRib<T>>> ribs,
          Set<String> matchingNodes,
          @Nullable Prefix network,
          String vrfRegex,
          RoutingProtocolSpecifier protocolSpec,
          @Nullable Map<Ip, Set<String>> ipOwners); static List<DiffRoutesOutput> getRoutesDiff(
      Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesInBase,
      Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesInRef);  }