Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | a9ae0d4 | 2017-08-06 22:10:47 -0700 | [diff] [blame] | 16 | package org.onosproject.store.mcast.impl; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 17 | |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 18 | |
| 19 | import com.google.common.base.Objects; |
| 20 | import com.google.common.collect.Sets; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 27 | import org.onlab.util.KryoNamespace; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 28 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.mcast.McastEvent; |
| 31 | import org.onosproject.net.mcast.McastRoute; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 32 | import org.onosproject.net.mcast.McastStore; |
| 33 | import org.onosproject.net.mcast.McastStoreDelegate; |
| 34 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 35 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 36 | import org.onosproject.store.service.ConsistentMap; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 37 | import org.onosproject.store.service.MapEvent; |
| 38 | import org.onosproject.store.service.MapEventListener; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 39 | import org.onosproject.store.service.Serializer; |
| 40 | import org.onosproject.store.service.StorageService; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 41 | import org.onosproject.store.service.Versioned; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame^] | 44 | import java.util.Collections; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 45 | import java.util.Map; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 46 | import java.util.Optional; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 47 | import java.util.Set; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 48 | import java.util.concurrent.ScheduledExecutorService; |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 49 | import java.util.concurrent.atomic.AtomicReference; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 50 | |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 52 | import static org.onosproject.net.mcast.McastRouteInfo.mcastRouteInfo; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * A distributed mcast store implementation. Routes are stored consistently |
| 57 | * across the cluster. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 61 | public class DistributedMcastStore |
| 62 | extends AbstractStore<McastEvent, McastStoreDelegate> |
| 63 | implements McastStore { |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 64 | |
Jon Hall | dbe4c53 | 2016-10-04 11:45:52 -0700 | [diff] [blame] | 65 | private static final String MCASTRIB = "onos-mcast-rib-table"; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 66 | private Logger log = getLogger(getClass()); |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 69 | protected StorageService storageService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 70 | |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 71 | private Map<McastRoute, MulticastData> mcastRoutes; |
| 72 | private ConsistentMap<McastRoute, MulticastData> mcastRib; |
| 73 | private MapEventListener<McastRoute, MulticastData> mcastRouteListener = |
| 74 | new McastRouteListener(); |
| 75 | |
| 76 | private ScheduledExecutorService executor; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 77 | |
| 78 | |
| 79 | @Activate |
| 80 | public void activate() { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 81 | mcastRib = storageService.<McastRoute, MulticastData>consistentMapBuilder() |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 82 | .withName(MCASTRIB) |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 83 | .withSerializer(Serializer.using(KryoNamespace.newBuilder() |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 84 | .register(KryoNamespaces.API) |
| 85 | .register( |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 86 | AtomicReference.class, |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 87 | MulticastData.class, |
| 88 | McastRoute.class, |
| 89 | McastRoute.Type.class |
| 90 | ).build())) |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 91 | .build(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 92 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 93 | mcastRoutes = mcastRib.asJavaMap(); |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 94 | mcastRib.addListener(mcastRouteListener); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 95 | |
| 96 | log.info("Started"); |
| 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 101 | mcastRib.removeListener(mcastRouteListener); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 105 | |
| 106 | private class McastRouteListener implements MapEventListener<McastRoute, MulticastData> { |
| 107 | @Override |
| 108 | public void event(MapEvent<McastRoute, MulticastData> event) { |
| 109 | final McastRoute route = event.key(); |
| 110 | final MulticastData newData = Optional.ofNullable(event.newValue()).map(Versioned::value).orElse(null); |
| 111 | final MulticastData oldData = Optional.ofNullable(event.oldValue()).map(Versioned::value).orElse(null); |
| 112 | |
| 113 | switch (event.type()) { |
| 114 | case INSERT: |
| 115 | checkNotNull(newData); |
| 116 | |
| 117 | if (newData.source() != null) { |
| 118 | notifyDelegate(new McastEvent(McastEvent.Type.SOURCE_ADDED, |
| 119 | mcastRouteInfo(route, |
| 120 | newData.sinks(), |
| 121 | newData.source()))); |
| 122 | } else if (!newData.sinks().isEmpty()) { |
| 123 | newData.sinks().forEach(sink -> |
| 124 | notifyDelegate(new McastEvent(McastEvent.Type.SINK_ADDED, |
| 125 | mcastRouteInfo(route, |
| 126 | sink, |
| 127 | newData.source()))) |
| 128 | ); |
| 129 | } else { |
| 130 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, |
| 131 | mcastRouteInfo(route))); |
| 132 | } |
| 133 | break; |
| 134 | case UPDATE: |
| 135 | checkNotNull(newData); |
| 136 | checkNotNull(oldData); |
| 137 | |
| 138 | if (!Objects.equal(oldData.source(), newData.source())) { |
| 139 | notifyDelegate(new McastEvent(McastEvent.Type.SOURCE_ADDED, |
| 140 | mcastRouteInfo(route, |
| 141 | newData.sinks(), |
| 142 | newData.source()))); |
| 143 | } else { |
| 144 | Sets.difference(newData.sinks(), oldData.sinks()).forEach(sink -> |
| 145 | notifyDelegate(new McastEvent(McastEvent.Type.SINK_ADDED, |
| 146 | mcastRouteInfo(route, |
| 147 | sink, |
| 148 | newData.source()))) |
| 149 | ); |
| 150 | |
| 151 | Sets.difference(oldData.sinks(), newData.sinks()).forEach(sink -> |
| 152 | notifyDelegate(new McastEvent(McastEvent.Type.SINK_REMOVED, |
| 153 | mcastRouteInfo(route, |
| 154 | sink, |
| 155 | newData.source()))) |
| 156 | ); |
| 157 | } |
| 158 | break; |
| 159 | case REMOVE: |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame^] | 160 | // Verify old data is not null |
| 161 | checkNotNull(oldData); |
| 162 | // Create a route removed event with just the route |
| 163 | // and the source connect point |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 164 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED, |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame^] | 165 | mcastRouteInfo(route, |
| 166 | Collections.emptySet(), |
| 167 | oldData.source() |
| 168 | ))); |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 169 | break; |
| 170 | default: |
| 171 | log.warn("Unknown mcast operation type: {}", event.type()); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 176 | @Override |
| 177 | public void storeRoute(McastRoute route, Type operation) { |
| 178 | switch (operation) { |
| 179 | case ADD: |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 180 | mcastRoutes.putIfAbsent(route, MulticastData.empty()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 181 | break; |
| 182 | case REMOVE: |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 183 | // before remove the route should check that source and sinks are removed? |
| 184 | mcastRoutes.remove(route); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 185 | break; |
| 186 | default: |
| 187 | log.warn("Unknown mcast operation type: {}", operation); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void storeSource(McastRoute route, ConnectPoint source) { |
| 193 | MulticastData data = mcastRoutes.compute(route, (k, v) -> { |
| 194 | if (v == null) { |
| 195 | return new MulticastData(source); |
| 196 | } else { |
| 197 | v.setSource(source); |
| 198 | } |
| 199 | return v; |
| 200 | }); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void storeSink(McastRoute route, ConnectPoint sink, Type operation) { |
| 205 | MulticastData data = mcastRoutes.compute(route, (k, v) -> { |
| 206 | switch (operation) { |
| 207 | case ADD: |
| 208 | if (v == null) { |
| 209 | v = MulticastData.empty(); |
| 210 | } |
| 211 | v.appendSink(sink); |
| 212 | break; |
| 213 | case REMOVE: |
| 214 | if (v != null) { |
| 215 | v.removeSink(sink); |
| 216 | } |
| 217 | break; |
| 218 | default: |
| 219 | log.warn("Unknown mcast operation type: {}", operation); |
| 220 | } |
| 221 | return v; |
| 222 | }); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public ConnectPoint sourceFor(McastRoute route) { |
| 227 | return mcastRoutes.getOrDefault(route, MulticastData.empty()).source(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Set<ConnectPoint> sinksFor(McastRoute route) { |
| 232 | return mcastRoutes.getOrDefault(route, MulticastData.empty()).sinks(); |
| 233 | } |
| 234 | |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 235 | @Override |
| 236 | public Set<McastRoute> getRoutes() { |
| 237 | return mcastRoutes.keySet(); |
| 238 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 239 | } |