blob: 35aed74c8646a248fce3bc746647931941dad9a4 [file] [log] [blame]
tom64b7aac2014-08-26 00:18:21 -07001package org.onlab.onos.net.provider;
2
tom132b58a2014-08-28 16:11:28 -07003import com.google.common.collect.ImmutableSet;
tom7e02cda2014-09-18 12:05:46 -07004import org.onlab.onos.net.DeviceId;
tom132b58a2014-08-28 16:11:28 -07005
tom64b7aac2014-08-26 00:18:21 -07006import java.util.HashMap;
7import java.util.Map;
tom132b58a2014-08-28 16:11:28 -07008import java.util.Set;
tom64b7aac2014-08-26 00:18:21 -07009
tom64b7aac2014-08-26 00:18:21 -070010import static com.google.common.base.Preconditions.checkNotNull;
tom132b58a2014-08-28 16:11:28 -070011import static com.google.common.base.Preconditions.checkState;
tom64b7aac2014-08-26 00:18:21 -070012
13/**
tom96dfcab2014-08-28 09:26:03 -070014 * Base implementation of provider registry.
tom64b7aac2014-08-26 00:18:21 -070015 *
16 * @param <P> type of the information provider
17 * @param <S> type of the provider service
18 */
tom96dfcab2014-08-28 09:26:03 -070019public abstract class AbstractProviderRegistry<P extends Provider, S extends ProviderService<P>>
20 implements ProviderRegistry<P, S> {
tom64b7aac2014-08-26 00:18:21 -070021
tome5ec3fd2014-09-04 15:18:06 -070022 private final Map<ProviderId, P> providers = new HashMap<>();
tom64b7aac2014-08-26 00:18:21 -070023 private final Map<ProviderId, S> services = new HashMap<>();
tom7e02cda2014-09-18 12:05:46 -070024 private final Map<String, P> providersByScheme = new HashMap<>();
tom64b7aac2014-08-26 00:18:21 -070025
26 /**
27 * Creates a new provider service bound to the specified provider.
28 *
29 * @param provider provider
30 * @return provider service
31 */
32 protected abstract S createProviderService(P provider);
33
34 @Override
35 public synchronized S register(P provider) {
36 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070037 checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
toma5368862014-10-01 12:35:48 -070038
39 // If the provider is a primary one, check for a conflict.
40 ProviderId pid = provider.id();
41 checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()),
42 "A primary provider with id %s is already registered",
43 providersByScheme.get(pid.scheme()));
44
tom64b7aac2014-08-26 00:18:21 -070045 S service = createProviderService(provider);
46 services.put(provider.id(), service);
tome5ec3fd2014-09-04 15:18:06 -070047 providers.put(provider.id(), provider);
toma5368862014-10-01 12:35:48 -070048
49 // Register the provider by URI scheme only if it is not ancillary.
50 if (!pid.isAncillary()) {
51 providersByScheme.put(pid.scheme(), provider);
52 }
53
tom64b7aac2014-08-26 00:18:21 -070054 return service;
55 }
56
57 @Override
58 public synchronized void unregister(P provider) {
59 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070060 S service = services.get(provider.id());
tomb1260e42014-08-26 18:39:57 -070061 if (service != null && service instanceof AbstractProviderService) {
tom64b7aac2014-08-26 00:18:21 -070062 ((AbstractProviderService) service).invalidate();
tom132b58a2014-08-28 16:11:28 -070063 services.remove(provider.id());
tome5ec3fd2014-09-04 15:18:06 -070064 providers.remove(provider.id());
alshabib23022f72014-10-17 14:50:00 -070065 if (!provider.id().isAncillary()) {
66 providersByScheme.remove(provider.id().scheme());
67 }
tom64b7aac2014-08-26 00:18:21 -070068 }
tom64b7aac2014-08-26 00:18:21 -070069 }
tom132b58a2014-08-28 16:11:28 -070070
71 @Override
72 public synchronized Set<ProviderId> getProviders() {
73 return ImmutableSet.copyOf(services.keySet());
74 }
75
tome5ec3fd2014-09-04 15:18:06 -070076 /**
77 * Returns the provider registered with the specified provider ID.
78 *
79 * @param providerId provider identifier
80 * @return provider
81 */
82 protected synchronized P getProvider(ProviderId providerId) {
83 return providers.get(providerId);
84 }
85
tom7e02cda2014-09-18 12:05:46 -070086 /**
87 * Returns the provider for the specified device ID based on URI scheme.
88 *
89 * @param deviceId device identifier
90 * @return provider bound to the URI scheme
91 */
92 protected synchronized P getProvider(DeviceId deviceId) {
93 return providersByScheme.get(deviceId.uri().getScheme());
94 }
95
tom64b7aac2014-08-26 00:18:21 -070096}