ONOS-6889 Move mcast from incubator to core
Change-Id: Icfc7294e7017b640321dd9343778cd11c2cd0cb8
diff --git a/core/net/src/main/java/org/onosproject/net/mcast/impl/MulticastRouteManager.java b/core/net/src/main/java/org/onosproject/net/mcast/impl/MulticastRouteManager.java
new file mode 100644
index 0000000..913dcdb
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/mcast/impl/MulticastRouteManager.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.net.mcast.impl;
+
+import java.util.Set;
+
+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.onosproject.event.AbstractListenerManager;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.mcast.McastEvent;
+import org.onosproject.net.mcast.McastListener;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.net.mcast.McastStore;
+import org.onosproject.net.mcast.McastStoreDelegate;
+import org.onosproject.net.mcast.MulticastRouteService;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * An implementation of a multicast route table.
+ */
+@Component(immediate = true)
+@Service
+public class MulticastRouteManager
+ extends AbstractListenerManager<McastEvent, McastListener>
+ implements MulticastRouteService {
+ //TODO: add MulticastRouteAdminService
+
+ private Logger log = getLogger(getClass());
+
+ private final McastStoreDelegate delegate = new InternalMcastStoreDelegate();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected McastStore store;
+
+ @Activate
+ public void activate() {
+ eventDispatcher.addSink(McastEvent.class, listenerRegistry);
+ store.setDelegate(delegate);
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ store.unsetDelegate(delegate);
+ eventDispatcher.removeSink(McastEvent.class);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void add(McastRoute route) {
+ checkNotNull(route, "Route cannot be null");
+ store.storeRoute(route, McastStore.Type.ADD);
+ }
+
+ @Override
+ public void remove(McastRoute route) {
+ checkNotNull(route, "Route cannot be null");
+ store.storeRoute(route, McastStore.Type.REMOVE);
+ }
+
+ @Override
+ public Set<McastRoute> getRoutes() {
+ return store.getRoutes();
+ }
+
+ @Override
+ public void addSource(McastRoute route, ConnectPoint connectPoint) {
+ checkNotNull(route, "Route cannot be null");
+ checkNotNull(connectPoint, "Source cannot be null");
+ store.storeSource(route, connectPoint);
+ }
+
+ @Override
+ public void addSink(McastRoute route, ConnectPoint connectPoint) {
+ checkNotNull(route, "Route cannot be null");
+ checkNotNull(connectPoint, "Sink cannot be null");
+ store.storeSink(route, connectPoint, McastStore.Type.ADD);
+
+ }
+
+ @Override
+ public void removeSink(McastRoute route, ConnectPoint connectPoint) {
+
+ checkNotNull(route, "Route cannot be null");
+ checkNotNull(connectPoint, "Sink cannot be null");
+
+ store.storeSink(route, connectPoint, McastStore.Type.REMOVE);
+ }
+
+ @Override
+ public ConnectPoint fetchSource(McastRoute route) {
+ return store.sourceFor(route);
+ }
+
+ @Override
+ public Set<ConnectPoint> fetchSinks(McastRoute route) {
+ return store.sinksFor(route);
+ }
+
+ private class InternalMcastStoreDelegate implements McastStoreDelegate {
+ @Override
+ public void notify(McastEvent event) {
+ post(event);
+ }
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/mcast/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/mcast/impl/package-info.java
new file mode 100644
index 0000000..19b8d61
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/mcast/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * An implementation of a multicast RIB.
+ */
+package org.onosproject.net.mcast.impl;
diff --git a/core/net/src/test/java/org/onosproject/net/mcast/impl/MulticastRouteManagerTest.java b/core/net/src/test/java/org/onosproject/net/mcast/impl/MulticastRouteManagerTest.java
new file mode 100644
index 0000000..f5b26c2
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/mcast/impl/MulticastRouteManagerTest.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.net.mcast.impl;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.common.event.impl.TestEventDispatcher;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.store.mcast.impl.DistributedMcastStore;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.mcast.McastEvent;
+import org.onosproject.net.mcast.McastListener;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+
+import static junit.framework.Assert.fail;
+import static junit.framework.TestCase.assertEquals;
+import static org.onosproject.net.NetTestTools.did;
+import static org.onosproject.net.NetTestTools.injectEventDispatcher;
+
+/**
+ * Tests for the multicast RIB.
+ */
+public class MulticastRouteManagerTest {
+
+ McastRoute r1 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
+ IpAddress.valueOf("1.1.1.2"),
+ McastRoute.Type.IGMP);
+
+ McastRoute r11 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
+ IpAddress.valueOf("1.1.1.2"),
+ McastRoute.Type.STATIC);
+
+ McastRoute r2 = new McastRoute(IpAddress.valueOf("2.2.2.1"),
+ IpAddress.valueOf("2.2.2.2"),
+ McastRoute.Type.PIM);
+
+ ConnectPoint cp1 = new ConnectPoint(did("1"), PortNumber.portNumber(1));
+
+ ConnectPoint cp2 = new ConnectPoint(did("2"), PortNumber.portNumber(2));
+
+ private TestMulticastListener listener = new TestMulticastListener();
+
+ private MulticastRouteManager manager;
+
+ private List<McastEvent> events;
+
+ private DistributedMcastStore mcastStore;
+
+ @Before
+ public void setUp() throws Exception {
+ manager = new MulticastRouteManager();
+ mcastStore = new DistributedMcastStore();
+ TestUtils.setField(mcastStore, "storageService", new TestStorageService());
+ injectEventDispatcher(manager, new TestEventDispatcher());
+ events = Lists.newArrayList();
+ manager.store = mcastStore;
+ mcastStore.activate();
+ manager.activate();
+ manager.addListener(listener);
+ }
+
+ @After
+ public void tearDown() {
+ manager.removeListener(listener);
+ manager.deactivate();
+ mcastStore.deactivate();
+ }
+
+ @Test
+ public void testAdd() {
+ manager.add(r1);
+
+ validateEvents(McastEvent.Type.ROUTE_ADDED);
+ }
+
+ @Test
+ public void testRemove() {
+ manager.add(r1);
+
+ manager.remove(r1);
+
+
+ validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED);
+ }
+
+ @Test
+ public void testAddSource() {
+ manager.addSource(r1, cp1);
+
+ validateEvents(McastEvent.Type.SOURCE_ADDED);
+ assertEquals("Route is not equal", cp1, manager.fetchSource(r1));
+ }
+
+ @Test
+ public void testAddSink() {
+ manager.addSink(r1, cp1);
+
+ validateEvents(McastEvent.Type.SINK_ADDED);
+ assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
+ }
+
+ @Test
+ public void testRemoveSink() {
+
+ manager.addSource(r1, cp1);
+ manager.addSink(r1, cp1);
+ manager.addSink(r1, cp2);
+ manager.removeSink(r1, cp2);
+
+ validateEvents(McastEvent.Type.SOURCE_ADDED,
+ McastEvent.Type.SINK_ADDED,
+ McastEvent.Type.SINK_ADDED,
+ McastEvent.Type.SINK_REMOVED);
+ assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
+ }
+
+ private void validateEvents(McastEvent.Type... evs) {
+ if (events.size() != evs.length) {
+ fail(String.format("Mismatch number of events# obtained -> %s : expected %s",
+ events, evs));
+ }
+
+ for (int i = 0; i < evs.length; i++) {
+ if (evs[i] != events.get(i).type()) {
+ fail(String.format("Mismatched events# obtained -> %s : expected %s",
+ events, evs));
+ }
+ }
+ }
+
+ class TestMulticastListener implements McastListener {
+ @Override
+ public void event(McastEvent event) {
+ events.add(event);
+ }
+ }
+
+ private class TestCoreService extends CoreServiceAdapter {
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return new DefaultApplicationId(0, name);
+ }
+ }
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/DistributedMcastStore.java b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/DistributedMcastStore.java
new file mode 100644
index 0000000..8a3db91
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/DistributedMcastStore.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.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.util.KryoNamespace;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.mcast.McastEvent;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.net.mcast.McastStore;
+import org.onosproject.net.mcast.McastStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+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.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.onosproject.net.mcast.McastRouteInfo.mcastRouteInfo;
+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 = "onos-mcast-rib-table";
+ private Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected 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(KryoNamespaces.API)
+ .register(
+ AtomicReference.class,
+ MulticastData.class,
+ McastRoute.class,
+ McastRoute.Type.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) {
+ notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_ADDED,
+ mcastRouteInfo(route)));
+ }
+ break;
+ case REMOVE:
+ if (mcastRoutes.remove(route) != null) {
+ notifyDelegate(new McastEvent(McastEvent.Type.ROUTE_REMOVED,
+ 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) {
+ notifyDelegate(new McastEvent(McastEvent.Type.SOURCE_ADDED,
+ 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:
+ notifyDelegate(new McastEvent(McastEvent.Type.SINK_ADDED,
+ mcastRouteInfo(route, sink,
+ data.source())));
+ break;
+ case REMOVE:
+ if (data != null) {
+ notifyDelegate(new McastEvent(McastEvent.Type.SINK_REMOVED,
+ 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();
+ }
+
+ @Override
+ public Set<McastRoute> getRoutes() {
+ return mcastRoutes.keySet();
+ }
+
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/MulticastData.java b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/MulticastData.java
new file mode 100644
index 0000000..3dd8297
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/MulticastData.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.store.mcast.impl;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Map;
+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 Map<ConnectPoint, Boolean> sinks;
+ private final AtomicBoolean isEmpty = new AtomicBoolean();
+
+ private MulticastData() {
+ this.sinks = Maps.newConcurrentMap();
+ isEmpty.set(true);
+ }
+
+ public MulticastData(ConnectPoint source) {
+ this.source.set(checkNotNull(source, "Multicast source cannot be null."));
+ this.sinks = Maps.newConcurrentMap();
+ isEmpty.set(false);
+ }
+
+ public ConnectPoint source() {
+ return source.get();
+ }
+
+ public Set<ConnectPoint> sinks() {
+ return ImmutableSet.copyOf(sinks.keySet());
+ }
+
+ public void setSource(ConnectPoint source) {
+ // FIXME: violates immutability
+ isEmpty.set(false);
+ this.source.set(source);
+ }
+
+ public void appendSink(ConnectPoint sink) {
+ checkNotNull(sink);
+ sinks.put(sink, true);
+ }
+
+ public void removeSink(ConnectPoint sink) {
+ checkNotNull(sink);
+ sinks.remove(sink);
+ }
+
+ public boolean isEmpty() {
+ return isEmpty.get();
+ }
+
+ public static MulticastData empty() {
+ return new MulticastData();
+ }
+
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/package-info.java b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/package-info.java
new file mode 100644
index 0000000..99f28b5
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/mcast/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.store.mcast.impl;