blob: 2d8610482774fb28113c0954d4f0f52702c91ee8 [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
Ilayda Ozdemirad741712020-06-10 08:31:27 +0000148 public void removeSources(McastRoute route, Set<ConnectPoint> sources) {
149 mcastRoutes.compute(route, (k, v) -> {
150 v.removeSources(HostId.NONE, sources);
151 return v;
152 });
153 }
154
155 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200156 public void removeSources(McastRoute route, HostId hostId, Set<ConnectPoint> sources) {
157 mcastRoutes.compute(route, (k, v) -> {
158 v.removeSources(hostId, sources);
159 return v;
160 });
161 }
162
163 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700164 public void addSink(McastRoute route, HostId hostId, 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, sinks);
167 return v;
168 });
169 }
170
171 @Override
172 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700173 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700174 v.addSinks(HostId.NONE, sinks);
175 return v;
176 });
177 }
178
179
180 @Override
181 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700182 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700183 v.removeSinks();
184 return v;
185 });
186 }
187
188 @Override
189 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700190 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700191 v.removeSinks(hostId);
192 return v;
193 });
194 }
195
196 @Override
197 public void removeSinks(McastRoute route, HostId hostId, 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, sinks);
200 return v;
201 });
202 }
203
204 @Override
205 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700206 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700207 v.removeSinks(HostId.NONE, sinks);
208 return v;
209 });
210 }
211
212 @Override
213 public Set<ConnectPoint> sourcesFor(McastRoute route) {
214 McastRouteData data = mcastRoutes.getOrDefault(route, null);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200215 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources().values().stream()
216 .flatMap(Collection::stream).collect(Collectors.toSet()));
217 }
218
219 @Override
220 public Set<ConnectPoint> sourcesFor(McastRoute route, HostId hostId) {
221 McastRouteData data = mcastRoutes.getOrDefault(route, null);
222 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -0700223 }
224
225 @Override
226 public Set<ConnectPoint> sinksFor(McastRoute route) {
227 McastRouteData data = mcastRoutes.getOrDefault(route, null);
228 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
229 .flatMap(Collection::stream).collect(Collectors.toSet()));
230 }
231
232 @Override
233 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
234 McastRouteData data = mcastRoutes.getOrDefault(route, null);
235 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
236 }
237
238 @Override
239 public Set<McastRoute> getRoutes() {
240 return ImmutableSet.copyOf(mcastRoutes.keySet());
241 }
242
243 @Override
244 public McastRouteData getRouteData(McastRoute route) {
245 return mcastRoutes.get(route);
246 }
247
248 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
249 @Override
250 public void event(MapEvent<McastRoute, McastRouteData> event) {
251 final McastRoute route = event.key();
252 final McastRouteData newData =
253 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
254 final McastRouteData oldData =
255 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
256
257 switch (event.type()) {
258 case INSERT:
259 checkNotNull(newData);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200260 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null,
Andrea Campanella545edb42018-03-20 16:37:29 -0700261 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
262 break;
263 case UPDATE:
264 checkNotNull(newData);
265 checkNotNull(oldData);
266
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200267 if (newData.allSources().size() > oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700268 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
269 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
270 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200271 } else if (newData.allSources().size() < oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700272 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
273 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
274 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
275 }
276 if (newData.allSinks().size() > oldData.allSinks().size()) {
277 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
278 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
279 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
280 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700281 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_REMOVED,
282 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
283 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
284 }
285 break;
286 case REMOVE:
287 // Verify old data is not null
288 checkNotNull(oldData);
289 // Create a route removed event with just the route
290 // and the source connect point
291 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
292 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
293 null));
294 break;
295 default:
296 log.warn("Unknown mcast operation type: {}", event.type());
297 }
298 }
299 }
300}