@Test public void testGroupMatchingBgpRoutesByPrefix() { Bgpv4Route bgpv4Route1 = Bgpv4Route.builder() .setNetwork(Prefix.parse("1.1.1.0/24")) .setNextHopIp(Ip.parse("1.1.1.2")) .setOriginType(OriginType.IGP) .setOriginatorIp(Ip.parse("1.1.1.2")) .setProtocol(RoutingProtocol.BGP) .setLocalPreference(1L) .setAdmin(10) .setMetric(30L) .setAsPath(AsPath.ofSingletonAsSets(ImmutableList.of(1L, 2L))) .build(); Bgpv4Route bgpv4Route2 = Bgpv4Route.builder() .setNetwork(Prefix.parse("1.1.1.0/24")) .setNextHopIp(Ip.parse("1.1.1.3")) .setAdmin(10) .setMetric(20L) .setOriginType(OriginType.IGP) .setOriginatorIp(Ip.parse("1.1.1.2")) .setProtocol(RoutingProtocol.BGP) .setLocalPreference(1L) .setAsPath(AsPath.ofSingletonAsSets(ImmutableList.of(1L, 2L))) .build(); Table<String, String, Set<Bgpv4Route>> bgpTable = HashBasedTable.create(); bgpTable.row("node").put(Configuration.DEFAULT_VRF_NAME, new HashSet<>()); bgpTable.row("node").get(Configuration.DEFAULT_VRF_NAME).add(bgpv4Route1); bgpTable.row("node").get(Configuration.DEFAULT_VRF_NAME).add(bgpv4Route2); Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> grouped = groupBgpRoutes(bgpTable, ImmutableSet.of("node"), ".*", null, ".*"); assertThat(grouped.keySet(), hasSize(1)); RouteRowKey expectedKey = new RouteRowKey("node", 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"), "bgp"), ImmutableSortedSet.of( RouteRowAttribute.builder() .setAdminDistance(10) .setMetric(30L) .setAsPath(AsPath.ofSingletonAsSets(ImmutableList.of(1L, 2L))) .setLocalPreference(1L) .setOriginType(OriginType.IGP) .build()), new RouteRowSecondaryKey(Ip.parse("1.1.1.3"), "bgp"), ImmutableSortedSet.of( RouteRowAttribute.builder() .setAdminDistance(10) .setMetric(20L) .setLocalPreference(1L) .setAsPath(AsPath.ofSingletonAsSets(ImmutableList.of(1L, 2L))) .setOriginType(OriginType.IGP) .build())); assertThat(innerGroup, equalTo(expectedInnerMap)); }
static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupBgpRoutes( Table<String, String, Set<Bgpv4Route>> bgpRoutes, Set<String> matchingNodes, String vrfRegex, @Nullable Prefix network, String protocolRegex) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledProtocolRegex = Pattern.compile(protocolRegex, Pattern.CASE_INSENSITIVE); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); matchingNodes.forEach( hostname -> bgpRoutes .row(hostname) .forEach( (vrfName, routes) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { routes.stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && compiledProtocolRegex .matcher(route.getProtocol().protocolName()) .matches()) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(hostname, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setOriginProtocol( route.getSrcProtocol() != null ? route.getSrcProtocol().protocolName() : null) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setAsPath(route.getAsPath()) .setLocalPreference(route.getLocalPreference()) .setCommunities( route.getCommunities().getCommunities().stream() .map(Community::toString) .collect(toImmutableList())) .setOriginType(route.getOriginType()) .setTag( route.getTag() == Route.UNSET_ROUTE_TAG ? null : route.getTag()) .build())); } })); return routesGroups; }
RoutesAnswererUtil { static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupBgpRoutes( Table<String, String, Set<Bgpv4Route>> bgpRoutes, Set<String> matchingNodes, String vrfRegex, @Nullable Prefix network, String protocolRegex) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledProtocolRegex = Pattern.compile(protocolRegex, Pattern.CASE_INSENSITIVE); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); matchingNodes.forEach( hostname -> bgpRoutes .row(hostname) .forEach( (vrfName, routes) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { routes.stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && compiledProtocolRegex .matcher(route.getProtocol().protocolName()) .matches()) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(hostname, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setOriginProtocol( route.getSrcProtocol() != null ? route.getSrcProtocol().protocolName() : null) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setAsPath(route.getAsPath()) .setLocalPreference(route.getLocalPreference()) .setCommunities( route.getCommunities().getCommunities().stream() .map(Community::toString) .collect(toImmutableList())) .setOriginType(route.getOriginType()) .setTag( route.getTag() == Route.UNSET_ROUTE_TAG ? null : route.getTag()) .build())); } })); return routesGroups; } }
RoutesAnswererUtil { static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupBgpRoutes( Table<String, String, Set<Bgpv4Route>> bgpRoutes, Set<String> matchingNodes, String vrfRegex, @Nullable Prefix network, String protocolRegex) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledProtocolRegex = Pattern.compile(protocolRegex, Pattern.CASE_INSENSITIVE); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); matchingNodes.forEach( hostname -> bgpRoutes .row(hostname) .forEach( (vrfName, routes) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { routes.stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && compiledProtocolRegex .matcher(route.getProtocol().protocolName()) .matches()) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(hostname, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setOriginProtocol( route.getSrcProtocol() != null ? route.getSrcProtocol().protocolName() : null) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setAsPath(route.getAsPath()) .setLocalPreference(route.getLocalPreference()) .setCommunities( route.getCommunities().getCommunities().stream() .map(Community::toString) .collect(toImmutableList())) .setOriginType(route.getOriginType()) .setTag( route.getTag() == Route.UNSET_ROUTE_TAG ? null : route.getTag()) .build())); } })); return routesGroups; }  }
RoutesAnswererUtil { static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupBgpRoutes( Table<String, String, Set<Bgpv4Route>> bgpRoutes, Set<String> matchingNodes, String vrfRegex, @Nullable Prefix network, String protocolRegex) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledProtocolRegex = Pattern.compile(protocolRegex, Pattern.CASE_INSENSITIVE); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); matchingNodes.forEach( hostname -> bgpRoutes .row(hostname) .forEach( (vrfName, routes) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { routes.stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && compiledProtocolRegex .matcher(route.getProtocol().protocolName()) .matches()) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(hostname, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setOriginProtocol( route.getSrcProtocol() != null ? route.getSrcProtocol().protocolName() : null) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setAsPath(route.getAsPath()) .setLocalPreference(route.getLocalPreference()) .setCommunities( route.getCommunities().getCommunities().stream() .map(Community::toString) .collect(toImmutableList())) .setOriginType(route.getOriginType()) .setTag( route.getTag() == Route.UNSET_ROUTE_TAG ? null : 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 { static Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> groupBgpRoutes( Table<String, String, Set<Bgpv4Route>> bgpRoutes, Set<String> matchingNodes, String vrfRegex, @Nullable Prefix network, String protocolRegex) { Map<RouteRowKey, Map<RouteRowSecondaryKey, SortedSet<RouteRowAttribute>>> routesGroups = new HashMap<>(); Pattern compiledProtocolRegex = Pattern.compile(protocolRegex, Pattern.CASE_INSENSITIVE); Pattern compiledVrfRegex = Pattern.compile(vrfRegex); matchingNodes.forEach( hostname -> bgpRoutes .row(hostname) .forEach( (vrfName, routes) -> { if (compiledVrfRegex.matcher(vrfName).matches()) { routes.stream() .filter( route -> (network == null || network.equals(route.getNetwork())) && compiledProtocolRegex .matcher(route.getProtocol().protocolName()) .matches()) .forEach( route -> routesGroups .computeIfAbsent( new RouteRowKey(hostname, vrfName, route.getNetwork()), k -> new HashMap<>()) .computeIfAbsent( new RouteRowSecondaryKey( route.getNextHopIp(), route.getProtocol().protocolName()), k -> new TreeSet<>()) .add( RouteRowAttribute.builder() .setOriginProtocol( route.getSrcProtocol() != null ? route.getSrcProtocol().protocolName() : null) .setAdminDistance(route.getAdministrativeCost()) .setMetric(route.getMetric()) .setAsPath(route.getAsPath()) .setLocalPreference(route.getLocalPreference()) .setCommunities( route.getCommunities().getCommunities().stream() .map(Community::toString) .collect(toImmutableList())) .setOriginType(route.getOriginType()) .setTag( route.getTag() == Route.UNSET_ROUTE_TAG ? null : 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);  }