blob: 46346d7b28fd6304b6e59cf4df3b3f89019fcf10 [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jonathan Hartbfc5c482016-04-05 18:57:00 -07003 *
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
17package org.onosproject.incubator.store.routing.impl;
18
19import com.google.common.collect.HashMultimap;
Jonathan Hartfd176612016-04-11 10:42:10 -070020import com.google.common.collect.ImmutableMap;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070021import com.google.common.collect.Multimap;
22import com.google.common.collect.Multimaps;
Jonathan Hartfd176612016-04-11 10:42:10 -070023import com.googlecode.concurrenttrees.common.KeyValuePair;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070024import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
25import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
26import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070027import org.onlab.packet.IpAddress;
28import org.onlab.packet.IpPrefix;
Charles Chan8fe9f4c2016-10-24 16:46:25 -070029import org.onosproject.incubator.net.routing.NextHopData;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070030import org.onosproject.incubator.net.routing.ResolvedRoute;
31import org.onosproject.incubator.net.routing.Route;
32import org.onosproject.incubator.net.routing.RouteEvent;
33import org.onosproject.incubator.net.routing.RouteStore;
34import org.onosproject.incubator.net.routing.RouteStoreDelegate;
35import org.onosproject.incubator.net.routing.RouteTableId;
36import org.onosproject.store.AbstractStore;
Jonathan Hartfd176612016-04-11 10:42:10 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070039
40import java.util.Collection;
41import java.util.Collections;
42import java.util.Iterator;
Jonathan Hartfd176612016-04-11 10:42:10 -070043import java.util.LinkedList;
44import java.util.List;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070045import java.util.Map;
46import java.util.Set;
47import java.util.concurrent.ConcurrentHashMap;
48
Charles Chanb21d69a2016-11-11 17:46:14 -080049import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hartc4c2d622017-02-10 14:13:57 -080050import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString;
Charles Chanb21d69a2016-11-11 17:46:14 -080051
Jonathan Hartbfc5c482016-04-05 18:57:00 -070052/**
53 * Route store based on in-memory storage.
54 */
Jonathan Hartbfc5c482016-04-05 18:57:00 -070055public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegate>
56 implements RouteStore {
57
Jonathan Hartfd176612016-04-11 10:42:10 -070058 private Logger log = LoggerFactory.getLogger(getClass());
59
Jonathan Hartbfc5c482016-04-05 18:57:00 -070060 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 Chan8fe9f4c2016-10-24 16:46:25 -070064 private Map<IpAddress, NextHopData> nextHops = new ConcurrentHashMap<>();
Jonathan Hartbfc5c482016-04-05 18:57:00 -070065
Charles Chan0214ded2016-11-18 17:48:37 -080066 /**
67 * Sets up local route store.
68 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -070069 public void activate() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070070 routeTables = new ConcurrentHashMap<>();
71
72 routeTables.put(IPV4, new RouteTable());
73 routeTables.put(IPV6, new RouteTable());
Charles Chan0214ded2016-11-18 17:48:37 -080074
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 Hartbfc5c482016-04-05 18:57:00 -070083 }
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 Hartfd176612016-04-11 10:42:10 -070094 Collection<Route> routes = table.getRoutesForNextHop(route.nextHop());
95
96 if (routes.isEmpty()) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070097 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 Hartfd176612016-04-11 10:42:10 -0700121 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
122 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
123 }
124
125 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700126 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800127 checkNotNull(ip);
128 checkNotNull(nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700129 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
Jonathan Hartfd176612016-04-11 10:42:10 -0700130
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700131 if (!routes.isEmpty() && !nextHopData.equals(nextHops.get(ip))) {
Charles Chane4d13102016-11-08 15:38:44 -0800132 NextHopData oldNextHopData = nextHops.put(ip, nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700133
134 for (Route route : routes) {
Charles Chane4d13102016-11-08 15:38:44 -0800135 if (oldNextHopData == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700136 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700137 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700138 } else {
139 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800140 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
141 new ResolvedRoute(route, oldNextHopData.mac(), oldNextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700142 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700143 }
144 }
145 }
146
147 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700148 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800149 checkNotNull(ip);
150 checkNotNull(nextHopData);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700151 if (nextHops.remove(ip, nextHopData)) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700152 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
153 for (Route route : routes) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700154 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800155 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700156 }
157 }
158 }
159
160 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700161 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700162 return nextHops.get(ip);
163 }
164
Jonathan Hartfd176612016-04-11 10:42:10 -0700165 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700166 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700167 return ImmutableMap.copyOf(nextHops);
168 }
169
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700170 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 Hartbfc5c482016-04-05 18:57:00 -0700179 /**
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 Chanb21d69a2016-11-11 17:46:14 -0800205
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 Hartbfc5c482016-04-05 18:57:00 -0700221 routeTable.put(createBinaryString(route.prefix()), route);
222
223 // TODO manage routes from multiple providers
Jonathan Hartfd176612016-04-11 10:42:10 -0700224
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700225 reverseIndex.put(route.nextHop(), route);
226
Jonathan Hartfd176612016-04-11 10:42:10 -0700227 if (oldRoute != null) {
228 reverseIndex.remove(oldRoute.nextHop(), oldRoute);
229
230 if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800231 nextHops.remove(oldRoute.nextHop());
Jonathan Hartfd176612016-04-11 10:42:10 -0700232 }
233 }
234
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700235 NextHopData nextHopData = nextHops.get(route.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700236
237 if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800238 // 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 Hart7f2d5742016-04-13 16:22:50 -0700241 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800242 oldResolvedRoute));
Charles Chanb21d69a2016-11-11 17:46:14 -0800243 // We know the new MAC address so update the route
244 } else if (nextHopData != null && oldNextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700245 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800246 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
247 oldResolvedRoute));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700248 }
249 return;
250 }
251
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700252 if (nextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700253 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700254 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartfd176612016-04-11 10:42:10 -0700255 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700256 }
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 Hartbfc5c482016-04-05 18:57:00 -0700268
269 if (removed != null) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700270 reverseIndex.remove(removed.nextHop(), removed);
Charles Chane4d13102016-11-08 15:38:44 -0800271 NextHopData oldNextHopData = getNextHop(removed.nextHop());
Charles Chanb21d69a2016-11-11 17:46:14 -0800272 // 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 Hartbfc5c482016-04-05 18:57:00 -0700278 }
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 Hartfd176612016-04-11 10:42:10 -0700298 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 Hartbfc5c482016-04-05 18:57:00 -0700309 }
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}