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