Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.impl; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 20 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 21 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 22 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 23 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.util.GuavaCollectors; |
| 27 | import org.onlab.util.Tools; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 28 | import org.onosproject.routeservice.ResolvedRoute; |
| 29 | import org.onosproject.routeservice.RouteEvent; |
| 30 | import org.onosproject.routeservice.RouteTableId; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.Collection; |
| 33 | import java.util.Collections; |
| 34 | import java.util.Map; |
| 35 | import java.util.Optional; |
| 36 | import java.util.Set; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 39 | import static org.onosproject.routeservice.RouteTools.createBinaryString; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Stores routes that have been resolved. |
| 43 | */ |
| 44 | public class DefaultResolvedRouteStore implements ResolvedRouteStore { |
| 45 | |
| 46 | private Map<RouteTableId, RouteTable> routeTables; |
| 47 | private static final RouteTableId IPV4 = new RouteTableId("ipv4"); |
| 48 | private static final RouteTableId IPV6 = new RouteTableId("ipv6"); |
| 49 | |
| 50 | /** |
| 51 | * Creates a new resolved route store. |
| 52 | */ |
| 53 | public DefaultResolvedRouteStore() { |
| 54 | routeTables = new ConcurrentHashMap<>(); |
| 55 | |
| 56 | routeTables.put(IPV4, new RouteTable()); |
| 57 | routeTables.put(IPV6, new RouteTable()); |
| 58 | } |
| 59 | |
| 60 | @Override |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 61 | public RouteEvent updateRoute(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
| 62 | return getDefaultRouteTable(route).update(route, alternatives); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public RouteEvent removeRoute(IpPrefix prefix) { |
| 67 | RouteTable table = getDefaultRouteTable(prefix.address()); |
| 68 | return table.remove(prefix); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Set<RouteTableId> getRouteTables() { |
| 73 | return routeTables.keySet(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Collection<ResolvedRoute> getRoutes(RouteTableId table) { |
| 78 | RouteTable routeTable = routeTables.get(table); |
| 79 | if (routeTable == null) { |
| 80 | return Collections.emptySet(); |
| 81 | } |
| 82 | return routeTable.getRoutes(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Optional<ResolvedRoute> getRoute(IpPrefix prefix) { |
| 87 | return getDefaultRouteTable(prefix.address()).getRoute(prefix); |
| 88 | } |
| 89 | |
| 90 | @Override |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 91 | public Collection<ResolvedRoute> getAllRoutes(IpPrefix prefix) { |
| 92 | return getDefaultRouteTable(prefix.address()).getAllRoutes(prefix); |
| 93 | } |
| 94 | |
| 95 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 96 | public Optional<ResolvedRoute> longestPrefixMatch(IpAddress ip) { |
| 97 | return getDefaultRouteTable(ip).longestPrefixMatch(ip); |
| 98 | } |
| 99 | |
| 100 | private RouteTable getDefaultRouteTable(ResolvedRoute route) { |
| 101 | return getDefaultRouteTable(route.prefix().address()); |
| 102 | } |
| 103 | |
| 104 | private RouteTable getDefaultRouteTable(IpAddress ip) { |
| 105 | RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6; |
| 106 | return routeTables.get(routeTableId); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Route table into which routes can be placed. |
| 111 | */ |
| 112 | private class RouteTable { |
| 113 | private final InvertedRadixTree<ResolvedRoute> routeTable; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 114 | private final Map<IpPrefix, Set<ResolvedRoute>> alternativeRoutes; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Creates a new route table. |
| 118 | */ |
| 119 | public RouteTable() { |
| 120 | routeTable = new ConcurrentInvertedRadixTree<>( |
| 121 | new DefaultByteArrayNodeFactory()); |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 122 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 123 | alternativeRoutes = new ConcurrentHashMap<>(); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adds or updates the route in the route table. |
| 128 | * |
| 129 | * @param route route to update |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 130 | * @param alternatives alternative routes |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 131 | */ |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 132 | public RouteEvent update(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
| 133 | Set<ResolvedRoute> immutableAlternatives = checkAlternatives(route, alternatives); |
| 134 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 135 | ResolvedRoute oldRoute = routeTable.put(createBinaryString(route.prefix()), route); |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 136 | Set<ResolvedRoute> oldRoutes = alternativeRoutes.put(route.prefix(), immutableAlternatives); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 137 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 138 | if (!route.equals(oldRoute)) { |
| 139 | if (oldRoute == null) { |
| 140 | return new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route, |
| 141 | immutableAlternatives); |
| 142 | } else { |
| 143 | return new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, route, |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 144 | oldRoute, immutableAlternatives, oldRoutes); |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 145 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 148 | if (!immutableAlternatives.equals(oldRoutes)) { |
| 149 | return new RouteEvent(RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED, |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 150 | route, null, immutableAlternatives, oldRoutes); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 151 | } |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 152 | |
| 153 | return null; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Checks that the best route is present in the alternatives list and |
| 158 | * returns an immutable set of alternatives. |
| 159 | * |
| 160 | * @param route best route |
| 161 | * @param alternatives alternatives |
| 162 | * @return immutable set of alternative routes |
| 163 | */ |
| 164 | private Set<ResolvedRoute> checkAlternatives(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
| 165 | if (!alternatives.contains(route)) { |
| 166 | return ImmutableSet.<ResolvedRoute>builder() |
| 167 | .addAll(alternatives) |
| 168 | .add(route) |
| 169 | .build(); |
| 170 | } else { |
| 171 | return ImmutableSet.copyOf(alternatives); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Removes the route from the route table. |
| 177 | * |
| 178 | * @param prefix prefix to remove |
| 179 | */ |
| 180 | public RouteEvent remove(IpPrefix prefix) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 181 | String key = createBinaryString(prefix); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 182 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 183 | ResolvedRoute route = routeTable.getValueForExactKey(key); |
| 184 | Set<ResolvedRoute> alternatives = alternativeRoutes.remove(prefix); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 185 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 186 | if (route != null) { |
| 187 | routeTable.remove(key); |
| 188 | return new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, route, alternatives); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 189 | } |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 190 | return null; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns all routes in the route table. |
| 195 | * |
| 196 | * @return all routes |
| 197 | */ |
| 198 | public Collection<ResolvedRoute> getRoutes() { |
| 199 | return Tools.stream(routeTable.getKeyValuePairsForKeysStartingWith("")) |
| 200 | .map(KeyValuePair::getValue) |
| 201 | .collect(GuavaCollectors.toImmutableList()); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the best route for the given prefix, if one exists. |
| 206 | * |
| 207 | * @param prefix IP prefix |
| 208 | * @return best route |
| 209 | */ |
| 210 | public Optional<ResolvedRoute> getRoute(IpPrefix prefix) { |
| 211 | return Optional.ofNullable(routeTable.getValueForExactKey(createBinaryString(prefix))); |
| 212 | } |
| 213 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 214 | public Collection<ResolvedRoute> getAllRoutes(IpPrefix prefix) { |
| 215 | return alternativeRoutes.getOrDefault(prefix, Collections.emptySet()); |
| 216 | } |
| 217 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 218 | /** |
| 219 | * Performs a longest prefix match with the given IP in the route table. |
| 220 | * |
| 221 | * @param ip IP address to look up |
| 222 | * @return most specific prefix containing the given |
| 223 | */ |
| 224 | public Optional<ResolvedRoute> longestPrefixMatch(IpAddress ip) { |
| 225 | return Tools.stream(routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix()))) |
| 226 | .reduce((a, b) -> b); // reduces to the last element in the stream |
| 227 | } |
| 228 | } |
| 229 | } |