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