tom

Formalized DeviceStore in preparation for separating managers and stores.

1 +package org.onlab.onos.net.device;
2 +
3 +import org.onlab.onos.net.Device;
4 +import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.MastershipRole;
6 +import org.onlab.onos.net.Port;
7 +import org.onlab.onos.net.PortNumber;
8 +import org.onlab.onos.net.provider.ProviderId;
9 +
10 +import java.util.List;
11 +
12 +/**
13 + * Manages inventory of infrastructure devices. It may do so using whatever
14 + * means are appropriate.
15 + */
16 +public interface DeviceStore {
17 +
18 + /**
19 + * Returns the number of devices known to the system.
20 + *
21 + * @return number of devices
22 + */
23 + int getDeviceCount();
24 +
25 + /**
26 + * Returns an iterable collection of all devices known to the system.
27 + *
28 + * @return device collection
29 + */
30 + Iterable<Device> getDevices();
31 +
32 + /**
33 + * Returns the device with the specified identifier.
34 + *
35 + * @param deviceId device identifier
36 + * @return device
37 + */
38 + Device getDevice(DeviceId deviceId);
39 +
40 + /**
41 + * Creates a new infrastructure device, or updates an existing one using
42 + * the supplied device description.
43 + *
44 + * @param providerId provider identifier
45 + * @param deviceId device identifier
46 + * @param deviceDescription device description
47 + * @return ready to send event describing what occurred; null if no change
48 + */
49 + DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
50 + DeviceDescription deviceDescription);
51 +
52 + /**
53 + * Removes the specified infrastructure device.
54 + *
55 + * @param deviceId device identifier
56 + * @return ready to send event describing what occurred; null if no change
57 + */
58 + DeviceEvent markOffline(DeviceId deviceId);
59 +
60 + /**
61 + * Updates the ports of the specified infrastructure device using the given
62 + * list of port descriptions. The list is assumed to be comprehensive.
63 + *
64 + * @param deviceId device identifier
65 + * @param portDescriptions list of port descriptions
66 + * @return ready to send events describing what occurred; empty list if no change
67 + */
68 + List<DeviceEvent> updatePorts(DeviceId deviceId,
69 + List<PortDescription> portDescriptions);
70 +
71 + /**
72 + * Updates the port status of the specified infrastructure device using the
73 + * given port description.
74 + *
75 + * @param deviceId device identifier
76 + * @param portDescription port description
77 + * @return ready to send event describing what occurred; null if no change
78 + */
79 + DeviceEvent updatePortStatus(DeviceId deviceId,
80 + PortDescription portDescription);
81 +
82 + /**
83 + * Returns the list of ports that belong to the specified device.
84 + *
85 + * @param deviceId device identifier
86 + * @return list of device ports
87 + */
88 + List<Port> getPorts(DeviceId deviceId);
89 +
90 + /**
91 + * Returns the specified device port.
92 + *
93 + * @param deviceId device identifier
94 + * @param portNumber port number
95 + * @return device port
96 + */
97 + Port getPort(DeviceId deviceId, PortNumber portNumber);
98 +
99 + /**
100 + * Indicates whether the specified device is available/online.
101 + *
102 + * @param deviceId device identifier
103 + * @return true if device is available
104 + */
105 + boolean isAvailable(DeviceId deviceId);
106 +
107 + /**
108 + * Returns the mastership role determined for this device.
109 + *
110 + * @param deviceId device identifier
111 + * @return mastership role
112 + */
113 + MastershipRole getRole(DeviceId deviceId);
114 +
115 + /**
116 + * Administratively sets the role of the specified device.
117 + *
118 + * @param deviceId device identifier
119 + * @param role mastership role to apply
120 + * @return mastership role change event or null if no change
121 + */
122 + DeviceEvent setRole(DeviceId deviceId, MastershipRole role);
123 +
124 + /**
125 + * Administratively removes the specified device from the store.
126 + *
127 + * @param deviceId device to be removed
128 + */
129 + DeviceEvent removeDevice(DeviceId deviceId);
130 +}
...@@ -21,6 +21,7 @@ import org.onlab.onos.net.device.DeviceProvider; ...@@ -21,6 +21,7 @@ import org.onlab.onos.net.device.DeviceProvider;
21 import org.onlab.onos.net.device.DeviceProviderRegistry; 21 import org.onlab.onos.net.device.DeviceProviderRegistry;
22 import org.onlab.onos.net.device.DeviceProviderService; 22 import org.onlab.onos.net.device.DeviceProviderService;
23 import org.onlab.onos.net.device.DeviceService; 23 import org.onlab.onos.net.device.DeviceService;
24 +import org.onlab.onos.net.device.DeviceStore;
24 import org.onlab.onos.net.device.PortDescription; 25 import org.onlab.onos.net.device.PortDescription;
25 import org.onlab.onos.net.provider.AbstractProviderRegistry; 26 import org.onlab.onos.net.provider.AbstractProviderRegistry;
26 import org.onlab.onos.net.provider.AbstractProviderService; 27 import org.onlab.onos.net.provider.AbstractProviderService;
...@@ -36,7 +37,7 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -36,7 +37,7 @@ import static org.slf4j.LoggerFactory.getLogger;
36 */ 37 */
37 @Component(immediate = true) 38 @Component(immediate = true)
38 @Service 39 @Service
39 -public class SimpleDeviceManager 40 +public class DeviceManager
40 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> 41 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
41 implements DeviceService, DeviceAdminService, DeviceProviderRegistry { 42 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
42 43
...@@ -51,7 +52,8 @@ public class SimpleDeviceManager ...@@ -51,7 +52,8 @@ public class SimpleDeviceManager
51 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> 52 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener>
52 listenerRegistry = new AbstractListenerRegistry<>(); 53 listenerRegistry = new AbstractListenerRegistry<>();
53 54
54 - private final SimpleDeviceStore store = new SimpleDeviceStore(); 55 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 + protected DeviceStore store;
55 57
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected EventDeliveryService eventDispatcher; 59 protected EventDeliveryService eventDispatcher;
......
1 package org.onlab.onos.net.trivial.device.impl; 1 package org.onlab.onos.net.trivial.device.impl;
2 2
3 import com.google.common.collect.ImmutableList; 3 import com.google.common.collect.ImmutableList;
4 +import org.apache.felix.scr.annotations.Activate;
5 +import org.apache.felix.scr.annotations.Component;
6 +import org.apache.felix.scr.annotations.Deactivate;
7 +import org.apache.felix.scr.annotations.Service;
4 import org.onlab.onos.net.DefaultDevice; 8 import org.onlab.onos.net.DefaultDevice;
5 import org.onlab.onos.net.DefaultPort; 9 import org.onlab.onos.net.DefaultPort;
6 import org.onlab.onos.net.Device; 10 import org.onlab.onos.net.Device;
...@@ -10,8 +14,10 @@ import org.onlab.onos.net.Port; ...@@ -10,8 +14,10 @@ import org.onlab.onos.net.Port;
10 import org.onlab.onos.net.PortNumber; 14 import org.onlab.onos.net.PortNumber;
11 import org.onlab.onos.net.device.DeviceDescription; 15 import org.onlab.onos.net.device.DeviceDescription;
12 import org.onlab.onos.net.device.DeviceEvent; 16 import org.onlab.onos.net.device.DeviceEvent;
17 +import org.onlab.onos.net.device.DeviceStore;
13 import org.onlab.onos.net.device.PortDescription; 18 import org.onlab.onos.net.device.PortDescription;
14 import org.onlab.onos.net.provider.ProviderId; 19 import org.onlab.onos.net.provider.ProviderId;
20 +import org.slf4j.Logger;
15 21
16 import java.util.ArrayList; 22 import java.util.ArrayList;
17 import java.util.Collections; 23 import java.util.Collections;
...@@ -26,12 +32,17 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -26,12 +32,17 @@ import java.util.concurrent.ConcurrentHashMap;
26 32
27 import static com.google.common.base.Preconditions.checkArgument; 33 import static com.google.common.base.Preconditions.checkArgument;
28 import static org.onlab.onos.net.device.DeviceEvent.Type.*; 34 import static org.onlab.onos.net.device.DeviceEvent.Type.*;
35 +import static org.slf4j.LoggerFactory.getLogger;
29 36
30 /** 37 /**
31 * Manages inventory of infrastructure DEVICES using trivial in-memory 38 * Manages inventory of infrastructure DEVICES using trivial in-memory
32 * structures implementation. 39 * structures implementation.
33 */ 40 */
34 -class SimpleDeviceStore { 41 +@Component(immediate = true)
42 +@Service
43 +public class SimpleDeviceStore implements DeviceStore {
44 +
45 + private final Logger log = getLogger(getClass());
35 46
36 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; 47 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
37 48
...@@ -40,44 +51,32 @@ class SimpleDeviceStore { ...@@ -40,44 +51,32 @@ class SimpleDeviceStore {
40 private final Set<DeviceId> availableDevices = new HashSet<>(); 51 private final Set<DeviceId> availableDevices = new HashSet<>();
41 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); 52 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
42 53
43 - /** 54 + @Activate
44 - * Returns the number of devices known to the system. 55 + public void activate() {
45 - * 56 + log.info("Started");
46 - * @return number of devices 57 + }
47 - */ 58 +
48 - int getDeviceCount() { 59 + @Deactivate
60 + public void deactivate() {
61 + log.info("Stopped");
62 + }
63 + @Override
64 + public int getDeviceCount() {
49 return devices.size(); 65 return devices.size();
50 } 66 }
51 67
52 - /** 68 + @Override
53 - * Returns an iterable collection of all devices known to the system. 69 + public Iterable<Device> getDevices() {
54 - *
55 - * @return device collection
56 - */
57 - Iterable<Device> getDevices() {
58 return Collections.unmodifiableSet(new HashSet<Device>(devices.values())); 70 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
59 } 71 }
60 72
61 - /** 73 + @Override
62 - * Returns the device with the specified identifier. 74 + public Device getDevice(DeviceId deviceId) {
63 - *
64 - * @param deviceId device identifier
65 - * @return device
66 - */
67 - Device getDevice(DeviceId deviceId) {
68 return devices.get(deviceId); 75 return devices.get(deviceId);
69 } 76 }
70 77
71 - /** 78 + @Override
72 - * Creates a new infrastructure device, or updates an existing one using 79 + public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
73 - * the supplied device description.
74 - *
75 - * @param providerId provider identifier
76 - * @param deviceId device identifier
77 - * @param deviceDescription device description
78 - * @return ready to send event describing what occurred; null if no change
79 - */
80 - DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
81 DeviceDescription deviceDescription) { 80 DeviceDescription deviceDescription) {
82 DefaultDevice device = devices.get(deviceId); 81 DefaultDevice device = devices.get(deviceId);
83 if (device == null) { 82 if (device == null) {
...@@ -130,13 +129,8 @@ class SimpleDeviceStore { ...@@ -130,13 +129,8 @@ class SimpleDeviceStore {
130 } 129 }
131 } 130 }
132 131
133 - /** 132 + @Override
134 - * Removes the specified infrastructure device. 133 + public DeviceEvent markOffline(DeviceId deviceId) {
135 - *
136 - * @param deviceId device identifier
137 - * @return ready to send event describing what occurred; null if no change
138 - */
139 - DeviceEvent markOffline(DeviceId deviceId) {
140 synchronized (this) { 134 synchronized (this) {
141 Device device = devices.get(deviceId); 135 Device device = devices.get(deviceId);
142 boolean removed = device != null && availableDevices.remove(deviceId); 136 boolean removed = device != null && availableDevices.remove(deviceId);
...@@ -145,15 +139,8 @@ class SimpleDeviceStore { ...@@ -145,15 +139,8 @@ class SimpleDeviceStore {
145 } 139 }
146 } 140 }
147 141
148 - /** 142 + @Override
149 - * Updates the ports of the specified infrastructure device using the given 143 + public List<DeviceEvent> updatePorts(DeviceId deviceId,
150 - * list of port descriptions. The list is assumed to be comprehensive.
151 - *
152 - * @param deviceId device identifier
153 - * @param portDescriptions list of port descriptions
154 - * @return ready to send events describing what occurred; empty list if no change
155 - */
156 - List<DeviceEvent> updatePorts(DeviceId deviceId,
157 List<PortDescription> portDescriptions) { 144 List<PortDescription> portDescriptions) {
158 List<DeviceEvent> events = new ArrayList<>(); 145 List<DeviceEvent> events = new ArrayList<>();
159 synchronized (this) { 146 synchronized (this) {
...@@ -230,15 +217,8 @@ class SimpleDeviceStore { ...@@ -230,15 +217,8 @@ class SimpleDeviceStore {
230 return ports; 217 return ports;
231 } 218 }
232 219
233 - /** 220 + @Override
234 - * Updates the port status of the specified infrastructure device using the 221 + public DeviceEvent updatePortStatus(DeviceId deviceId,
235 - * given port description.
236 - *
237 - * @param deviceId device identifier
238 - * @param portDescription port description
239 - * @return ready to send event describing what occurred; null if no change
240 - */
241 - DeviceEvent updatePortStatus(DeviceId deviceId,
242 PortDescription portDescription) { 222 PortDescription portDescription) {
243 synchronized (this) { 223 synchronized (this) {
244 Device device = devices.get(deviceId); 224 Device device = devices.get(deviceId);
...@@ -249,58 +229,31 @@ class SimpleDeviceStore { ...@@ -249,58 +229,31 @@ class SimpleDeviceStore {
249 } 229 }
250 } 230 }
251 231
252 - /** 232 + @Override
253 - * Returns the list of ports that belong to the specified device. 233 + public List<Port> getPorts(DeviceId deviceId) {
254 - *
255 - * @param deviceId device identifier
256 - * @return list of device ports
257 - */
258 - List<Port> getPorts(DeviceId deviceId) {
259 Map<PortNumber, Port> ports = devicePorts.get(deviceId); 234 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
260 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values()); 235 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
261 } 236 }
262 237
263 - /** 238 + @Override
264 - * Returns the specified device port. 239 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
265 - *
266 - * @param deviceId device identifier
267 - * @param portNumber port number
268 - * @return device port
269 - */
270 - Port getPort(DeviceId deviceId, PortNumber portNumber) {
271 Map<PortNumber, Port> ports = devicePorts.get(deviceId); 240 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
272 return ports == null ? null : ports.get(portNumber); 241 return ports == null ? null : ports.get(portNumber);
273 } 242 }
274 243
275 - /** 244 + @Override
276 - * Indicates whether the specified device is available/online. 245 + public boolean isAvailable(DeviceId deviceId) {
277 - *
278 - * @param deviceId device identifier
279 - * @return true if device is available
280 - */
281 - boolean isAvailable(DeviceId deviceId) {
282 return availableDevices.contains(deviceId); 246 return availableDevices.contains(deviceId);
283 } 247 }
284 248
285 - /** 249 + @Override
286 - * Returns the mastership role determined for this device. 250 + public MastershipRole getRole(DeviceId deviceId) {
287 - *
288 - * @param deviceId device identifier
289 - * @return mastership role
290 - */
291 - MastershipRole getRole(DeviceId deviceId) {
292 MastershipRole role = roles.get(deviceId); 251 MastershipRole role = roles.get(deviceId);
293 return role != null ? role : MastershipRole.NONE; 252 return role != null ? role : MastershipRole.NONE;
294 } 253 }
295 254
296 - /** 255 + @Override
297 - * Administratively sets the role of the specified device. 256 + public DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
298 - *
299 - * @param deviceId device identifier
300 - * @param role mastership role to apply
301 - * @return mastership role change event or null if no change
302 - */
303 - DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
304 synchronized (this) { 257 synchronized (this) {
305 Device device = getDevice(deviceId); 258 Device device = getDevice(deviceId);
306 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); 259 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
...@@ -310,12 +263,8 @@ class SimpleDeviceStore { ...@@ -310,12 +263,8 @@ class SimpleDeviceStore {
310 } 263 }
311 } 264 }
312 265
313 - /** 266 + @Override
314 - * Administratively removes the specified device from the store. 267 + public DeviceEvent removeDevice(DeviceId deviceId) {
315 - *
316 - * @param deviceId device to be removed
317 - */
318 - DeviceEvent removeDevice(DeviceId deviceId) {
319 synchronized (this) { 268 synchronized (this) {
320 roles.remove(deviceId); 269 roles.remove(deviceId);
321 Device device = devices.remove(deviceId); 270 Device device = devices.remove(deviceId);
......
...@@ -36,7 +36,7 @@ import static org.onlab.onos.net.device.DeviceEvent.Type.*; ...@@ -36,7 +36,7 @@ import static org.onlab.onos.net.device.DeviceEvent.Type.*;
36 /** 36 /**
37 * Test codifying the device service & device provider service contracts. 37 * Test codifying the device service & device provider service contracts.
38 */ 38 */
39 -public class SimpleDeviceManagerTest { 39 +public class DeviceManagerTest {
40 40
41 private static final ProviderId PID = new ProviderId("of", "foo"); 41 private static final ProviderId PID = new ProviderId("of", "foo");
42 private static final DeviceId DID1 = deviceId("of:foo"); 42 private static final DeviceId DID1 = deviceId("of:foo");
...@@ -51,8 +51,7 @@ public class SimpleDeviceManagerTest { ...@@ -51,8 +51,7 @@ public class SimpleDeviceManagerTest {
51 private static final PortNumber P2 = PortNumber.portNumber(2); 51 private static final PortNumber P2 = PortNumber.portNumber(2);
52 private static final PortNumber P3 = PortNumber.portNumber(3); 52 private static final PortNumber P3 = PortNumber.portNumber(3);
53 53
54 - 54 + private DeviceManager mgr;
55 - private SimpleDeviceManager mgr;
56 55
57 protected DeviceService service; 56 protected DeviceService service;
58 protected DeviceAdminService admin; 57 protected DeviceAdminService admin;
...@@ -63,10 +62,11 @@ public class SimpleDeviceManagerTest { ...@@ -63,10 +62,11 @@ public class SimpleDeviceManagerTest {
63 62
64 @Before 63 @Before
65 public void setUp() { 64 public void setUp() {
66 - mgr = new SimpleDeviceManager(); 65 + mgr = new DeviceManager();
67 service = mgr; 66 service = mgr;
68 admin = mgr; 67 admin = mgr;
69 registry = mgr; 68 registry = mgr;
69 + mgr.store = new SimpleDeviceStore();
70 mgr.eventDispatcher = new TestEventDispatcher(); 70 mgr.eventDispatcher = new TestEventDispatcher();
71 mgr.activate(); 71 mgr.activate();
72 72
......
...@@ -22,7 +22,7 @@ import org.onlab.onos.net.link.LinkService; ...@@ -22,7 +22,7 @@ import org.onlab.onos.net.link.LinkService;
22 import org.onlab.onos.net.provider.AbstractProvider; 22 import org.onlab.onos.net.provider.AbstractProvider;
23 import org.onlab.onos.net.provider.ProviderId; 23 import org.onlab.onos.net.provider.ProviderId;
24 import org.onlab.onos.event.impl.TestEventDispatcher; 24 import org.onlab.onos.event.impl.TestEventDispatcher;
25 -import org.onlab.onos.net.trivial.device.impl.SimpleDeviceManager; 25 +import org.onlab.onos.net.trivial.device.impl.DeviceManager;
26 26
27 import java.util.ArrayList; 27 import java.util.ArrayList;
28 import java.util.Iterator; 28 import java.util.Iterator;
...@@ -66,7 +66,7 @@ public class SimpleLinkManagerTest { ...@@ -66,7 +66,7 @@ public class SimpleLinkManagerTest {
66 admin = mgr; 66 admin = mgr;
67 registry = mgr; 67 registry = mgr;
68 mgr.eventDispatcher = new TestEventDispatcher(); 68 mgr.eventDispatcher = new TestEventDispatcher();
69 - mgr.deviceService = new SimpleDeviceManager(); 69 + mgr.deviceService = new DeviceManager();
70 mgr.activate(); 70 mgr.activate();
71 71
72 service.addListener(listener); 72 service.addListener(listener);
......
...@@ -15,7 +15,7 @@ import org.onlab.onos.net.topology.GraphDescription; ...@@ -15,7 +15,7 @@ import org.onlab.onos.net.topology.GraphDescription;
15 import org.onlab.onos.net.topology.TopologyProvider; 15 import org.onlab.onos.net.topology.TopologyProvider;
16 import org.onlab.onos.net.topology.TopologyProviderRegistry; 16 import org.onlab.onos.net.topology.TopologyProviderRegistry;
17 import org.onlab.onos.net.topology.TopologyProviderService; 17 import org.onlab.onos.net.topology.TopologyProviderService;
18 -import org.onlab.onos.net.trivial.device.impl.SimpleDeviceManager; 18 +import org.onlab.onos.net.trivial.device.impl.DeviceManager;
19 import org.onlab.onos.net.trivial.link.impl.SimpleLinkManager; 19 import org.onlab.onos.net.trivial.link.impl.SimpleLinkManager;
20 20
21 import java.util.List; 21 import java.util.List;
...@@ -122,7 +122,7 @@ public class DefaultTopologyProviderTest { ...@@ -122,7 +122,7 @@ public class DefaultTopologyProviderTest {
122 } 122 }
123 } 123 }
124 124
125 - private class TestDeviceService extends SimpleDeviceManager { 125 + private class TestDeviceService extends DeviceManager {
126 TestDeviceService() { 126 TestDeviceService() {
127 eventDispatcher = new TestEventDispatcher(); 127 eventDispatcher = new TestEventDispatcher();
128 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); 128 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
......