Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 20 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 23 | import java.util.Set; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 24 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkState; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 29 | * Base implementation of provider registry. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 30 | * |
| 31 | * @param <P> type of the information provider |
| 32 | * @param <S> type of the provider service |
| 33 | */ |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 34 | public abstract class AbstractProviderRegistry<P extends Provider, S extends ProviderService<P>> |
| 35 | implements ProviderRegistry<P, S> { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 36 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 37 | private final Map<ProviderId, P> providers = new HashMap<>(); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 38 | private final Map<ProviderId, S> services = new HashMap<>(); |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 39 | private final Map<String, P> providersByScheme = new HashMap<>(); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 40 | |
| 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 49 | /** |
| 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 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 58 | @Override |
| 59 | public synchronized S register(P provider) { |
| 60 | checkNotNull(provider, "Provider cannot be null"); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 61 | checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id()); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 62 | |
| 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 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 69 | S service = createProviderService(provider); |
| 70 | services.put(provider.id(), service); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 71 | providers.put(provider.id(), provider); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 72 | |
| 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 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 78 | return service; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public synchronized void unregister(P provider) { |
| 83 | checkNotNull(provider, "Provider cannot be null"); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 84 | S service = services.get(provider.id()); |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 85 | if (service != null && service instanceof AbstractProviderService) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 86 | ((AbstractProviderService) service).invalidate(); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 87 | services.remove(provider.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 88 | providers.remove(provider.id()); |
alshabib | 23022f7 | 2014-10-17 14:50:00 -0700 | [diff] [blame] | 89 | if (!provider.id().isAncillary()) { |
| 90 | providersByScheme.remove(provider.id().scheme()); |
| 91 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 92 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 93 | } |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public synchronized Set<ProviderId> getProviders() { |
| 97 | return ImmutableSet.copyOf(services.keySet()); |
| 98 | } |
| 99 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 100 | /** |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 101 | * 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. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 104 | * |
| 105 | * @param providerId provider identifier |
| 106 | * @return provider |
| 107 | */ |
| 108 | protected synchronized P getProvider(ProviderId providerId) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 109 | P provider = providers.get(providerId); |
| 110 | return provider != null ? provider : defaultProvider(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 111 | } |
| 112 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 113 | /** |
| 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 120 | P provider = providersByScheme.get(deviceId.uri().getScheme()); |
| 121 | return provider != null ? provider : defaultProvider(); |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 124 | /** |
| 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 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 134 | } |