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 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 19 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 20 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 21 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 22 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 25 | import org.onosproject.incubator.net.routing.InternalRouteEvent; |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.routing.NextHopData; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.routing.Route; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 28 | import org.onosproject.incubator.net.routing.RouteSet; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.routing.RouteStore; |
| 30 | import org.onosproject.incubator.net.routing.RouteStoreDelegate; |
| 31 | import org.onosproject.incubator.net.routing.RouteTableId; |
| 32 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.Collections; |
| 38 | import java.util.Iterator; |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 39 | import java.util.LinkedList; |
| 40 | import java.util.List; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 41 | import java.util.Map; |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 44 | import java.util.stream.Collectors; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 45 | |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 46 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | c4c2d62 | 2017-02-10 14:13:57 -0800 | [diff] [blame] | 47 | import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString; |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Route store based on in-memory storage. |
| 51 | */ |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 52 | public class LocalRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 53 | implements RouteStore { |
| 54 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 55 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 57 | private Map<RouteTableId, RouteTable> routeTables; |
| 58 | private static final RouteTableId IPV4 = new RouteTableId("ipv4"); |
| 59 | private static final RouteTableId IPV6 = new RouteTableId("ipv6"); |
| 60 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Sets up local route store. |
| 63 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 64 | public void activate() { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 65 | routeTables = new ConcurrentHashMap<>(); |
| 66 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 67 | routeTables.put(IPV4, new RouteTable(IPV4)); |
| 68 | routeTables.put(IPV6, new RouteTable(IPV6)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 69 | |
| 70 | log.info("Started"); |
| 71 | } |
| 72 | |
| 73 | /** |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 74 | * Cleans up local route store. |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 75 | */ |
| 76 | public void deactivate() { |
| 77 | log.info("Stopped"); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void updateRoute(Route route) { |
| 82 | getDefaultRouteTable(route).update(route); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void removeRoute(Route route) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 87 | getDefaultRouteTable(route).remove(route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Set<RouteTableId> getRouteTables() { |
| 92 | return routeTables.keySet(); |
| 93 | } |
| 94 | |
| 95 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 96 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 97 | RouteTable routeTable = routeTables.get(table); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 98 | if (routeTable != null) { |
| 99 | return routeTable.getRouteSets(); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 100 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 101 | return null; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Route longestPrefixMatch(IpAddress ip) { |
| 106 | return getDefaultRouteTable(ip).longestPrefixMatch(ip); |
| 107 | } |
| 108 | |
| 109 | @Override |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 110 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 111 | return getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
| 112 | } |
| 113 | |
| 114 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 115 | public RouteSet getRoutes(IpPrefix prefix) { |
| 116 | return getDefaultRouteTable(prefix.address()).getRoutes(prefix); |
| 117 | } |
| 118 | |
| 119 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 120 | public void updateNextHop(IpAddress ip, NextHopData nextHopData) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 121 | // No longer needed |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 125 | public void removeNextHop(IpAddress ip, NextHopData nextHopData) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 126 | // No longer needed |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 130 | public NextHopData getNextHop(IpAddress ip) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 131 | // No longer needed |
| 132 | return null; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 135 | @Override |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 136 | public Map<IpAddress, NextHopData> getNextHops() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 137 | // No longer needed |
| 138 | return Collections.emptyMap(); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 141 | private RouteTable getDefaultRouteTable(Route route) { |
| 142 | return getDefaultRouteTable(route.prefix().address()); |
| 143 | } |
| 144 | |
| 145 | private RouteTable getDefaultRouteTable(IpAddress ip) { |
| 146 | RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6; |
| 147 | return routeTables.get(routeTableId); |
| 148 | } |
| 149 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Route table into which routes can be placed. |
| 152 | */ |
| 153 | private class RouteTable { |
| 154 | private final InvertedRadixTree<Route> routeTable; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 155 | private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>(); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 156 | private final RouteTableId id; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Creates a new route table. |
| 160 | */ |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 161 | public RouteTable(RouteTableId id) { |
| 162 | this.id = checkNotNull(id); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 163 | routeTable = new ConcurrentInvertedRadixTree<>( |
| 164 | new DefaultByteArrayNodeFactory()); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Adds or updates the route in the route table. |
| 169 | * |
| 170 | * @param route route to update |
| 171 | */ |
| 172 | public void update(Route route) { |
| 173 | synchronized (this) { |
| 174 | Route oldRoute = routes.put(route.prefix(), route); |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 175 | |
| 176 | // No need to proceed if the new route is the same |
| 177 | if (route.equals(oldRoute)) { |
| 178 | return; |
| 179 | } |
| 180 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 181 | routeTable.put(createBinaryString(route.prefix()), route); |
| 182 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 183 | notifyDelegate(new InternalRouteEvent( |
| 184 | InternalRouteEvent.Type.ROUTE_ADDED, singletonRouteSet(route))); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Removes the route from the route table. |
| 190 | * |
| 191 | * @param route route to remove |
| 192 | */ |
| 193 | public void remove(Route route) { |
| 194 | synchronized (this) { |
| 195 | Route removed = routes.remove(route.prefix()); |
| 196 | routeTable.remove(createBinaryString(route.prefix())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 197 | |
| 198 | if (removed != null) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 199 | notifyDelegate(new InternalRouteEvent( |
| 200 | InternalRouteEvent.Type.ROUTE_REMOVED, emptyRouteSet(route.prefix()))); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns the routes pointing to a particular next hop. |
| 207 | * |
| 208 | * @param ip next hop IP address |
| 209 | * @return routes for the next hop |
| 210 | */ |
| 211 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 212 | return routes.values() |
| 213 | .stream() |
| 214 | .filter(route -> route.nextHop().equals(ip)) |
| 215 | .collect(Collectors.toSet()); |
| 216 | } |
| 217 | |
| 218 | public RouteSet getRoutes(IpPrefix prefix) { |
| 219 | Route route = routes.get(prefix); |
| 220 | if (route != null) { |
| 221 | return singletonRouteSet(route); |
| 222 | } |
| 223 | return null; |
| 224 | } |
| 225 | |
| 226 | public Collection<RouteSet> getRouteSets() { |
| 227 | return routes.values().stream() |
| 228 | .map(this::singletonRouteSet) |
| 229 | .collect(Collectors.toSet()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns all routes in the route table. |
| 234 | * |
| 235 | * @return all routes |
| 236 | */ |
| 237 | public Collection<Route> getRoutes() { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 238 | Iterator<KeyValuePair<Route>> it = |
| 239 | routeTable.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 240 | |
| 241 | List<Route> routes = new LinkedList<>(); |
| 242 | |
| 243 | while (it.hasNext()) { |
| 244 | KeyValuePair<Route> entry = it.next(); |
| 245 | routes.add(entry.getValue()); |
| 246 | } |
| 247 | |
| 248 | return routes; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Performs a longest prefix match with the given IP in the route table. |
| 253 | * |
| 254 | * @param ip IP address to look up |
| 255 | * @return most specific prefix containing the given |
| 256 | */ |
| 257 | public Route longestPrefixMatch(IpAddress ip) { |
| 258 | Iterable<Route> prefixes = |
| 259 | routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix())); |
| 260 | |
| 261 | Iterator<Route> it = prefixes.iterator(); |
| 262 | |
| 263 | Route route = null; |
| 264 | while (it.hasNext()) { |
| 265 | route = it.next(); |
| 266 | } |
| 267 | |
| 268 | return route; |
| 269 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 270 | |
| 271 | private RouteSet singletonRouteSet(Route route) { |
| 272 | return new RouteSet(id, route.prefix(), Collections.singleton(route)); |
| 273 | } |
| 274 | |
| 275 | private RouteSet emptyRouteSet(IpPrefix prefix) { |
| 276 | return new RouteSet(id, prefix, Collections.emptySet()); |
| 277 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } |