Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.provider; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 17 | |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 22 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 25 | import java.util.Set; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 26 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkState; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 31 | * Base implementation of provider registry. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 32 | * |
| 33 | * @param <P> type of the information provider |
| 34 | * @param <S> type of the provider service |
| 35 | */ |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 36 | public abstract class AbstractProviderRegistry<P extends Provider, S extends ProviderService<P>> |
| 37 | implements ProviderRegistry<P, S> { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 38 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 39 | private final Map<ProviderId, P> providers = new HashMap<>(); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 40 | private final Map<ProviderId, S> services = new HashMap<>(); |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 41 | private final Map<String, P> providersByScheme = new HashMap<>(); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Creates a new provider service bound to the specified provider. |
| 45 | * |
| 46 | * @param provider provider |
| 47 | * @return provider service |
| 48 | */ |
| 49 | protected abstract S createProviderService(P provider); |
| 50 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Returns the default fall-back provider. Default implementation return null. |
| 53 | * |
| 54 | * @return default provider |
| 55 | */ |
| 56 | protected P defaultProvider() { |
| 57 | return null; |
| 58 | } |
| 59 | |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 60 | private static final Logger log = LoggerFactory |
| 61 | .getLogger(AbstractProviderRegistry.class); |
| 62 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 63 | @Override |
| 64 | public synchronized S register(P provider) { |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 65 | |
| 66 | log.debug("registering provider {} :{}", provider, provider.id()); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 67 | checkNotNull(provider, "Provider cannot be null"); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 68 | |
| 69 | // If the provider is a primary one, check for a conflict. |
| 70 | ProviderId pid = provider.id(); |
| 71 | checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()), |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 72 | "A primary provider with id %s is already registered", |
| 73 | providersByScheme.get(pid.scheme())); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 74 | |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 75 | checkState(pid.isAncillary() || !services.containsKey(provider.id()), |
| 76 | "Provider %s already registered", provider.id()); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 77 | |
| 78 | // Register the provider by URI scheme only if it is not ancillary. |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 79 | S service = null; |
| 80 | |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 81 | if (!pid.isAncillary()) { |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 82 | service = createProviderService(provider); |
| 83 | services.put(provider.id(), service); |
| 84 | providers.put(provider.id(), provider); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 85 | providersByScheme.put(pid.scheme(), provider); |
Ray Milkey | 48ea5a0 | 2019-02-07 08:01:52 -0800 | [diff] [blame] | 86 | } else { |
| 87 | ProviderId servicePid = new ProviderId(provider.id().scheme(), provider.id().id(), false); |
| 88 | service = services.get(servicePid); |
| 89 | checkState(service != null, |
| 90 | "Primary provider with id %s not registered yet", pid); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 91 | } |
| 92 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 93 | return service; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public synchronized void unregister(P provider) { |
| 98 | checkNotNull(provider, "Provider cannot be null"); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 99 | S service = services.get(provider.id()); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 100 | if (service instanceof AbstractProviderService) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 101 | ((AbstractProviderService) service).invalidate(); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 102 | services.remove(provider.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 103 | providers.remove(provider.id()); |
alshabib | 23022f7 | 2014-10-17 14:50:00 -0700 | [diff] [blame] | 104 | if (!provider.id().isAncillary()) { |
| 105 | providersByScheme.remove(provider.id().scheme()); |
| 106 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 107 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 108 | } |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | public synchronized Set<ProviderId> getProviders() { |
| 112 | return ImmutableSet.copyOf(services.keySet()); |
| 113 | } |
| 114 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 115 | /** |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 116 | * Returns the provider registered with the specified provider ID or null |
| 117 | * if none is found for the given provider family and default fall-back is |
| 118 | * not supported. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 119 | * |
| 120 | * @param providerId provider identifier |
| 121 | * @return provider |
| 122 | */ |
| 123 | protected synchronized P getProvider(ProviderId providerId) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 124 | P provider = providers.get(providerId); |
| 125 | return provider != null ? provider : defaultProvider(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 126 | } |
| 127 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Returns the provider for the specified device ID based on URI scheme. |
| 130 | * |
| 131 | * @param deviceId device identifier |
| 132 | * @return provider bound to the URI scheme |
| 133 | */ |
| 134 | protected synchronized P getProvider(DeviceId deviceId) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 135 | P provider = providersByScheme.get(deviceId.uri().getScheme()); |
| 136 | return provider != null ? provider : defaultProvider(); |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 139 | /** |
| 140 | * Returns the provider registered with the specified scheme. |
| 141 | * |
| 142 | * @param scheme provider scheme |
| 143 | * @return provider |
| 144 | */ |
| 145 | protected synchronized P getProvider(String scheme) { |
| 146 | return providersByScheme.get(scheme); |
| 147 | } |
| 148 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 149 | } |