blob: 04b0f380309f6d89b171312df45336ff2b8a1234 [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice.store;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070018
Jonathan Hartfd176612016-04-11 10:42:10 -070019import com.googlecode.concurrenttrees.common.KeyValuePair;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070020import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
21import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
22import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070023import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
Ray Milkey69ec8712017-08-08 13:00:43 -070025import org.onosproject.routeservice.InternalRouteEvent;
26import org.onosproject.routeservice.Route;
27import org.onosproject.routeservice.RouteSet;
28import org.onosproject.routeservice.RouteStore;
29import org.onosproject.routeservice.RouteStoreDelegate;
30import org.onosproject.routeservice.RouteTableId;
31import org.onosproject.routeservice.RouteTools;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070032import org.onosproject.store.AbstractStore;
Jonathan Hartfd176612016-04-11 10:42:10 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070035
36import java.util.Collection;
37import java.util.Collections;
38import java.util.Iterator;
Jonathan Hartfd176612016-04-11 10:42:10 -070039import java.util.LinkedList;
40import java.util.List;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070041import java.util.Map;
42import java.util.Set;
43import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart96c146b2017-02-24 16:32:00 -080044import java.util.stream.Collectors;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070045
Charles Chanb21d69a2016-11-11 17:46:14 -080046import static com.google.common.base.Preconditions.checkNotNull;
47
Jonathan Hartbfc5c482016-04-05 18:57:00 -070048/**
49 * Route store based on in-memory storage.
50 */
Jonathan Hart96c146b2017-02-24 16:32:00 -080051public class LocalRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Jonathan Hartbfc5c482016-04-05 18:57:00 -070052 implements RouteStore {
53
Jonathan Hartfd176612016-04-11 10:42:10 -070054 private Logger log = LoggerFactory.getLogger(getClass());
55
Jonathan Hartbfc5c482016-04-05 18:57:00 -070056 private Map<RouteTableId, RouteTable> routeTables;
57 private static final RouteTableId IPV4 = new RouteTableId("ipv4");
58 private static final RouteTableId IPV6 = new RouteTableId("ipv6");
59
Charles Chan0214ded2016-11-18 17:48:37 -080060 /**
61 * Sets up local route store.
62 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -070063 public void activate() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070064 routeTables = new ConcurrentHashMap<>();
65
Jonathan Hart96c146b2017-02-24 16:32:00 -080066 routeTables.put(IPV4, new RouteTable(IPV4));
67 routeTables.put(IPV6, new RouteTable(IPV6));
Charles Chan0214ded2016-11-18 17:48:37 -080068
69 log.info("Started");
70 }
71
72 /**
Jonathan Hart96c146b2017-02-24 16:32:00 -080073 * Cleans up local route store.
Charles Chan0214ded2016-11-18 17:48:37 -080074 */
75 public void deactivate() {
76 log.info("Stopped");
Jonathan Hartbfc5c482016-04-05 18:57:00 -070077 }
78
79 @Override
80 public void updateRoute(Route route) {
81 getDefaultRouteTable(route).update(route);
82 }
83
84 @Override
85 public void removeRoute(Route route) {
Jonathan Hart96c146b2017-02-24 16:32:00 -080086 getDefaultRouteTable(route).remove(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -070087 }
88
89 @Override
Daniel Ginsburg83b76452018-06-09 01:43:59 +030090 public void replaceRoute(Route route) {
91 getDefaultRouteTable(route).replace(route);
92 }
93
94 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -070095 public Set<RouteTableId> getRouteTables() {
96 return routeTables.keySet();
97 }
98
99 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800100 public Collection<RouteSet> getRoutes(RouteTableId table) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700101 RouteTable routeTable = routeTables.get(table);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800102 if (routeTable != null) {
103 return routeTable.getRouteSets();
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700104 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800105 return null;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700106 }
107
108 @Override
Jonathan Hartfd176612016-04-11 10:42:10 -0700109 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
110 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
111 }
112
113 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800114 public RouteSet getRoutes(IpPrefix prefix) {
115 return getDefaultRouteTable(prefix.address()).getRoutes(prefix);
116 }
117
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700118 private RouteTable getDefaultRouteTable(Route route) {
119 return getDefaultRouteTable(route.prefix().address());
120 }
121
122 private RouteTable getDefaultRouteTable(IpAddress ip) {
123 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
124 return routeTables.get(routeTableId);
125 }
126
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700127 /**
128 * Route table into which routes can be placed.
129 */
130 private class RouteTable {
131 private final InvertedRadixTree<Route> routeTable;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700132 private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>();
Jonathan Hart96c146b2017-02-24 16:32:00 -0800133 private final RouteTableId id;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700134
135 /**
136 * Creates a new route table.
137 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800138 public RouteTable(RouteTableId id) {
139 this.id = checkNotNull(id);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700140 routeTable = new ConcurrentInvertedRadixTree<>(
141 new DefaultByteArrayNodeFactory());
142 }
143
144 /**
145 * Adds or updates the route in the route table.
146 *
147 * @param route route to update
148 */
149 public void update(Route route) {
150 synchronized (this) {
151 Route oldRoute = routes.put(route.prefix(), route);
Charles Chanb21d69a2016-11-11 17:46:14 -0800152
153 // No need to proceed if the new route is the same
154 if (route.equals(oldRoute)) {
155 return;
156 }
157
Ray Milkey69ec8712017-08-08 13:00:43 -0700158 routeTable.put(RouteTools.createBinaryString(route.prefix()), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700159
Jonathan Hart96c146b2017-02-24 16:32:00 -0800160 notifyDelegate(new InternalRouteEvent(
161 InternalRouteEvent.Type.ROUTE_ADDED, singletonRouteSet(route)));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700162 }
163 }
164
165 /**
166 * Removes the route from the route table.
167 *
168 * @param route route to remove
169 */
170 public void remove(Route route) {
171 synchronized (this) {
172 Route removed = routes.remove(route.prefix());
Ray Milkey69ec8712017-08-08 13:00:43 -0700173 routeTable.remove(RouteTools.createBinaryString(route.prefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700174
175 if (removed != null) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800176 notifyDelegate(new InternalRouteEvent(
177 InternalRouteEvent.Type.ROUTE_REMOVED, emptyRouteSet(route.prefix())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700178 }
179 }
180 }
181
182 /**
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300183 * Replace the route in the route table.
184 */
185 public void replace(Route route) {
186 update(route);
187 }
188
189 /**
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700190 * Returns the routes pointing to a particular next hop.
191 *
192 * @param ip next hop IP address
193 * @return routes for the next hop
194 */
195 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800196 return routes.values()
197 .stream()
198 .filter(route -> route.nextHop().equals(ip))
199 .collect(Collectors.toSet());
200 }
201
202 public RouteSet getRoutes(IpPrefix prefix) {
203 Route route = routes.get(prefix);
204 if (route != null) {
205 return singletonRouteSet(route);
206 }
207 return null;
208 }
209
210 public Collection<RouteSet> getRouteSets() {
211 return routes.values().stream()
212 .map(this::singletonRouteSet)
213 .collect(Collectors.toSet());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700214 }
215
216 /**
217 * Returns all routes in the route table.
218 *
219 * @return all routes
220 */
221 public Collection<Route> getRoutes() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700222 Iterator<KeyValuePair<Route>> it =
223 routeTable.getKeyValuePairsForKeysStartingWith("").iterator();
224
225 List<Route> routes = new LinkedList<>();
226
227 while (it.hasNext()) {
228 KeyValuePair<Route> entry = it.next();
229 routes.add(entry.getValue());
230 }
231
232 return routes;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700233 }
234
235 /**
236 * Performs a longest prefix match with the given IP in the route table.
237 *
238 * @param ip IP address to look up
239 * @return most specific prefix containing the given
240 */
241 public Route longestPrefixMatch(IpAddress ip) {
242 Iterable<Route> prefixes =
Ray Milkey69ec8712017-08-08 13:00:43 -0700243 routeTable.getValuesForKeysPrefixing(RouteTools.createBinaryString(ip.toIpPrefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700244
245 Iterator<Route> it = prefixes.iterator();
246
247 Route route = null;
248 while (it.hasNext()) {
249 route = it.next();
250 }
251
252 return route;
253 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800254
255 private RouteSet singletonRouteSet(Route route) {
256 return new RouteSet(id, route.prefix(), Collections.singleton(route));
257 }
258
259 private RouteSet emptyRouteSet(IpPrefix prefix) {
260 return new RouteSet(id, prefix, Collections.emptySet());
261 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700262 }
263
264}