blob: 991c2392ba8d5ca301eae765c26ea77ce4d06d4d [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;
50
Jonathan Hartbfc5c482016-04-05 18:57:00 -070051/**
52 * Route store based on in-memory storage.
53 */
Jonathan Hartbfc5c482016-04-05 18:57:00 -070054public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegate>
55 implements RouteStore {
56
Jonathan Hartfd176612016-04-11 10:42:10 -070057 private Logger log = LoggerFactory.getLogger(getClass());
58
Jonathan Hartbfc5c482016-04-05 18:57:00 -070059 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 Chan8fe9f4c2016-10-24 16:46:25 -070063 private Map<IpAddress, NextHopData> nextHops = new ConcurrentHashMap<>();
Jonathan Hartbfc5c482016-04-05 18:57:00 -070064
Charles Chan0214ded2016-11-18 17:48:37 -080065 /**
66 * Sets up local route store.
67 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -070068 public void activate() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070069 routeTables = new ConcurrentHashMap<>();
70
71 routeTables.put(IPV4, new RouteTable());
72 routeTables.put(IPV6, new RouteTable());
Charles Chan0214ded2016-11-18 17:48:37 -080073
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 Hartbfc5c482016-04-05 18:57:00 -070082 }
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 Hartfd176612016-04-11 10:42:10 -070093 Collection<Route> routes = table.getRoutesForNextHop(route.nextHop());
94
95 if (routes.isEmpty()) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070096 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 Hartfd176612016-04-11 10:42:10 -0700120 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
121 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
122 }
123
124 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700125 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800126 checkNotNull(ip);
127 checkNotNull(nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700128 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
Jonathan Hartfd176612016-04-11 10:42:10 -0700129
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700130 if (!routes.isEmpty() && !nextHopData.equals(nextHops.get(ip))) {
Charles Chane4d13102016-11-08 15:38:44 -0800131 NextHopData oldNextHopData = nextHops.put(ip, nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700132
133 for (Route route : routes) {
Charles Chane4d13102016-11-08 15:38:44 -0800134 if (oldNextHopData == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700135 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700136 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700137 } else {
138 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800139 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
140 new ResolvedRoute(route, oldNextHopData.mac(), oldNextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700141 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700142 }
143 }
144 }
145
146 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700147 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800148 checkNotNull(ip);
149 checkNotNull(nextHopData);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700150 if (nextHops.remove(ip, nextHopData)) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700151 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
152 for (Route route : routes) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700153 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800154 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700155 }
156 }
157 }
158
159 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700160 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700161 return nextHops.get(ip);
162 }
163
Jonathan Hartfd176612016-04-11 10:42:10 -0700164 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700165 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700166 return ImmutableMap.copyOf(nextHops);
167 }
168
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700169 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 Chanb21d69a2016-11-11 17:46:14 -0800220
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 Hartbfc5c482016-04-05 18:57:00 -0700236 routeTable.put(createBinaryString(route.prefix()), route);
237
238 // TODO manage routes from multiple providers
Jonathan Hartfd176612016-04-11 10:42:10 -0700239
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700240 reverseIndex.put(route.nextHop(), route);
241
Jonathan Hartfd176612016-04-11 10:42:10 -0700242 if (oldRoute != null) {
243 reverseIndex.remove(oldRoute.nextHop(), oldRoute);
244
245 if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800246 nextHops.remove(oldRoute.nextHop());
Jonathan Hartfd176612016-04-11 10:42:10 -0700247 }
248 }
249
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700250 NextHopData nextHopData = nextHops.get(route.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700251
252 if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800253 // 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 Hart7f2d5742016-04-13 16:22:50 -0700256 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800257 oldResolvedRoute));
Charles Chanb21d69a2016-11-11 17:46:14 -0800258 // We know the new MAC address so update the route
259 } else if (nextHopData != null && oldNextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700260 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800261 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
262 oldResolvedRoute));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700263 }
264 return;
265 }
266
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700267 if (nextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700268 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700269 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartfd176612016-04-11 10:42:10 -0700270 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700271 }
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 Hartbfc5c482016-04-05 18:57:00 -0700283
284 if (removed != null) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700285 reverseIndex.remove(removed.nextHop(), removed);
Charles Chane4d13102016-11-08 15:38:44 -0800286 NextHopData oldNextHopData = getNextHop(removed.nextHop());
Charles Chanb21d69a2016-11-11 17:46:14 -0800287 // 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 Hartbfc5c482016-04-05 18:57:00 -0700293 }
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 Hartfd176612016-04-11 10:42:10 -0700313 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 Hartbfc5c482016-04-05 18:57:00 -0700324 }
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}