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