Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -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; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.util.KryoNamespace; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 24 | import org.onosproject.routeservice.InternalRouteEvent; |
| 25 | import org.onosproject.routeservice.Route; |
| 26 | import org.onosproject.routeservice.RouteSet; |
| 27 | import org.onosproject.routeservice.RouteStore; |
| 28 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 29 | import org.onosproject.routeservice.RouteTableId; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 30 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 31 | import org.onosproject.store.service.DistributedSet; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 32 | import org.onosproject.store.service.Serializer; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 33 | import org.onosproject.store.service.SetEvent; |
| 34 | import org.onosproject.store.service.SetEventListener; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 35 | import org.onosproject.store.service.StorageService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.Collection; |
| 40 | import java.util.Collections; |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 41 | import java.util.HashMap; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 42 | import java.util.Map; |
| 43 | import java.util.Set; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 44 | import java.util.concurrent.ConcurrentHashMap; |
| 45 | import java.util.concurrent.ExecutorService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 46 | import java.util.concurrent.Executors; |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 47 | import java.util.stream.Collectors; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Route store based on distributed storage. |
| 53 | */ |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 54 | public class DistributedRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 55 | implements RouteStore { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 56 | |
| 57 | protected StorageService storageService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 58 | |
| 59 | private static final RouteTableId IPV4 = new RouteTableId("ipv4"); |
| 60 | private static final RouteTableId IPV6 = new RouteTableId("ipv6"); |
| 61 | private static final Logger log = LoggerFactory.getLogger(DistributedRouteStore.class); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 62 | private final SetEventListener<RouteTableId> masterRouteTableListener = |
| 63 | new MasterRouteTableListener(); |
| 64 | private final RouteStoreDelegate ourDelegate = new InternalRouteStoreDelegate(); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 65 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 66 | // Stores the route tables that have been created |
| 67 | private DistributedSet<RouteTableId> masterRouteTable; |
| 68 | // Local memory map to store route table object |
| 69 | private Map<RouteTableId, RouteTable> routeTables; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 70 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 71 | private ExecutorService executor; |
| 72 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 73 | public DistributedRouteStore(StorageService storageService) { |
| 74 | this.storageService = storageService; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets up distributed route store. |
| 79 | */ |
| 80 | public void activate() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 81 | routeTables = new ConcurrentHashMap<>(); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 82 | executor = Executors.newSingleThreadExecutor(groupedThreads("onos/route", "store", log)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 83 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 84 | KryoNamespace masterRouteTableSerializer = KryoNamespace.newBuilder() |
| 85 | .register(RouteTableId.class) |
| 86 | .build(); |
| 87 | |
| 88 | masterRouteTable = storageService.<RouteTableId>setBuilder() |
| 89 | .withName("onos-master-route-table") |
| 90 | .withSerializer(Serializer.using(masterRouteTableSerializer)) |
| 91 | .build() |
| 92 | .asDistributedSet(); |
| 93 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 94 | masterRouteTable.addListener(masterRouteTableListener); |
| 95 | |
| 96 | // Add default tables (add is idempotent) |
| 97 | masterRouteTable.add(IPV4); |
| 98 | masterRouteTable.add(IPV6); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 99 | |
Yi Tseng | 46654c3 | 2017-08-08 19:09:19 -0700 | [diff] [blame] | 100 | masterRouteTable.forEach(this::createRouteTable); |
| 101 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 102 | log.info("Started"); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Cleans up distributed route store. |
| 107 | */ |
| 108 | public void deactivate() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 109 | masterRouteTable.removeListener(masterRouteTableListener); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 110 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 111 | routeTables.values().forEach(RouteTable::shutdown); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 112 | |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void updateRoute(Route route) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 118 | getDefaultRouteTable(route).update(route); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 122 | public void updateRoutes(Collection<Route> routes) { |
| 123 | Map<RouteTableId, Set<Route>> computedTables = computeRouteTablesFromRoutes(routes); |
| 124 | computedTables.forEach( |
| 125 | ((routeTableId, routesToAdd) -> getDefaultRouteTable(routeTableId).update(routesToAdd)) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 130 | public void removeRoute(Route route) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 131 | getDefaultRouteTable(route).remove(route); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 135 | public void removeRoutes(Collection<Route> routes) { |
| 136 | Map<RouteTableId, Set<Route>> computedTables = computeRouteTablesFromRoutes(routes); |
| 137 | computedTables.forEach( |
| 138 | ((routeTableId, routesToRemove) -> getDefaultRouteTable(routeTableId).remove(routesToRemove)) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | @Override |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 143 | public void replaceRoute(Route route) { |
| 144 | getDefaultRouteTable(route).replace(route); |
| 145 | } |
| 146 | |
| 147 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 148 | public Set<RouteTableId> getRouteTables() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 149 | return ImmutableSet.copyOf(masterRouteTable); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 153 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
| 154 | RouteTable routeTable = routeTables.get(table); |
| 155 | if (routeTable == null) { |
| 156 | return Collections.emptySet(); |
| 157 | } else { |
| 158 | return ImmutableSet.copyOf(routeTable.getRoutes()); |
| 159 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 163 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 164 | return getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
| 165 | } |
| 166 | |
| 167 | @Override |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 168 | public Collection<RouteSet> getRoutesForNextHops(Collection<IpAddress> nextHops) { |
| 169 | Map<RouteTableId, Set<IpAddress>> computedTables = computeRouteTablesFromIps(nextHops); |
| 170 | return computedTables.entrySet().stream() |
| 171 | .map(entry -> getDefaultRouteTable(entry.getKey()).getRoutesForNextHops(entry.getValue())) |
| 172 | .flatMap(Collection::stream) |
| 173 | .collect(Collectors.toList()); |
| 174 | } |
| 175 | |
| 176 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 177 | public RouteSet getRoutes(IpPrefix prefix) { |
| 178 | return getDefaultRouteTable(prefix.address()).getRoutes(prefix); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 181 | private void createRouteTable(RouteTableId tableId) { |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 182 | routeTables.computeIfAbsent(tableId, id -> new DefaultRouteTable(id, ourDelegate, storageService, executor)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 185 | private void destroyRouteTable(RouteTableId tableId) { |
| 186 | RouteTable table = routeTables.remove(tableId); |
| 187 | if (table != null) { |
| 188 | table.destroy(); |
| 189 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 192 | private RouteTable getDefaultRouteTable(Route route) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 193 | return getDefaultRouteTable(route.prefix().address()); |
| 194 | } |
| 195 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 196 | private RouteTable getDefaultRouteTable(IpAddress ip) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 197 | RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 198 | return routeTables.getOrDefault(routeTableId, EmptyRouteTable.instance()); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 199 | } |
| 200 | |
pier | 8a86db2 | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 201 | private RouteTable getDefaultRouteTable(RouteTableId routeTableId) { |
| 202 | return routeTables.getOrDefault(routeTableId, EmptyRouteTable.instance()); |
| 203 | } |
| 204 | |
| 205 | private Map<RouteTableId, Set<Route>> computeRouteTablesFromRoutes(Collection<Route> routes) { |
| 206 | Map<RouteTableId, Set<Route>> computedTables = new HashMap<>(); |
| 207 | routes.forEach(route -> { |
| 208 | RouteTableId routeTableId = (route.prefix().address().isIp4()) ? IPV4 : IPV6; |
| 209 | Set<Route> tempRoutes = computedTables.computeIfAbsent(routeTableId, k -> Sets.newHashSet()); |
| 210 | tempRoutes.add(route); |
| 211 | }); |
| 212 | return computedTables; |
| 213 | } |
| 214 | |
| 215 | private Map<RouteTableId, Set<IpAddress>> computeRouteTablesFromIps(Collection<IpAddress> ipAddresses) { |
| 216 | Map<RouteTableId, Set<IpAddress>> computedTables = new HashMap<>(); |
| 217 | ipAddresses.forEach(ipAddress -> { |
| 218 | RouteTableId routeTableId = (ipAddress.isIp4()) ? IPV4 : IPV6; |
| 219 | Set<IpAddress> tempIpAddresses = computedTables.computeIfAbsent(routeTableId, k -> Sets.newHashSet()); |
| 220 | tempIpAddresses.add(ipAddress); |
| 221 | }); |
| 222 | return computedTables; |
| 223 | } |
| 224 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 225 | private class InternalRouteStoreDelegate implements RouteStoreDelegate { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 226 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 227 | public void notify(InternalRouteEvent event) { |
| 228 | executor.execute(() -> DistributedRouteStore.this.notifyDelegate(event)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 232 | private class MasterRouteTableListener implements SetEventListener<RouteTableId> { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 233 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 234 | public void event(SetEvent<RouteTableId> event) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 235 | switch (event.type()) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 236 | case ADD: |
| 237 | executor.execute(() -> createRouteTable(event.entry())); |
| 238 | break; |
| 239 | case REMOVE: |
| 240 | executor.execute(() -> destroyRouteTable(event.entry())); |
| 241 | break; |
| 242 | default: |
| 243 | break; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |