tom

Adding some flesh to the simple device manager.

package org.onlab.onos.event;
/**
* Abstraction of an entity capable of accepting events to be posted and
* then dispatching them to the appropriate event sink.
*/
public interface EventDispatchService extends EventDispatcher, EventSinkBroker {
}
......@@ -6,13 +6,13 @@ package org.onlab.onos.event;
* Similarly, whether the events are accepted and dispatched synchronously
* or asynchronously is unspecified as well.
*/
public interface EventDispatcher<E extends Event> {
public interface EventDispatcher {
/**
* Posts the specified event for dispatching.
*
* @param event event to be posted
*/
void post(E event);
void post(Event event);
}
......
......@@ -34,17 +34,17 @@ public interface DeviceProviderService extends ProviderService<DeviceProvider> {
* determine what has changed.
* <p/>
*
* @param deviceId identity of the device
* @param ports list of device ports
* @param deviceId identity of the device
* @param portDescriptions list of device ports
*/
void updatePorts(DeviceId deviceId, List<PortDescription> ports);
void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions);
/**
* Used to notify the core about port status change of a single port.
*
* @param deviceId identity of the device
* @param port description of the port that changed
* @param deviceId identity of the device
* @param portDescription description of the port that changed
*/
void portStatusChanged(DeviceId deviceId, PortDescription port);
void portStatusChanged(DeviceId deviceId, PortDescription portDescription);
}
......
package org.onlab.onos.net.trivial.impl;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
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;
}
}
......@@ -4,12 +4,20 @@ 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.Service;
import org.onlab.onos.event.AbstractListenerManager;
import org.onlab.onos.event.EventDispatchService;
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.DeviceListener;
import org.onlab.onos.net.device.DeviceProvider;
import org.onlab.onos.net.device.DeviceProviderBroker;
import org.onlab.onos.net.device.DeviceProviderService;
import org.onlab.onos.net.device.DeviceService;
import org.onlab.onos.net.device.PortDescription;
import org.onlab.onos.net.provider.AbstractProviderBroker;
import org.onlab.onos.net.provider.AbstractProviderService;
......@@ -18,47 +26,86 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Provides basic implementation of the device SB &amp; NB APIs.
*/
@Component(immediate = true)
@Service
public class SimpleDeviceManager implements DeviceProviderBroker {
public class SimpleDeviceManager
extends AbstractProviderBroker<DeviceProvider, DeviceProviderService>
implements DeviceService, DeviceProviderBroker {
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 Logger log = LoggerFactory.getLogger(SimpleDeviceManager.class);
private final DeviceProviderBroker broker = new InternalBroker();
private final AbstractListenerManager<DeviceEvent, DeviceListener>
listenerManager = new AbstractListenerManager<>();
private EventDispatchService eventDispatcher;
private final DeviceStore store = new DeviceStore();
@Activate
public void activate() {
// eventDispatcher.addSink(DeviceEvent.class, listenerManager);
log.info("Started");
}
@Deactivate
public void deactivate() {
// eventDispatcher.removeSink(DeviceEvent.class);
log.info("Stopped");
}
@Override
public DeviceProviderService register(DeviceProvider provider) {
log.info("Registering provider {}", provider.id());
return broker.register(provider);
public MastershipRole getRole(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return null;
}
@Override
public void unregister(DeviceProvider provider) {
log.info("Unregistering provider {}", provider.id());
broker.unregister(provider);
public Iterable<Device> getDevices() {
return null;
}
// Internal delegate for tracking various providers and issuing them a
// personalized provider service.
private class InternalBroker extends AbstractProviderBroker<DeviceProvider, DeviceProviderService>
implements DeviceProviderBroker {
@Override
protected DeviceProviderService createProviderService(DeviceProvider provider) {
return new InternalDeviceProviderService(provider);
}
@Override
public Device getDevice(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return null;
}
@Override
public List<Port> getPorts(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return null;
}
@Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portNumber, PORT_NUMBER_NULL);
return null;
}
@Override
public void addListener(DeviceListener listener) {
listenerManager.addListener(listener);
}
@Override
public void removeListener(DeviceListener listener) {
listenerManager.removeListener(listener);
}
@Override
protected DeviceProviderService createProviderService(DeviceProvider provider) {
return new InternalDeviceProviderService(provider);
}
// Personalized device provider service issued to the supplied provider.
......@@ -71,26 +118,49 @@ public class SimpleDeviceManager implements DeviceProviderBroker {
@Override
public MastershipRole deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
log.info("Device {} connected: {}", deviceId, deviceDescription);
DeviceEvent event = store.createOrUpdateDevice(deviceId, deviceDescription);
post(event);
return MastershipRole.MASTER;
}
@Override
public void deviceDisconnected(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
log.info("Device {} disconnected", deviceId);
DeviceEvent event = store.removeDevice(deviceId);
post(event);
}
@Override
public void updatePorts(DeviceId deviceId, List<PortDescription> ports) {
public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portDescriptions, "Port descriptions list cannot be null");
// FIXME: fix the interface to accept DeviceId separately
log.info("Device {} ports updated: {}", ports);
log.info("Device {} ports updated: {}", portDescriptions);
List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
for (DeviceEvent event : events) {
post(event);
}
}
@Override
public void portStatusChanged(DeviceId deviceId, PortDescription port) {
log.info("Device {} port status changed: {}", deviceId, port);
public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
log.info("Device {} port status changed: {}", deviceId, portDescription);
DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
post(event);
}
}
// Posts the specified event to a local event dispatcher
private void post(DeviceEvent event) {
if (event != null && eventDispatcher != null) {
eventDispatcher.post(event);
}
}
}
......
......@@ -36,13 +36,11 @@ public class SimpleHostManager implements HostProviderBroker {
@Override
public HostProviderService register(HostProvider provider) {
log.info("Registering provider {}", provider.id());
return broker.register(provider);
}
@Override
public void unregister(HostProvider provider) {
log.info("Unregistering provider {}", provider.id());
broker.unregister(provider);
}
......
......@@ -36,13 +36,11 @@ public class SimpleLinkManager implements LinkProviderBroker {
@Override
public LinkProviderService register(LinkProvider provider) {
log.info("Registering provider {}", provider.id());
return broker.register(provider);
}
@Override
public void unregister(LinkProvider provider) {
log.info("Unregistering provider {}", provider.id());
broker.unregister(provider);
}
......