blob: 8c90a1d56f9651a5af8981b806a9baf2e2e89f68 [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) {
106 mcastRoutes.put(route, McastRouteData.empty());
107 }
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();
126 return v;
127 });
128 }
129
130 @Override
131 public void removeSources(McastRoute route, Set<ConnectPoint> sources) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700132 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700133 v.removeSources(sources);
134 return v;
135 });
136
137 }
138
139 @Override
140 public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700141 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700142 v.addSinks(hostId, sinks);
143 return v;
144 });
145 }
146
147 @Override
148 public void addSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700149 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700150 v.addSinks(HostId.NONE, sinks);
151 return v;
152 });
153 }
154
155
156 @Override
157 public void removeSinks(McastRoute route) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700158 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700159 v.removeSinks();
160 return v;
161 });
162 }
163
164 @Override
165 public void removeSink(McastRoute route, HostId hostId) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700166 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700167 v.removeSinks(hostId);
168 return v;
169 });
170 }
171
172 @Override
173 public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700174 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700175 v.removeSinks(hostId, sinks);
176 return v;
177 });
178 }
179
180 @Override
181 public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) {
Andrea Campanella7c8bcdf2018-03-26 23:29:11 -0700182 mcastRoutes.compute(route, (k, v) -> {
Andrea Campanella545edb42018-03-20 16:37:29 -0700183 v.removeSinks(HostId.NONE, sinks);
184 return v;
185 });
186 }
187
188 @Override
189 public Set<ConnectPoint> sourcesFor(McastRoute route) {
190 McastRouteData data = mcastRoutes.getOrDefault(route, null);
191 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sources());
192 }
193
194 @Override
195 public Set<ConnectPoint> sinksFor(McastRoute route) {
196 McastRouteData data = mcastRoutes.getOrDefault(route, null);
197 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks().values().stream()
198 .flatMap(Collection::stream).collect(Collectors.toSet()));
199 }
200
201 @Override
202 public Set<ConnectPoint> sinksFor(McastRoute route, HostId hostId) {
203 McastRouteData data = mcastRoutes.getOrDefault(route, null);
204 return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data.sinks(hostId));
205 }
206
207 @Override
208 public Set<McastRoute> getRoutes() {
209 return ImmutableSet.copyOf(mcastRoutes.keySet());
210 }
211
212 @Override
213 public McastRouteData getRouteData(McastRoute route) {
214 return mcastRoutes.get(route);
215 }
216
217 private class McastRouteListener implements MapEventListener<McastRoute, McastRouteData> {
218 @Override
219 public void event(MapEvent<McastRoute, McastRouteData> event) {
220 final McastRoute route = event.key();
221 final McastRouteData newData =
222 Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null);
223 final McastRouteData oldData =
224 Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null);
225
226 switch (event.type()) {
227 case INSERT:
228 checkNotNull(newData);
229 McastEvent.Type type;
230 if (!newData.sources().isEmpty() || !newData.sinks().isEmpty()) {
231 type = McastEvent.Type.SOURCES_ADDED;
232 } else if (!newData.sinks().isEmpty()) {
233 type = McastEvent.Type.SINKS_ADDED;
234 } else {
235 type = McastEvent.Type.ROUTE_ADDED;
236 }
237 notifyDelegate(new McastEvent(type, null,
238 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
239 break;
240 case UPDATE:
241 checkNotNull(newData);
242 checkNotNull(oldData);
243
244 if (!Sets.difference(newData.sources(), oldData.sources()).isEmpty()) {
245 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED,
246 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
247 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
248 }
249 if (!Sets.difference(oldData.sources(), newData.sources()).isEmpty()) {
250 notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_REMOVED,
251 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
252 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
253 }
254 if (newData.allSinks().size() > oldData.allSinks().size()) {
255 notifyDelegate(new McastEvent(McastEvent.Type.SINKS_ADDED,
256 mcastRouteUpdate(route, oldData.sources(), oldData.sinks()),
257 mcastRouteUpdate(route, newData.sources(), newData.sinks())));
258 } else if (newData.allSinks().size() < oldData.allSinks().size()) {
259 log.info("Removed");
260 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}