blob: c24f005b90c83b5e21daea1c78c174f118b15aff [file] [log] [blame]
Jonathan Hart96c146b2017-02-24 16:32:00 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart96c146b2017-02-24 16:32:00 -08003 *
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 Hart96c146b2017-02-24 16:32:00 -080018
Jordan Halterman3b137372018-04-30 14:42:41 -070019import java.util.Collection;
20import java.util.Collections;
pier8a86db22019-10-16 16:58:20 +020021import java.util.HashMap;
Jordan Halterman3b137372018-04-30 14:42:41 -070022import java.util.Map;
pier594843a2018-12-21 22:02:25 +010023import java.util.Objects;
Jordan Halterman3b137372018-04-30 14:42:41 -070024import java.util.concurrent.ExecutorService;
25import java.util.function.Consumer;
26import java.util.stream.Collectors;
27
Daniel Ginsburg83b76452018-06-09 01:43:59 +030028import com.google.common.collect.Sets;
Jonathan Hart96c146b2017-02-24 16:32:00 -080029import org.onlab.packet.IpAddress;
30import org.onlab.packet.IpPrefix;
Jordan Halterman5f5ceb62018-10-01 23:17:57 -070031import org.onosproject.cluster.NodeId;
Jonathan Hart96c146b2017-02-24 16:32:00 -080032import org.onlab.util.KryoNamespace;
Ray Milkey69ec8712017-08-08 13:00:43 -070033import org.onosproject.routeservice.InternalRouteEvent;
34import org.onosproject.routeservice.Route;
35import org.onosproject.routeservice.RouteSet;
36import org.onosproject.routeservice.RouteStoreDelegate;
37import org.onosproject.routeservice.RouteTableId;
Jonathan Hart96c146b2017-02-24 16:32:00 -080038import org.onosproject.store.serializers.KryoNamespaces;
Jordan Halterman3b137372018-04-30 14:42:41 -070039import org.onosproject.store.service.ConsistentMultimap;
Jonathan Hart1f67d282017-05-25 14:23:01 -070040import org.onosproject.store.service.DistributedPrimitive;
Jordan Halterman3b137372018-04-30 14:42:41 -070041import org.onosproject.store.service.MultimapEvent;
42import org.onosproject.store.service.MultimapEventListener;
Jonathan Hart96c146b2017-02-24 16:32:00 -080043import org.onosproject.store.service.Serializer;
44import org.onosproject.store.service.StorageService;
45import org.onosproject.store.service.Versioned;
46
pier8a86db22019-10-16 16:58:20 +020047
pier594843a2018-12-21 22:02:25 +010048import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart96c146b2017-02-24 16:32:00 -080049import static com.google.common.base.Preconditions.checkNotNull;
50
51/**
52 * Default implementation of a route table based on a consistent map.
53 */
54public class DefaultRouteTable implements RouteTable {
55
56 private final RouteTableId id;
Jordan Halterman5f5ceb62018-10-01 23:17:57 -070057
58 // The route map stores RawRoute instead of Route to translate the polymorphic IpPrefix and IpAddress types
59 // into monomorphic types (specifically String). Using strings in the stored RawRoute is necessary to ensure
60 // the serialized bytes are consistent whether e.g. IpAddress or Ip4Address is used when storing a route.
61 private final ConsistentMultimap<String, RawRoute> routes;
62
Jonathan Hart96c146b2017-02-24 16:32:00 -080063 private final RouteStoreDelegate delegate;
Jonathan Hart1f67d282017-05-25 14:23:01 -070064 private final ExecutorService executor;
Jonathan Hart96c146b2017-02-24 16:32:00 -080065 private final RouteTableListener listener = new RouteTableListener();
66
Jonathan Hart1f67d282017-05-25 14:23:01 -070067 private final Consumer<DistributedPrimitive.Status> statusChangeListener;
68
Jonathan Hart96c146b2017-02-24 16:32:00 -080069 /**
70 * Creates a new route table.
71 *
72 * @param id route table ID
73 * @param delegate route store delegate to notify of events
74 * @param storageService storage service
Jonathan Hart1f67d282017-05-25 14:23:01 -070075 * @param executor executor service
Jonathan Hart96c146b2017-02-24 16:32:00 -080076 */
77 public DefaultRouteTable(RouteTableId id, RouteStoreDelegate delegate,
Jonathan Hart1f67d282017-05-25 14:23:01 -070078 StorageService storageService, ExecutorService executor) {
Jonathan Hart96c146b2017-02-24 16:32:00 -080079 this.delegate = checkNotNull(delegate);
80 this.id = checkNotNull(id);
81 this.routes = buildRouteMap(checkNotNull(storageService));
Jonathan Hart1f67d282017-05-25 14:23:01 -070082 this.executor = checkNotNull(executor);
Jonathan Hart96c146b2017-02-24 16:32:00 -080083
Jonathan Hart1f67d282017-05-25 14:23:01 -070084 statusChangeListener = status -> {
85 if (status.equals(DistributedPrimitive.Status.ACTIVE)) {
86 executor.execute(this::notifyExistingRoutes);
87 }
88 };
89 routes.addStatusChangeListener(statusChangeListener);
90
91 notifyExistingRoutes();
92
Jordan Halterman3b137372018-04-30 14:42:41 -070093 routes.addListener(listener, executor);
Jonathan Hart1f67d282017-05-25 14:23:01 -070094 }
95
96 private void notifyExistingRoutes() {
Jordan Halterman3b137372018-04-30 14:42:41 -070097 getRoutes().forEach(routeSet -> delegate.notify(
98 new InternalRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, routeSet)));
Jonathan Hart96c146b2017-02-24 16:32:00 -080099 }
100
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700101 private ConsistentMultimap<String, RawRoute> buildRouteMap(StorageService storageService) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800102 KryoNamespace routeTableSerializer = KryoNamespace.newBuilder()
103 .register(KryoNamespaces.API)
104 .register(Route.class)
105 .register(Route.Source.class)
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700106 .register(RawRoute.class)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800107 .build();
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700108 return storageService.<String, RawRoute>consistentMultimapBuilder()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800109 .withName("onos-routes-" + id.name())
110 .withRelaxedReadConsistency()
111 .withSerializer(Serializer.using(routeTableSerializer))
112 .build();
113 }
114
115 @Override
116 public RouteTableId id() {
117 return id;
118 }
119
120 @Override
121 public void shutdown() {
Jonathan Hart1f67d282017-05-25 14:23:01 -0700122 routes.removeStatusChangeListener(statusChangeListener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800123 routes.removeListener(listener);
124 }
125
126 @Override
127 public void destroy() {
128 shutdown();
129 routes.destroy();
130 }
131
132 @Override
133 public void update(Route route) {
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700134 routes.put(route.prefix().toString(), new RawRoute(route));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800135 }
136
137 @Override
pier8a86db22019-10-16 16:58:20 +0200138 public void update(Collection<Route> routesAdded) {
139 Map<String, Collection<? extends RawRoute>> computedRoutes = new HashMap<>();
140 computeRoutesToAdd(routesAdded).forEach((prefix, routes) -> computedRoutes.computeIfAbsent(
141 prefix, k -> Sets.newHashSet(routes)));
142 routes.putAll(computedRoutes);
143 }
144
145 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800146 public void remove(Route route) {
Jordan Haltermanfd822362018-12-22 17:01:18 -0800147 getRoutes(route.prefix())
148 .routes()
149 .stream()
150 .filter(r -> r.equals(route))
151 .findAny()
152 .ifPresent(matchRoute -> {
153 routes.remove(matchRoute.prefix().toString(), new RawRoute(matchRoute));
154 });
Jonathan Hart96c146b2017-02-24 16:32:00 -0800155 }
156
157 @Override
pier8a86db22019-10-16 16:58:20 +0200158 public void remove(Collection<Route> routesRemoved) {
159 Map<String, Collection<? extends RawRoute>> computedRoutes = new HashMap<>();
160 computeRoutesToRemove(routesRemoved).forEach((prefix, routes) -> computedRoutes.computeIfAbsent(
161 prefix, k -> Sets.newHashSet(routes)));
162 routes.removeAll(computedRoutes);
163 }
164
165 @Override
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300166 public void replace(Route route) {
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700167 routes.replaceValues(route.prefix().toString(), Sets.newHashSet(new RawRoute(route)));
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300168 }
169
170 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800171 public Collection<RouteSet> getRoutes() {
Jordan Halterman3b137372018-04-30 14:42:41 -0700172 return routes.stream()
173 .map(Map.Entry::getValue)
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700174 .collect(Collectors.groupingBy(RawRoute::prefix))
Jordan Halterman3b137372018-04-30 14:42:41 -0700175 .entrySet()
176 .stream()
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700177 .map(entry -> new RouteSet(id,
178 IpPrefix.valueOf(entry.getKey()),
179 entry.getValue().stream().map(RawRoute::route).collect(Collectors.toSet())))
Jordan Halterman3b137372018-04-30 14:42:41 -0700180 .collect(Collectors.toList());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800181 }
182
183 @Override
184 public RouteSet getRoutes(IpPrefix prefix) {
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700185 Versioned<Collection<? extends RawRoute>> routeSet = routes.get(prefix.toString());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800186 if (routeSet != null) {
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700187 return new RouteSet(id, prefix, routeSet.value().stream().map(RawRoute::route).collect(Collectors.toSet()));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800188 }
189 return null;
190 }
191
192 @Override
193 public Collection<Route> getRoutesForNextHop(IpAddress nextHop) {
Jordan Halterman3b137372018-04-30 14:42:41 -0700194 return routes.stream()
195 .map(Map.Entry::getValue)
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700196 .filter(r -> IpAddress.valueOf(r.nextHop()).equals(nextHop))
197 .map(RawRoute::route)
Jordan Halterman3b137372018-04-30 14:42:41 -0700198 .collect(Collectors.toSet());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800199 }
200
pier8a86db22019-10-16 16:58:20 +0200201 @Override
202 public Collection<RouteSet> getRoutesForNextHops(Collection<IpAddress> nextHops) {
203 // First create a reduced snapshot of the store iterating one time the map
204 Map<String, Collection<? extends RawRoute>> filteredRouteStore = new HashMap<>();
205 routes.values().stream()
206 .filter(r -> nextHops.contains(IpAddress.valueOf(r.nextHop())))
207 .forEach(r -> filteredRouteStore.computeIfAbsent(r.prefix, k -> {
208 // We need to get all the routes because the resolve logic
209 // will use the alternatives as well
210 Versioned<Collection<? extends RawRoute>> routeSet = routes.get(k);
211 if (routeSet != null) {
212 return routeSet.value();
213 }
214 return null;
215 }));
216 // Return the collection of the routeSet we have to resolve
217 return filteredRouteStore.entrySet().stream()
218 .map(entry -> new RouteSet(id, IpPrefix.valueOf(entry.getKey()),
219 entry.getValue().stream().map(RawRoute::route).collect(Collectors.toSet())))
220 .collect(Collectors.toSet());
221 }
222
223 private Map<String, Collection<RawRoute>> computeRoutesToAdd(Collection<Route> routesAdded) {
224 Map<String, Collection<RawRoute>> computedRoutes = new HashMap<>();
225 routesAdded.forEach(route -> {
226 Collection<RawRoute> tempRoutes = computedRoutes.computeIfAbsent(
227 route.prefix().toString(), k -> Sets.newHashSet());
228 tempRoutes.add(new RawRoute(route));
229 });
230 return computedRoutes;
231 }
232
233 private Map<String, Collection<RawRoute>> computeRoutesToRemove(Collection<Route> routesRemoved) {
234 Map<String, Collection<RawRoute>> computedRoutes = new HashMap<>();
235 routesRemoved.forEach(route -> getRoutes(route.prefix())
236 .routes()
237 .stream()
238 .filter(r -> r.equals(route))
239 .findAny()
240 .ifPresent(matchRoute -> {
241 Collection<RawRoute> tempRoutes = computedRoutes.computeIfAbsent(
242 matchRoute.prefix().toString(), k -> Sets.newHashSet());
243 tempRoutes.add(new RawRoute(matchRoute));
244 }));
245 return computedRoutes;
246 }
247
Jonathan Hart96c146b2017-02-24 16:32:00 -0800248 private class RouteTableListener
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700249 implements MultimapEventListener<String, RawRoute> {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800250
251 private InternalRouteEvent createRouteEvent(
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700252 InternalRouteEvent.Type type, MultimapEvent<String, RawRoute> event) {
253 Collection<? extends RawRoute> currentRoutes = Versioned.valueOrNull(routes.get(event.key()));
Jordan Halterman3b137372018-04-30 14:42:41 -0700254 return new InternalRouteEvent(type, new RouteSet(
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700255 id, IpPrefix.valueOf(event.key()), currentRoutes != null ?
256 currentRoutes.stream().map(RawRoute::route).collect(Collectors.toSet())
257 : Collections.emptySet()));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800258 }
259
260 @Override
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700261 public void event(MultimapEvent<String, RawRoute> event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800262 InternalRouteEvent ire = null;
263 switch (event.type()) {
264 case INSERT:
265 ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, event);
266 break;
Jonathan Hart96c146b2017-02-24 16:32:00 -0800267 case REMOVE:
268 ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_REMOVED, event);
269 break;
270 default:
271 break;
272 }
Jordan Halterman3b137372018-04-30 14:42:41 -0700273 delegate.notify(ire);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800274 }
275 }
Jonathan Hart1f67d282017-05-25 14:23:01 -0700276
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700277 /**
278 * Represents a route object stored in the underlying ConsistentMultimap.
279 */
280 private static class RawRoute {
281 private Route.Source source;
282 private String prefix;
283 private String nextHop;
284 private NodeId sourceNode;
285
286 RawRoute(Route route) {
287 this.source = route.source();
288 this.prefix = route.prefix().toString();
289 this.nextHop = route.nextHop().toString();
290 this.sourceNode = route.sourceNode();
291 }
292
293 String prefix() {
294 return prefix;
295 }
296
297 String nextHop() {
298 return nextHop;
299 }
300
301 Route route() {
302 return new Route(source, IpPrefix.valueOf(prefix), IpAddress.valueOf(nextHop), sourceNode);
303 }
pier594843a2018-12-21 22:02:25 +0100304
305 public int hashCode() {
306 return Objects.hash(prefix, nextHop);
307 }
308
309 @Override
310 public boolean equals(Object other) {
311 if (this == other) {
312 return true;
313 }
314
315 if (!(other instanceof RawRoute)) {
316 return false;
317 }
318
319 RawRoute that = (RawRoute) other;
320
321 return Objects.equals(this.prefix, that.prefix) &&
322 Objects.equals(this.nextHop, that.nextHop);
323 }
324
325 @Override
326 public String toString() {
327 return toStringHelper(this)
328 .add("prefix", prefix)
329 .add("nextHop", nextHop)
330 .toString();
331 }
332
Jordan Halterman5f5ceb62018-10-01 23:17:57 -0700333 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800334}