Fix bug where an ancillary provider was allowed to be created even if there was no primary
Change-Id: I910269240582a0eccc6e2d8e4de0c9c3f5e4ad65
diff --git a/core/api/src/main/java/org/onosproject/net/provider/AbstractProviderRegistry.java b/core/api/src/main/java/org/onosproject/net/provider/AbstractProviderRegistry.java
index 2c89221..bfd54bf 100644
--- a/core/api/src/main/java/org/onosproject/net/provider/AbstractProviderRegistry.java
+++ b/core/api/src/main/java/org/onosproject/net/provider/AbstractProviderRegistry.java
@@ -17,6 +17,8 @@
import com.google.common.collect.ImmutableSet;
import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
@@ -55,24 +57,37 @@
return null;
}
+ private static final Logger log = LoggerFactory
+ .getLogger(AbstractProviderRegistry.class);
+
@Override
public synchronized S register(P provider) {
+
+ log.debug("registering provider {} :{}", provider, provider.id());
checkNotNull(provider, "Provider cannot be null");
- checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
// If the provider is a primary one, check for a conflict.
ProviderId pid = provider.id();
checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()),
- "A primary provider with id %s is already registered",
- providersByScheme.get(pid.scheme()));
+ "A primary provider with id %s is already registered",
+ providersByScheme.get(pid.scheme()));
- S service = createProviderService(provider);
- services.put(provider.id(), service);
- providers.put(provider.id(), provider);
+ checkState(pid.isAncillary() || !services.containsKey(provider.id()),
+ "Provider %s already registered", provider.id());
// Register the provider by URI scheme only if it is not ancillary.
+ S service = null;
+
if (!pid.isAncillary()) {
+ service = createProviderService(provider);
+ services.put(provider.id(), service);
+ providers.put(provider.id(), provider);
providersByScheme.put(pid.scheme(), provider);
+ } else {
+ ProviderId servicePid = new ProviderId(provider.id().scheme(), provider.id().id(), false);
+ service = services.get(servicePid);
+ checkState(service != null,
+ "Primary provider with id %s not registered yet", pid);
}
return service;
diff --git a/core/api/src/test/java/org/onosproject/net/provider/AbstractProviderRegistryTest.java b/core/api/src/test/java/org/onosproject/net/provider/AbstractProviderRegistryTest.java
index ccefcf0..be1f048 100644
--- a/core/api/src/test/java/org/onosproject/net/provider/AbstractProviderRegistryTest.java
+++ b/core/api/src/test/java/org/onosproject/net/provider/AbstractProviderRegistryTest.java
@@ -68,9 +68,11 @@
public void ancillaryProviders() {
TestProviderRegistry registry = new TestProviderRegistry();
TestProvider pFoo = new TestProvider(new ProviderId("of", "foo"));
- TestProvider pBar = new TestProvider(new ProviderId("of", "bar", true));
+ TestProvider pBar = new TestProvider(new ProviderId("of2", "bar"));
+ TestProvider aBar = new TestProvider(new ProviderId("of", "foo", true));
registry.register(pFoo);
registry.register(pBar);
+ registry.register(aBar);
assertEquals("incorrect provider count", 2, registry.getProviders().size());
}