Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 3 | * |
| 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 Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.store; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 18 | |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Map; |
pier | 594843a | 2018-12-21 22:02:25 +0100 | [diff] [blame] | 22 | import java.util.Objects; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ExecutorService; |
| 24 | import java.util.function.Consumer; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 27 | import com.google.common.collect.Sets; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 31 | import org.onlab.util.KryoNamespace; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 32 | import org.onosproject.routeservice.InternalRouteEvent; |
| 33 | import org.onosproject.routeservice.Route; |
| 34 | import org.onosproject.routeservice.RouteSet; |
| 35 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 36 | import org.onosproject.routeservice.RouteTableId; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 37 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 38 | import org.onosproject.store.service.ConsistentMultimap; |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.DistributedPrimitive; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 40 | import org.onosproject.store.service.MultimapEvent; |
| 41 | import org.onosproject.store.service.MultimapEventListener; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 42 | import org.onosproject.store.service.Serializer; |
| 43 | import org.onosproject.store.service.StorageService; |
| 44 | import org.onosproject.store.service.Versioned; |
| 45 | |
pier | 594843a | 2018-12-21 22:02:25 +0100 | [diff] [blame] | 46 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | |
| 49 | /** |
| 50 | * Default implementation of a route table based on a consistent map. |
| 51 | */ |
| 52 | public class DefaultRouteTable implements RouteTable { |
| 53 | |
| 54 | private final RouteTableId id; |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 55 | |
| 56 | // The route map stores RawRoute instead of Route to translate the polymorphic IpPrefix and IpAddress types |
| 57 | // into monomorphic types (specifically String). Using strings in the stored RawRoute is necessary to ensure |
| 58 | // the serialized bytes are consistent whether e.g. IpAddress or Ip4Address is used when storing a route. |
| 59 | private final ConsistentMultimap<String, RawRoute> routes; |
| 60 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 61 | private final RouteStoreDelegate delegate; |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 62 | private final ExecutorService executor; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 63 | private final RouteTableListener listener = new RouteTableListener(); |
| 64 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 65 | private final Consumer<DistributedPrimitive.Status> statusChangeListener; |
| 66 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Creates a new route table. |
| 69 | * |
| 70 | * @param id route table ID |
| 71 | * @param delegate route store delegate to notify of events |
| 72 | * @param storageService storage service |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 73 | * @param executor executor service |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 74 | */ |
| 75 | public DefaultRouteTable(RouteTableId id, RouteStoreDelegate delegate, |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 76 | StorageService storageService, ExecutorService executor) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 77 | this.delegate = checkNotNull(delegate); |
| 78 | this.id = checkNotNull(id); |
| 79 | this.routes = buildRouteMap(checkNotNull(storageService)); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 80 | this.executor = checkNotNull(executor); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 81 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 82 | statusChangeListener = status -> { |
| 83 | if (status.equals(DistributedPrimitive.Status.ACTIVE)) { |
| 84 | executor.execute(this::notifyExistingRoutes); |
| 85 | } |
| 86 | }; |
| 87 | routes.addStatusChangeListener(statusChangeListener); |
| 88 | |
| 89 | notifyExistingRoutes(); |
| 90 | |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 91 | routes.addListener(listener, executor); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private void notifyExistingRoutes() { |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 95 | getRoutes().forEach(routeSet -> delegate.notify( |
| 96 | new InternalRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, routeSet))); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 99 | private ConsistentMultimap<String, RawRoute> buildRouteMap(StorageService storageService) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 100 | KryoNamespace routeTableSerializer = KryoNamespace.newBuilder() |
| 101 | .register(KryoNamespaces.API) |
| 102 | .register(Route.class) |
| 103 | .register(Route.Source.class) |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 104 | .register(RawRoute.class) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 105 | .build(); |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 106 | return storageService.<String, RawRoute>consistentMultimapBuilder() |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 107 | .withName("onos-routes-" + id.name()) |
| 108 | .withRelaxedReadConsistency() |
| 109 | .withSerializer(Serializer.using(routeTableSerializer)) |
| 110 | .build(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public RouteTableId id() { |
| 115 | return id; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void shutdown() { |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 120 | routes.removeStatusChangeListener(statusChangeListener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 121 | routes.removeListener(listener); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void destroy() { |
| 126 | shutdown(); |
| 127 | routes.destroy(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void update(Route route) { |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 132 | routes.put(route.prefix().toString(), new RawRoute(route)); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void remove(Route route) { |
Jordan Halterman | fd82236 | 2018-12-22 17:01:18 -0800 | [diff] [blame^] | 137 | getRoutes(route.prefix()) |
| 138 | .routes() |
| 139 | .stream() |
| 140 | .filter(r -> r.equals(route)) |
| 141 | .findAny() |
| 142 | .ifPresent(matchRoute -> { |
| 143 | routes.remove(matchRoute.prefix().toString(), new RawRoute(matchRoute)); |
| 144 | }); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 148 | public void replace(Route route) { |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 149 | routes.replaceValues(route.prefix().toString(), Sets.newHashSet(new RawRoute(route))); |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 153 | public Collection<RouteSet> getRoutes() { |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 154 | return routes.stream() |
| 155 | .map(Map.Entry::getValue) |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 156 | .collect(Collectors.groupingBy(RawRoute::prefix)) |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 157 | .entrySet() |
| 158 | .stream() |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 159 | .map(entry -> new RouteSet(id, |
| 160 | IpPrefix.valueOf(entry.getKey()), |
| 161 | entry.getValue().stream().map(RawRoute::route).collect(Collectors.toSet()))) |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 162 | .collect(Collectors.toList()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public RouteSet getRoutes(IpPrefix prefix) { |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 167 | Versioned<Collection<? extends RawRoute>> routeSet = routes.get(prefix.toString()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 168 | if (routeSet != null) { |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 169 | return new RouteSet(id, prefix, routeSet.value().stream().map(RawRoute::route).collect(Collectors.toSet())); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 170 | } |
| 171 | return null; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Collection<Route> getRoutesForNextHop(IpAddress nextHop) { |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 176 | return routes.stream() |
| 177 | .map(Map.Entry::getValue) |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 178 | .filter(r -> IpAddress.valueOf(r.nextHop()).equals(nextHop)) |
| 179 | .map(RawRoute::route) |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 180 | .collect(Collectors.toSet()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private class RouteTableListener |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 184 | implements MultimapEventListener<String, RawRoute> { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 185 | |
| 186 | private InternalRouteEvent createRouteEvent( |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 187 | InternalRouteEvent.Type type, MultimapEvent<String, RawRoute> event) { |
| 188 | Collection<? extends RawRoute> currentRoutes = Versioned.valueOrNull(routes.get(event.key())); |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 189 | return new InternalRouteEvent(type, new RouteSet( |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 190 | id, IpPrefix.valueOf(event.key()), currentRoutes != null ? |
| 191 | currentRoutes.stream().map(RawRoute::route).collect(Collectors.toSet()) |
| 192 | : Collections.emptySet())); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 196 | public void event(MultimapEvent<String, RawRoute> event) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 197 | InternalRouteEvent ire = null; |
| 198 | switch (event.type()) { |
| 199 | case INSERT: |
| 200 | ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, event); |
| 201 | break; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 202 | case REMOVE: |
| 203 | ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_REMOVED, event); |
| 204 | break; |
| 205 | default: |
| 206 | break; |
| 207 | } |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 208 | delegate.notify(ire); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 211 | |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Represents a route object stored in the underlying ConsistentMultimap. |
| 214 | */ |
| 215 | private static class RawRoute { |
| 216 | private Route.Source source; |
| 217 | private String prefix; |
| 218 | private String nextHop; |
| 219 | private NodeId sourceNode; |
| 220 | |
| 221 | RawRoute(Route route) { |
| 222 | this.source = route.source(); |
| 223 | this.prefix = route.prefix().toString(); |
| 224 | this.nextHop = route.nextHop().toString(); |
| 225 | this.sourceNode = route.sourceNode(); |
| 226 | } |
| 227 | |
| 228 | String prefix() { |
| 229 | return prefix; |
| 230 | } |
| 231 | |
| 232 | String nextHop() { |
| 233 | return nextHop; |
| 234 | } |
| 235 | |
| 236 | Route route() { |
| 237 | return new Route(source, IpPrefix.valueOf(prefix), IpAddress.valueOf(nextHop), sourceNode); |
| 238 | } |
pier | 594843a | 2018-12-21 22:02:25 +0100 | [diff] [blame] | 239 | |
| 240 | public int hashCode() { |
| 241 | return Objects.hash(prefix, nextHop); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public boolean equals(Object other) { |
| 246 | if (this == other) { |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | if (!(other instanceof RawRoute)) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | RawRoute that = (RawRoute) other; |
| 255 | |
| 256 | return Objects.equals(this.prefix, that.prefix) && |
| 257 | Objects.equals(this.nextHop, that.nextHop); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public String toString() { |
| 262 | return toStringHelper(this) |
| 263 | .add("prefix", prefix) |
| 264 | .add("nextHop", nextHop) |
| 265 | .toString(); |
| 266 | } |
| 267 | |
Jordan Halterman | 5f5ceb6 | 2018-10-01 23:17:57 -0700 | [diff] [blame] | 268 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 269 | } |