Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Laboratory |
| 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 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 16 | package org.onosproject.incubator.store.mcast.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.mcast.McastEvent; |
| 27 | import org.onosproject.net.mcast.McastRoute; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 28 | import org.onosproject.net.mcast.McastStore; |
| 29 | import org.onosproject.net.mcast.McastStoreDelegate; |
| 30 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 31 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.slf4j.Logger; |
| 36 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 37 | import java.util.Map; |
| 38 | import java.util.Set; |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 39 | import java.util.concurrent.atomic.AtomicReference; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 40 | |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 41 | import static org.onosproject.net.mcast.McastRouteInfo.mcastRouteInfo; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * A distributed mcast store implementation. Routes are stored consistently |
| 46 | * across the cluster. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 50 | public class DistributedMcastStore extends AbstractStore<McastEvent, McastStoreDelegate> |
| 51 | implements McastStore { |
| 52 | //FIXME the number of events that will potentially be generated here is |
alshabib | 6ec1ee7 | 2015-12-18 19:42:24 -0800 | [diff] [blame] | 53 | // not sustainable, consider changing this to an eventually consistent |
| 54 | // map and not emitting events but rather use a provider-like mechanism |
| 55 | // to program the dataplane. |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 56 | |
| 57 | private static final String MCASTRIB = "mcast-rib-table"; |
| 58 | private Logger log = getLogger(getClass()); |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 61 | protected StorageService storageService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 62 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 63 | protected ConsistentMap<McastRoute, MulticastData> mcastRib; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 64 | protected Map<McastRoute, MulticastData> mcastRoutes; |
| 65 | |
| 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 70 | mcastRib = storageService.<McastRoute, MulticastData>consistentMapBuilder() |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 71 | .withName(MCASTRIB) |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 72 | .withSerializer(Serializer.using(KryoNamespace.newBuilder() |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 73 | .register(KryoNamespaces.API) |
| 74 | .register( |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 75 | AtomicReference.class, |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 76 | MulticastData.class, |
| 77 | McastRoute.class, |
| 78 | McastRoute.Type.class |
| 79 | ).build())) |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 80 | //.withRelaxedReadConsistency() |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 81 | .build(); |
| 82 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 83 | mcastRoutes = mcastRib.asJavaMap(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 84 | |
| 85 | |
| 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void storeRoute(McastRoute route, Type operation) { |
| 96 | switch (operation) { |
| 97 | case ADD: |
| 98 | if (mcastRoutes.putIfAbsent(route, MulticastData.empty()) == null) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 99 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED, |
| 100 | mcastRouteInfo(route))); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 101 | } |
| 102 | break; |
| 103 | case REMOVE: |
| 104 | if (mcastRoutes.remove(route) != null) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 105 | notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED, |
| 106 | mcastRouteInfo(route))); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 107 | } |
| 108 | break; |
| 109 | default: |
| 110 | log.warn("Unknown mcast operation type: {}", operation); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void storeSource(McastRoute route, ConnectPoint source) { |
| 116 | MulticastData data = mcastRoutes.compute(route, (k, v) -> { |
| 117 | if (v == null) { |
| 118 | return new MulticastData(source); |
| 119 | } else { |
| 120 | v.setSource(source); |
| 121 | } |
| 122 | return v; |
| 123 | }); |
| 124 | |
| 125 | |
| 126 | if (data != null) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 127 | notifyDelegate(new McastEvent(McastEvent.Type.SOURCE_ADDED, |
| 128 | mcastRouteInfo(route, |
| 129 | data.sinks(), |
| 130 | source))); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void storeSink(McastRoute route, ConnectPoint sink, Type operation) { |
| 137 | MulticastData data = mcastRoutes.compute(route, (k, v) -> { |
| 138 | switch (operation) { |
| 139 | case ADD: |
| 140 | if (v == null) { |
| 141 | v = MulticastData.empty(); |
| 142 | } |
| 143 | v.appendSink(sink); |
| 144 | break; |
| 145 | case REMOVE: |
| 146 | if (v != null) { |
| 147 | v.removeSink(sink); |
| 148 | } |
| 149 | break; |
| 150 | default: |
| 151 | log.warn("Unknown mcast operation type: {}", operation); |
| 152 | } |
| 153 | return v; |
| 154 | }); |
| 155 | |
| 156 | |
| 157 | if (data != null) { |
| 158 | switch (operation) { |
| 159 | case ADD: |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 160 | notifyDelegate(new McastEvent(McastEvent.Type.SINK_ADDED, |
| 161 | mcastRouteInfo(route, sink, |
| 162 | data.source()))); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 163 | break; |
| 164 | case REMOVE: |
| 165 | if (data != null) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 166 | notifyDelegate(new McastEvent(McastEvent.Type.SINK_REMOVED, |
| 167 | mcastRouteInfo(route, |
| 168 | sink, |
| 169 | data.source()))); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 170 | } |
| 171 | break; |
| 172 | default: |
| 173 | log.warn("Unknown mcast operation type: {}", operation); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public ConnectPoint sourceFor(McastRoute route) { |
| 181 | return mcastRoutes.getOrDefault(route, MulticastData.empty()).source(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Set<ConnectPoint> sinksFor(McastRoute route) { |
| 186 | return mcastRoutes.getOrDefault(route, MulticastData.empty()).sinks(); |
| 187 | } |
| 188 | |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 189 | @Override |
| 190 | public Set<McastRoute> getRoutes() { |
| 191 | return mcastRoutes.keySet(); |
| 192 | } |
| 193 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 194 | } |