blob: 78847832719f854167b3db1d8dfa2c7245290006 [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
90 public Set<RouteTableId> getRouteTables() {
91 return routeTables.keySet();
92 }
93
94 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -080095 public Collection<RouteSet> getRoutes(RouteTableId table) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070096 RouteTable routeTable = routeTables.get(table);
Jonathan Hart96c146b2017-02-24 16:32:00 -080097 if (routeTable != null) {
98 return routeTable.getRouteSets();
Jonathan Hartbfc5c482016-04-05 18:57:00 -070099 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800100 return null;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700101 }
102
103 @Override
Jonathan Hartfd176612016-04-11 10:42:10 -0700104 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
105 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
106 }
107
108 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800109 public RouteSet getRoutes(IpPrefix prefix) {
110 return getDefaultRouteTable(prefix.address()).getRoutes(prefix);
111 }
112
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700113 private RouteTable getDefaultRouteTable(Route route) {
114 return getDefaultRouteTable(route.prefix().address());
115 }
116
117 private RouteTable getDefaultRouteTable(IpAddress ip) {
118 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
119 return routeTables.get(routeTableId);
120 }
121
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700122 /**
123 * Route table into which routes can be placed.
124 */
125 private class RouteTable {
126 private final InvertedRadixTree<Route> routeTable;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700127 private final Map<IpPrefix, Route> routes = new ConcurrentHashMap<>();
Jonathan Hart96c146b2017-02-24 16:32:00 -0800128 private final RouteTableId id;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700129
130 /**
131 * Creates a new route table.
132 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800133 public RouteTable(RouteTableId id) {
134 this.id = checkNotNull(id);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700135 routeTable = new ConcurrentInvertedRadixTree<>(
136 new DefaultByteArrayNodeFactory());
137 }
138
139 /**
140 * Adds or updates the route in the route table.
141 *
142 * @param route route to update
143 */
144 public void update(Route route) {
145 synchronized (this) {
146 Route oldRoute = routes.put(route.prefix(), route);
Charles Chanb21d69a2016-11-11 17:46:14 -0800147
148 // No need to proceed if the new route is the same
149 if (route.equals(oldRoute)) {
150 return;
151 }
152
Ray Milkey69ec8712017-08-08 13:00:43 -0700153 routeTable.put(RouteTools.createBinaryString(route.prefix()), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700154
Jonathan Hart96c146b2017-02-24 16:32:00 -0800155 notifyDelegate(new InternalRouteEvent(
156 InternalRouteEvent.Type.ROUTE_ADDED, singletonRouteSet(route)));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700157 }
158 }
159
160 /**
161 * Removes the route from the route table.
162 *
163 * @param route route to remove
164 */
165 public void remove(Route route) {
166 synchronized (this) {
167 Route removed = routes.remove(route.prefix());
Ray Milkey69ec8712017-08-08 13:00:43 -0700168 routeTable.remove(RouteTools.createBinaryString(route.prefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700169
170 if (removed != null) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800171 notifyDelegate(new InternalRouteEvent(
172 InternalRouteEvent.Type.ROUTE_REMOVED, emptyRouteSet(route.prefix())));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700173 }
174 }
175 }
176
177 /**
178 * Returns the routes pointing to a particular next hop.
179 *
180 * @param ip next hop IP address
181 * @return routes for the next hop
182 */
183 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800184 return routes.values()
185 .stream()
186 .filter(route -> route.nextHop().equals(ip))
187 .collect(Collectors.toSet());
188 }
189
190 public RouteSet getRoutes(IpPrefix prefix) {
191 Route route = routes.get(prefix);
192 if (route != null) {
193 return singletonRouteSet(route);
194 }
195 return null;
196 }
197
198 public Collection<RouteSet> getRouteSets() {
199 return routes.values().stream()
200 .map(this::singletonRouteSet)
201 .collect(Collectors.toSet());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700202 }
203
204 /**
205 * Returns all routes in the route table.
206 *
207 * @return all routes
208 */
209 public Collection<Route> getRoutes() {
Jonathan Hartfd176612016-04-11 10:42:10 -0700210 Iterator<KeyValuePair<Route>> it =
211 routeTable.getKeyValuePairsForKeysStartingWith("").iterator();
212
213 List<Route> routes = new LinkedList<>();
214
215 while (it.hasNext()) {
216 KeyValuePair<Route> entry = it.next();
217 routes.add(entry.getValue());
218 }
219
220 return routes;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700221 }
222
223 /**
224 * Performs a longest prefix match with the given IP in the route table.
225 *
226 * @param ip IP address to look up
227 * @return most specific prefix containing the given
228 */
229 public Route longestPrefixMatch(IpAddress ip) {
230 Iterable<Route> prefixes =
Ray Milkey69ec8712017-08-08 13:00:43 -0700231 routeTable.getValuesForKeysPrefixing(RouteTools.createBinaryString(ip.toIpPrefix()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700232
233 Iterator<Route> it = prefixes.iterator();
234
235 Route route = null;
236 while (it.hasNext()) {
237 route = it.next();
238 }
239
240 return route;
241 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800242
243 private RouteSet singletonRouteSet(Route route) {
244 return new RouteSet(id, route.prefix(), Collections.singleton(route));
245 }
246
247 private RouteSet emptyRouteSet(IpPrefix prefix) {
248 return new RouteSet(id, prefix, Collections.emptySet());
249 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700250 }
251
252}