blob: 6901041b3e70d8cd82708f3f5e05f68959488743 [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.NextHopData;
Charles Chan0214ded2016-11-18 17:48:37 -080025import org.onosproject.incubator.net.routing.Route;
Jonathan Hart96c146b2017-02-24 16:32:00 -080026import org.onosproject.incubator.net.routing.RouteSet;
Charles Chan0214ded2016-11-18 17:48:37 -080027import org.onosproject.incubator.net.routing.RouteStore;
28import org.onosproject.incubator.net.routing.RouteStoreDelegate;
29import org.onosproject.incubator.net.routing.RouteTableId;
30import org.onosproject.store.AbstractStore;
Jonathan Hart96c146b2017-02-24 16:32:00 -080031import org.onosproject.store.service.DistributedSet;
Charles Chan0214ded2016-11-18 17:48:37 -080032import org.onosproject.store.service.Serializer;
Jonathan Hart96c146b2017-02-24 16:32:00 -080033import org.onosproject.store.service.SetEvent;
34import org.onosproject.store.service.SetEventListener;
Charles Chan0214ded2016-11-18 17:48:37 -080035import org.onosproject.store.service.StorageService;
Charles Chan0214ded2016-11-18 17:48:37 -080036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.util.Collection;
40import java.util.Collections;
Charles Chan0214ded2016-11-18 17:48:37 -080041import java.util.Map;
42import java.util.Set;
Jonathan Hart96c146b2017-02-24 16:32:00 -080043import java.util.concurrent.ConcurrentHashMap;
44import java.util.concurrent.ExecutorService;
Charles Chan0214ded2016-11-18 17:48:37 -080045import java.util.concurrent.Executors;
Charles Chan0214ded2016-11-18 17:48:37 -080046
47/**
48 * Route store based on distributed storage.
49 */
Jonathan Hart96c146b2017-02-24 16:32:00 -080050public class DistributedRouteStore extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080051 implements RouteStore {
Jonathan Hart96c146b2017-02-24 16:32:00 -080052
53 protected StorageService storageService;
Charles Chan0214ded2016-11-18 17:48:37 -080054
55 private static final RouteTableId IPV4 = new RouteTableId("ipv4");
56 private static final RouteTableId IPV6 = new RouteTableId("ipv6");
57 private static final Logger log = LoggerFactory.getLogger(DistributedRouteStore.class);
Jonathan Hart96c146b2017-02-24 16:32:00 -080058 private final SetEventListener<RouteTableId> masterRouteTableListener =
59 new MasterRouteTableListener();
60 private final RouteStoreDelegate ourDelegate = new InternalRouteStoreDelegate();
Charles Chan0214ded2016-11-18 17:48:37 -080061
Jonathan Hart96c146b2017-02-24 16:32:00 -080062 // Stores the route tables that have been created
63 private DistributedSet<RouteTableId> masterRouteTable;
64 // Local memory map to store route table object
65 private Map<RouteTableId, RouteTable> routeTables;
Charles Chan0214ded2016-11-18 17:48:37 -080066
Jonathan Hart96c146b2017-02-24 16:32:00 -080067 private ExecutorService executor;
68
Charles Chan0214ded2016-11-18 17:48:37 -080069 public DistributedRouteStore(StorageService storageService) {
70 this.storageService = storageService;
71 }
72
73 /**
74 * Sets up distributed route store.
75 */
76 public void activate() {
Jonathan Hart96c146b2017-02-24 16:32:00 -080077 routeTables = new ConcurrentHashMap<>();
78 executor = Executors.newSingleThreadExecutor();
Charles Chan0214ded2016-11-18 17:48:37 -080079
Jonathan Hart96c146b2017-02-24 16:32:00 -080080 KryoNamespace masterRouteTableSerializer = KryoNamespace.newBuilder()
81 .register(RouteTableId.class)
82 .build();
83
84 masterRouteTable = storageService.<RouteTableId>setBuilder()
85 .withName("onos-master-route-table")
86 .withSerializer(Serializer.using(masterRouteTableSerializer))
87 .build()
88 .asDistributedSet();
89
90 masterRouteTable.forEach(this::createRouteTable);
91
92 masterRouteTable.addListener(masterRouteTableListener);
93
94 // Add default tables (add is idempotent)
95 masterRouteTable.add(IPV4);
96 masterRouteTable.add(IPV6);
Charles Chan0214ded2016-11-18 17:48:37 -080097
98 log.info("Started");
99 }
100
101 /**
102 * Cleans up distributed route store.
103 */
104 public void deactivate() {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800105 masterRouteTable.removeListener(masterRouteTableListener);
Charles Chan0214ded2016-11-18 17:48:37 -0800106
Jonathan Hart96c146b2017-02-24 16:32:00 -0800107 routeTables.values().forEach(RouteTable::shutdown);
Charles Chan0214ded2016-11-18 17:48:37 -0800108
109 log.info("Stopped");
110 }
111
112 @Override
113 public void updateRoute(Route route) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800114 getDefaultRouteTable(route).update(route);
Charles Chan0214ded2016-11-18 17:48:37 -0800115 }
116
117 @Override
118 public void removeRoute(Route route) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800119 getDefaultRouteTable(route).remove(route);
Charles Chan0214ded2016-11-18 17:48:37 -0800120 }
121
122 @Override
123 public Set<RouteTableId> getRouteTables() {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800124 return ImmutableSet.copyOf(masterRouteTable);
Charles Chan0214ded2016-11-18 17:48:37 -0800125 }
126
127 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800128 public Collection<RouteSet> getRoutes(RouteTableId table) {
129 RouteTable routeTable = routeTables.get(table);
130 if (routeTable == null) {
131 return Collections.emptySet();
132 } else {
133 return ImmutableSet.copyOf(routeTable.getRoutes());
134 }
Charles Chan0214ded2016-11-18 17:48:37 -0800135 }
136
137 @Override
138 public Route longestPrefixMatch(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800139 // Not supported
140 return null;
Charles Chan0214ded2016-11-18 17:48:37 -0800141 }
142
143 @Override
144 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800145 return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
146 }
147
148 @Override
149 public RouteSet getRoutes(IpPrefix prefix) {
150 return getDefaultRouteTable(prefix.address()).getRoutes(prefix);
Charles Chan0214ded2016-11-18 17:48:37 -0800151 }
152
153 @Override
154 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800155 // Not supported
Charles Chan0214ded2016-11-18 17:48:37 -0800156 }
157
158 @Override
159 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800160 // Not supported
Charles Chan0214ded2016-11-18 17:48:37 -0800161 }
162
163 @Override
164 public NextHopData getNextHop(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800165 // Not supported
166 return null;
Charles Chan0214ded2016-11-18 17:48:37 -0800167 }
168
169 @Override
170 public Map<IpAddress, NextHopData> getNextHops() {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800171 // Not supported
172 return Collections.emptyMap();
Charles Chan0214ded2016-11-18 17:48:37 -0800173 }
174
Jonathan Hart96c146b2017-02-24 16:32:00 -0800175 private void createRouteTable(RouteTableId tableId) {
176 routeTables.computeIfAbsent(tableId, id -> new DefaultRouteTable(id, ourDelegate, storageService));
Charles Chan0214ded2016-11-18 17:48:37 -0800177 }
178
Jonathan Hart96c146b2017-02-24 16:32:00 -0800179 private void destroyRouteTable(RouteTableId tableId) {
180 RouteTable table = routeTables.remove(tableId);
181 if (table != null) {
182 table.destroy();
183 }
Charles Chan0214ded2016-11-18 17:48:37 -0800184 }
185
Jonathan Hart96c146b2017-02-24 16:32:00 -0800186 private RouteTable getDefaultRouteTable(Route route) {
Charles Chan0214ded2016-11-18 17:48:37 -0800187 return getDefaultRouteTable(route.prefix().address());
188 }
189
Jonathan Hart96c146b2017-02-24 16:32:00 -0800190 private RouteTable getDefaultRouteTable(IpAddress ip) {
Charles Chan0214ded2016-11-18 17:48:37 -0800191 RouteTableId routeTableId = (ip.isIp4()) ? IPV4 : IPV6;
Jonathan Hart96c146b2017-02-24 16:32:00 -0800192 return routeTables.getOrDefault(routeTableId, EmptyRouteTable.instance());
Charles Chan0214ded2016-11-18 17:48:37 -0800193 }
194
Jonathan Hart96c146b2017-02-24 16:32:00 -0800195 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
Charles Chan0214ded2016-11-18 17:48:37 -0800196 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800197 public void notify(InternalRouteEvent event) {
198 executor.execute(() -> DistributedRouteStore.this.notifyDelegate(event));
Charles Chan0214ded2016-11-18 17:48:37 -0800199 }
200 }
201
Jonathan Hart96c146b2017-02-24 16:32:00 -0800202 private class MasterRouteTableListener implements SetEventListener<RouteTableId> {
Charles Chan0214ded2016-11-18 17:48:37 -0800203 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800204 public void event(SetEvent<RouteTableId> event) {
Charles Chan0214ded2016-11-18 17:48:37 -0800205 switch (event.type()) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800206 case ADD:
207 executor.execute(() -> createRouteTable(event.entry()));
208 break;
209 case REMOVE:
210 executor.execute(() -> destroyRouteTable(event.entry()));
211 break;
212 default:
213 break;
Charles Chan0214ded2016-11-18 17:48:37 -0800214 }
215 }
216 }
217}