Added simple link manager implementation.
Added link admin stuff; unit tests to come.
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
index b8f5788..cfe25f7 100644
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
@@ -152,6 +152,7 @@
public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
+ checkValidity();
log.info("Device {} connected", deviceId);
DeviceEvent event = store.createOrUpdateDevice(provider().id(),
deviceId, deviceDescription);
@@ -161,6 +162,7 @@
@Override
public void deviceDisconnected(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
+ checkValidity();
log.info("Device {} disconnected", deviceId);
DeviceEvent event = store.markOffline(deviceId);
post(event);
@@ -170,6 +172,7 @@
public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portDescriptions, "Port descriptions list cannot be null");
+ checkValidity();
log.info("Device {} ports updated", deviceId);
List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
for (DeviceEvent event : events) {
@@ -181,13 +184,14 @@
public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
+ checkValidity();
log.info("Device {} port status changed", deviceId);
DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
post(event);
}
}
- // Posts the specified event to a local event dispatcher
+ // Posts the specified event to the local event dispatcher.
private void post(DeviceEvent event) {
if (event != null && eventDispatcher != null) {
eventDispatcher.post(event);
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
index dd63469..3c05aba 100644
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
@@ -45,7 +45,7 @@
*
* @return number of devices
*/
- public int getDeviceCount() {
+ int getDeviceCount() {
return devices.size();
}
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkManager.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkManager.java
index b5d8cc4..1c7306f 100644
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkManager.java
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkManager.java
@@ -1,5 +1,6 @@
package org.onlab.onos.net.trivial.impl;
+import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -8,16 +9,24 @@
import org.apache.felix.scr.annotations.Service;
import org.onlab.onos.event.AbstractListenerRegistry;
import org.onlab.onos.event.EventDeliveryService;
+import org.onlab.onos.net.ConnectPoint;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.link.LinkAdminService;
import org.onlab.onos.net.link.LinkDescription;
import org.onlab.onos.net.link.LinkEvent;
import org.onlab.onos.net.link.LinkListener;
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.provider.AbstractProviderRegistry;
import org.onlab.onos.net.provider.AbstractProviderService;
import org.slf4j.Logger;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
/**
@@ -27,7 +36,11 @@
@Service
public class SimpleLinkManager
extends AbstractProviderRegistry<LinkProvider, LinkProviderService>
- implements LinkProviderRegistry {
+ implements LinkService, LinkAdminService, LinkProviderRegistry {
+
+ private static final String DEVICE_ID_NULL = "Device ID cannot be null";
+ private static final String LINK_DESC_NULL = "Link description cannot be null";
+ private static final String CONNECT_POINT_NULL = "Connection point cannot be null";
private final Logger log = getLogger(getClass());
@@ -56,6 +69,82 @@
return new InternalLinkProviderService(provider);
}
+ @Override
+ public int getLinkCount() {
+ return store.getLinkCount();
+ }
+
+ @Override
+ public Iterable<Link> getLinks() {
+ return store.getLinks();
+ }
+
+ @Override
+ public Set<Link> getDeviceLinks(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ return Sets.union(store.getDeviceEgressLinks(deviceId),
+ store.getDeviceIngressLinks(deviceId));
+ }
+
+ @Override
+ public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ return store.getDeviceEgressLinks(deviceId);
+ }
+
+ @Override
+ public Set<Link> getDeviceInressLinks(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ return store.getDeviceIngressLinks(deviceId);
+ }
+
+ @Override
+ public Set<Link> getLinks(ConnectPoint connectPoint) {
+ checkNotNull(connectPoint, CONNECT_POINT_NULL);
+ return Sets.union(store.getEgressLinks(connectPoint),
+ store.getIngressLinks(connectPoint));
+ }
+
+ @Override
+ public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
+ checkNotNull(connectPoint, CONNECT_POINT_NULL);
+ return store.getEgressLinks(connectPoint);
+ }
+
+ @Override
+ public Set<Link> getInressLinks(ConnectPoint connectPoint) {
+ checkNotNull(connectPoint, CONNECT_POINT_NULL);
+ return store.getIngressLinks(connectPoint);
+ }
+
+ @Override
+ public Set<Link> getLinks(ConnectPoint src, ConnectPoint dst) {
+ checkNotNull(src, CONNECT_POINT_NULL);
+ checkNotNull(dst, CONNECT_POINT_NULL);
+ return Sets.intersection(store.getEgressLinks(src),
+ store.getIngressLinks(dst));
+ }
+
+ @Override
+ public void removeLinks(ConnectPoint connectPoint) {
+ removeLinks(getLinks(connectPoint));
+ }
+
+ @Override
+ public void removeLinks(DeviceId deviceId) {
+ removeLinks(getDeviceLinks(deviceId));
+ }
+
+ @Override
+ public void addListener(LinkListener listener) {
+ listenerRegistry.addListener(listener);
+ }
+
+ @Override
+ public void removeListener(LinkListener listener) {
+ listenerRegistry.removeListener(listener);
+ }
+
// Personalized link provider service issued to the supplied provider.
private class InternalLinkProviderService extends AbstractProviderService<LinkProvider>
implements LinkProviderService {
@@ -66,12 +155,54 @@
@Override
public void linkDetected(LinkDescription linkDescription) {
+ checkNotNull(linkDescription, LINK_DESC_NULL);
+ checkValidity();
log.info("Link {} detected", linkDescription);
+ LinkEvent event = store.createOrUpdateLink(provider().id(),
+ linkDescription);
+ post(event);
}
@Override
public void linkVanished(LinkDescription linkDescription) {
+ checkNotNull(linkDescription, LINK_DESC_NULL);
+ checkValidity();
log.info("Link {} vanished", linkDescription);
+ LinkEvent event = store.removeLink(linkDescription.src(),
+ linkDescription.dst());
+ post(event);
+ }
+
+ @Override
+ public void linksVanished(ConnectPoint connectPoint) {
+ checkNotNull(connectPoint, "Connect point cannot be null");
+ checkValidity();
+ log.info("Link for connection point {} vanished", connectPoint);
+ removeLinks(getLinks(connectPoint));
+ }
+
+ @Override
+ public void linksVanished(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ checkValidity();
+ log.info("Link for device {} vanished", deviceId);
+ removeLinks(getDeviceLinks(deviceId));
}
}
+
+ // Removes all links in the specified set and emits appropriate events.
+ private void removeLinks(Set<Link> links) {
+ for (Link link : links) {
+ LinkEvent event = store.removeLink(link.src(), link.dst());
+ post(event);
+ }
+ }
+
+ // Posts the specified event to the local event dispatcher.
+ private void post(LinkEvent event) {
+ if (event != null && eventDispatcher != null) {
+ eventDispatcher.post(event);
+ }
+ }
+
}
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkStore.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkStore.java
index 8b6a53d..c1d6273 100644
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkStore.java
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleLinkStore.java
@@ -1,8 +1,184 @@
package org.onlab.onos.net.trivial.impl;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Multimap;
+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.provider.ProviderId;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
/**
* Manages inventory of infrastructure links using trivial in-memory link
* implementation.
*/
-public class SimpleLinkStore {
+class SimpleLinkStore {
+
+ // Link inventory
+ private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
+
+ // Egress and ingress link sets
+ private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
+ private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
+
+ private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{});
+
+ /**
+ * Returns the number of links in the store.
+ *
+ * @return number of links
+ */
+ int getLinkCount() {
+ return links.size();
+ }
+
+ /**
+ * Returns an iterable collection of all links in the inventory.
+ *
+ * @return collection of all links
+ */
+ 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) {
+ 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) {
+ return ImmutableSet.copyOf(dstLinks.get(deviceId));
+ }
+
+ /**
+ * 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) {
+ Set<Link> egress = new HashSet<>();
+ for (Link link : srcLinks.get(src.deviceId())) {
+ if (link.src().equals(src)) {
+ egress.add(link);
+ }
+ }
+ 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) {
+ Set<Link> ingress = new HashSet<>();
+ for (Link link : dstLinks.get(dst.deviceId())) {
+ if (link.src().equals(dst)) {
+ ingress.add(link);
+ }
+ }
+ 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
+ */
+ public LinkEvent createOrUpdateLink(ProviderId providerId,
+ LinkDescription linkDescription) {
+ LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
+ DefaultLink link = links.get(key);
+ if (link == null) {
+ return createLink(providerId, key, linkDescription);
+ }
+ return updateLink(link, linkDescription);
+ }
+
+ // Creates and stores the link and returns the appropriate event.
+ private LinkEvent createLink(ProviderId providerId, LinkKey key,
+ LinkDescription linkDescription) {
+ DefaultLink link = new DefaultLink(providerId, key.src, key.dst,
+ linkDescription.type());
+ synchronized (this) {
+ links.put(key, link);
+ srcLinks.put(link.src().deviceId(), link);
+ dstLinks.put(link.dst().deviceId(), link);
+ }
+ return new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
+ }
+
+ // Updates, if necessary the specified link and returns the appropriate event.
+ private LinkEvent updateLink(DefaultLink link, LinkDescription linkDescription) {
+ 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) {
+ synchronized (this) {
+ Link link = links.remove(new LinkKey(src, dst));
+ srcLinks.remove(link.src().deviceId(), link);
+ dstLinks.remove(link.dst().deviceId(), link);
+ return link == null ? null : new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
+ }
+ }
+
+ // Auxiliary key to track links.
+ private class LinkKey {
+ final ConnectPoint src;
+ final ConnectPoint dst;
+
+ LinkKey(ConnectPoint src, ConnectPoint dst) {
+ this.src = src;
+ this.dst = dst;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(src, dst);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof LinkKey) {
+ final LinkKey other = (LinkKey) obj;
+ return Objects.equals(this.src, other.src) &&
+ Objects.equals(this.dst, other.dst);
+ }
+ return false;
+ }
+ }
}