blob: 9f0ae4f610a966bc197eeebac159cb7e1f90ac16 [file] [log] [blame]
Charles Chan0214ded2016-11-18 17:48:37 -08001/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.incubator.store.routing.impl;
18
Jonathan Hart96c146b2017-02-24 16:32:00 -080019import com.google.common.collect.ImmutableSet;
Charles Chan0214ded2016-11-18 17:48:37 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
22import org.onlab.util.KryoNamespace;
Jonathan Hart96c146b2017-02-24 16:32:00 -080023import org.onosproject.incubator.net.routing.InternalRouteEvent;
Charles Chan0214ded2016-11-18 17:48:37 -080024import org.onosproject.incubator.net.routing.Route;
Jonathan Hart96c146b2017-02-24 16:32:00 -080025import org.onosproject.incubator.net.routing.RouteSet;
Charles Chan0214ded2016-11-18 17:48:37 -080026import org.onosproject.incubator.net.routing.RouteStore;
27import org.onosproject.incubator.net.routing.RouteStoreDelegate;
28import org.onosproject.incubator.net.routing.RouteTableId;
29import org.onosproject.store.AbstractStore;
Jonathan Hart96c146b2017-02-24 16:32:00 -080030import org.onosproject.store.service.DistributedSet;
Charles Chan0214ded2016-11-18 17:48:37 -080031import org.onosproject.store.service.Serializer;
Jonathan Hart96c146b2017-02-24 16:32:00 -080032import org.onosproject.store.service.SetEvent;
33import org.onosproject.store.service.SetEventListener;
Charles Chan0214ded2016-11-18 17:48:37 -080034import org.onosproject.store.service.StorageService;
Charles Chan0214ded2016-11-18 17:48:37 -080035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.util.Collection;
39import java.util.Collections;
Charles Chan0214ded2016-11-18 17:48:37 -080040import java.util.Map;
41import java.util.Set;
Jonathan Hart96c146b2017-02-24 16:32:00 -080042import java.util.concurrent.ConcurrentHashMap;
43import java.util.concurrent.ExecutorService;
Charles Chan0214ded2016-11-18 17:48:37 -080044import java.util.concurrent.Executors;
Charles Chan0214ded2016-11-18 17:48:37 -080045
Jonathan Hart1f67d282017-05-25 14:23:01 -070046import static org.onlab.util.Tools.groupedThreads;
47
Charles Chan0214ded2016-11-18 17:48:37 -080048/**
49 * Route store based on distributed storage.
50 */
Jonathan Hart96c146b2017-02-24 16:32:00 -080051public class DistributedRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080052 implements RouteStore {
Jonathan Hart96c146b2017-02-24 16:32:00 -080053
54 protected StorageService storageService;
Charles Chan0214ded2016-11-18 17:48:37 -080055
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 Hart96c146b2017-02-24 16:32:00 -080059 private final SetEventListener<RouteTableId> masterRouteTableListener =
60 new MasterRouteTableListener();
61 private final RouteStoreDelegate ourDelegate = new InternalRouteStoreDelegate();
Charles Chan0214ded2016-11-18 17:48:37 -080062
Jonathan Hart96c146b2017-02-24 16:32:00 -080063 // 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 Chan0214ded2016-11-18 17:48:37 -080067
Jonathan Hart96c146b2017-02-24 16:32:00 -080068 private ExecutorService executor;
69
Charles Chan0214ded2016-11-18 17:48:37 -080070 public DistributedRouteStore(StorageService storageService) {
71 this.storageService = storageService;
72 }
73
74 /**
75 * Sets up distributed route store.
76 */
77 public void activate() {
Jonathan Hart96c146b2017-02-24 16:32:00 -080078 routeTables = new ConcurrentHashMap<>();
Jonathan Hart1f67d282017-05-25 14:23:01 -070079 executor = Executors.newSingleThreadExecutor(groupedThreads("onos/route", "store", log));
Charles Chan0214ded2016-11-18 17:48:37 -080080
Jonathan Hart96c146b2017-02-24 16:32:00 -080081 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
91 masterRouteTable.forEach(this::createRouteTable);
92
93 masterRouteTable.addListener(masterRouteTableListener);
94
95 // Add default tables (add is idempotent)
96 masterRouteTable.add(IPV4);
97 masterRouteTable.add(IPV6);
Charles Chan0214ded2016-11-18 17:48:37 -080098
99 log.info("Started");
100 }
101
102 /**
103 * Cleans up distributed route store.
104 */
105 public void deactivate() {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800106 masterRouteTable.removeListener(masterRouteTableListener);
Charles Chan0214ded2016-11-18 17:48:37 -0800107
Jonathan Hart96c146b2017-02-24 16:32:00 -0800108 routeTables.values().forEach(RouteTable::shutdown);
Charles Chan0214ded2016-11-18 17:48:37 -0800109
110 log.info("Stopped");
111 }
112
113 @Override
114 public void updateRoute(Route route) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800115 getDefaultRouteTable(route).update(route);
Charles Chan0214ded2016-11-18 17:48:37 -0800116 }
117
118 @Override
119 public void removeRoute(Route route) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800120 getDefaultRouteTable(route).remove(route);
Charles Chan0214ded2016-11-18 17:48:37 -0800121 }
122
123 @Override
124 public Set<RouteTableId> getRouteTables() {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800125 return ImmutableSet.copyOf(masterRouteTable);
Charles Chan0214ded2016-11-18 17:48:37 -0800126 }
127
128 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800129 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 Chan0214ded2016-11-18 17:48:37 -0800136 }
137
138 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800139 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800140 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
141 }
142
143 @Override
144 public RouteSet getRoutes(IpPrefix prefix) {
145 return getDefaultRouteTable(prefix.address()).getRoutes(prefix);
Charles Chan0214ded2016-11-18 17:48:37 -0800146 }
147
Jonathan Hart96c146b2017-02-24 16:32:00 -0800148 private void createRouteTable(RouteTableId tableId) {
Jonathan Hart1f67d282017-05-25 14:23:01 -0700149 routeTables.computeIfAbsent(tableId, id -> new DefaultRouteTable(id, ourDelegate, storageService, executor));
Charles Chan0214ded2016-11-18 17:48:37 -0800150 }
151
Jonathan Hart96c146b2017-02-24 16:32:00 -0800152 private void destroyRouteTable(RouteTableId tableId) {
153 RouteTable table = routeTables.remove(tableId);
154 if (table != null) {
155 table.destroy();
156 }
Charles Chan0214ded2016-11-18 17:48:37 -0800157 }
158
Jonathan Hart96c146b2017-02-24 16:32:00 -0800159 private RouteTable getDefaultRouteTable(Route route) {
Charles Chan0214ded2016-11-18 17:48:37 -0800160 return getDefaultRouteTable(route.prefix().address());
161 }
162
Jonathan Hart96c146b2017-02-24 16:32:00 -0800163 private RouteTable getDefaultRouteTable(IpAddress ip) {
Charles Chan0214ded2016-11-18 17:48:37 -0800164 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
Jonathan Hart96c146b2017-02-24 16:32:00 -0800165 return routeTables.getOrDefault(routeTableId, EmptyRouteTable.instance());
Charles Chan0214ded2016-11-18 17:48:37 -0800166 }
167
Jonathan Hart96c146b2017-02-24 16:32:00 -0800168 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
Charles Chan0214ded2016-11-18 17:48:37 -0800169 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800170 public void notify(InternalRouteEvent event) {
171 executor.execute(() -> DistributedRouteStore.this.notifyDelegate(event));
Charles Chan0214ded2016-11-18 17:48:37 -0800172 }
173 }
174
Jonathan Hart96c146b2017-02-24 16:32:00 -0800175 private class MasterRouteTableListener implements SetEventListener<RouteTableId> {
Charles Chan0214ded2016-11-18 17:48:37 -0800176 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800177 public void event(SetEvent<RouteTableId> event) {
Charles Chan0214ded2016-11-18 17:48:37 -0800178 switch (event.type()) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800179 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 Chan0214ded2016-11-18 17:48:37 -0800187 }
188 }
189 }
190}