Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
| 49 | @Override |
| 50 | public synchronized S register(P provider) { |
| 51 | checkNotNull(provider, "Provider cannot be null"); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 52 | checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id()); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 53 | |
| 54 | // If the provider is a primary one, check for a conflict. |
| 55 | ProviderId pid = provider.id(); |
| 56 | checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()), |
| 57 | "A primary provider with id %s is already registered", |
| 58 | providersByScheme.get(pid.scheme())); |
| 59 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 60 | S service = createProviderService(provider); |
| 61 | services.put(provider.id(), service); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 62 | providers.put(provider.id(), provider); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 63 | |
| 64 | // Register the provider by URI scheme only if it is not ancillary. |
| 65 | if (!pid.isAncillary()) { |
| 66 | providersByScheme.put(pid.scheme(), provider); |
| 67 | } |
| 68 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 69 | return service; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public synchronized void unregister(P provider) { |
| 74 | checkNotNull(provider, "Provider cannot be null"); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 75 | S service = services.get(provider.id()); |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 76 | if (service != null && service instanceof AbstractProviderService) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 77 | ((AbstractProviderService) service).invalidate(); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 78 | services.remove(provider.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 79 | providers.remove(provider.id()); |
alshabib | 23022f7 | 2014-10-17 14:50:00 -0700 | [diff] [blame] | 80 | if (!provider.id().isAncillary()) { |
| 81 | providersByScheme.remove(provider.id().scheme()); |
| 82 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 83 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 84 | } |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 85 | |
| 86 | @Override |
| 87 | public synchronized Set<ProviderId> getProviders() { |
| 88 | return ImmutableSet.copyOf(services.keySet()); |
| 89 | } |
| 90 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Returns the provider registered with the specified provider ID. |
| 93 | * |
| 94 | * @param providerId provider identifier |
| 95 | * @return provider |
| 96 | */ |
| 97 | protected synchronized P getProvider(ProviderId providerId) { |
| 98 | return providers.get(providerId); |
| 99 | } |
| 100 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Returns the provider for the specified device ID based on URI scheme. |
| 103 | * |
| 104 | * @param deviceId device identifier |
| 105 | * @return provider bound to the URI scheme |
| 106 | */ |
| 107 | protected synchronized P getProvider(DeviceId deviceId) { |
| 108 | return providersByScheme.get(deviceId.uri().getScheme()); |
| 109 | } |
| 110 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 111 | } |