blob: bcf5fae07c858bf08c216de28443a9c0be91343c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'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
49 @Override
50 public synchronized S register(P provider) {
51 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070052 checkState(!services.containsKey(provider.id()), "Provider %s already registered", provider.id());
toma5368862014-10-01 12:35:48 -070053
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
tom64b7aac2014-08-26 00:18:21 -070060 S service = createProviderService(provider);
61 services.put(provider.id(), service);
tome5ec3fd2014-09-04 15:18:06 -070062 providers.put(provider.id(), provider);
toma5368862014-10-01 12:35:48 -070063
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
tom64b7aac2014-08-26 00:18:21 -070069 return service;
70 }
71
72 @Override
73 public synchronized void unregister(P provider) {
74 checkNotNull(provider, "Provider cannot be null");
tom132b58a2014-08-28 16:11:28 -070075 S service = services.get(provider.id());
tomb1260e42014-08-26 18:39:57 -070076 if (service != null && service instanceof AbstractProviderService) {
tom64b7aac2014-08-26 00:18:21 -070077 ((AbstractProviderService) service).invalidate();
tom132b58a2014-08-28 16:11:28 -070078 services.remove(provider.id());
tome5ec3fd2014-09-04 15:18:06 -070079 providers.remove(provider.id());
alshabib23022f72014-10-17 14:50:00 -070080 if (!provider.id().isAncillary()) {
81 providersByScheme.remove(provider.id().scheme());
82 }
tom64b7aac2014-08-26 00:18:21 -070083 }
tom64b7aac2014-08-26 00:18:21 -070084 }
tom132b58a2014-08-28 16:11:28 -070085
86 @Override
87 public synchronized Set<ProviderId> getProviders() {
88 return ImmutableSet.copyOf(services.keySet());
89 }
90
tome5ec3fd2014-09-04 15:18:06 -070091 /**
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
tom7e02cda2014-09-18 12:05:46 -0700101 /**
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
tom64b7aac2014-08-26 00:18:21 -0700111}