Adding DeviceAdminService facade and tests for SimpleDeviceManager.
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/DeviceStore.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/DeviceStore.java
deleted file mode 100644
index c874600..0000000
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/DeviceStore.java
+++ /dev/null
@@ -1,102 +0,0 @@
-package org.onlab.onos.net.trivial.impl;
-
-import org.onlab.onos.net.Device;
-import org.onlab.onos.net.DeviceId;
-import org.onlab.onos.net.Port;
-import org.onlab.onos.net.PortNumber;
-import org.onlab.onos.net.device.DeviceDescription;
-import org.onlab.onos.net.device.DeviceEvent;
-import org.onlab.onos.net.device.PortDescription;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * Manages inventory of infrastructure devices.
- */
-public class DeviceStore {
-
- private final Map<DeviceId, Device> devices = new ConcurrentHashMap<>();
-
- /**
- * Creates a new infrastructure device, or updates an existing one using
- * the supplied device description.
- *
- * @param deviceId device identifier
- * @param deviceDescription device description
- * @return ready to send event describing what occurred; null if no change
- */
- public DeviceEvent createOrUpdateDevice(DeviceId deviceId,
- DeviceDescription deviceDescription) {
- return null;
- }
-
- /**
- * Removes the specified infrastructure device.
- *
- * @param deviceId device identifier
- * @return ready to send event describing what occurred; null if no change
- */
- public DeviceEvent removeDevice(DeviceId deviceId) {
- return null;
- }
-
- /**
- * Updates the ports of the specified infrastructure device using the given
- * list of port descriptions. The list is assumed to be comprehensive.
- *
- * @param deviceId device identifier
- * @param portDescriptions list of port descriptions
- * @return ready to send events describing what occurred; empty list if no change
- */
- public List<DeviceEvent> updatePorts(DeviceId deviceId,
- List<PortDescription> portDescriptions) {
- return new ArrayList<>();
- }
-
- /**
- * Updates the port status of the specified infrastructure device using the
- * given port description.
- *
- * @param deviceId device identifier
- * @param portDescription port description
- * @return ready to send event describing what occurred; null if no change
- */
- public DeviceEvent updatePortStatus(DeviceId deviceId,
- PortDescription portDescription) {
- return null;
- }
-
- /**
- * Returns the device with the specified identifier.
- *
- * @param deviceId device identifier
- * @return device
- */
- public Device getDevice(DeviceId deviceId) {
- return null;
- }
-
- /**
- * Returns the list of ports that belong to the specified device.
- *
- * @param deviceId device identifier
- * @return list of device ports
- */
- public List<Port> getPorts(DeviceId deviceId) {
- return null;
- }
-
- /**
- * Returns the specified device port.
- *
- * @param deviceId device identifier
- * @param portNumber port number
- * @return device port
- */
- public Port getPort(DeviceId deviceId, PortNumber portNumber) {
- return null;
- }
-}
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 fee30a5..9cfc784 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
@@ -13,6 +13,7 @@
import org.onlab.onos.net.MastershipRole;
import org.onlab.onos.net.Port;
import org.onlab.onos.net.PortNumber;
+import org.onlab.onos.net.device.DeviceAdminService;
import org.onlab.onos.net.device.DeviceDescription;
import org.onlab.onos.net.device.DeviceEvent;
import org.onlab.onos.net.device.DeviceListener;
@@ -37,22 +38,23 @@
@Service
public class SimpleDeviceManager
extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
- implements DeviceService, DeviceProviderRegistry {
+ implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
- public static final String DEVICE_ID_NULL = "Device ID cannot be null";
- public static final String PORT_NUMBER_NULL = "Port number cannot be null";
- public static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
- public static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
+ private static final String DEVICE_ID_NULL = "Device ID cannot be null";
+ private static final String PORT_NUMBER_NULL = "Port number cannot be null";
+ private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
+ private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
+ private static final String ROLE_NULL = "Role cannot be null";
private final Logger log = getLogger(getClass());
private final AbstractListenerRegistry<DeviceEvent, DeviceListener>
listenerRegistry = new AbstractListenerRegistry<>();
- private final DeviceStore store = new DeviceStore();
+ private final SimpleDeviceStore store = new SimpleDeviceStore();
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- private EventDeliveryService eventDispatcher;
+ protected EventDeliveryService eventDispatcher;
@Activate
public void activate() {
@@ -69,12 +71,12 @@
@Override
public MastershipRole getRole(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
- return null;
+ return store.getRole(deviceId);
}
@Override
public Iterable<Device> getDevices() {
- return null;
+ return store.getDevices();
}
@Override
@@ -111,6 +113,28 @@
return new InternalDeviceProviderService(provider);
}
+ @Override
+ public void setRole(DeviceId deviceId, MastershipRole newRole) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ checkNotNull(newRole, ROLE_NULL);
+ DeviceEvent event = store.setRole(deviceId, newRole);
+ if (event != null) {
+ Device device = event.subject();
+ DeviceProvider provider = getProvider(device.providerId());
+ if (provider != null) {
+ provider.roleChanged(device, newRole);
+ }
+ post(event);
+ }
+ }
+
+ @Override
+ public void removeDevice(DeviceId deviceId) {
+ checkNotNull(deviceId, DEVICE_ID_NULL);
+ DeviceEvent event = store.removeDevice(deviceId);
+ post(event);
+ }
+
// Personalized device provider service issued to the supplied provider.
private class InternalDeviceProviderService extends AbstractProviderService<DeviceProvider>
implements DeviceProviderService {
@@ -124,7 +148,8 @@
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
log.info("Device {} connected: {}", deviceId, deviceDescription);
- DeviceEvent event = store.createOrUpdateDevice(deviceId, deviceDescription);
+ DeviceEvent event = store.createOrUpdateDevice(provider().id(),
+ deviceId, deviceDescription);
post(event);
}
@@ -132,7 +157,7 @@
public void deviceDisconnected(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
log.info("Device {} disconnected", deviceId);
- DeviceEvent event = store.removeDevice(deviceId);
+ DeviceEvent event = store.markOffline(deviceId);
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
new file mode 100644
index 0000000..1fdf89f
--- /dev/null
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
@@ -0,0 +1,214 @@
+package org.onlab.onos.net.trivial.impl;
+
+import org.onlab.onos.net.DefaultDevice;
+import org.onlab.onos.net.Device;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.net.MastershipRole;
+import org.onlab.onos.net.Port;
+import org.onlab.onos.net.PortNumber;
+import org.onlab.onos.net.device.DeviceDescription;
+import org.onlab.onos.net.device.DeviceEvent;
+import org.onlab.onos.net.device.PortDescription;
+import org.onlab.onos.net.provider.ProviderId;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
+import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED;
+import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED;
+
+/**
+ * Manages inventory of infrastructure devices.
+ */
+class SimpleDeviceStore {
+
+ private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
+ private final Set<DeviceId> available = new HashSet<>();
+ private final Map<DeviceId, MastershipRole> roles = new HashMap<>();
+
+ /**
+ * Returns an iterable collection of all devices known to the system.
+ *
+ * @return device collection
+ */
+ Iterable<Device> getDevices() {
+ return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
+ }
+
+ /**
+ * Returns the device with the specified identifier.
+ *
+ * @param deviceId device identifier
+ * @return device
+ */
+ Device getDevice(DeviceId deviceId) {
+ return devices.get(deviceId);
+ }
+
+ /**
+ * Creates a new infrastructure device, or updates an existing one using
+ * the supplied device description.
+ *
+ * @param providerId provider identifier
+ * @param deviceId device identifier
+ * @param deviceDescription device description
+ * @return ready to send event describing what occurred; null if no change
+ */
+ DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
+ DeviceDescription deviceDescription) {
+ DefaultDevice device = devices.get(deviceId);
+ if (device == null) {
+ return createDevice(providerId, deviceId, deviceDescription);
+ }
+ return updateDevice(providerId, device, deviceDescription);
+ }
+
+ // Creates the device and returns the appropriate event if necessary.
+ private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
+ DeviceDescription desc) {
+ DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
+ desc.manufacturer(),
+ desc.hwVersion(), desc.swVersion(),
+ desc.serialNumber());
+ synchronized (this) {
+ devices.put(deviceId, device);
+ available.add(deviceId);
+ }
+ return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device);
+ }
+
+ // Updates the device and returns the appropriate event if necessary.
+ private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
+ DeviceDescription desc) {
+ // We allow only certain attributes to trigger update
+ if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
+ !Objects.equals(device.swVersion(), desc.swVersion())) {
+ DefaultDevice updated = new DefaultDevice(providerId, device.id(),
+ desc.type(),
+ desc.manufacturer(),
+ desc.hwVersion(),
+ desc.swVersion(),
+ desc.serialNumber());
+ synchronized (this) {
+ devices.put(device.id(), updated);
+ available.add(device.id());
+ }
+ return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device);
+ }
+
+ // Otherwise merely attempt to change availability
+ synchronized (this) {
+ boolean added = available.add(device.id());
+ return added ? new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device) : null;
+ }
+ }
+
+ /**
+ * Removes the specified infrastructure device.
+ *
+ * @param deviceId device identifier
+ * @return ready to send event describing what occurred; null if no change
+ */
+ DeviceEvent markOffline(DeviceId deviceId) {
+ synchronized (this) {
+ Device device = devices.get(deviceId);
+ checkArgument(device != null, "Device with ID %s is not found", deviceId);
+ boolean removed = available.remove(deviceId);
+ return removed ? new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device) : null;
+ }
+ }
+
+ /**
+ * Updates the ports of the specified infrastructure device using the given
+ * list of port descriptions. The list is assumed to be comprehensive.
+ *
+ * @param deviceId device identifier
+ * @param portDescriptions list of port descriptions
+ * @return ready to send events describing what occurred; empty list if no change
+ */
+ List<DeviceEvent> updatePorts(DeviceId deviceId,
+ List<PortDescription> portDescriptions) {
+ return new ArrayList<>();
+ }
+
+ /**
+ * Updates the port status of the specified infrastructure device using the
+ * given port description.
+ *
+ * @param deviceId device identifier
+ * @param portDescription port description
+ * @return ready to send event describing what occurred; null if no change
+ */
+ DeviceEvent updatePortStatus(DeviceId deviceId,
+ PortDescription portDescription) {
+ return null;
+ }
+
+ /**
+ * Returns the list of ports that belong to the specified device.
+ *
+ * @param deviceId device identifier
+ * @return list of device ports
+ */
+ List<Port> getPorts(DeviceId deviceId) {
+ return null;
+ }
+
+ /**
+ * Returns the specified device port.
+ *
+ * @param deviceId device identifier
+ * @param portNumber port number
+ * @return device port
+ */
+ Port getPort(DeviceId deviceId, PortNumber portNumber) {
+ return null;
+ }
+
+ /**
+ * Returns the mastership role determined for this device.
+ *
+ * @param deviceId device identifier
+ * @return mastership role
+ */
+ MastershipRole getRole(DeviceId deviceId) {
+ MastershipRole role = roles.get(deviceId);
+ return role != null ? role : MastershipRole.NONE;
+ }
+
+ /**
+ * Administratively sets the role of the specified device.
+ *
+ * @param deviceId device identifier
+ * @param role mastership role to apply
+ * @return mastership role change event or null if no change
+ */
+ DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
+ Device device = getDevice(deviceId);
+ checkArgument(device != null, "Device with ID %s not found");
+ MastershipRole oldRole = roles.put(deviceId, role);
+ return oldRole == role ? null : new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device);
+ }
+
+ /**
+ * Administratively removes the specified device from the store.
+ *
+ * @param deviceId device to be removed
+ */
+ DeviceEvent removeDevice(DeviceId deviceId) {
+ synchronized (this) {
+ roles.remove(deviceId);
+ Device device = devices.remove(deviceId);
+ return device != null ? new DeviceEvent(DEVICE_REMOVED, device) : null;
+ }
+ }
+}