alshabib
Committed by Gerrit Code Review

initial impl of a multicast route table

Change-Id: Ic86a0665d1ade6b85b05e602ead2bd9c0a7dde07
......@@ -21,6 +21,8 @@ import org.onosproject.net.ConnectPoint;
import java.util.Optional;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* An entity representing a multicast event. Event either add or remove
* sinks or sources.
......@@ -48,11 +50,6 @@ public class McastEvent extends AbstractEvent<McastEvent.Type, McastRoute> {
SOURCE_ADDED,
/**
* A source for a mcast route (ie. the subject) has been removed.
*/
SOURCE_REMOVED,
/**
* A sink for a mcast route (ie. the subject) has been added.
*/
SINK_ADDED,
......@@ -75,7 +72,7 @@ public class McastEvent extends AbstractEvent<McastEvent.Type, McastRoute> {
source = Optional.empty();
}
protected McastEvent(McastEvent.Type type, McastRoute subject,
public McastEvent(McastEvent.Type type, McastRoute subject,
ConnectPoint sink,
ConnectPoint source) {
super(type, subject);
......@@ -83,7 +80,7 @@ public class McastEvent extends AbstractEvent<McastEvent.Type, McastRoute> {
this.source = Optional.ofNullable(source);
}
protected McastEvent(McastEvent.Type type, McastRoute subject, long time,
public McastEvent(McastEvent.Type type, McastRoute subject, long time,
ConnectPoint sink,
ConnectPoint source) {
super(type, subject, time);
......@@ -102,12 +99,20 @@ public class McastEvent extends AbstractEvent<McastEvent.Type, McastRoute> {
}
/**
* The source which has been removed or added. The field may not be set
* if the source has not been detected yet or has been removed.
* The source which has been removed or added.
* @return an optional connect point
*/
public Optional<ConnectPoint> source() {
return source;
}
@Override
public String toString() {
return toStringHelper(this)
.add("type", type())
.add("route", subject())
.add("source", source)
.add("sinks", sink).toString();
}
}
......
......@@ -20,6 +20,7 @@ import com.google.common.base.Objects;
import org.onlab.packet.IpPrefix;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An entity representing a multicast route consisting of a source
......@@ -50,6 +51,9 @@ public class McastRoute {
private final Type type;
public McastRoute(IpPrefix source, IpPrefix group, Type type) {
checkNotNull(source, "Multicast route must have a source");
checkNotNull(group, "Multicast route must specify a group address");
checkNotNull(type, "Must indicate what type of route");
this.source = source;
this.group = group;
this.type = type;
......
......@@ -24,7 +24,7 @@ import java.util.List;
* A service interface for maintaining multicast information.
*/
@Beta
public interface MulticastRouteTable {
public interface MulticastRouteService {
/**
* Adds a route to the information base.
......@@ -59,14 +59,6 @@ public interface MulticastRouteTable {
void addSink(McastRoute route, ConnectPoint connectPoint);
/**
* Removes a source connection from the route.
*
* @param route the multicast route
* @param connectPoint a source connect point
*/
void removeSource(McastRoute route, ConnectPoint connectPoint);
/**
* Removes a sink from the route.
*
* @param route the multicast route
......
/*
* 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.
*/
/**
* External model entities of the multicast RIB.
*/
package org.onosproject.net.mcast;
\ No newline at end of file
/*
* 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.net.mcast.impl;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.onosproject.net.ConnectPoint;
import java.util.Collections;
import java.util.List;
/**
* Simple entity maintaining a mapping between a source and a collection of sink
* connect points.
*/
public final class MulticastData {
private final ConnectPoint source;
private final List<ConnectPoint> sinks;
private MulticastData() {
this.source = null;
this.sinks = Collections.EMPTY_LIST;
}
public MulticastData(ConnectPoint source, List<ConnectPoint> sinks) {
this.source = source;
this.sinks = sinks;
}
public MulticastData(ConnectPoint source, ConnectPoint sink) {
this.source = source;
this.sinks = Lists.newArrayList(sink);
}
public MulticastData(ConnectPoint source) {
this.source = source;
this.sinks = Lists.newArrayList();
}
public ConnectPoint source() {
return source;
}
public List<ConnectPoint> sinks() {
return ImmutableList.copyOf(sinks);
}
public void appendSink(ConnectPoint sink) {
sinks.add(sink);
}
public boolean removeSink(ConnectPoint sink) {
return sinks.remove(sink);
}
public boolean isEmpty() {
return source == null && sinks.size() == 0;
}
public static MulticastData empty() {
return new MulticastData();
}
}
/*
* 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.net.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.core.ApplicationId;
import org.onosproject.core.CoreService;
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.MulticastRouteService;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.Versioned;
import org.slf4j.Logger;
import java.util.List;
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 static final String MCASTRIB = "mcast-rib-table";
private Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
private StorageService storageService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
private CoreService coreService;
protected ApplicationId appId;
protected ConsistentMap<McastRoute, MulticastData> mcastRoutes;
@Activate
public void activate() {
eventDispatcher.addSink(McastEvent.class, listenerRegistry);
appId = coreService.registerApplication("org.onosproject.mcastrib");
mcastRoutes = storageService.<McastRoute, MulticastData>consistentMapBuilder()
.withApplicationId(appId)
.withName(MCASTRIB)
.withSerializer(Serializer.using(KryoNamespace.newBuilder().register(
MulticastData.class,
McastRoute.class,
McastRoute.Type.class,
IpPrefix.class,
List.class,
ConnectPoint.class
).build())).build();
log.info("Started");
}
@Deactivate
public void deactivate() {
log.info("Stopped");
}
@Override
public void add(McastRoute route) {
mcastRoutes.put(route, MulticastData.empty());
post(new McastEvent(McastEvent.Type.ROUTE_ADDED, route, null, null));
}
@Override
public void remove(McastRoute route) {
mcastRoutes.remove(route);
post(new McastEvent(McastEvent.Type.ROUTE_REMOVED, route, null, null));
}
@Override
public void addSource(McastRoute route, ConnectPoint connectPoint) {
Versioned<MulticastData> d = mcastRoutes.compute(route, (k, v) -> {
if (v.isEmpty()) {
return new MulticastData(connectPoint);
} else {
log.warn("Route {} is already in use.", route);
return v;
}
});
if (d != null) {
post(new McastEvent(McastEvent.Type.SOURCE_ADDED,
route, null, connectPoint));
}
}
@Override
public void addSink(McastRoute route, ConnectPoint connectPoint) {
mcastRoutes.compute(route, (k, v) -> {
if (!v.isEmpty()) {
v.appendSink(connectPoint);
post(new McastEvent(McastEvent.Type.SINK_ADDED, route,
connectPoint, v.source()));
} else {
log.warn("Route {} does not exist");
}
return v;
});
}
@Override
public void removeSink(McastRoute route, ConnectPoint connectPoint) {
mcastRoutes.compute(route, (k, v) -> {
if (v.removeSink(connectPoint)) {
post(new McastEvent(McastEvent.Type.SINK_REMOVED, route,
connectPoint, v.source()));
}
return v;
});
}
@Override
public ConnectPoint fetchSource(McastRoute route) {
MulticastData d = mcastRoutes.asJavaMap().getOrDefault(route,
MulticastData.empty());
return d.source();
}
@Override
public List<ConnectPoint> fetchSinks(McastRoute route) {
MulticastData d = mcastRoutes.asJavaMap().getOrDefault(route,
MulticastData.empty());
return d.sinks();
}
}
/*
* 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.
*/
/**
* An implementation of a multicast RIB.
*/
package org.onosproject.incubator.net.mcast.impl;
\ No newline at end of file
/*
* 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.net.mcast.impl;
import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.junit.TestUtils;
import org.onlab.packet.IpPrefix;
import org.onosproject.common.event.impl.TestEventDispatcher;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.IdGenerator;
import org.onosproject.core.Version;
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 java.util.Set;
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(IpPrefix.valueOf("1.1.1.1/8"),
IpPrefix.valueOf("1.1.1.2/8"),
McastRoute.Type.IGMP);
McastRoute r11 = new McastRoute(IpPrefix.valueOf("1.1.1.1/8"),
IpPrefix.valueOf("1.1.1.2/8"),
McastRoute.Type.STATIC);
McastRoute r2 = new McastRoute(IpPrefix.valueOf("2.2.2.1/8"),
IpPrefix.valueOf("2.2.2.2/8"),
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;
@Before
public void setUp() throws Exception {
manager = new MulticastRouteManager();
injectEventDispatcher(manager, new TestEventDispatcher());
TestUtils.setField(manager, "storageService", new TestStorageService());
TestUtils.setField(manager, "coreService", new TestCoreService());
events = Lists.newArrayList();
manager.activate();
manager.addListener(listener);
}
@After
public void tearDown() {
manager.removeListener(listener);
manager.deactivate();
}
@Test
public void testAdd() {
manager.add(r1);
assertEquals("Add failed", manager.mcastRoutes.size(), 1);
validateEvents(McastEvent.Type.ROUTE_ADDED);
}
@Test
public void testRemove() {
manager.add(r1);
manager.remove(r1);
assertEquals("Remove failed", manager.mcastRoutes.size(), 0);
validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED);
}
@Test
public void testAddSource() {
manager.add(r1);
manager.addSource(r1, cp1);
validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.SOURCE_ADDED);
assertEquals("Route is not equal", cp1, manager.fetchSource(r1));
}
@Test
public void testAddSink() {
manager.add(r1);
manager.addSource(r1, cp1);
manager.addSink(r1, cp1);
validateEvents(McastEvent.Type.ROUTE_ADDED,
McastEvent.Type.SOURCE_ADDED,
McastEvent.Type.SINK_ADDED);
assertEquals("Route is not equal", Lists.newArrayList(cp1), manager.fetchSinks(r1));
}
@Test
public void testRemoveSink() {
manager.add(r1);
manager.addSource(r1, cp1);
manager.addSink(r1, cp1);
manager.addSink(r1, cp2);
manager.removeSink(r1, cp2);
validateEvents(McastEvent.Type.ROUTE_ADDED,
McastEvent.Type.SOURCE_ADDED,
McastEvent.Type.SINK_ADDED,
McastEvent.Type.SINK_ADDED,
McastEvent.Type.SINK_REMOVED);
assertEquals("Route is not equal", Lists.newArrayList(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("Mismtached events# obtained -> %s : expected %s",
events, evs));
}
}
}
class TestMulticastListener implements McastListener {
@Override
public void event(McastEvent event) {
events.add(event);
}
}
private class TestCoreService implements CoreService {
@Override
public Version version() {
return null;
}
@Override
public Set<ApplicationId> getAppIds() {
return null;
}
@Override
public ApplicationId getAppId(Short id) {
return null;
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
@Override
public ApplicationId registerApplication(String identifier) {
return new DefaultApplicationId(0, identifier);
}
@Override
public IdGenerator getIdGenerator(String topic) {
return null;
}
}
}