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.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.mcast.api.McastEvent; |
| 28 | import org.onosproject.mcast.api.McastRoute; |
| 29 | import org.onosproject.mcast.api.McastRouteData; |
| 30 | import org.onosproject.mcast.api.McastStore; |
| 31 | import org.onosproject.mcast.api.McastStoreDelegate; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.HostId; |
| 34 | import org.onosproject.store.AbstractStore; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.service.ConsistentMap; |
| 37 | import org.onosproject.store.service.MapEvent; |
| 38 | import org.onosproject.store.service.MapEventListener; |
| 39 | import org.onosproject.store.service.Serializer; |
| 40 | import org.onosproject.store.service.StorageService; |
| 41 | import org.onosproject.store.service.Versioned; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.Map; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 48 | import java.util.concurrent.atomic.AtomicReference; |
| 49 | import java.util.stream.Collectors; |
| 50 | |
| 51 | import static com.google.common.base.Preconditions.checkNotNull; |
| 52 | import static org.onosproject.mcast.api.McastRouteUpdate.mcastRouteUpdate; |
| 53 | import 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 |
| 61 | public 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 76 | @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) { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 105 | mcastRoutes.putIfAbsent(route, McastRouteData.empty()); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void removeRoute(McastRoute route) { |
| 110 | mcastRoutes.remove(route); |
| 111 | } |
| 112 | |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 113 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 114 | @Override |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 115 | public void storeSource(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 116 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 117 | v.addSources(hostId, connectPoints); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 118 | return v; |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | @Override |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 123 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 132 | public void removeSources(McastRoute route) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 133 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 134 | v.removeSources(); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 135 | // Since we have cleared the sources, we should remove the route |
| 136 | return null; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
| 140 | @Override |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 141 | public void removeSource(McastRoute route, HostId source) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 142 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 143 | v.removeSource(source); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 144 | // Since there are no sources, we should remove the route |
| 145 | return v.sources().isEmpty() ? null : v; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 146 | }); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 150 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 158 | public void addSink(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 159 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 160 | v.addSinks(hostId, sinks); |
| 161 | return v; |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void addSinks(McastRoute route, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 167 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 168 | v.addSinks(HostId.NONE, sinks); |
| 169 | return v; |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | @Override |
| 175 | public void removeSinks(McastRoute route) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 176 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 177 | v.removeSinks(); |
| 178 | return v; |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void removeSink(McastRoute route, HostId hostId) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 184 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 185 | v.removeSinks(hostId); |
| 186 | return v; |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void removeSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 192 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 193 | v.removeSinks(hostId, sinks); |
| 194 | return v; |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void removeSinks(McastRoute route, Set<ConnectPoint> sinks) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 200 | mcastRoutes.compute(route, (k, v) -> { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 201 | 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 Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 209 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 217 | } |
| 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 Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 254 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, null, |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 255 | mcastRouteUpdate(route, newData.sources(), newData.sinks()))); |
| 256 | break; |
| 257 | case UPDATE: |
| 258 | checkNotNull(newData); |
| 259 | checkNotNull(oldData); |
| 260 | |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 261 | if (newData.allSources().size() > oldData.allSources().size()) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 262 | notifyDelegate(new McastEvent(McastEvent.Type.SOURCES_ADDED, |
| 263 | mcastRouteUpdate(route, oldData.sources(), oldData.sinks()), |
| 264 | mcastRouteUpdate(route, newData.sources(), newData.sinks()))); |
Andrea Campanella | d4fd89f | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 265 | } else if (newData.allSources().size() < oldData.allSources().size()) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 266 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 275 | 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 | } |