yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.virtual.impl.provider; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | import org.onosproject.incubator.net.virtual.provider.VirtualProvider; |
| 24 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 25 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderService; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 30 | |
| 31 | import java.util.HashMap; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
| 35 | import java.util.stream.Collectors; |
| 36 | |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | import static com.google.common.base.Preconditions.checkState; |
| 39 | |
| 40 | /** |
| 41 | * Implementation of the virtual provider registry and providerService registry service. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | @Service |
| 45 | public class VirtualProviderManager |
| 46 | implements VirtualProviderRegistryService { |
| 47 | |
| 48 | private final Map<ProviderId, VirtualProvider> providers = new HashMap<>(); |
| 49 | private final Map<ProviderId, VirtualProviderService> servicesWithProvider = new HashMap<>(); |
| 50 | private final Map<String, VirtualProvider> providersByScheme = new HashMap<>(); |
| 51 | private final Map<NetworkId, Set<VirtualProviderService>> servicesByNetwork = new HashMap<>(); |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 52 | private static Logger log = LoggerFactory.getLogger(VirtualProviderManager.class); |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 53 | |
| 54 | @Override |
| 55 | public synchronized void registerProvider(VirtualProvider virtualProvider) { |
| 56 | checkNotNull(virtualProvider, "Provider cannot be null"); |
| 57 | checkState(!providers.containsKey(virtualProvider.id()), |
| 58 | "Provider %s already registered", virtualProvider.id()); |
| 59 | |
| 60 | // If the provider is a primary one, check for a conflict. |
| 61 | ProviderId pid = virtualProvider.id(); |
| 62 | checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()), |
| 63 | "A primary provider with id %s is already registered", |
| 64 | providersByScheme.get(pid.scheme())); |
| 65 | |
| 66 | providers.put(virtualProvider.id(), virtualProvider); |
| 67 | |
| 68 | // Register the provider by URI scheme only if it is not ancillary. |
| 69 | if (!pid.isAncillary()) { |
| 70 | providersByScheme.put(pid.scheme(), virtualProvider); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public synchronized void unregisterProvider(VirtualProvider virtualProvider) { |
| 76 | checkNotNull(virtualProvider, "Provider cannot be null"); |
| 77 | |
sangyun-han | 43c5670 | 2017-07-11 11:55:28 +0900 | [diff] [blame] | 78 | //TODO: invalidate provider services which subscribe the provider |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 79 | providers.remove(virtualProvider.id()); |
| 80 | |
| 81 | if (!virtualProvider.id().isAncillary()) { |
| 82 | providersByScheme.remove(virtualProvider.id().scheme()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public synchronized void |
| 88 | registerProviderService(NetworkId networkId, |
| 89 | VirtualProviderService virtualProviderService) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 90 | Set<VirtualProviderService> services = |
| 91 | servicesByNetwork.computeIfAbsent(networkId, k -> new HashSet<>()); |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 92 | |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 93 | services.add(virtualProviderService); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public synchronized void |
| 98 | unregisterProviderService(NetworkId networkId, |
| 99 | VirtualProviderService virtualProviderService) { |
| 100 | Set<VirtualProviderService> services = servicesByNetwork.get(networkId); |
| 101 | |
| 102 | if (services != null) { |
| 103 | services.remove(virtualProviderService); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public synchronized Set<ProviderId> getProviders() { |
| 109 | return ImmutableSet.copyOf(providers.keySet()); |
| 110 | } |
| 111 | |
| 112 | @Override |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 113 | public Set<ProviderId> getProvidersByService(VirtualProviderService |
| 114 | virtualProviderService) { |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 115 | Class clazz = getProviderClass(virtualProviderService); |
| 116 | |
| 117 | return ImmutableSet.copyOf(providers.values().stream() |
yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 118 | .filter(clazz::isInstance) |
| 119 | .map(VirtualProvider::id) |
| 120 | .collect(Collectors.toSet())); |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public synchronized VirtualProvider getProvider(ProviderId providerId) { |
| 125 | return providers.get(providerId); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public synchronized VirtualProvider getProvider(DeviceId deviceId) { |
| 130 | return providersByScheme.get(deviceId.uri().getScheme()); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public synchronized VirtualProvider getProvider(String scheme) { |
| 135 | return providersByScheme.get(scheme); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public synchronized VirtualProviderService |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 140 | getProviderService(NetworkId networkId, Class<? extends VirtualProvider> providerClass) { |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 141 | Set<VirtualProviderService> services = servicesByNetwork.get(networkId); |
| 142 | |
| 143 | if (services == null) { |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | return services.stream() |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 148 | .filter(s -> getProviderClass(s).equals(providerClass)) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 149 | .findFirst().orElse(null); |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the class type of parameter type. |
| 154 | * More specifically, it returns the class type of provider service's provider type. |
| 155 | * |
| 156 | * @param service a virtual provider service |
| 157 | * @return the class type of provider service of the service |
| 158 | */ |
| 159 | private Class getProviderClass(VirtualProviderService service) { |
| 160 | String className = service.getClass().getGenericSuperclass().getTypeName(); |
| 161 | String pramType = className.split("<")[1].split(">")[0]; |
| 162 | |
| 163 | try { |
| 164 | return Class.forName(pramType); |
| 165 | } catch (ClassNotFoundException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 166 | log.warn("getProviderClass()", e); |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return null; |
| 170 | } |
| 171 | } |