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