Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.mcast.impl; |
| 17 | |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.mcast.api.McastEvent; |
| 22 | import org.onosproject.mcast.api.McastRoute; |
| 23 | import org.onosproject.mcast.api.McastRouteData; |
| 24 | import org.onosproject.mcast.api.McastStore; |
| 25 | import org.onosproject.mcast.api.McastStoreDelegate; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.HostId; |
| 28 | import org.onosproject.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.annotations.Activate; |
| 37 | import org.osgi.service.component.annotations.Component; |
| 38 | import org.osgi.service.component.annotations.Deactivate; |
| 39 | import org.osgi.service.component.annotations.Reference; |
| 40 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Collection; |
| 44 | import java.util.Map; |
| 45 | import java.util.Optional; |
| 46 | import java.util.Set; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicReference; |
| 48 | import java.util.stream.Collectors; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
| 51 | import static org.onosproject.mcast.api.McastRouteUpdate.mcastRouteUpdate; |
| 52 | import static org.slf4j.LoggerFactory.getLogger; |
| 53 | |
| 54 | /** |
| 55 | * New distributed mcast route store implementation. Routes are stored consistently |
| 56 | * across the cluster. |
| 57 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | @Component(immediate = true, service = McastStore.class) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 59 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 67 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 74 | @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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void storeRoute(McastRoute route) { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 102 | mcastRoutes.putIfAbsent(route, McastRouteData.empty()); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void removeRoute(McastRoute route) { |
| 107 | mcastRoutes.remove(route); |
| 108 | } |
| 109 | |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 110 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 111 | @Override |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 112 | public void storeSource(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 113 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 114 | v.addSources(hostId, connectPoints); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 115 | return v; |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | @Override |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 120 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 129 | public void removeSources(McastRoute route) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 130 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 131 | v.removeSources(); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 132 | // Since we have cleared the sources, we should remove the route |
| 133 | return null; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 134 | }); |
| 135 | } |
| 136 | |
| 137 | @Override |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 138 | public void removeSource(McastRoute route, HostId source) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 139 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 140 | v.removeSource(source); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 141 | // Since there are no sources, we should remove the route |
| 142 | return v.sources().isEmpty() ? null : v; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 143 | }); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Ilayda Ozdemir | 69a93c4 | 2020-06-10 08:31:27 +0000 | [diff] [blame] | 147 | 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 Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 155 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 163 | public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 164 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 165 | v.addSinks(hostId, sinks); |
| 166 | return v; |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void addSinks(McastRoute route, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 172 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 173 | v.addSinks(HostId.NONE, sinks); |
| 174 | return v; |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | @Override |
| 180 | public void removeSinks(McastRoute route) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 181 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 182 | v.removeSinks(); |
| 183 | return v; |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void removeSink(McastRoute route, HostId hostId) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 189 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 190 | v.removeSinks(hostId); |
| 191 | return v; |
| 192 | }); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 197 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 198 | v.removeSinks(hostId, sinks); |
| 199 | return v; |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 205 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 206 | 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 Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 214 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 222 | } |
| 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 Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 259 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null, |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 260 | mcastRouteUpdate(route, newData.sources(), newData.sinks()))); |
| 261 | break; |
| 262 | case UPDATE: |
| 263 | checkNotNull(newData); |
| 264 | checkNotNull(oldData); |
| 265 | |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 266 | if (newData.allSources().size() > oldData.allSources().size()) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 267 | notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED, |
| 268 | mcastRouteUpdate(route, oldData.sources(), oldData.sinks()), |
| 269 | mcastRouteUpdate(route, newData.sources(), newData.sinks()))); |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 270 | } else if (newData.allSources().size() < oldData.allSources().size()) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 271 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 280 | 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 | } |