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; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.util.KryoNamespace; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 23 | import org.onosproject.routeservice.InternalRouteEvent; |
| 24 | import org.onosproject.routeservice.Route; |
| 25 | import org.onosproject.routeservice.RouteSet; |
| 26 | import org.onosproject.routeservice.RouteStore; |
| 27 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 28 | import org.onosproject.routeservice.RouteTableId; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 29 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 30 | import org.onosproject.store.service.DistributedSet; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 31 | import org.onosproject.store.service.Serializer; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 32 | import org.onosproject.store.service.SetEvent; |
| 33 | import org.onosproject.store.service.SetEventListener; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 34 | import org.onosproject.store.service.StorageService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 40 | import java.util.Map; |
| 41 | import java.util.Set; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 42 | import java.util.concurrent.ConcurrentHashMap; |
| 43 | import java.util.concurrent.ExecutorService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 44 | import java.util.concurrent.Executors; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 45 | |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 46 | import static org.onlab.util.Tools.groupedThreads; |
| 47 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Route store based on distributed storage. |
| 50 | */ |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 51 | public class DistributedRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 52 | implements RouteStore { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 53 | |
| 54 | protected StorageService storageService; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 55 | |
| 56 | private static final RouteTableId IPV4 = new RouteTableId("ipv4"); |
| 57 | private static final RouteTableId IPV6 = new RouteTableId("ipv6"); |
| 58 | private static final Logger log = LoggerFactory.getLogger(DistributedRouteStore.class); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 59 | private final SetEventListener<RouteTableId> masterRouteTableListener = |
| 60 | new MasterRouteTableListener(); |
| 61 | private final RouteStoreDelegate ourDelegate = new InternalRouteStoreDelegate(); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 62 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 63 | // Stores the route tables that have been created |
| 64 | private DistributedSet<RouteTableId> masterRouteTable; |
| 65 | // Local memory map to store route table object |
| 66 | private Map<RouteTableId, RouteTable> routeTables; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 67 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 68 | private ExecutorService executor; |
| 69 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 70 | public DistributedRouteStore(StorageService storageService) { |
| 71 | this.storageService = storageService; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets up distributed route store. |
| 76 | */ |
| 77 | public void activate() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 78 | routeTables = new ConcurrentHashMap<>(); |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 79 | executor = Executors.newSingleThreadExecutor(groupedThreads("onos/route", "store", log)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 80 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 81 | KryoNamespace masterRouteTableSerializer = KryoNamespace.newBuilder() |
| 82 | .register(RouteTableId.class) |
| 83 | .build(); |
| 84 | |
| 85 | masterRouteTable = storageService.<RouteTableId>setBuilder() |
| 86 | .withName("onos-master-route-table") |
| 87 | .withSerializer(Serializer.using(masterRouteTableSerializer)) |
| 88 | .build() |
| 89 | .asDistributedSet(); |
| 90 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 91 | masterRouteTable.addListener(masterRouteTableListener); |
| 92 | |
| 93 | // Add default tables (add is idempotent) |
| 94 | masterRouteTable.add(IPV4); |
| 95 | masterRouteTable.add(IPV6); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 96 | |
Yi Tseng | 46654c3 | 2017-08-08 19:09:19 -0700 | [diff] [blame] | 97 | masterRouteTable.forEach(this::createRouteTable); |
| 98 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 99 | log.info("Started"); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Cleans up distributed route store. |
| 104 | */ |
| 105 | public void deactivate() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 106 | masterRouteTable.removeListener(masterRouteTableListener); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 107 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 108 | routeTables.values().forEach(RouteTable::shutdown); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 109 | |
| 110 | log.info("Stopped"); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void updateRoute(Route route) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 115 | getDefaultRouteTable(route).update(route); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void removeRoute(Route route) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 120 | getDefaultRouteTable(route).remove(route); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Set<RouteTableId> getRouteTables() { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 125 | return ImmutableSet.copyOf(masterRouteTable); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 129 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
| 130 | RouteTable routeTable = routeTables.get(table); |
| 131 | if (routeTable == null) { |
| 132 | return Collections.emptySet(); |
| 133 | } else { |
| 134 | return ImmutableSet.copyOf(routeTable.getRoutes()); |
| 135 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 139 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 140 | return getDefaultRouteTable(ip).getRoutesForNextHop(ip); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public RouteSet getRoutes(IpPrefix prefix) { |
| 145 | return getDefaultRouteTable(prefix.address()).getRoutes(prefix); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 148 | private void createRouteTable(RouteTableId tableId) { |
Jonathan Hart | 1f67d28 | 2017-05-25 14:23:01 -0700 | [diff] [blame] | 149 | routeTables.computeIfAbsent(tableId, id -> new DefaultRouteTable(id, ourDelegate, storageService, executor)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 152 | private void destroyRouteTable(RouteTableId tableId) { |
| 153 | RouteTable table = routeTables.remove(tableId); |
| 154 | if (table != null) { |
| 155 | table.destroy(); |
| 156 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 159 | private RouteTable getDefaultRouteTable(Route route) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 160 | return getDefaultRouteTable(route.prefix().address()); |
| 161 | } |
| 162 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 163 | private RouteTable getDefaultRouteTable(IpAddress ip) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 164 | RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 165 | return routeTables.getOrDefault(routeTableId, EmptyRouteTable.instance()); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 168 | private class InternalRouteStoreDelegate implements RouteStoreDelegate { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 169 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 170 | public void notify(InternalRouteEvent event) { |
| 171 | executor.execute(() -> DistributedRouteStore.this.notifyDelegate(event)); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 175 | private class MasterRouteTableListener implements SetEventListener<RouteTableId> { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 176 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 177 | public void event(SetEvent<RouteTableId> event) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 178 | switch (event.type()) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 179 | case ADD: |
| 180 | executor.execute(() -> createRouteTable(event.entry())); |
| 181 | break; |
| 182 | case REMOVE: |
| 183 | executor.execute(() -> destroyRouteTable(event.entry())); |
| 184 | break; |
| 185 | default: |
| 186 | break; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |