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