tom

Adding some flesh to the simple device manager.

1 +package org.onlab.onos.event;
2 +
3 +/**
4 + * Abstraction of an entity capable of accepting events to be posted and
5 + * then dispatching them to the appropriate event sink.
6 + */
7 +public interface EventDispatchService extends EventDispatcher, EventSinkBroker {
8 +}
...@@ -6,13 +6,13 @@ package org.onlab.onos.event; ...@@ -6,13 +6,13 @@ package org.onlab.onos.event;
6 * Similarly, whether the events are accepted and dispatched synchronously 6 * Similarly, whether the events are accepted and dispatched synchronously
7 * or asynchronously is unspecified as well. 7 * or asynchronously is unspecified as well.
8 */ 8 */
9 -public interface EventDispatcher<E extends Event> { 9 +public interface EventDispatcher {
10 10
11 /** 11 /**
12 * Posts the specified event for dispatching. 12 * Posts the specified event for dispatching.
13 * 13 *
14 * @param event event to be posted 14 * @param event event to be posted
15 */ 15 */
16 - void post(E event); 16 + void post(Event event);
17 17
18 } 18 }
......
...@@ -34,17 +34,17 @@ public interface DeviceProviderService extends ProviderService<DeviceProvider> { ...@@ -34,17 +34,17 @@ public interface DeviceProviderService extends ProviderService<DeviceProvider> {
34 * determine what has changed. 34 * determine what has changed.
35 * <p/> 35 * <p/>
36 * 36 *
37 - * @param deviceId identity of the device 37 + * @param deviceId identity of the device
38 - * @param ports list of device ports 38 + * @param portDescriptions list of device ports
39 */ 39 */
40 - void updatePorts(DeviceId deviceId, List<PortDescription> ports); 40 + void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions);
41 41
42 /** 42 /**
43 * Used to notify the core about port status change of a single port. 43 * Used to notify the core about port status change of a single port.
44 * 44 *
45 - * @param deviceId identity of the device 45 + * @param deviceId identity of the device
46 - * @param port description of the port that changed 46 + * @param portDescription description of the port that changed
47 */ 47 */
48 - void portStatusChanged(DeviceId deviceId, PortDescription port); 48 + void portStatusChanged(DeviceId deviceId, PortDescription portDescription);
49 49
50 } 50 }
......
1 +package org.onlab.onos.net.trivial.impl;
2 +
3 +import org.onlab.onos.net.Device;
4 +import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.device.DeviceDescription;
6 +import org.onlab.onos.net.device.DeviceEvent;
7 +import org.onlab.onos.net.device.PortDescription;
8 +
9 +import java.util.ArrayList;
10 +import java.util.List;
11 +import java.util.Map;
12 +import java.util.concurrent.ConcurrentHashMap;
13 +
14 +/**
15 + * Manages inventory of infrastructure devices.
16 + */
17 +public class DeviceStore {
18 +
19 + private final Map<DeviceId, Device> devices = new ConcurrentHashMap<>();
20 +
21 + /**
22 + * Creates a new infrastructure device, or updates an existing one using
23 + * the supplied device description.
24 + *
25 + * @param deviceId device identifier
26 + * @param deviceDescription device description
27 + * @return ready to send event describing what occurred; null if no change
28 + */
29 + public DeviceEvent createOrUpdateDevice(DeviceId deviceId,
30 + DeviceDescription deviceDescription) {
31 + return null;
32 + }
33 +
34 + /**
35 + * Removes the specified infrastructure device.
36 + *
37 + * @param deviceId device identifier
38 + * @return ready to send event describing what occurred; null if no change
39 + */
40 + public DeviceEvent removeDevice(DeviceId deviceId) {
41 + return null;
42 + }
43 +
44 + /**
45 + * Updates the ports of the specified infrastructure device using the given
46 + * list of port descriptions. The list is assumed to be comprehensive.
47 + *
48 + * @param deviceId device identifier
49 + * @param portDescriptions list of port descriptions
50 + * @return ready to send events describing what occurred; empty list if no change
51 + */
52 + public List<DeviceEvent> updatePorts(DeviceId deviceId,
53 + List<PortDescription> portDescriptions) {
54 + return new ArrayList<>();
55 + }
56 +
57 + /**
58 + * Updates the port status of the specified infrastructure device using the
59 + * given port description.
60 + *
61 + * @param deviceId device identifier
62 + * @param portDescription port description
63 + * @return ready to send event describing what occurred; null if no change
64 + */
65 + public DeviceEvent updatePortStatus(DeviceId deviceId,
66 + PortDescription portDescription) {
67 + return null;
68 + }
69 +}
...@@ -4,12 +4,20 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -4,12 +4,20 @@ import org.apache.felix.scr.annotations.Activate;
4 import org.apache.felix.scr.annotations.Component; 4 import org.apache.felix.scr.annotations.Component;
5 import org.apache.felix.scr.annotations.Deactivate; 5 import org.apache.felix.scr.annotations.Deactivate;
6 import org.apache.felix.scr.annotations.Service; 6 import org.apache.felix.scr.annotations.Service;
7 +import org.onlab.onos.event.AbstractListenerManager;
8 +import org.onlab.onos.event.EventDispatchService;
9 +import org.onlab.onos.net.Device;
7 import org.onlab.onos.net.DeviceId; 10 import org.onlab.onos.net.DeviceId;
8 import org.onlab.onos.net.MastershipRole; 11 import org.onlab.onos.net.MastershipRole;
12 +import org.onlab.onos.net.Port;
13 +import org.onlab.onos.net.PortNumber;
9 import org.onlab.onos.net.device.DeviceDescription; 14 import org.onlab.onos.net.device.DeviceDescription;
15 +import org.onlab.onos.net.device.DeviceEvent;
16 +import org.onlab.onos.net.device.DeviceListener;
10 import org.onlab.onos.net.device.DeviceProvider; 17 import org.onlab.onos.net.device.DeviceProvider;
11 import org.onlab.onos.net.device.DeviceProviderBroker; 18 import org.onlab.onos.net.device.DeviceProviderBroker;
12 import org.onlab.onos.net.device.DeviceProviderService; 19 import org.onlab.onos.net.device.DeviceProviderService;
20 +import org.onlab.onos.net.device.DeviceService;
13 import org.onlab.onos.net.device.PortDescription; 21 import org.onlab.onos.net.device.PortDescription;
14 import org.onlab.onos.net.provider.AbstractProviderBroker; 22 import org.onlab.onos.net.provider.AbstractProviderBroker;
15 import org.onlab.onos.net.provider.AbstractProviderService; 23 import org.onlab.onos.net.provider.AbstractProviderService;
...@@ -18,47 +26,86 @@ import org.slf4j.LoggerFactory; ...@@ -18,47 +26,86 @@ import org.slf4j.LoggerFactory;
18 26
19 import java.util.List; 27 import java.util.List;
20 28
29 +import static com.google.common.base.Preconditions.checkNotNull;
30 +
21 /** 31 /**
22 * Provides basic implementation of the device SB &amp; NB APIs. 32 * Provides basic implementation of the device SB &amp; NB APIs.
23 */ 33 */
24 @Component(immediate = true) 34 @Component(immediate = true)
25 @Service 35 @Service
26 -public class SimpleDeviceManager implements DeviceProviderBroker { 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";
27 44
28 private Logger log = LoggerFactory.getLogger(SimpleDeviceManager.class); 45 private Logger log = LoggerFactory.getLogger(SimpleDeviceManager.class);
29 46
30 - private final DeviceProviderBroker broker = new InternalBroker(); 47 + private final AbstractListenerManager<DeviceEvent, DeviceListener>
48 + listenerManager = new AbstractListenerManager<>();
49 +
50 + private EventDispatchService eventDispatcher;
51 +
52 + private final DeviceStore store = new DeviceStore();
31 53
32 @Activate 54 @Activate
33 public void activate() { 55 public void activate() {
56 +// eventDispatcher.addSink(DeviceEvent.class, listenerManager);
34 log.info("Started"); 57 log.info("Started");
35 } 58 }
36 59
37 @Deactivate 60 @Deactivate
38 public void deactivate() { 61 public void deactivate() {
62 +// eventDispatcher.removeSink(DeviceEvent.class);
39 log.info("Stopped"); 63 log.info("Stopped");
40 } 64 }
41 65
42 @Override 66 @Override
43 - public DeviceProviderService register(DeviceProvider provider) { 67 + public MastershipRole getRole(DeviceId deviceId) {
44 - log.info("Registering provider {}", provider.id()); 68 + checkNotNull(deviceId, DEVICE_ID_NULL);
45 - return broker.register(provider); 69 + return null;
46 } 70 }
47 71
48 @Override 72 @Override
49 - public void unregister(DeviceProvider provider) { 73 + public Iterable<Device> getDevices() {
50 - log.info("Unregistering provider {}", provider.id()); 74 + return null;
51 - broker.unregister(provider);
52 } 75 }
53 76
54 - // Internal delegate for tracking various providers and issuing them a 77 + @Override
55 - // personalized provider service. 78 + public Device getDevice(DeviceId deviceId) {
56 - private class InternalBroker extends AbstractProviderBroker<DeviceProvider, DeviceProviderService> 79 + checkNotNull(deviceId, DEVICE_ID_NULL);
57 - implements DeviceProviderBroker { 80 + return null;
58 - @Override 81 + }
59 - protected DeviceProviderService createProviderService(DeviceProvider provider) { 82 +
60 - return new InternalDeviceProviderService(provider); 83 + @Override
61 - } 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);
62 } 109 }
63 110
64 // Personalized device provider service issued to the supplied provider. 111 // Personalized device provider service issued to the supplied provider.
...@@ -71,26 +118,49 @@ public class SimpleDeviceManager implements DeviceProviderBroker { ...@@ -71,26 +118,49 @@ public class SimpleDeviceManager implements DeviceProviderBroker {
71 118
72 @Override 119 @Override
73 public MastershipRole deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { 120 public MastershipRole deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
121 + checkNotNull(deviceId, DEVICE_ID_NULL);
122 + checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
74 log.info("Device {} connected: {}", deviceId, deviceDescription); 123 log.info("Device {} connected: {}", deviceId, deviceDescription);
124 + DeviceEvent event = store.createOrUpdateDevice(deviceId, deviceDescription);
125 + post(event);
75 return MastershipRole.MASTER; 126 return MastershipRole.MASTER;
76 } 127 }
77 128
78 @Override 129 @Override
79 public void deviceDisconnected(DeviceId deviceId) { 130 public void deviceDisconnected(DeviceId deviceId) {
131 + checkNotNull(deviceId, DEVICE_ID_NULL);
80 log.info("Device {} disconnected", deviceId); 132 log.info("Device {} disconnected", deviceId);
81 - 133 + DeviceEvent event = store.removeDevice(deviceId);
134 + post(event);
82 } 135 }
83 136
84 @Override 137 @Override
85 - public void updatePorts(DeviceId deviceId, List<PortDescription> ports) { 138 + public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
139 + checkNotNull(deviceId, DEVICE_ID_NULL);
140 + checkNotNull(portDescriptions, "Port descriptions list cannot be null");
86 // FIXME: fix the interface to accept DeviceId separately 141 // FIXME: fix the interface to accept DeviceId separately
87 - log.info("Device {} ports updated: {}", ports); 142 + log.info("Device {} ports updated: {}", portDescriptions);
88 - 143 + List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
144 + for (DeviceEvent event : events) {
145 + post(event);
146 + }
89 } 147 }
90 148
91 @Override 149 @Override
92 - public void portStatusChanged(DeviceId deviceId, PortDescription port) { 150 + public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
93 - log.info("Device {} port status changed: {}", deviceId, port); 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);
94 } 156 }
95 } 157 }
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 +
96 } 166 }
......
...@@ -36,13 +36,11 @@ public class SimpleHostManager implements HostProviderBroker { ...@@ -36,13 +36,11 @@ public class SimpleHostManager implements HostProviderBroker {
36 36
37 @Override 37 @Override
38 public HostProviderService register(HostProvider provider) { 38 public HostProviderService register(HostProvider provider) {
39 - log.info("Registering provider {}", provider.id());
40 return broker.register(provider); 39 return broker.register(provider);
41 } 40 }
42 41
43 @Override 42 @Override
44 public void unregister(HostProvider provider) { 43 public void unregister(HostProvider provider) {
45 - log.info("Unregistering provider {}", provider.id());
46 broker.unregister(provider); 44 broker.unregister(provider);
47 } 45 }
48 46
......
...@@ -36,13 +36,11 @@ public class SimpleLinkManager implements LinkProviderBroker { ...@@ -36,13 +36,11 @@ public class SimpleLinkManager implements LinkProviderBroker {
36 36
37 @Override 37 @Override
38 public LinkProviderService register(LinkProvider provider) { 38 public LinkProviderService register(LinkProvider provider) {
39 - log.info("Registering provider {}", provider.id());
40 return broker.register(provider); 39 return broker.register(provider);
41 } 40 }
42 41
43 @Override 42 @Override
44 public void unregister(LinkProvider provider) { 43 public void unregister(LinkProvider provider) {
45 - log.info("Unregistering provider {}", provider.id());
46 broker.unregister(provider); 44 broker.unregister(provider);
47 } 45 }
48 46
......