blob: 6dc6c3d28b041fee1c962de9fa927d2cc3b60384 [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);
Andrea Campanella545edb42018-03-20 16:37:29 -070097 log.info("Stopped");
98 }
99
100 @Override
101 public void storeRoute(McastRoute route) {
Pier139babb2018-03-23 14:59:49 -0700102 mcastRoutes.putIfAbsent(route, McastRouteData.empty());
Andrea Campanella545edb42018-03-20 16:37:29 -0700103 }
104
105 @Override
106 public void removeRoute(McastRoute route) {
107 mcastRoutes.remove(route);
108 }
109
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200110
Andrea Campanella545edb42018-03-20 16:37:29 -0700111 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200112 public void storeSource(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700113 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200114 v.addSources(hostId, connectPoints);
Andrea Campanella545edb42018-03-20 16:37:29 -0700115 return v;
116 });
117 }
118
119 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200120 public void storeSources(McastRoute route, Set<ConnectPoint> sources) {
121 mcastRoutes.compute(route, (k, v) -> {
122 v.addSources(HostId.NONE, sources);
123 return v;
124 });
125 }
126
127
128 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700129 public void removeSources(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700130 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700131 v.removeSources();
Pier139babb2018-03-23 14:59:49 -0700132 // Since we have cleared the sources, we should remove the route
133 return null;
Andrea Campanella545edb42018-03-20 16:37:29 -0700134 });
135 }
136
137 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200138 public void removeSource(McastRoute route, HostId source) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700139 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200140 v.removeSource(source);
Pier139babb2018-03-23 14:59:49 -0700141 // Since there are no sources, we should remove the route
142 return v.sources().isEmpty() ? null : v;
Andrea Campanella545edb42018-03-20 16:37:29 -0700143 });
Andrea Campanella545edb42018-03-20 16:37:29 -0700144 }
145
146 @Override
Ilayda Ozdemir69a93c42020-06-10 08:31:27 +0000147 public void removeSources(McastRoute route, Set<ConnectPoint> sources) {
148 mcastRoutes.compute(route, (k, v) -> {
149 v.removeSources(HostId.NONE, sources);
150 return v;
151 });
152 }
153
154 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200155 public void removeSources(McastRoute route, HostId hostId, Set<ConnectPoint> sources) {
156 mcastRoutes.compute(route, (k, v) -> {
157 v.removeSources(hostId, sources);
158 return v;
159 });
160 }
161
162 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700163 public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700164 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700165 v.addSinks(hostId, sinks);
166 return v;
167 });
168 }
169
170 @Override
171 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700172 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700173 v.addSinks(HostId.NONE, sinks);
174 return v;
175 });
176 }
177
178
179 @Override
180 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700181 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700182 v.removeSinks();
183 return v;
184 });
185 }
186
187 @Override
188 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700189 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700190 v.removeSinks(hostId);
191 return v;
192 });
193 }
194
195 @Override
196 public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700197 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700198 v.removeSinks(hostId, sinks);
199 return v;
200 });
201 }
202
203 @Override
204 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700205 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700206 v.removeSinks(HostId.NONE, sinks);
207 return v;
208 });
209 }
210
211 @Override
212 public Set<ConnectPoint> sourcesFor(McastRoute route) {
213 McastRouteData data = mcastRoutes.getOrDefault(route, null);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200214 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources().values().stream()
215 .flatMap(Collection::stream).collect(Collectors.toSet()));
216 }
217
218 @Override
219 public Set<ConnectPoint> sourcesFor(McastRoute route, HostId hostId) {
220 McastRouteData data = mcastRoutes.getOrDefault(route, null);
221 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -0700222 }
223
224 @Override
225 public Set<ConnectPoint> sinksFor(McastRoute route) {
226 McastRouteData data = mcastRoutes.getOrDefault(route, null);
227 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
228 .flatMap(Collection::stream).collect(Collectors.toSet()));
229 }
230
231 @Override
232 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
233 McastRouteData data = mcastRoutes.getOrDefault(route, null);
234 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
235 }
236
237 @Override
238 public Set<McastRoute> getRoutes() {
239 return ImmutableSet.copyOf(mcastRoutes.keySet());
240 }
241
242 @Override
243 public McastRouteData getRouteData(McastRoute route) {
244 return mcastRoutes.get(route);
245 }
246
247 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
248 @Override
249 public void event(MapEvent<McastRoute, McastRouteData> event) {
250 final McastRoute route = event.key();
251 final McastRouteData newData =
252 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
253 final McastRouteData oldData =
254 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
255
256 switch (event.type()) {
257 case INSERT:
258 checkNotNull(newData);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200259 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null,
Andrea Campanella545edb42018-03-20 16:37:29 -0700260 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
261 break;
262 case UPDATE:
263 checkNotNull(newData);
264 checkNotNull(oldData);
265
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200266 if (newData.allSources().size() > oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700267 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
268 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
269 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200270 } else if (newData.allSources().size() < oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700271 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
272 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
273 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
274 }
275 if (newData.allSinks().size() > oldData.allSinks().size()) {
276 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
277 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
278 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
279 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700280 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_REMOVED,
281 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
282 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
283 }
284 break;
285 case REMOVE:
286 // Verify old data is not null
287 checkNotNull(oldData);
288 // Create a route removed event with just the route
289 // and the source connect point
290 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
291 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
292 null));
293 break;
294 default:
295 log.warn("Unknown mcast operation type: {}", event.type());
296 }
297 }
298 }
299}