tom

Added some tests.

1 package org.onlab.onos.net.provider; 1 package org.onlab.onos.net.provider;
2 2
3 +import com.google.common.collect.ImmutableSet;
4 +
3 import java.util.HashMap; 5 import java.util.HashMap;
4 import java.util.Map; 6 import java.util.Map;
7 +import java.util.Set;
5 8
6 -import static com.google.common.base.Preconditions.checkArgument;
7 import static com.google.common.base.Preconditions.checkNotNull; 9 import static com.google.common.base.Preconditions.checkNotNull;
10 +import static com.google.common.base.Preconditions.checkState;
8 11
9 /** 12 /**
10 * Base implementation of provider registry. 13 * Base implementation of provider registry.
...@@ -28,7 +31,7 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro ...@@ -28,7 +31,7 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
28 @Override 31 @Override
29 public synchronized S register(P provider) { 32 public synchronized S register(P provider) {
30 checkNotNull(provider, "Provider cannot be null"); 33 checkNotNull(provider, "Provider cannot be null");
31 - checkArgument(!services.containsKey(provider), "Provider %s already registered", provider.id()); 34 + checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
32 S service = createProviderService(provider); 35 S service = createProviderService(provider);
33 services.put(provider.id(), service); 36 services.put(provider.id(), service);
34 return service; 37 return service;
...@@ -37,10 +40,16 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro ...@@ -37,10 +40,16 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
37 @Override 40 @Override
38 public synchronized void unregister(P provider) { 41 public synchronized void unregister(P provider) {
39 checkNotNull(provider, "Provider cannot be null"); 42 checkNotNull(provider, "Provider cannot be null");
40 - S service = services.get(provider); 43 + S service = services.get(provider.id());
41 if (service != null && service instanceof AbstractProviderService) { 44 if (service != null && service instanceof AbstractProviderService) {
42 ((AbstractProviderService) service).invalidate(); 45 ((AbstractProviderService) service).invalidate();
43 - services.remove(provider); 46 + services.remove(provider.id());
44 } 47 }
45 } 48 }
49 +
50 + @Override
51 + public synchronized Set<ProviderId> getProviders() {
52 + return ImmutableSet.copyOf(services.keySet());
53 + }
54 +
46 } 55 }
......
1 package org.onlab.onos.net.provider; 1 package org.onlab.onos.net.provider;
2 2
3 +import java.util.Set;
4 +
3 /** 5 /**
4 * Registry for tracking information providers with the core. 6 * Registry for tracking information providers with the core.
5 * 7 *
...@@ -29,4 +31,11 @@ public interface ProviderRegistry<P extends Provider, S extends ProviderService< ...@@ -29,4 +31,11 @@ public interface ProviderRegistry<P extends Provider, S extends ProviderService<
29 */ 31 */
30 void unregister(P provider); 32 void unregister(P provider);
31 33
34 + /**
35 + * Returns a set of currently registered provider identities.
36 + *
37 + * @return set of provider identifiers
38 + */
39 + Set<ProviderId> getProviders();
40 +
32 } 41 }
......
1 +package org.onlab.onos.net.provider;
2 +
3 +import org.junit.Test;
4 +
5 +import static org.hamcrest.MatcherAssert.assertThat;
6 +import static org.junit.Assert.assertEquals;
7 +
8 +/**
9 + * Test of the base provider registry.
10 + */
11 +public class AbstractProviderRegistryTest {
12 +
13 + private class TestProviderService extends AbstractProviderService<TestProvider> {
14 + protected TestProviderService(TestProvider provider) {
15 + super(provider);
16 + }
17 + }
18 +
19 + private class TestProviderRegistry extends AbstractProviderRegistry<TestProvider, TestProviderService> {
20 + @Override
21 + protected TestProviderService createProviderService(TestProvider provider) {
22 + return new TestProviderService(provider);
23 + }
24 + }
25 +
26 + @Test
27 + public void basics() {
28 + TestProviderRegistry registry = new TestProviderRegistry();
29 + assertEquals("incorrect provider count", 0, registry.getProviders().size());
30 +
31 + ProviderId fooId = new ProviderId("foo");
32 + TestProvider pFoo = new TestProvider(fooId);
33 + TestProviderService psFoo = registry.register(pFoo);
34 + assertEquals("incorrect provider count", 1, registry.getProviders().size());
35 + assertThat("provider not found", registry.getProviders().contains(fooId));
36 + assertEquals("incorrect provider", psFoo.provider(), pFoo);
37 +
38 + ProviderId barId = new ProviderId("bar");
39 + TestProvider pBar = new TestProvider(barId);
40 + TestProviderService psBar = registry.register(pBar);
41 + assertEquals("incorrect provider count", 2, registry.getProviders().size());
42 + assertThat("provider not found", registry.getProviders().contains(barId));
43 + assertEquals("incorrect provider", psBar.provider(), pBar);
44 +
45 + psFoo.checkValidity();
46 + registry.unregister(pFoo);
47 + psBar.checkValidity();
48 + assertEquals("incorrect provider count", 1, registry.getProviders().size());
49 + assertThat("provider not found", registry.getProviders().contains(barId));
50 + }
51 +
52 + @Test(expected = IllegalStateException.class)
53 + public void duplicateRegistration() {
54 + TestProviderRegistry registry = new TestProviderRegistry();
55 + TestProvider pFoo = new TestProvider(new ProviderId("foo"));
56 + registry.register(pFoo);
57 + registry.register(pFoo);
58 + }
59 +
60 + @Test
61 + public void voidUnregistration() {
62 + TestProviderRegistry registry = new TestProviderRegistry();
63 + registry.unregister(new TestProvider(new ProviderId("foo")));
64 + }
65 +
66 + @Test(expected = IllegalStateException.class)
67 + public void unregistration() {
68 + TestProviderRegistry registry = new TestProviderRegistry();
69 + TestProvider pFoo = new TestProvider(new ProviderId("foo"));
70 + TestProviderService psFoo = registry.register(pFoo);
71 + registry.unregister(pFoo);
72 + psFoo.checkValidity();
73 + }
74 +}
...@@ -2,6 +2,8 @@ package org.onlab.onos.net.trivial.impl; ...@@ -2,6 +2,8 @@ package org.onlab.onos.net.trivial.impl;
2 2
3 import org.onlab.onos.net.Device; 3 import org.onlab.onos.net.Device;
4 import org.onlab.onos.net.DeviceId; 4 import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.Port;
6 +import org.onlab.onos.net.PortNumber;
5 import org.onlab.onos.net.device.DeviceDescription; 7 import org.onlab.onos.net.device.DeviceDescription;
6 import org.onlab.onos.net.device.DeviceEvent; 8 import org.onlab.onos.net.device.DeviceEvent;
7 import org.onlab.onos.net.device.PortDescription; 9 import org.onlab.onos.net.device.PortDescription;
...@@ -66,4 +68,35 @@ public class DeviceStore { ...@@ -66,4 +68,35 @@ public class DeviceStore {
66 PortDescription portDescription) { 68 PortDescription portDescription) {
67 return null; 69 return null;
68 } 70 }
71 +
72 + /**
73 + * Returns the device with the specified identifier.
74 + *
75 + * @param deviceId device identifier
76 + * @return device
77 + */
78 + public Device getDevice(DeviceId deviceId) {
79 + return null;
80 + }
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 + public List<Port> getPorts(DeviceId deviceId) {
89 + return null;
90 + }
91 +
92 + /**
93 + * Returns the specified device port.
94 + *
95 + * @param deviceId device identifier
96 + * @param portNumber port number
97 + * @return device port
98 + */
99 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
100 + return null;
101 + }
69 } 102 }
......
...@@ -80,20 +80,20 @@ public class SimpleDeviceManager ...@@ -80,20 +80,20 @@ public class SimpleDeviceManager
80 @Override 80 @Override
81 public Device getDevice(DeviceId deviceId) { 81 public Device getDevice(DeviceId deviceId) {
82 checkNotNull(deviceId, DEVICE_ID_NULL); 82 checkNotNull(deviceId, DEVICE_ID_NULL);
83 - return null; 83 + return store.getDevice(deviceId);
84 } 84 }
85 85
86 @Override 86 @Override
87 public List<Port> getPorts(DeviceId deviceId) { 87 public List<Port> getPorts(DeviceId deviceId) {
88 checkNotNull(deviceId, DEVICE_ID_NULL); 88 checkNotNull(deviceId, DEVICE_ID_NULL);
89 - return null; 89 + return store.getPorts(deviceId);
90 } 90 }
91 91
92 @Override 92 @Override
93 public Port getPort(DeviceId deviceId, PortNumber portNumber) { 93 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
94 checkNotNull(deviceId, DEVICE_ID_NULL); 94 checkNotNull(deviceId, DEVICE_ID_NULL);
95 checkNotNull(portNumber, PORT_NUMBER_NULL); 95 checkNotNull(portNumber, PORT_NUMBER_NULL);
96 - return null; 96 + return store.getPort(deviceId, portNumber);
97 } 97 }
98 98
99 @Override 99 @Override
......