blob: 78d161048f60aec4bec9d65987658b87f2fe8aff [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;
27import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.packet.IpAddress;
31import org.onlab.packet.IpPrefix;
Charles Chan8fe9f4c2016-10-24 16:46:25 -070032import org.onosproject.incubator.net.routing.NextHopData;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070033import org.onosproject.incubator.net.routing.ResolvedRoute;
34import org.onosproject.incubator.net.routing.Route;
35import org.onosproject.incubator.net.routing.RouteEvent;
36import org.onosproject.incubator.net.routing.RouteStore;
37import org.onosproject.incubator.net.routing.RouteStoreDelegate;
38import org.onosproject.incubator.net.routing.RouteTableId;
39import org.onosproject.store.AbstractStore;
Jonathan Hartfd176612016-04-11 10:42:10 -070040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070042
43import java.util.Collection;
44import java.util.Collections;
45import java.util.Iterator;
Jonathan Hartfd176612016-04-11 10:42:10 -070046import java.util.LinkedList;
47import java.util.List;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070048import java.util.Map;
49import java.util.Set;
50import java.util.concurrent.ConcurrentHashMap;
51
Charles Chanb21d69a2016-11-11 17:46:14 -080052import static com.google.common.base.Preconditions.checkNotNull;
53
Jonathan Hartbfc5c482016-04-05 18:57:00 -070054/**
55 * Route store based on in-memory storage.
56 */
57@Service
58@Component
59public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegate>
60 implements RouteStore {
61
Jonathan Hartfd176612016-04-11 10:42:10 -070062 private Logger log = LoggerFactory.getLogger(getClass());
63
Jonathan Hartbfc5c482016-04-05 18:57:00 -070064 private Map<RouteTableId, RouteTable> routeTables;
65 private static final RouteTableId IPV4 = new RouteTableId("ipv4");
66 private static final RouteTableId IPV6 = new RouteTableId("ipv6");
67
Charles Chan8fe9f4c2016-10-24 16:46:25 -070068 private Map<IpAddress, NextHopData> nextHops = new ConcurrentHashMap<>();
Jonathan Hartbfc5c482016-04-05 18:57:00 -070069
70 @Activate
Jonathan Hart6c2e7962016-04-11 13:54:09 -070071 public void activate() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070072 routeTables = new ConcurrentHashMap<>();
73
74 routeTables.put(IPV4, new RouteTable());
75 routeTables.put(IPV6, new RouteTable());
76 }
77
78 @Override
79 public void updateRoute(Route route) {
80 getDefaultRouteTable(route).update(route);
81 }
82
83 @Override
84 public void removeRoute(Route route) {
85 RouteTable table = getDefaultRouteTable(route);
86 table.remove(route);
Jonathan Hartfd176612016-04-11 10:42:10 -070087 Collection<Route> routes = table.getRoutesForNextHop(route.nextHop());
88
89 if (routes.isEmpty()) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070090 nextHops.remove(route.nextHop());
91 }
92 }
93
94 @Override
95 public Set<RouteTableId> getRouteTables() {
96 return routeTables.keySet();
97 }
98
99 @Override
100 public Collection<Route> getRoutes(RouteTableId table) {
101 RouteTable routeTable = routeTables.get(table);
102 if (routeTable == null) {
103 return Collections.emptySet();
104 }
105 return routeTable.getRoutes();
106 }
107
108 @Override
109 public Route longestPrefixMatch(IpAddress ip) {
110 return getDefaultRouteTable(ip).longestPrefixMatch(ip);
111 }
112
113 @Override
Jonathan Hartfd176612016-04-11 10:42:10 -0700114 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
115 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
116 }
117
118 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700119 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800120 checkNotNull(ip);
121 checkNotNull(nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700122 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
Jonathan Hartfd176612016-04-11 10:42:10 -0700123
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700124 if (!routes.isEmpty() && !nextHopData.equals(nextHops.get(ip))) {
Charles Chane4d13102016-11-08 15:38:44 -0800125 NextHopData oldNextHopData = nextHops.put(ip, nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700126
127 for (Route route : routes) {
Charles Chane4d13102016-11-08 15:38:44 -0800128 if (oldNextHopData == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700129 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700130 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700131 } else {
132 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800133 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
134 new ResolvedRoute(route, oldNextHopData.mac(), oldNextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700135 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700136 }
137 }
138 }
139
140 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700141 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800142 checkNotNull(ip);
143 checkNotNull(nextHopData);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700144 if (nextHops.remove(ip, nextHopData)) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700145 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
146 for (Route route : routes) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700147 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800148 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700149 }
150 }
151 }
152
153 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700154 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700155 return nextHops.get(ip);
156 }
157
Jonathan Hartfd176612016-04-11 10:42:10 -0700158 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700159 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700160 return ImmutableMap.copyOf(nextHops);
161 }
162
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700163 private RouteTable getDefaultRouteTable(Route route) {
164 return getDefaultRouteTable(route.prefix().address());
165 }
166
167 private RouteTable getDefaultRouteTable(IpAddress ip) {
168 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
169 return routeTables.get(routeTableId);
170 }
171
172 private static String createBinaryString(IpPrefix ipPrefix) {
173 byte[] octets = ipPrefix.address().toOctets();
174 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
175 result.append("0");
176 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
177 int byteOffset = i / Byte.SIZE;
178 int bitOffset = i % Byte.SIZE;
179 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
180 byte value = octets[byteOffset];
181 boolean isSet = ((value & mask) != 0);
182 result.append(isSet ? "1" : "0");
183 }
184
185 return result.toString();
186 }
187
188 /**
189 * Route table into which routes can be placed.
190 */
191 private class RouteTable {
192 private final InvertedRadixTree<Route> routeTable;
193
194 private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>();
195 private final Multimap<IpAddress, Route> reverseIndex =
196 Multimaps.synchronizedMultimap(HashMultimap.create());
197
198 /**
199 * Creates a new route table.
200 */
201 public RouteTable() {
202 routeTable = new ConcurrentInvertedRadixTree<>(
203 new DefaultByteArrayNodeFactory());
204 }
205
206 /**
207 * Adds or updates the route in the route table.
208 *
209 * @param route route to update
210 */
211 public void update(Route route) {
212 synchronized (this) {
213 Route oldRoute = routes.put(route.prefix(), route);
Charles Chanb21d69a2016-11-11 17:46:14 -0800214
215 // No need to proceed if the new route is the same
216 if (route.equals(oldRoute)) {
217 return;
218 }
219
220 NextHopData oldNextHopData = null;
221 ResolvedRoute oldResolvedRoute = null;
222 if (oldRoute != null) {
223 oldNextHopData = nextHops.get(oldRoute.nextHop());
224 if (oldNextHopData != null) {
225 oldResolvedRoute = new ResolvedRoute(oldRoute,
226 oldNextHopData.mac(), oldNextHopData.location());
227 }
228 }
229
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700230 routeTable.put(createBinaryString(route.prefix()), route);
231
232 // TODO manage routes from multiple providers
Jonathan Hartfd176612016-04-11 10:42:10 -0700233
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700234 reverseIndex.put(route.nextHop(), route);
235
Jonathan Hartfd176612016-04-11 10:42:10 -0700236 if (oldRoute != null) {
237 reverseIndex.remove(oldRoute.nextHop(), oldRoute);
238
239 if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800240 nextHops.remove(oldRoute.nextHop());
Jonathan Hartfd176612016-04-11 10:42:10 -0700241 }
242 }
243
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700244 NextHopData nextHopData = nextHops.get(route.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700245
246 if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) {
Charles Chanb21d69a2016-11-11 17:46:14 -0800247 // We don't know the new MAC address yet so delete the route
248 // Don't send ROUTE_REMOVED if the route was unresolved
249 if (nextHopData == null && oldNextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700250 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800251 oldResolvedRoute));
Charles Chanb21d69a2016-11-11 17:46:14 -0800252 // We know the new MAC address so update the route
253 } else if (nextHopData != null && oldNextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700254 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800255 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
256 oldResolvedRoute));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700257 }
258 return;
259 }
260
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700261 if (nextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700262 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700263 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartfd176612016-04-11 10:42:10 -0700264 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700265 }
266 }
267
268 /**
269 * Removes the route from the route table.
270 *
271 * @param route route to remove
272 */
273 public void remove(Route route) {
274 synchronized (this) {
275 Route removed = routes.remove(route.prefix());
276 routeTable.remove(createBinaryString(route.prefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700277
278 if (removed != null) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700279 reverseIndex.remove(removed.nextHop(), removed);
Charles Chane4d13102016-11-08 15:38:44 -0800280 NextHopData oldNextHopData = getNextHop(removed.nextHop());
Charles Chanb21d69a2016-11-11 17:46:14 -0800281 // Don't send ROUTE_REMOVED if the route was unresolved
282 if (oldNextHopData != null) {
283 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
284 new ResolvedRoute(route, oldNextHopData.mac(),
285 oldNextHopData.location())));
286 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700287 }
288 }
289 }
290
291 /**
292 * Returns the routes pointing to a particular next hop.
293 *
294 * @param ip next hop IP address
295 * @return routes for the next hop
296 */
297 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
298 return reverseIndex.get(ip);
299 }
300
301 /**
302 * Returns all routes in the route table.
303 *
304 * @return all routes
305 */
306 public Collection<Route> getRoutes() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700307 Iterator<KeyValuePair<Route>> it =
308 routeTable.getKeyValuePairsForKeysStartingWith("").iterator();
309
310 List<Route> routes = new LinkedList<>();
311
312 while (it.hasNext()) {
313 KeyValuePair<Route> entry = it.next();
314 routes.add(entry.getValue());
315 }
316
317 return routes;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700318 }
319
320 /**
321 * Performs a longest prefix match with the given IP in the route table.
322 *
323 * @param ip IP address to look up
324 * @return most specific prefix containing the given
325 */
326 public Route longestPrefixMatch(IpAddress ip) {
327 Iterable<Route> prefixes =
328 routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix()));
329
330 Iterator<Route> it = prefixes.iterator();
331
332 Route route = null;
333 while (it.hasNext()) {
334 route = it.next();
335 }
336
337 return route;
338 }
339 }
340
341}