blob: 70bca92b426d46f4baaa408d8b50a0f982766f9c [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))) {
Charles Chane4d13102016-11-08 15:38:44 -0800121 NextHopData oldNextHopData = nextHops.put(ip, nextHopData);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700122
123 for (Route route : routes) {
Charles Chane4d13102016-11-08 15:38:44 -0800124 if (oldNextHopData == 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 Chane4d13102016-11-08 15:38:44 -0800129 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
130 new ResolvedRoute(route, oldNextHopData.mac(), oldNextHopData.location())));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700131 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700132 }
133 }
134 }
135
136 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700137 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
138 if (nextHops.remove(ip, nextHopData)) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700139 Collection<Route> routes = getDefaultRouteTable(ip).getRoutesForNextHop(ip);
140 for (Route route : routes) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700141 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800142 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700143 }
144 }
145 }
146
147 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700148 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700149 return nextHops.get(ip);
150 }
151
Jonathan Hartfd176612016-04-11 10:42:10 -0700152 @Override
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700153 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700154 return ImmutableMap.copyOf(nextHops);
155 }
156
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700157 private RouteTable getDefaultRouteTable(Route route) {
158 return getDefaultRouteTable(route.prefix().address());
159 }
160
161 private RouteTable getDefaultRouteTable(IpAddress ip) {
162 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
163 return routeTables.get(routeTableId);
164 }
165
166 private static String createBinaryString(IpPrefix ipPrefix) {
167 byte[] octets = ipPrefix.address().toOctets();
168 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
169 result.append("0");
170 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
171 int byteOffset = i / Byte.SIZE;
172 int bitOffset = i % Byte.SIZE;
173 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
174 byte value = octets[byteOffset];
175 boolean isSet = ((value & mask) != 0);
176 result.append(isSet ? "1" : "0");
177 }
178
179 return result.toString();
180 }
181
182 /**
183 * Route table into which routes can be placed.
184 */
185 private class RouteTable {
186 private final InvertedRadixTree<Route> routeTable;
187
188 private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>();
189 private final Multimap<IpAddress, Route> reverseIndex =
190 Multimaps.synchronizedMultimap(HashMultimap.create());
191
192 /**
193 * Creates a new route table.
194 */
195 public RouteTable() {
196 routeTable = new ConcurrentInvertedRadixTree<>(
197 new DefaultByteArrayNodeFactory());
198 }
199
200 /**
201 * Adds or updates the route in the route table.
202 *
203 * @param route route to update
204 */
205 public void update(Route route) {
Charles Chane4d13102016-11-08 15:38:44 -0800206 NextHopData oldNextHopData = null;
207
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700208 synchronized (this) {
209 Route oldRoute = routes.put(route.prefix(), route);
210 routeTable.put(createBinaryString(route.prefix()), route);
211
212 // TODO manage routes from multiple providers
Jonathan Hartfd176612016-04-11 10:42:10 -0700213
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700214 reverseIndex.put(route.nextHop(), route);
215
Jonathan Hartfd176612016-04-11 10:42:10 -0700216 if (oldRoute != null) {
217 reverseIndex.remove(oldRoute.nextHop(), oldRoute);
218
219 if (reverseIndex.get(oldRoute.nextHop()).isEmpty()) {
Charles Chane4d13102016-11-08 15:38:44 -0800220 oldNextHopData = nextHops.remove(oldRoute.nextHop());
Jonathan Hartfd176612016-04-11 10:42:10 -0700221 }
222 }
223
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700224 if (route.equals(oldRoute)) {
225 // No need to send events if the new route is the same
226 return;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700227 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700228
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700229 NextHopData nextHopData = nextHops.get(route.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700230
231 if (oldRoute != null && !oldRoute.nextHop().equals(route.nextHop())) {
Charles Chane4d13102016-11-08 15:38:44 -0800232 ResolvedRoute oldResolvedRoute =
233 new ResolvedRoute(oldRoute,
234 (oldNextHopData == null) ? null : oldNextHopData.mac(),
235 (oldNextHopData == null) ? null : oldNextHopData.location());
236
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700237 if (nextHopData == null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700238 // We don't know the new MAC address yet so delete the route
239 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800240 oldResolvedRoute));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700241 } else {
242 // We know the new MAC address so update the route
243 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800244 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location()),
245 oldResolvedRoute));
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700246 }
247 return;
248 }
249
250
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700251 if (nextHopData != null) {
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700252 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700253 new ResolvedRoute(route, nextHopData.mac(), nextHopData.location())));
Jonathan Hartfd176612016-04-11 10:42:10 -0700254 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700255 }
256 }
257
258 /**
259 * Removes the route from the route table.
260 *
261 * @param route route to remove
262 */
263 public void remove(Route route) {
264 synchronized (this) {
265 Route removed = routes.remove(route.prefix());
266 routeTable.remove(createBinaryString(route.prefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700267
268 if (removed != null) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700269 reverseIndex.remove(removed.nextHop(), removed);
Charles Chane4d13102016-11-08 15:38:44 -0800270 NextHopData oldNextHopData = getNextHop(removed.nextHop());
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700271 notifyDelegate(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800272 new ResolvedRoute(route, oldNextHopData.mac(),
273 oldNextHopData.location())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700274 }
275 }
276 }
277
278 /**
279 * Returns the routes pointing to a particular next hop.
280 *
281 * @param ip next hop IP address
282 * @return routes for the next hop
283 */
284 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
285 return reverseIndex.get(ip);
286 }
287
288 /**
289 * Returns all routes in the route table.
290 *
291 * @return all routes
292 */
293 public Collection<Route> getRoutes() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700294 Iterator<KeyValuePair<Route>> it =
295 routeTable.getKeyValuePairsForKeysStartingWith("").iterator();
296
297 List<Route> routes = new LinkedList<>();
298
299 while (it.hasNext()) {
300 KeyValuePair<Route> entry = it.next();
301 routes.add(entry.getValue());
302 }
303
304 return routes;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700305 }
306
307 /**
308 * Performs a longest prefix match with the given IP in the route table.
309 *
310 * @param ip IP address to look up
311 * @return most specific prefix containing the given
312 */
313 public Route longestPrefixMatch(IpAddress ip) {
314 Iterable<Route> prefixes =
315 routeTable.getValuesForKeysPrefixing(createBinaryString(ip.toIpPrefix()));
316
317 Iterator<Route> it = prefixes.iterator();
318
319 Route route = null;
320 while (it.hasNext()) {
321 route = it.next();
322 }
323
324 return route;
325 }
326 }
327
328}