blob: 2c89221f63b84174c0d8a89d3978cf60fc9a57ef [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.provider;
tom64b7aac2014-08-26 00:18:21 -070017
tom132b58a2014-08-28 16:11:28 -070018import com.google.common.collect.ImmutableSet;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.DeviceId;
tom132b58a2014-08-28 16:11:28 -070020
tom64b7aac2014-08-26 00:18:21 -070021import java.util.HashMap;
22import java.util.Map;
tom132b58a2014-08-28 16:11:28 -070023import java.util.Set;
tom64b7aac2014-08-26 00:18:21 -070024
tom64b7aac2014-08-26 00:18:21 -070025import static com.google.common.base.Preconditions.checkNotNull;
tom132b58a2014-08-28 16:11:28 -070026import static com.google.common.base.Preconditions.checkState;
tom64b7aac2014-08-26 00:18:21 -070027
28/**
tom96dfcab2014-08-28 09:26:03 -070029 * Base implementation of provider registry.
tom64b7aac2014-08-26 00:18:21 -070030 *
31 * @param <P> type of the information provider
32 * @param <S> type of the provider service
33 */
tom96dfcab2014-08-28 09:26:03 -070034public abstract class AbstractProviderRegistry<P extends Provider, S extends ProviderService<P>>
35 implements ProviderRegistry<P, S> {
tom64b7aac2014-08-26 00:18:21 -070036
tome5ec3fd2014-09-04 15:18:06 -070037 private final Map<ProviderId, P> providers = new HashMap<>();
tom64b7aac2014-08-26 00:18:21 -070038 private final Map<ProviderId, S> services = new HashMap<>();
tom7e02cda2014-09-18 12:05:46 -070039 private final Map<String, P> providersByScheme = new HashMap<>();
tom64b7aac2014-08-26 00:18:21 -070040
41 /**
42 * Creates a new provider service bound to the specified provider.
43 *
44 * @param provider provider
45 * @return provider service
46 */
47 protected abstract S createProviderService(P provider);
48
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080049 /**
50 * Returns the default fall-back provider. Default implementation return null.
51 *
52 * @return default provider
53 */
54 protected P defaultProvider() {
55 return null;
56 }
57
tom64b7aac2014-08-26 00:18:21 -070058 @Override
59 public synchronized S register(P provider) {
60 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070061 checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
toma5368862014-10-01 12:35:48 -070062
63 // If the provider is a primary one, check for a conflict.
64 ProviderId pid = provider.id();
65 checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()),
66 "A primary provider with id %s is already registered",
67 providersByScheme.get(pid.scheme()));
68
tom64b7aac2014-08-26 00:18:21 -070069 S service = createProviderService(provider);
70 services.put(provider.id(), service);
tome5ec3fd2014-09-04 15:18:06 -070071 providers.put(provider.id(), provider);
toma5368862014-10-01 12:35:48 -070072
73 // Register the provider by URI scheme only if it is not ancillary.
74 if (!pid.isAncillary()) {
75 providersByScheme.put(pid.scheme(), provider);
76 }
77
tom64b7aac2014-08-26 00:18:21 -070078 return service;
79 }
80
81 @Override
82 public synchronized void unregister(P provider) {
83 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070084 S service = services.get(provider.id());
Thomas Vachuska5b38dc02018-05-10 15:24:40 -070085 if (service instanceof AbstractProviderService) {
tom64b7aac2014-08-26 00:18:21 -070086 ((AbstractProviderService) service).invalidate();
tom132b58a2014-08-28 16:11:28 -070087 services.remove(provider.id());
tome5ec3fd2014-09-04 15:18:06 -070088 providers.remove(provider.id());
alshabib23022f72014-10-17 14:50:00 -070089 if (!provider.id().isAncillary()) {
90 providersByScheme.remove(provider.id().scheme());
91 }
tom64b7aac2014-08-26 00:18:21 -070092 }
tom64b7aac2014-08-26 00:18:21 -070093 }
tom132b58a2014-08-28 16:11:28 -070094
95 @Override
96 public synchronized Set<ProviderId> getProviders() {
97 return ImmutableSet.copyOf(services.keySet());
98 }
99
tome5ec3fd2014-09-04 15:18:06 -0700100 /**
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800101 * Returns the provider registered with the specified provider ID or null
102 * if none is found for the given provider family and default fall-back is
103 * not supported.
tome5ec3fd2014-09-04 15:18:06 -0700104 *
105 * @param providerId provider identifier
106 * @return provider
107 */
108 protected synchronized P getProvider(ProviderId providerId) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800109 P provider = providers.get(providerId);
110 return provider != null ? provider : defaultProvider();
tome5ec3fd2014-09-04 15:18:06 -0700111 }
112
tom7e02cda2014-09-18 12:05:46 -0700113 /**
114 * Returns the provider for the specified device ID based on URI scheme.
115 *
116 * @param deviceId device identifier
117 * @return provider bound to the URI scheme
118 */
119 protected synchronized P getProvider(DeviceId deviceId) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800120 P provider = providersByScheme.get(deviceId.uri().getScheme());
121 return provider != null ? provider : defaultProvider();
tom7e02cda2014-09-18 12:05:46 -0700122 }
123
Madan Jampaniad3c5262016-01-20 00:50:17 -0800124 /**
125 * Returns the provider registered with the specified scheme.
126 *
127 * @param scheme provider scheme
128 * @return provider
129 */
130 protected synchronized P getProvider(String scheme) {
131 return providersByScheme.get(scheme);
132 }
133
tom64b7aac2014-08-26 00:18:21 -0700134}