Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [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 | |
| 17 | package org.onosproject.incubator.store.routing.impl; |
| 18 | |
| 19 | import com.google.common.collect.HashMultimap; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Multimaps; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 23 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 24 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 25 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 26 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.routing.NextHopData; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 30 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 31 | import org.onosproject.incubator.net.routing.Route; |
| 32 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 33 | import org.onosproject.incubator.net.routing.RouteStore; |
| 34 | import org.onosproject.incubator.net.routing.RouteStoreDelegate; |
| 35 | import org.onosproject.incubator.net.routing.RouteTableId; |
| 36 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.Collection; |
| 41 | import java.util.Collections; |
| 42 | import java.util.Iterator; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 43 | import java.util.LinkedList; |
| 44 | import java.util.List; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 45 | import java.util.Map; |
| 46 | import java.util.Set; |
| 47 | import java.util.concurrent.ConcurrentHashMap; |
| 48 | |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 49 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | c4c2d62 | 2017-02-10 14:13:57 -0800 | [diff] [blame] | 50 | import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString; |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 51 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Route store based on in-memory storage. |
| 54 | */ |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 55 | public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegate> |
| 56 | implements RouteStore { |
| 57 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 58 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 60 | private Map<RouteTableId, RouteTable> routeTables; |
| 61 | private static final RouteTableId IPV4 = new RouteTableId("ipv4"); |
| 62 | private static final RouteTableId IPV6 = new RouteTableId("ipv6"); |
| 63 | |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 64 | private Map<IpAddress, NextHopData> nextHops = new ConcurrentHashMap<>(); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 65 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Sets up local route store. |
| 68 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 69 | public void activate() { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 70 | routeTables = new ConcurrentHashMap<>(); |
| 71 | |
| 72 | routeTables.put(IPV4, new RouteTable()); |
| 73 | routeTables.put(IPV6, new RouteTable()); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 74 | |
| 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Cleans up local route store. Currently nothing is done here. |
| 80 | */ |
| 81 | public void deactivate() { |
| 82 | log.info("Stopped"); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void updateRoute(Route route) { |
| 87 | getDefaultRouteTable(route).update(route); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void removeRoute(Route route) { |
| 92 | RouteTable table = getDefaultRouteTable(route); |
| 93 | table.remove(route); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 94 | Collection<Route> routes = table.getRoutesForNextHop(route.nextHop()); |
| 95 | |
| 96 | if (routes.isEmpty()) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 97 | nextHops.remove(route.nextHop()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<RouteTableId> getRouteTables() { |
| 103 | return routeTables.keySet(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Collection<Route> getRoutes(RouteTableId table) { |
| 108 | RouteTable routeTable = routeTables.get(table); |
| 109 | if (routeTable == null) { |
| 110 | return Collections.emptySet(); |
| 111 | } |
| 112 | return routeTable.getRoutes(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Route longestPrefixMatch(IpAddress ip) { |
| 117 | return getDefaultRouteTable(ip).longestPrefixMatch(ip); |
| 118 | } |
| 119 | |
| 120 | @Override |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 121 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 122 | return getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
| 123 | } |
| 124 | |
| 125 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 126 | public void updateNextHop(IpAddress ip, NextHopData nextHopData) { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 127 | checkNotNull(ip); |
| 128 | checkNotNull(nextHopData); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 129 | Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 130 | |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 131 | if (!routes.isEmpty() && !nextHopData.equals(nextHops.get(ip))) { |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 132 | NextHopData oldNextHopData = nextHops.put(ip, nextHopData); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 133 | |
| 134 | for (Route route : routes) { |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 135 | if (oldNextHopData == null) { |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 136 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 137 | new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()))); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 138 | } else { |
| 139 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 140 | new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()), |
| 141 | new ResolvedRoute(route, oldNextHopData.mac(), oldNextHopData.location()))); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 142 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 148 | public void removeNextHop(IpAddress ip, NextHopData nextHopData) { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 149 | checkNotNull(ip); |
| 150 | checkNotNull(nextHopData); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 151 | if (nextHops.remove(ip, nextHopData)) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 152 | Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
| 153 | for (Route route : routes) { |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 154 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 155 | new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()))); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 161 | public NextHopData getNextHop(IpAddress ip) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 162 | return nextHops.get(ip); |
| 163 | } |
| 164 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 165 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 166 | public Map<IpAddress, NextHopData> getNextHops() { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 167 | return ImmutableMap.copyOf(nextHops); |
| 168 | } |
| 169 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 170 | private RouteTable getDefaultRouteTable(Route route) { |
| 171 | return getDefaultRouteTable(route.prefix().address()); |
| 172 | } |
| 173 | |
| 174 | private RouteTable getDefaultRouteTable(IpAddress ip) { |
| 175 | RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6; |
| 176 | return routeTables.get(routeTableId); |
| 177 | } |
| 178 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Route table into which routes can be placed. |
| 181 | */ |
| 182 | private class RouteTable { |
| 183 | private final InvertedRadixTree<Route> routeTable; |
| 184 | |
| 185 | private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>(); |
| 186 | private final Multimap<IpAddress, Route> reverseIndex = |
| 187 | Multimaps.synchronizedMultimap(HashMultimap.create()); |
| 188 | |
| 189 | /** |
| 190 | * Creates a new route table. |
| 191 | */ |
| 192 | public RouteTable() { |
| 193 | routeTable = new ConcurrentInvertedRadixTree<>( |
| 194 | new DefaultByteArrayNodeFactory()); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Adds or updates the route in the route table. |
| 199 | * |
| 200 | * @param route route to update |
| 201 | */ |
| 202 | public void update(Route route) { |
| 203 | synchronized (this) { |
| 204 | Route oldRoute = routes.put(route.prefix(), route); |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 205 | |
| 206 | // No need to proceed if the new route is the same |
| 207 | if (route.equals(oldRoute)) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | NextHopData oldNextHopData = null; |
| 212 | ResolvedRoute oldResolvedRoute = null; |
| 213 | if (oldRoute != null) { |
| 214 | oldNextHopData = nextHops.get(oldRoute.nextHop()); |
| 215 | if (oldNextHopData != null) { |
| 216 | oldResolvedRoute = new ResolvedRoute(oldRoute, |
| 217 | oldNextHopData.mac(), oldNextHopData.location()); |
| 218 | } |
| 219 | } |
| 220 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 221 | routeTable.put(createBinaryString(route.prefix()), route); |
| 222 | |
| 223 | // TODO manage routes from multiple providers |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 224 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 225 | reverseIndex.put(route.nextHop(), route); |
| 226 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 227 | if (oldRoute != null) { |
| 228 | reverseIndex.remove(oldRoute.nextHop(), oldRoute); |
| 229 | |
| 230 | if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 231 | nextHops.remove(oldRoute.nextHop()); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 235 | NextHopData nextHopData = nextHops.get(route.nextHop()); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 236 | |
| 237 | if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 238 | // We don't know the new MAC address yet so delete the route |
| 239 | // Don't send ROUTE_REMOVED if the route was unresolved |
| 240 | if (nextHopData == null && oldNextHopData != null) { |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 241 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 242 | oldResolvedRoute)); |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 243 | // We know the new MAC address so update the route |
| 244 | } else if (nextHopData != null && oldNextHopData != null) { |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 245 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 246 | new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()), |
| 247 | oldResolvedRoute)); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 248 | } |
| 249 | return; |
| 250 | } |
| 251 | |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 252 | if (nextHopData != null) { |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 253 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 254 | new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()))); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 255 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Removes the route from the route table. |
| 261 | * |
| 262 | * @param route route to remove |
| 263 | */ |
| 264 | public void remove(Route route) { |
| 265 | synchronized (this) { |
| 266 | Route removed = routes.remove(route.prefix()); |
| 267 | routeTable.remove(createBinaryString(route.prefix())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 268 | |
| 269 | if (removed != null) { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 270 | reverseIndex.remove(removed.nextHop(), removed); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 271 | NextHopData oldNextHopData = getNextHop(removed.nextHop()); |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 272 | // Don't send ROUTE_REMOVED if the route was unresolved |
| 273 | if (oldNextHopData != null) { |
| 274 | notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, |
| 275 | new ResolvedRoute(route, oldNextHopData.mac(), |
| 276 | oldNextHopData.location()))); |
| 277 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Returns the routes pointing to a particular next hop. |
| 284 | * |
| 285 | * @param ip next hop IP address |
| 286 | * @return routes for the next hop |
| 287 | */ |
| 288 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 289 | return reverseIndex.get(ip); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns all routes in the route table. |
| 294 | * |
| 295 | * @return all routes |
| 296 | */ |
| 297 | public Collection<Route> getRoutes() { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 298 | Iterator<KeyValuePair<Route>> it = |
| 299 | routeTable.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 300 | |
| 301 | List<Route> routes = new LinkedList<>(); |
| 302 | |
| 303 | while (it.hasNext()) { |
| 304 | KeyValuePair<Route> entry = it.next(); |
| 305 | routes.add(entry.getValue()); |
| 306 | } |
| 307 | |
| 308 | return routes; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Performs a longest prefix match with the given IP in the route table. |
| 313 | * |
| 314 | * @param ip IP address to look up |
| 315 | * @return most specific prefix containing the given |
| 316 | */ |
| 317 | public Route longestPrefixMatch(IpAddress ip) { |
| 318 | Iterable<Route> prefixes = |
| 319 | routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix())); |
| 320 | |
| 321 | Iterator<Route> it = prefixes.iterator(); |
| 322 | |
| 323 | Route route = null; |
| 324 | while (it.hasNext()) { |
| 325 | route = it.next(); |
| 326 | } |
| 327 | |
| 328 | return route; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | } |