blob: 84e213126f93bd64acdfa8ed8d7c334c3aede20d [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;
20import com.google.common.collect.Sets;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.mcast.api.McastEvent;
29import org.onosproject.mcast.api.McastRoute;
30import org.onosproject.mcast.api.McastRouteData;
31import org.onosproject.mcast.api.McastStore;
32import org.onosproject.mcast.api.McastStoreDelegate;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.HostId;
35import org.onosproject.store.AbstractStore;
36import org.onosproject.store.serializers.KryoNamespaces;
37import org.onosproject.store.service.ConsistentMap;
38import org.onosproject.store.service.MapEvent;
39import org.onosproject.store.service.MapEventListener;
40import org.onosproject.store.service.Serializer;
41import org.onosproject.store.service.StorageService;
42import org.onosproject.store.service.Versioned;
43import org.slf4j.Logger;
44
45import java.util.Collection;
46import java.util.Map;
47import java.util.Optional;
48import java.util.Set;
Andrea Campanella545edb42018-03-20 16:37:29 -070049import java.util.concurrent.atomic.AtomicReference;
50import java.util.stream.Collectors;
51
52import static com.google.common.base.Preconditions.checkNotNull;
53import static org.onosproject.mcast.api.McastRouteUpdate.mcastRouteUpdate;
54import static org.slf4j.LoggerFactory.getLogger;
55
56/**
57 * New distributed mcast route store implementation. Routes are stored consistently
58 * across the cluster.
59 */
60@Component(immediate = true)
61@Service
62public class DistributedMcastRoutesStore
63 extends AbstractStore<McastEvent, McastStoreDelegate>
64 implements McastStore {
65
66 private static final String MCASTRIB = "onos-mcast-route-table";
67 private Logger log = getLogger(getClass());
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected StorageService storageService;
71
72 private Map<McastRoute, McastRouteData> mcastRoutes;
73 private ConsistentMap<McastRoute, McastRouteData> mcastRib;
74 private MapEventListener<McastRoute, McastRouteData> mcastRouteListener =
75 new McastRouteListener();
76
Andrea Campanella545edb42018-03-20 16:37:29 -070077 @Activate
78 public void activate() {
79 mcastRib = storageService.<McastRoute, McastRouteData>consistentMapBuilder()
80 .withName(MCASTRIB)
81 .withSerializer(Serializer.using(KryoNamespace.newBuilder()
82 .register(KryoNamespaces.API)
83 .register(
84 McastRoute.class,
85 AtomicReference.class,
86 McastRouteData.class,
87 McastRoute.Type.class
88 ).build()))
89 .build();
90
91 mcastRoutes = mcastRib.asJavaMap();
92 mcastRib.addListener(mcastRouteListener);
93
94 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
99 mcastRib.removeListener(mcastRouteListener);
100 mcastRib.destroy();
101 log.info("Stopped");
102 }
103
104 @Override
105 public void storeRoute(McastRoute route) {
Pier139babb2018-03-23 14:59:49 -0700106 mcastRoutes.putIfAbsent(route, McastRouteData.empty());
Andrea Campanella545edb42018-03-20 16:37:29 -0700107 }
108
109 @Override
110 public void removeRoute(McastRoute route) {
111 mcastRoutes.remove(route);
112 }
113
114 @Override
115 public void storeSources(McastRoute route, Set<ConnectPoint> sources) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700116 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700117 v.addSources(sources);
118 return v;
119 });
120 }
121
122 @Override
123 public void removeSources(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700124 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700125 v.removeSources();
Pier139babb2018-03-23 14:59:49 -0700126 // Since we have cleared the sources, we should remove the route
127 return null;
Andrea Campanella545edb42018-03-20 16:37:29 -0700128 });
129 }
130
131 @Override
132 public void removeSources(McastRoute route, Set<ConnectPoint> sources) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700133 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700134 v.removeSources(sources);
Pier139babb2018-03-23 14:59:49 -0700135 // Since there are no sources, we should remove the route
136 return v.sources().isEmpty() ? null : v;
Andrea Campanella545edb42018-03-20 16:37:29 -0700137 });
Andrea Campanella545edb42018-03-20 16:37:29 -0700138 }
139
140 @Override
141 public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700142 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700143 v.addSinks(hostId, sinks);
144 return v;
145 });
146 }
147
148 @Override
149 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700150 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700151 v.addSinks(HostId.NONE, sinks);
152 return v;
153 });
154 }
155
156
157 @Override
158 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700159 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700160 v.removeSinks();
161 return v;
162 });
163 }
164
165 @Override
166 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700167 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700168 v.removeSinks(hostId);
169 return v;
170 });
171 }
172
173 @Override
174 public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700175 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700176 v.removeSinks(hostId, sinks);
177 return v;
178 });
179 }
180
181 @Override
182 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700183 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700184 v.removeSinks(HostId.NONE, sinks);
185 return v;
186 });
187 }
188
189 @Override
190 public Set<ConnectPoint> sourcesFor(McastRoute route) {
191 McastRouteData data = mcastRoutes.getOrDefault(route, null);
192 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources());
193 }
194
195 @Override
196 public Set<ConnectPoint> sinksFor(McastRoute route) {
197 McastRouteData data = mcastRoutes.getOrDefault(route, null);
198 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
199 .flatMap(Collection::stream).collect(Collectors.toSet()));
200 }
201
202 @Override
203 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
204 McastRouteData data = mcastRoutes.getOrDefault(route, null);
205 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
206 }
207
208 @Override
209 public Set<McastRoute> getRoutes() {
210 return ImmutableSet.copyOf(mcastRoutes.keySet());
211 }
212
213 @Override
214 public McastRouteData getRouteData(McastRoute route) {
215 return mcastRoutes.get(route);
216 }
217
218 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
219 @Override
220 public void event(MapEvent<McastRoute, McastRouteData> event) {
221 final McastRoute route = event.key();
222 final McastRouteData newData =
223 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
224 final McastRouteData oldData =
225 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
226
227 switch (event.type()) {
228 case INSERT:
229 checkNotNull(newData);
230 McastEvent.Type type;
231 if (!newData.sources().isEmpty() || !newData.sinks().isEmpty()) {
232 type = McastEvent.Type.SOURCES_ADDED;
233 } else if (!newData.sinks().isEmpty()) {
234 type = McastEvent.Type.SINKS_ADDED;
235 } else {
236 type = McastEvent.Type.ROUTE_ADDED;
237 }
238 notifyDelegate(new McastEvent(type, null,
239 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
240 break;
241 case UPDATE:
242 checkNotNull(newData);
243 checkNotNull(oldData);
244
245 if (!Sets.difference(newData.sources(), oldData.sources()).isEmpty()) {
246 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
247 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
248 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
249 }
250 if (!Sets.difference(oldData.sources(), newData.sources()).isEmpty()) {
251 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
252 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
253 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
254 }
255 if (newData.allSinks().size() > oldData.allSinks().size()) {
256 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
257 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
258 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
259 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
Andrea Campanella545edb42018-03-20 16:37:29 -0700260 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_REMOVED,
261 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
262 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
263 }
264 break;
265 case REMOVE:
266 // Verify old data is not null
267 checkNotNull(oldData);
268 // Create a route removed event with just the route
269 // and the source connect point
270 notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
271 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
272 null));
273 break;
274 default:
275 log.warn("Unknown mcast operation type: {}", event.type());
276 }
277 }
278 }
279}