blob: a1e31201c9581931dd38a54d0cca30188ad6eb14 [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
52/**
53 * Route store based on in-memory storage.
54 */
55@Service
56@Component
57public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegate>
58 implements RouteStore {
59
Jonathan Hartfd176612016-04-11 10:42:10 -070060 private Logger log = LoggerFactory.getLogger(getClass());
61
Jonathan Hartbfc5c482016-04-05 18:57:00 -070062 private Map<RouteTableId, RouteTable> routeTables;
63 private static final RouteTableId IPV4 = new RouteTableId("ipv4");
64 private static final RouteTableId IPV6 = new RouteTableId("ipv6");
65
Charles Chan8fe9f4c2016-10-24 16:46:25 -070066 private Map<IpAddress, NextHopData> nextHops = new ConcurrentHashMap<>();
Jonathan Hartbfc5c482016-04-05 18:57:00 -070067
68 @Activate
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());
74 }
75
76 @Override
77 public void updateRoute(Route route) {
78 getDefaultRouteTable(route).update(route);
79 }
80
81 @Override
82 public void removeRoute(Route route) {
83 RouteTable table = getDefaultRouteTable(route);
84 table.remove(route);
Jonathan Hartfd176612016-04-11 10:42:10 -070085 Collection<Route> routes = table.getRoutesForNextHop(route.nextHop());
86
87 if (routes.isEmpty()) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070088 nextHops.remove(route.nextHop());
89 }
90 }
91
92 @Override
93 public Set<RouteTableId> getRouteTables() {
94 return routeTables.keySet();
95 }
96
97 @Override
98 public Collection<Route> getRoutes(RouteTableId table) {
99 RouteTable routeTable = routeTables.get(table);
100 if (routeTable == null) {
101 return Collections.emptySet();
102 }
103 return routeTable.getRoutes();
104 }
105
106 @Override
107 public Route longestPrefixMatch(IpAddress ip) {
108 return getDefaultRouteTable(ip).longestPrefixMatch(ip);
109 }
110
111 @Override
Jonathan Hartfd176612016-04-11 10:42:10 -0700112 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
113 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
114 }
115
116 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700117 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700118 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
Jonathan Hartfd176612016-04-11 10:42:10 -0700119
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700120 if (!routes.isEmpty() && !nextHopData.equals(nextHops.get(ip))) {
121 NextHopData oldNextHop = nextHops.put(ip, nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700122
123 for (Route route : routes) {
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700124 if (oldNextHop == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700125 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700126 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700127 } else {
128 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700129 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700130 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700131 }
132 }
133 }
134
135 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700136 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
137 if (nextHops.remove(ip, nextHopData)) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700138 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
139 for (Route route : routes) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700140 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700141 new ResolvedRoute(route, null, null)));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700142 }
143 }
144 }
145
146 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700147 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700148 return nextHops.get(ip);
149 }
150
Jonathan Hartfd176612016-04-11 10:42:10 -0700151 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700152 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700153 return ImmutableMap.copyOf(nextHops);
154 }
155
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700156 private RouteTable getDefaultRouteTable(Route route) {
157 return getDefaultRouteTable(route.prefix().address());
158 }
159
160 private RouteTable getDefaultRouteTable(IpAddress ip) {
161 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
162 return routeTables.get(routeTableId);
163 }
164
165 private static String createBinaryString(IpPrefix ipPrefix) {
166 byte[] octets = ipPrefix.address().toOctets();
167 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
168 result.append("0");
169 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
170 int byteOffset = i / Byte.SIZE;
171 int bitOffset = i % Byte.SIZE;
172 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
173 byte value = octets[byteOffset];
174 boolean isSet = ((value & mask) != 0);
175 result.append(isSet ? "1" : "0");
176 }
177
178 return result.toString();
179 }
180
181 /**
182 * Route table into which routes can be placed.
183 */
184 private class RouteTable {
185 private final InvertedRadixTree<Route> routeTable;
186
187 private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>();
188 private final Multimap<IpAddress, Route> reverseIndex =
189 Multimaps.synchronizedMultimap(HashMultimap.create());
190
191 /**
192 * Creates a new route table.
193 */
194 public RouteTable() {
195 routeTable = new ConcurrentInvertedRadixTree<>(
196 new DefaultByteArrayNodeFactory());
197 }
198
199 /**
200 * Adds or updates the route in the route table.
201 *
202 * @param route route to update
203 */
204 public void update(Route route) {
205 synchronized (this) {
206 Route oldRoute = routes.put(route.prefix(), route);
207 routeTable.put(createBinaryString(route.prefix()), route);
208
209 // TODO manage routes from multiple providers
Jonathan Hartfd176612016-04-11 10:42:10 -0700210
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700211 reverseIndex.put(route.nextHop(), route);
212
Jonathan Hartfd176612016-04-11 10:42:10 -0700213 if (oldRoute != null) {
214 reverseIndex.remove(oldRoute.nextHop(), oldRoute);
215
216 if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) {
217 nextHops.remove(oldRoute.nextHop());
218 }
219 }
220
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700221 if (route.equals(oldRoute)) {
222 // No need to send events if the new route is the same
223 return;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700224 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700225
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700226 NextHopData nextHopData = nextHops.get(route.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700227
228 if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) {
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700229 if (nextHopData == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700230 // We don't know the new MAC address yet so delete the route
231 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700232 new ResolvedRoute(oldRoute, null, null)));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700233 } else {
234 // We know the new MAC address so update the route
235 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700236 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700237 }
238 return;
239 }
240
241
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700242 if (nextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700243 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700244 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartfd176612016-04-11 10:42:10 -0700245 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700246 }
247 }
248
249 /**
250 * Removes the route from the route table.
251 *
252 * @param route route to remove
253 */
254 public void remove(Route route) {
255 synchronized (this) {
256 Route removed = routes.remove(route.prefix());
257 routeTable.remove(createBinaryString(route.prefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700258
259 if (removed != null) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700260 reverseIndex.remove(removed.nextHop(), removed);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700261 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700262 new ResolvedRoute(route, null, null)));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700263 }
264 }
265 }
266
267 /**
268 * Returns the routes pointing to a particular next hop.
269 *
270 * @param ip next hop IP address
271 * @return routes for the next hop
272 */
273 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
274 return reverseIndex.get(ip);
275 }
276
277 /**
278 * Returns all routes in the route table.
279 *
280 * @return all routes
281 */
282 public Collection<Route> getRoutes() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700283 Iterator<KeyValuePair<Route>> it =
284 routeTable.getKeyValuePairsForKeysStartingWith("").iterator();
285
286 List<Route> routes = new LinkedList<>();
287
288 while (it.hasNext()) {
289 KeyValuePair<Route> entry = it.next();
290 routes.add(entry.getValue());
291 }
292
293 return routes;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700294 }
295
296 /**
297 * Performs a longest prefix match with the given IP in the route table.
298 *
299 * @param ip IP address to look up
300 * @return most specific prefix containing the given
301 */
302 public Route longestPrefixMatch(IpAddress ip) {
303 Iterable<Route> prefixes =
304 routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix()));
305
306 Iterator<Route> it = prefixes.iterator();
307
308 Route route = null;
309 while (it.hasNext()) {
310 route = it.next();
311 }
312
313 return route;
314 }
315 }
316
317}