blob: c9baac3c6460e7ce6079c8e0a039d94bf8955d6e [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.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.mcast.api.McastEvent;
28import org.onosproject.mcast.api.McastRoute;
29import org.onosproject.mcast.api.McastRouteData;
30import org.onosproject.mcast.api.McastStore;
31import org.onosproject.mcast.api.McastStoreDelegate;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.HostId;
34import org.onosproject.store.AbstractStore;
35import org.onosproject.store.serializers.KryoNamespaces;
36import org.onosproject.store.service.ConsistentMap;
37import org.onosproject.store.service.MapEvent;
38import org.onosproject.store.service.MapEventListener;
39import org.onosproject.store.service.Serializer;
40import org.onosproject.store.service.StorageService;
41import org.onosproject.store.service.Versioned;
42import org.slf4j.Logger;
43
44import java.util.Collection;
45import java.util.Map;
46import java.util.Optional;
47import java.util.Set;
Andrea Campanella545edb42018-03-20 16:37:29 -070048import java.util.concurrent.atomic.AtomicReference;
49import java.util.stream.Collectors;
50
51import static com.google.common.base.Preconditions.checkNotNull;
52import static org.onosproject.mcast.api.McastRouteUpdate.mcastRouteUpdate;
53import static org.slf4j.LoggerFactory.getLogger;
54
55/**
56 * New distributed mcast route store implementation. Routes are stored consistently
57 * across the cluster.
58 */
59@Component(immediate = true)
60@Service
61public class DistributedMcastRoutesStore
62 extends AbstractStore<McastEvent, McastStoreDelegate>
63 implements McastStore {
64
65 private static final String MCASTRIB = "onos-mcast-route-table";
66 private Logger log = getLogger(getClass());
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected StorageService storageService;
70
71 private Map<McastRoute, McastRouteData> mcastRoutes;
72 private ConsistentMap<McastRoute, McastRouteData> mcastRib;
73 private MapEventListener<McastRoute, McastRouteData> mcastRouteListener =
74 new McastRouteListener();
75
Andrea Campanella545edb42018-03-20 16:37:29 -070076 @Activate
77 public void activate() {
78 mcastRib = storageService.<McastRoute, McastRouteData>consistentMapBuilder()
79 .withName(MCASTRIB)
80 .withSerializer(Serializer.using(KryoNamespace.newBuilder()
81 .register(KryoNamespaces.API)
82 .register(
83 McastRoute.class,
84 AtomicReference.class,
85 McastRouteData.class,
86 McastRoute.Type.class
87 ).build()))
88 .build();
89
90 mcastRoutes = mcastRib.asJavaMap();
91 mcastRib.addListener(mcastRouteListener);
92
93 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
98 mcastRib.removeListener(mcastRouteListener);
99 mcastRib.destroy();
100 log.info("Stopped");
101 }
102
103 @Override
104 public void storeRoute(McastRoute route) {
Pier139babb2018-03-23 14:59:49 -0700105 mcastRoutes.putIfAbsent(route, McastRouteData.empty());
Andrea Campanella545edb42018-03-20 16:37:29 -0700106 }
107
108 @Override
109 public void removeRoute(McastRoute route) {
110 mcastRoutes.remove(route);
111 }
112
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200113
Andrea Campanella545edb42018-03-20 16:37:29 -0700114 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200115 public void storeSource(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700116 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200117 v.addSources(hostId, connectPoints);
Andrea Campanella545edb42018-03-20 16:37:29 -0700118 return v;
119 });
120 }
121
122 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200123 public void storeSources(McastRoute route, Set<ConnectPoint> sources) {
124 mcastRoutes.compute(route, (k, v) -> {
125 v.addSources(HostId.NONE, sources);
126 return v;
127 });
128 }
129
130
131 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700132 public void removeSources(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700133 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700134 v.removeSources();
Pier139babb2018-03-23 14:59:49 -0700135 // Since we have cleared the sources, we should remove the route
136 return null;
Andrea Campanella545edb42018-03-20 16:37:29 -0700137 });
138 }
139
140 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200141 public void removeSource(McastRoute route, HostId source) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700142 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200143 v.removeSource(source);
Pier139babb2018-03-23 14:59:49 -0700144 // Since there are no sources, we should remove the route
145 return v.sources().isEmpty() ? null : v;
Andrea Campanella545edb42018-03-20 16:37:29 -0700146 });
Andrea Campanella545edb42018-03-20 16:37:29 -0700147 }
148
149 @Override
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200150 public void removeSources(McastRoute route, HostId hostId, Set<ConnectPoint> sources) {
151 mcastRoutes.compute(route, (k, v) -> {
152 v.removeSources(hostId, sources);
153 return v;
154 });
155 }
156
157 @Override
Andrea Campanella545edb42018-03-20 16:37:29 -0700158 public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700159 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700160 v.addSinks(hostId, sinks);
161 return v;
162 });
163 }
164
165 @Override
166 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700167 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700168 v.addSinks(HostId.NONE, sinks);
169 return v;
170 });
171 }
172
173
174 @Override
175 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700176 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700177 v.removeSinks();
178 return v;
179 });
180 }
181
182 @Override
183 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700184 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700185 v.removeSinks(hostId);
186 return v;
187 });
188 }
189
190 @Override
191 public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700192 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700193 v.removeSinks(hostId, sinks);
194 return v;
195 });
196 }
197
198 @Override
199 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700200 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700201 v.removeSinks(HostId.NONE, sinks);
202 return v;
203 });
204 }
205
206 @Override
207 public Set<ConnectPoint> sourcesFor(McastRoute route) {
208 McastRouteData data = mcastRoutes.getOrDefault(route, null);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200209 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources().values().stream()
210 .flatMap(Collection::stream).collect(Collectors.toSet()));
211 }
212
213 @Override
214 public Set<ConnectPoint> sourcesFor(McastRoute route, HostId hostId) {
215 McastRouteData data = mcastRoutes.getOrDefault(route, null);
216 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -0700217 }
218
219 @Override
220 public Set<ConnectPoint> sinksFor(McastRoute route) {
221 McastRouteData data = mcastRoutes.getOrDefault(route, null);
222 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
223 .flatMap(Collection::stream).collect(Collectors.toSet()));
224 }
225
226 @Override
227 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
228 McastRouteData data = mcastRoutes.getOrDefault(route, null);
229 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
230 }
231
232 @Override
233 public Set<McastRoute> getRoutes() {
234 return ImmutableSet.copyOf(mcastRoutes.keySet());
235 }
236
237 @Override
238 public McastRouteData getRouteData(McastRoute route) {
239 return mcastRoutes.get(route);
240 }
241
242 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
243 @Override
244 public void event(MapEvent<McastRoute, McastRouteData> event) {
245 final McastRoute route = event.key();
246 final McastRouteData newData =
247 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
248 final McastRouteData oldData =
249 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
250
251 switch (event.type()) {
252 case INSERT:
253 checkNotNull(newData);
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200254 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null,
Andrea Campanella545edb42018-03-20 16:37:29 -0700255 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
256 break;
257 case UPDATE:
258 checkNotNull(newData);
259 checkNotNull(oldData);
260
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200261 if (newData.allSources().size() > oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700262 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
263 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
264 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200265 } else if (newData.allSources().size() < oldData.allSources().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700266 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
267 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
268 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
269 }
270 if (newData.allSinks().size() > oldData.allSinks().size()) {
271 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
272 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
273 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
274 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700275 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_REMOVED,
276 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
277 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
278 }
279 break;
280 case REMOVE:
281 // Verify old data is not null
282 checkNotNull(oldData);
283 // Create a route removed event with just the route
284 // and the source connect point
285 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
286 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
287 null));
288 break;
289 default:
290 log.warn("Unknown mcast operation type: {}", event.type());
291 }
292 }
293 }
294}