tom

Added some tests.

package org.onlab.onos.net.provider;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* Base implementation of provider registry.
......@@ -28,7 +31,7 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
@Override
public synchronized S register(P provider) {
checkNotNull(provider, "Provider cannot be null");
checkArgument(!services.containsKey(provider), "Provider %s already registered", provider.id());
checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
S service = createProviderService(provider);
services.put(provider.id(), service);
return service;
......@@ -37,10 +40,16 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
@Override
public synchronized void unregister(P provider) {
checkNotNull(provider, "Provider cannot be null");
S service = services.get(provider);
S service = services.get(provider.id());
if (service != null && service instanceof AbstractProviderService) {
((AbstractProviderService) service).invalidate();
services.remove(provider);
services.remove(provider.id());
}
}
@Override
public synchronized Set<ProviderId> getProviders() {
return ImmutableSet.copyOf(services.keySet());
}
}
......
package org.onlab.onos.net.provider;
import java.util.Set;
/**
* Registry for tracking information providers with the core.
*
......@@ -29,4 +31,11 @@ public interface ProviderRegistry<P extends Provider, S extends ProviderService<
*/
void unregister(P provider);
/**
* Returns a set of currently registered provider identities.
*
* @return set of provider identifiers
*/
Set<ProviderId> getProviders();
}
......
package org.onlab.onos.net.provider;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
/**
* Test of the base provider registry.
*/
public class AbstractProviderRegistryTest {
private class TestProviderService extends AbstractProviderService<TestProvider> {
protected TestProviderService(TestProvider provider) {
super(provider);
}
}
private class TestProviderRegistry extends AbstractProviderRegistry<TestProvider, TestProviderService> {
@Override
protected TestProviderService createProviderService(TestProvider provider) {
return new TestProviderService(provider);
}
}
@Test
public void basics() {
TestProviderRegistry registry = new TestProviderRegistry();
assertEquals("incorrect provider count", 0, registry.getProviders().size());
ProviderId fooId = new ProviderId("foo");
TestProvider pFoo = new TestProvider(fooId);
TestProviderService psFoo = registry.register(pFoo);
assertEquals("incorrect provider count", 1, registry.getProviders().size());
assertThat("provider not found", registry.getProviders().contains(fooId));
assertEquals("incorrect provider", psFoo.provider(), pFoo);
ProviderId barId = new ProviderId("bar");
TestProvider pBar = new TestProvider(barId);
TestProviderService psBar = registry.register(pBar);
assertEquals("incorrect provider count", 2, registry.getProviders().size());
assertThat("provider not found", registry.getProviders().contains(barId));
assertEquals("incorrect provider", psBar.provider(), pBar);
psFoo.checkValidity();
registry.unregister(pFoo);
psBar.checkValidity();
assertEquals("incorrect provider count", 1, registry.getProviders().size());
assertThat("provider not found", registry.getProviders().contains(barId));
}
@Test(expected = IllegalStateException.class)
public void duplicateRegistration() {
TestProviderRegistry registry = new TestProviderRegistry();
TestProvider pFoo = new TestProvider(new ProviderId("foo"));
registry.register(pFoo);
registry.register(pFoo);
}
@Test
public void voidUnregistration() {
TestProviderRegistry registry = new TestProviderRegistry();
registry.unregister(new TestProvider(new ProviderId("foo")));
}
@Test(expected = IllegalStateException.class)
public void unregistration() {
TestProviderRegistry registry = new TestProviderRegistry();
TestProvider pFoo = new TestProvider(new ProviderId("foo"));
TestProviderService psFoo = registry.register(pFoo);
registry.unregister(pFoo);
psFoo.checkValidity();
}
}
......@@ -2,6 +2,8 @@ package org.onlab.onos.net.trivial.impl;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.Port;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.device.DeviceDescription;
import org.onlab.onos.net.device.DeviceEvent;
import org.onlab.onos.net.device.PortDescription;
......@@ -66,4 +68,35 @@ public class DeviceStore {
PortDescription portDescription) {
return null;
}
/**
* Returns the device with the specified identifier.
*
* @param deviceId device identifier
* @return device
*/
public Device getDevice(DeviceId deviceId) {
return null;
}
/**
* Returns the list of ports that belong to the specified device.
*
* @param deviceId device identifier
* @return list of device ports
*/
public List<Port> getPorts(DeviceId deviceId) {
return null;
}
/**
* Returns the specified device port.
*
* @param deviceId device identifier
* @param portNumber port number
* @return device port
*/
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
return null;
}
}
......
......@@ -80,20 +80,20 @@ public class SimpleDeviceManager
@Override
public Device getDevice(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return null;
return store.getDevice(deviceId);
}
@Override
public List<Port> getPorts(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return null;
return store.getPorts(deviceId);
}
@Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portNumber, PORT_NUMBER_NULL);
return null;
return store.getPort(deviceId, portNumber);
}
@Override
......