tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Service; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 7 | import org.onlab.onos.event.AbstractListenerManager; |
| 8 | import org.onlab.onos.event.EventDispatchService; |
| 9 | import org.onlab.onos.net.Device; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.DeviceId; |
| 11 | import org.onlab.onos.net.MastershipRole; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 12 | import org.onlab.onos.net.Port; |
| 13 | import org.onlab.onos.net.PortNumber; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.device.DeviceDescription; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 15 | import org.onlab.onos.net.device.DeviceEvent; |
| 16 | import org.onlab.onos.net.device.DeviceListener; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.device.DeviceProvider; |
| 18 | import org.onlab.onos.net.device.DeviceProviderBroker; |
| 19 | import org.onlab.onos.net.device.DeviceProviderService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 20 | import org.onlab.onos.net.device.DeviceService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.device.PortDescription; |
| 22 | import org.onlab.onos.net.provider.AbstractProviderBroker; |
| 23 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 31 | /** |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 32 | * Provides basic implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 33 | */ |
| 34 | @Component(immediate = true) |
| 35 | @Service |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 36 | public class SimpleDeviceManager |
| 37 | extends AbstractProviderBroker<DeviceProvider, DeviceProviderService> |
| 38 | implements DeviceService, DeviceProviderBroker { |
| 39 | |
| 40 | public static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 41 | public static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 42 | public static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 43 | public static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 44 | |
| 45 | private Logger log = LoggerFactory.getLogger(SimpleDeviceManager.class); |
| 46 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 47 | private final AbstractListenerManager<DeviceEvent, DeviceListener> |
| 48 | listenerManager = new AbstractListenerManager<>(); |
| 49 | |
| 50 | private EventDispatchService eventDispatcher; |
| 51 | |
| 52 | private final DeviceStore store = new DeviceStore(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 53 | |
| 54 | @Activate |
| 55 | public void activate() { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 56 | // eventDispatcher.addSink(DeviceEvent.class, listenerManager); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 57 | log.info("Started"); |
| 58 | } |
| 59 | |
| 60 | @Deactivate |
| 61 | public void deactivate() { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 62 | // eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
| 66 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 67 | public MastershipRole getRole(DeviceId deviceId) { |
| 68 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 69 | return null; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 73 | public Iterable<Device> getDevices() { |
| 74 | return null; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 75 | } |
| 76 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 77 | @Override |
| 78 | public Device getDevice(DeviceId deviceId) { |
| 79 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public List<Port> getPorts(DeviceId deviceId) { |
| 85 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 91 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 92 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void addListener(DeviceListener listener) { |
| 98 | listenerManager.addListener(listener); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void removeListener(DeviceListener listener) { |
| 103 | listenerManager.removeListener(listener); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected DeviceProviderService createProviderService(DeviceProvider provider) { |
| 108 | return new InternalDeviceProviderService(provider); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Personalized device provider service issued to the supplied provider. |
| 112 | private class InternalDeviceProviderService extends AbstractProviderService<DeviceProvider> |
| 113 | implements DeviceProviderService { |
| 114 | |
| 115 | public InternalDeviceProviderService(DeviceProvider provider) { |
| 116 | super(provider); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public MastershipRole deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 121 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 122 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 123 | log.info("Device {} connected: {}", deviceId, deviceDescription); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 124 | DeviceEvent event = store.createOrUpdateDevice(deviceId, deviceDescription); |
| 125 | post(event); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 126 | return MastershipRole.MASTER; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 131 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 132 | log.info("Device {} disconnected", deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 133 | DeviceEvent event = store.removeDevice(deviceId); |
| 134 | post(event); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 138 | public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { |
| 139 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 140 | checkNotNull(portDescriptions, "Port descriptions list cannot be null"); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 141 | // FIXME: fix the interface to accept DeviceId separately |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 142 | log.info("Device {} ports updated: {}", portDescriptions); |
| 143 | List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions); |
| 144 | for (DeviceEvent event : events) { |
| 145 | post(event); |
| 146 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 150 | public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { |
| 151 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 152 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
| 153 | log.info("Device {} port status changed: {}", deviceId, portDescription); |
| 154 | DeviceEvent event = store.updatePortStatus(deviceId, portDescription); |
| 155 | post(event); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame^] | 158 | |
| 159 | // Posts the specified event to a local event dispatcher |
| 160 | private void post(DeviceEvent event) { |
| 161 | if (event != null && eventDispatcher != null) { |
| 162 | eventDispatcher.post(event); |
| 163 | } |
| 164 | } |
| 165 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 166 | } |