[Falcon] Refactored mcast store implementation.
Change-Id: Ie3fbc675d02c5abe5f5a419d2fc12dbe8fb4ec35
refactored mcast store implementation
Change-Id: I67d70d678813184c522c78e0771f6b8f8f9c25f8
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java
new file mode 100644
index 0000000..2ad34d8
--- /dev/null
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java
@@ -0,0 +1,177 @@
+package org.onosproject.incubator.store.mcast.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.packet.IpPrefix;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.mcast.McastEvent;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.net.mcast.McastRouteInfo;
+import org.onosproject.net.mcast.McastStore;
+import org.onosproject.net.mcast.McastStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * A distributed mcast store implementation. Routes are stored consistently
+ * across the cluster.
+ */
+@Component(immediate = true)
+@Service
+public class DistributedMcastStore extends AbstractStore<McastEvent, McastStoreDelegate>
+ implements McastStore {
+ //FIXME the number of events that will potentially be generated here is
+ // not sustainable, consider changing this to an eventually consistent
+ // map and not emitting events but rather use a provider-like mechanism
+ // to program the dataplane.
+
+ private static final String MCASTRIB = "mcast-rib-table";
+ private Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private StorageService storageService;
+
+ protected ConsistentMap<McastRoute, MulticastData> mcastRIB;
+ protected Map<McastRoute, MulticastData> mcastRoutes;
+
+
+ @Activate
+ public void activate() {
+
+ mcastRIB = storageService.<McastRoute, MulticastData>consistentMapBuilder()
+ .withName(MCASTRIB)
+ .withSerializer(Serializer.using(KryoNamespace.newBuilder().register(
+ MulticastData.class,
+ McastRoute.class,
+ McastRoute.Type.class,
+ IpPrefix.class,
+ List.class,
+ ConnectPoint.class
+ ).build()))
+ .withRelaxedReadConsistency()
+ .build();
+
+ mcastRoutes = mcastRIB.asJavaMap();
+
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ log.info("Stopped");
+ }
+
+ @Override
+ public void storeRoute(McastRoute route, Type operation) {
+ switch (operation) {
+ case ADD:
+ if (mcastRoutes.putIfAbsent(route, MulticastData.empty()) == null) {
+ delegate.notify(new McastEvent(McastEvent.Type.ROUTE_ADDED,
+ McastRouteInfo.mcastRouteInfo(route)));
+ }
+ break;
+ case REMOVE:
+ if (mcastRoutes.remove(route) != null) {
+ delegate.notify(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
+ McastRouteInfo.mcastRouteInfo(route)));
+ }
+ break;
+ default:
+ log.warn("Unknown mcast operation type: {}", operation);
+ }
+ }
+
+ @Override
+ public void storeSource(McastRoute route, ConnectPoint source) {
+ MulticastData data = mcastRoutes.compute(route, (k, v) -> {
+ if (v == null) {
+ return new MulticastData(source);
+ } else {
+ v.setSource(source);
+ }
+ return v;
+ });
+
+
+ if (data != null) {
+ delegate.notify(new McastEvent(McastEvent.Type.SOURCE_ADDED,
+ McastRouteInfo.mcastRouteInfo(route,
+ data.sinks(),
+ source)));
+ }
+
+ }
+
+ @Override
+ public void storeSink(McastRoute route, ConnectPoint sink, Type operation) {
+ MulticastData data = mcastRoutes.compute(route, (k, v) -> {
+ switch (operation) {
+ case ADD:
+ if (v == null) {
+ v = MulticastData.empty();
+ }
+ v.appendSink(sink);
+ break;
+ case REMOVE:
+ if (v != null) {
+ v.removeSink(sink);
+ }
+ break;
+ default:
+ log.warn("Unknown mcast operation type: {}", operation);
+ }
+ return v;
+ });
+
+
+ if (data != null) {
+ switch (operation) {
+ case ADD:
+ delegate.notify(new McastEvent(
+ McastEvent.Type.SINK_ADDED,
+ McastRouteInfo.mcastRouteInfo(route,
+ sink,
+ data.source())));
+ break;
+ case REMOVE:
+ if (data != null) {
+ delegate.notify(new McastEvent(
+ McastEvent.Type.SINK_REMOVED,
+ McastRouteInfo.mcastRouteInfo(route,
+ sink,
+ data.source())));
+ }
+ break;
+ default:
+ log.warn("Unknown mcast operation type: {}", operation);
+ }
+ }
+
+ }
+
+ @Override
+ public ConnectPoint sourceFor(McastRoute route) {
+ return mcastRoutes.getOrDefault(route, MulticastData.empty()).source();
+ }
+
+ @Override
+ public Set<ConnectPoint> sinksFor(McastRoute route) {
+ return mcastRoutes.getOrDefault(route, MulticastData.empty()).sinks();
+ }
+
+}
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java
new file mode 100644
index 0000000..412d6ef
--- /dev/null
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.incubator.store.mcast.impl;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Simple entity maintaining a mapping between a source and a collection of sink
+ * connect points.
+ */
+public final class MulticastData {
+
+ private final AtomicReference<ConnectPoint> source =
+ new AtomicReference<>();
+ private final Set<ConnectPoint> sinks;
+ private final AtomicBoolean isEmpty = new AtomicBoolean();
+
+ private MulticastData() {
+ this.sinks = Sets.newConcurrentHashSet();
+ isEmpty.set(true);
+ }
+
+ public MulticastData(ConnectPoint source) {
+ this.source.set(checkNotNull(source, "Multicast source cannot be null."));
+ this.sinks = Sets.newConcurrentHashSet();
+ isEmpty.set(false);
+ }
+
+ public ConnectPoint source() {
+ return source.get();
+ }
+
+ public Set<ConnectPoint> sinks() {
+ return ImmutableSet.copyOf(sinks);
+ }
+
+ public void setSource(ConnectPoint source) {
+ isEmpty.set(false);
+ this.source.set(source);
+ }
+
+ public void appendSink(ConnectPoint sink) {
+ sinks.add(sink);
+ }
+
+ public boolean removeSink(ConnectPoint sink) {
+ return sinks.remove(sink);
+ }
+
+ public boolean isEmpty() {
+ return isEmpty.get();
+ }
+
+ public static MulticastData empty() {
+ return new MulticastData();
+ }
+
+}
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/package-info.java b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/package-info.java
new file mode 100644
index 0000000..d18b9e1
--- /dev/null
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * A distributed multicast store implementation that stores multicast rib
+ * data consistently across the cluster.
+ */
+package org.onosproject.incubator.store.mcast.impl;