tom

Formalized LinkStore in preparation for separating managers and stores.

package org.onlab.onos.net.link;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.provider.ProviderId;
import java.util.Set;
/**
* Manages inventory of infrastructure links using whatever means are appropriate.
*/
public interface LinkStore {
/**
* Returns the number of links in the store.
*
* @return number of links
*/
int getLinkCount();
/**
* Returns an iterable collection of all links in the inventory.
*
* @return collection of all links
*/
Iterable<Link> getLinks();
/**
* Returns all links egressing from the specified device.
*
* @param deviceId device identifier
* @return set of device links
*/
Set<Link> getDeviceEgressLinks(DeviceId deviceId);
/**
* Returns all links ingressing from the specified device.
*
* @param deviceId device identifier
* @return set of device links
*/
Set<Link> getDeviceIngressLinks(DeviceId deviceId);
/**
* Returns the link between the two end-points.
*
* @param src source connection point
* @param dst destination connection point
* @return link or null if one not found between the end-points
*/
Link getLink(ConnectPoint src, ConnectPoint dst);
/**
* Returns all links egressing from the specified connection point.
*
* @param src source connection point
* @return set of connection point links
*/
Set<Link> getEgressLinks(ConnectPoint src);
/**
* Returns all links ingressing to the specified connection point.
*
* @param dst destination connection point
* @return set of connection point links
*/
Set<Link> getIngressLinks(ConnectPoint dst);
/**
* Creates a new link, or updates an existing one, based on the given
* information.
*
* @param providerId provider identity
* @param linkDescription link description
* @return create or update link event, or null if no change resulted
*/
public LinkEvent createOrUpdateLink(ProviderId providerId,
LinkDescription linkDescription);
/**
* Removes the link based on the specified information.
*
* @param src link source
* @param dst link destination
* @return remove link event, or null if no change resulted
*/
LinkEvent removeLink(ConnectPoint src, ConnectPoint dst);
}
......@@ -27,6 +27,7 @@ import org.onlab.onos.net.link.LinkProvider;
import org.onlab.onos.net.link.LinkProviderRegistry;
import org.onlab.onos.net.link.LinkProviderService;
import org.onlab.onos.net.link.LinkService;
import org.onlab.onos.net.link.LinkStore;
import org.onlab.onos.net.provider.AbstractProviderRegistry;
import org.onlab.onos.net.provider.AbstractProviderService;
import org.slf4j.Logger;
......@@ -38,7 +39,7 @@ import com.google.common.collect.Sets;
*/
@Component(immediate = true)
@Service
public class SimpleLinkManager
public class LinkManager
extends AbstractProviderRegistry<LinkProvider, LinkProviderService>
implements LinkService, LinkAdminService, LinkProviderRegistry {
......@@ -51,10 +52,12 @@ public class SimpleLinkManager
protected final AbstractListenerRegistry<LinkEvent, LinkListener>
listenerRegistry = new AbstractListenerRegistry<>();
private final SimpleLinkStore store = new SimpleLinkStore();
private final DeviceListener deviceListener = new InnerDeviceListener();
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected LinkStore store;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DeviceService deviceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......
......@@ -3,12 +3,15 @@ package org.onlab.onos.net.trivial.link.impl;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DefaultLink;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.link.LinkDescription;
import org.onlab.onos.net.link.LinkEvent;
import org.onlab.onos.net.link.LinkStore;
import org.onlab.onos.net.provider.ProviderId;
import java.util.Collections;
......@@ -20,15 +23,15 @@ import java.util.concurrent.ConcurrentHashMap;
import static org.onlab.onos.net.Link.Type.DIRECT;
import static org.onlab.onos.net.Link.Type.INDIRECT;
import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
import static org.onlab.onos.net.link.LinkEvent.Type.LINK_UPDATED;
import static org.onlab.onos.net.link.LinkEvent.Type.*;
/**
* Manages inventory of infrastructure links using trivial in-memory structures
* implementation.
*/
class SimpleLinkStore {
@Component(immediate = true)
@Service
public class SimpleLinkStore implements LinkStore {
// Link inventory
private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
......@@ -39,62 +42,33 @@ class SimpleLinkStore {
private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{});
/**
* Returns the number of links in the store.
*
* @return number of links
*/
int getLinkCount() {
@Override
public int getLinkCount() {
return links.size();
}
/**
* Returns an iterable collection of all links in the inventory.
*
* @return collection of all links
*/
Iterable<Link> getLinks() {
@Override
public Iterable<Link> getLinks() {
return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
}
/**
* Returns all links egressing from the specified device.
*
* @param deviceId device identifier
* @return set of device links
*/
Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
@Override
public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
return ImmutableSet.copyOf(srcLinks.get(deviceId));
}
/**
* Returns all links ingressing from the specified device.
*
* @param deviceId device identifier
* @return set of device links
*/
Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
@Override
public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
return ImmutableSet.copyOf(dstLinks.get(deviceId));
}
/**
* Returns the link between the two end-points.
*
* @param src source connection point
* @param dst destination connection point
* @return link or null if one not found between the end-points
*/
Link getLink(ConnectPoint src, ConnectPoint dst) {
@Override
public Link getLink(ConnectPoint src, ConnectPoint dst) {
return links.get(new LinkKey(src, dst));
}
/**
* Returns all links egressing from the specified connection point.
*
* @param src source connection point
* @return set of connection point links
*/
Set<Link> getEgressLinks(ConnectPoint src) {
@Override
public Set<Link> getEgressLinks(ConnectPoint src) {
Set<Link> egress = new HashSet<>();
for (Link link : srcLinks.get(src.deviceId())) {
if (link.src().equals(src)) {
......@@ -104,13 +78,8 @@ class SimpleLinkStore {
return egress;
}
/**
* Returns all links ingressing to the specified connection point.
*
* @param dst destination connection point
* @return set of connection point links
*/
Set<Link> getIngressLinks(ConnectPoint dst) {
@Override
public Set<Link> getIngressLinks(ConnectPoint dst) {
Set<Link> ingress = new HashSet<>();
for (Link link : dstLinks.get(dst.deviceId())) {
if (link.dst().equals(dst)) {
......@@ -120,14 +89,7 @@ class SimpleLinkStore {
return ingress;
}
/**
* Creates a new link, or updates an existing one, based on the given
* information.
*
* @param providerId provider identity
* @param linkDescription link description
* @return create or update link event, or null if no change resulted
*/
@Override
public LinkEvent createOrUpdateLink(ProviderId providerId,
LinkDescription linkDescription) {
LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
......@@ -171,14 +133,8 @@ class SimpleLinkStore {
return null;
}
/**
* Removes the link based on the specified information.
*
* @param src link source
* @param dst link destination
* @return remove link event, or null if no change resulted
*/
LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
@Override
public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
synchronized (this) {
Link link = links.remove(new LinkKey(src, dst));
if (link != null) {
......
......@@ -38,7 +38,7 @@ import static org.onlab.onos.net.link.LinkEvent.Type.*;
/**
* Test codifying the link service & link provider service contracts.
*/
public class SimpleLinkManagerTest {
public class LinkManagerTest {
private static final ProviderId PID = new ProviderId("of", "foo");
private static final DeviceId DID1 = deviceId("of:foo");
......@@ -50,7 +50,7 @@ public class SimpleLinkManagerTest {
private static final PortNumber P3 = PortNumber.portNumber(3);
private SimpleLinkManager mgr;
private LinkManager mgr;
protected LinkService service;
protected LinkAdminService admin;
......@@ -61,10 +61,11 @@ public class SimpleLinkManagerTest {
@Before
public void setUp() {
mgr = new SimpleLinkManager();
mgr = new LinkManager();
service = mgr;
admin = mgr;
registry = mgr;
mgr.store = new SimpleLinkStore();
mgr.eventDispatcher = new TestEventDispatcher();
mgr.deviceService = new DeviceManager();
mgr.activate();
......
......@@ -16,7 +16,7 @@ import org.onlab.onos.net.topology.TopologyProvider;
import org.onlab.onos.net.topology.TopologyProviderRegistry;
import org.onlab.onos.net.topology.TopologyProviderService;
import org.onlab.onos.net.trivial.device.impl.DeviceManager;
import org.onlab.onos.net.trivial.link.impl.SimpleLinkManager;
import org.onlab.onos.net.trivial.link.impl.LinkManager;
import java.util.List;
import java.util.Set;
......@@ -140,7 +140,7 @@ public class DefaultTopologyProviderTest {
}
}
private class TestLinkService extends SimpleLinkManager {
private class TestLinkService extends LinkManager {
TestLinkService() {
eventDispatcher = new TestEventDispatcher();
eventDispatcher.addSink(LinkEvent.class, listenerRegistry);
......