blob: e3526c42989304fe2cea87117bca159f6a73a03c [file] [log] [blame]
Andrea Campanella545edb42018-03-20 16:37:29 -07001/*
2 * Copyright 2015-present Open Networking Foundation
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 */
16package org.onosproject.mcast.impl;
17
18
19import com.google.common.collect.ImmutableSet;
Andrea Campanella545edb42018-03-20 16:37:29 -070020import org.onlab.util.KryoNamespace;
21import org.onosproject.mcast.api.McastEvent;
22import org.onosproject.mcast.api.McastRoute;
23import org.onosproject.mcast.api.McastRouteData;
24import org.onosproject.mcast.api.McastStore;
25import org.onosproject.mcast.api.McastStoreDelegate;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.HostId;
28import org.onosproject.store.AbstractStore;
29import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.ConsistentMap;
31import org.onosproject.store.service.MapEvent;
32import org.onosproject.store.service.MapEventListener;
33import org.onosproject.store.service.Serializer;
34import org.onosproject.store.service.StorageService;
35import org.onosproject.store.service.Versioned;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036import org.osgi.service.component.annotations.Activate;
37import org.osgi.service.component.annotations.Component;
38import org.osgi.service.component.annotations.Deactivate;
39import org.osgi.service.component.annotations.Reference;
40import org.osgi.service.component.annotations.ReferenceCardinality;
Andrea Campanella545edb42018-03-20 16:37:29 -070041import org.slf4j.Logger;
42
43import java.util.Collection;
44import java.util.Map;
45import java.util.Optional;
46import java.util.Set;
Andrea Campanella545edb42018-03-20 16:37:29 -070047import java.util.concurrent.atomic.AtomicReference;
48import java.util.stream.Collectors;
49
50import static com.google.common.base.Preconditions.checkNotNull;
51import static org.onosproject.mcast.api.McastRouteUpdate.mcastRouteUpdate;
52import static org.slf4j.LoggerFactory.getLogger;
53
54/**
55 * New distributed mcast route store implementation. Routes are stored consistently
56 * across the cluster.
57 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058@Component(immediate = true, service = McastStore.class)
Andrea Campanella545edb42018-03-20 16:37:29 -070059public class DistributedMcastRoutesStore
60 extends AbstractStore<McastEvent, McastStoreDelegate>
61 implements McastStore {
62
63 private static final String MCASTRIB = "onos-mcast-route-table";
64 private Logger log = getLogger(getClass());
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanella545edb42018-03-20 16:37:29 -070067 protected StorageService storageService;
68
69 private Map<McastRoute, McastRouteData> mcastRoutes;
70 private ConsistentMap<McastRoute, McastRouteData> mcastRib;
71 private MapEventListener<McastRoute, McastRouteData> mcastRouteListener =
72 new McastRouteListener();
73
Andrea Campanella545edb42018-03-20 16:37:29 -070074 @Activate
75 public void activate() {
76 mcastRib = storageService.<McastRoute, McastRouteData>consistentMapBuilder()
77 .withName(MCASTRIB)
78 .withSerializer(Serializer.using(KryoNamespace.newBuilder()
79 .register(KryoNamespaces.API)
80 .register(
81 McastRoute.class,
82 AtomicReference.class,
83 McastRouteData.class,
84 McastRoute.Type.class
85 ).build()))
86 .build();
87
88 mcastRoutes = mcastRib.asJavaMap();
89 mcastRib.addListener(mcastRouteListener);
90
91 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
96 mcastRib.removeListener(mcastRouteListener);
97 mcastRib.destroy();
98 log.info("Stopped");
99 }
100
101 @Override
102 public void storeRoute(McastRoute route) {
Pier139babb2018-03-23 14:59:49 -0700103 mcastRoutes.putIfAbsent(route, McastRouteData.empty());
Andrea Campanella545edb42018-03-20 16:37:29 -0700104 }
105
106 @Override
107 public void removeRoute(McastRoute route) {
108 mcastRoutes.remove(route);
109 }
110
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200111
Andrea Campanella545edb42018-03-20 16:37:29 -0700112 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200113 public void storeSource(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700114 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200115 v.addSources(hostId, connectPoints);
Andrea Campanella545edb42018-03-20 16:37:29 -0700116 return v;
117 });
118 }
119
120 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200121 public void storeSources(McastRoute route, Set<ConnectPoint> sources) {
122 mcastRoutes.compute(route, (k, v) -> {
123 v.addSources(HostId.NONE, sources);
124 return v;
125 });
126 }
127
128
129 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700130 public void removeSources(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700131 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700132 v.removeSources();
Pier139babb2018-03-23 14:59:49 -0700133 // Since we have cleared the sources, we should remove the route
134 return null;
Andrea Campanella545edb42018-03-20 16:37:29 -0700135 });
136 }
137
138 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200139 public void removeSource(McastRoute route, HostId source) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700140 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200141 v.removeSource(source);
Pier139babb2018-03-23 14:59:49 -0700142 // Since there are no sources, we should remove the route
143 return v.sources().isEmpty() ? null : v;
Andrea Campanella545edb42018-03-20 16:37:29 -0700144 });
Andrea Campanella545edb42018-03-20 16:37:29 -0700145 }
146
147 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200148 public void removeSources(McastRoute route, HostId hostId, Set<ConnectPoint> sources) {
149 mcastRoutes.compute(route, (k, v) -> {
150 v.removeSources(hostId, sources);
151 return v;
152 });
153 }
154
155 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700156 public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700157 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700158 v.addSinks(hostId, sinks);
159 return v;
160 });
161 }
162
163 @Override
164 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700165 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700166 v.addSinks(HostId.NONE, sinks);
167 return v;
168 });
169 }
170
171
172 @Override
173 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700174 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700175 v.removeSinks();
176 return v;
177 });
178 }
179
180 @Override
181 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700182 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700183 v.removeSinks(hostId);
184 return v;
185 });
186 }
187
188 @Override
189 public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700190 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700191 v.removeSinks(hostId, sinks);
192 return v;
193 });
194 }
195
196 @Override
197 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700198 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700199 v.removeSinks(HostId.NONE, sinks);
200 return v;
201 });
202 }
203
204 @Override
205 public Set<ConnectPoint> sourcesFor(McastRoute route) {
206 McastRouteData data = mcastRoutes.getOrDefault(route, null);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200207 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources().values().stream()
208 .flatMap(Collection::stream).collect(Collectors.toSet()));
209 }
210
211 @Override
212 public Set<ConnectPoint> sourcesFor(McastRoute route, HostId hostId) {
213 McastRouteData data = mcastRoutes.getOrDefault(route, null);
214 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -0700215 }
216
217 @Override
218 public Set<ConnectPoint> sinksFor(McastRoute route) {
219 McastRouteData data = mcastRoutes.getOrDefault(route, null);
220 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
221 .flatMap(Collection::stream).collect(Collectors.toSet()));
222 }
223
224 @Override
225 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
226 McastRouteData data = mcastRoutes.getOrDefault(route, null);
227 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
228 }
229
230 @Override
231 public Set<McastRoute> getRoutes() {
232 return ImmutableSet.copyOf(mcastRoutes.keySet());
233 }
234
235 @Override
236 public McastRouteData getRouteData(McastRoute route) {
237 return mcastRoutes.get(route);
238 }
239
240 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
241 @Override
242 public void event(MapEvent<McastRoute, McastRouteData> event) {
243 final McastRoute route = event.key();
244 final McastRouteData newData =
245 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
246 final McastRouteData oldData =
247 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
248
249 switch (event.type()) {
250 case INSERT:
251 checkNotNull(newData);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200252 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null,
Andrea Campanella545edb42018-03-20 16:37:29 -0700253 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
254 break;
255 case UPDATE:
256 checkNotNull(newData);
257 checkNotNull(oldData);
258
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200259 if (newData.allSources().size() > oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700260 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
261 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
262 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200263 } else if (newData.allSources().size() < oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700264 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
265 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
266 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
267 }
268 if (newData.allSinks().size() > oldData.allSinks().size()) {
269 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
270 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
271 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
272 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700273 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_REMOVED,
274 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
275 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
276 }
277 break;
278 case REMOVE:
279 // Verify old data is not null
280 checkNotNull(oldData);
281 // Create a route removed event with just the route
282 // and the source connect point
283 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
284 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
285 null));
286 break;
287 default:
288 log.warn("Unknown mcast operation type: {}", event.type());
289 }
290 }
291 }
292}