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 | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | import java.util.function.Consumer; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | import com.google.common.collect.ImmutableSet; |
Daniel Ginsburg | c27fa859 | 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; |
| 30 | import org.onlab.util.KryoNamespace; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 31 | import org.onosproject.routeservice.InternalRouteEvent; |
| 32 | import org.onosproject.routeservice.Route; |
| 33 | import org.onosproject.routeservice.RouteSet; |
| 34 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 35 | import org.onosproject.routeservice.RouteTableId; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 36 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 37 | import org.onosproject.store.service.ConsistentMultimap; |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 38 | import org.onosproject.store.service.DistributedPrimitive; |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.MultimapEvent; |
| 40 | import org.onosproject.store.service.MultimapEventListener; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 41 | import org.onosproject.store.service.Serializer; |
| 42 | import org.onosproject.store.service.StorageService; |
| 43 | import org.onosproject.store.service.Versioned; |
| 44 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | |
| 47 | /** |
| 48 | * Default implementation of a route table based on a consistent map. |
| 49 | */ |
| 50 | public class DefaultRouteTable implements RouteTable { |
| 51 | |
| 52 | private final RouteTableId id; |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 53 | private final ConsistentMultimap<IpPrefix, Route> routes; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 54 | private final RouteStoreDelegate delegate; |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 55 | private final ExecutorService executor; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 56 | private final RouteTableListener listener = new RouteTableListener(); |
| 57 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 58 | private final Consumer<DistributedPrimitive.Status> statusChangeListener; |
| 59 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Creates a new route table. |
| 62 | * |
| 63 | * @param id route table ID |
| 64 | * @param delegate route store delegate to notify of events |
| 65 | * @param storageService storage service |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 66 | * @param executor executor service |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 67 | */ |
| 68 | public DefaultRouteTable(RouteTableId id, RouteStoreDelegate delegate, |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 69 | StorageService storageService, ExecutorService executor) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 70 | this.delegate = checkNotNull(delegate); |
| 71 | this.id = checkNotNull(id); |
| 72 | this.routes = buildRouteMap(checkNotNull(storageService)); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 73 | this.executor = checkNotNull(executor); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 74 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 75 | statusChangeListener = status -> { |
| 76 | if (status.equals(DistributedPrimitive.Status.ACTIVE)) { |
| 77 | executor.execute(this::notifyExistingRoutes); |
| 78 | } |
| 79 | }; |
| 80 | routes.addStatusChangeListener(statusChangeListener); |
| 81 | |
| 82 | notifyExistingRoutes(); |
| 83 | |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 84 | routes.addListener(listener, executor); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private void notifyExistingRoutes() { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 88 | getRoutes().forEach(routeSet -> delegate.notify( |
| 89 | new InternalRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, routeSet))); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 92 | private ConsistentMultimap<IpPrefix, Route> buildRouteMap(StorageService storageService) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 93 | KryoNamespace routeTableSerializer = KryoNamespace.newBuilder() |
| 94 | .register(KryoNamespaces.API) |
| 95 | .register(Route.class) |
| 96 | .register(Route.Source.class) |
| 97 | .build(); |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 98 | return storageService.<IpPrefix, Route>consistentMultimapBuilder() |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 99 | .withName("onos-routes-" + id.name()) |
| 100 | .withRelaxedReadConsistency() |
| 101 | .withSerializer(Serializer.using(routeTableSerializer)) |
| 102 | .build(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public RouteTableId id() { |
| 107 | return id; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void shutdown() { |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 112 | routes.removeStatusChangeListener(statusChangeListener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 113 | routes.removeListener(listener); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void destroy() { |
| 118 | shutdown(); |
| 119 | routes.destroy(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void update(Route route) { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 124 | routes.put(route.prefix(), route); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void remove(Route route) { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 129 | routes.remove(route.prefix(), route); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Daniel Ginsburg | c27fa859 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 133 | public void replace(Route route) { |
| 134 | routes.replaceValues(route.prefix(), Sets.newHashSet(route)); |
| 135 | } |
| 136 | |
| 137 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 138 | public Collection<RouteSet> getRoutes() { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 139 | return routes.stream() |
| 140 | .map(Map.Entry::getValue) |
| 141 | .collect(Collectors.groupingBy(Route::prefix)) |
| 142 | .entrySet() |
| 143 | .stream() |
| 144 | .map(entry -> new RouteSet(id, entry.getKey(), ImmutableSet.copyOf(entry.getValue()))) |
| 145 | .collect(Collectors.toList()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public RouteSet getRoutes(IpPrefix prefix) { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 150 | Versioned<Collection<? extends Route>> routeSet = routes.get(prefix); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 151 | |
| 152 | if (routeSet != null) { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 153 | return new RouteSet(id, prefix, ImmutableSet.copyOf(routeSet.value())); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 154 | } |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Collection<Route> getRoutesForNextHop(IpAddress nextHop) { |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 160 | return routes.stream() |
| 161 | .map(Map.Entry::getValue) |
| 162 | .filter(r -> r.nextHop().equals(nextHop)) |
| 163 | .collect(Collectors.toSet()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private class RouteTableListener |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 167 | implements MultimapEventListener<IpPrefix, Route> { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 168 | |
| 169 | private InternalRouteEvent createRouteEvent( |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 170 | InternalRouteEvent.Type type, MultimapEvent<IpPrefix, Route> event) { |
| 171 | Collection<? extends Route> currentRoutes = Versioned.valueOrNull(routes.get(event.key())); |
| 172 | return new InternalRouteEvent(type, new RouteSet( |
| 173 | id, event.key(), currentRoutes != null ? ImmutableSet.copyOf(currentRoutes) : Collections.emptySet())); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 177 | public void event(MultimapEvent<IpPrefix, Route> event) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 178 | InternalRouteEvent ire = null; |
| 179 | switch (event.type()) { |
| 180 | case INSERT: |
| 181 | ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_ADDED, event); |
| 182 | break; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 183 | case REMOVE: |
| 184 | ire = createRouteEvent(InternalRouteEvent.Type.ROUTE_REMOVED, event); |
| 185 | break; |
| 186 | default: |
| 187 | break; |
| 188 | } |
Jordan Halterman | c17c7ca | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 189 | delegate.notify(ire); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 192 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 193 | } |