2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.impl;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
16 import java.util.EventListener;
17 import java.util.HashMap;
19 import java.util.Map.Entry;
21 import java.util.WeakHashMap;
23 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
24 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
25 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
26 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
31 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
32 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
33 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
34 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
35 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
38 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.RpcService;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class RpcProviderRegistryImpl implements RpcProviderRegistry, RouteChangePublisher<RpcContextIdentifier, InstanceIdentifier<?>> {
46 private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL;
48 // cache of proxy objects where each value in the map corresponds to a specific RpcService
49 private final LoadingCache<Class<? extends RpcService>, RpcService> publicProxies = CacheBuilder.newBuilder().weakKeys().
50 build(new CacheLoader<Class<? extends RpcService>, RpcService>() {
52 public RpcService load(final Class<? extends RpcService> type) {
53 final RpcService proxy = rpcFactory.getDirectProxyFor(type);
54 LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this);
59 private final Map<Class<? extends RpcService>, RpcRouter<?>> rpcRouters = new WeakHashMap<>();
60 private final ListenerRegistry<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> routeChangeListeners = ListenerRegistry
62 private final ListenerRegistry<RouterInstantiationListener> routerInstantiationListener = ListenerRegistry.create();
64 private final static Logger LOG = LoggerFactory.getLogger(RpcProviderRegistryImpl.class);
66 private final String name;
68 private final ListenerRegistry<GlobalRpcRegistrationListener> globalRpcListeners = ListenerRegistry.create();
70 public String getName() {
74 public RpcProviderRegistryImpl(final String name) {
80 public final <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
81 final T implementation) throws IllegalStateException {
82 return getRpcRouter(type).addRoutedRpcImplementation(implementation);
86 public final <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
87 throws IllegalStateException {
88 @SuppressWarnings("unchecked")
89 RpcRouter<T> potentialRouter = (RpcRouter<T>) rpcRouters.get(type);
90 if (potentialRouter != null) {
91 checkState(potentialRouter.getDefaultService() == null,
92 "Default service for routed RPC already registered.");
93 return potentialRouter.registerDefaultService(implementation);
95 T publicProxy = getRpcService(type);
96 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
97 checkState(currentDelegate == null, "Rpc service is already registered");
98 LOG.debug("Registering {} as global implementation of {} in {}", implementation, type.getSimpleName(), this);
99 RuntimeCodeHelper.setDelegate(publicProxy, implementation);
100 notifyGlobalRpcAdded(type);
101 return new RpcProxyRegistration<T>(type, implementation, this);
104 @SuppressWarnings("unchecked")
106 public final <T extends RpcService> T getRpcService(final Class<T> type) {
107 return (T) publicProxies.getUnchecked(type);
110 @SuppressWarnings({ "unchecked", "rawtypes" })
111 public <T extends RpcService> RpcRouter<T> getRpcRouter(final Class<T> type) {
112 RpcRouter<?> potentialRouter = rpcRouters.get(type);
113 if (potentialRouter != null) {
114 return (RpcRouter<T>) potentialRouter;
116 synchronized (this) {
118 * Potential Router could be instantiated by other thread while we
119 * were waiting for the lock.
121 potentialRouter = rpcRouters.get(type);
122 if (potentialRouter != null) {
123 return (RpcRouter<T>) potentialRouter;
125 RpcRouter<T> router = rpcFactory.getRouterFor(type, name);
126 router.registerRouteChangeListener(new RouteChangeForwarder(type));
127 LOG.debug("Registering router {} as global implementation of {} in {}", router, type.getSimpleName(), this);
128 RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
129 rpcRouters.put(type, router);
130 notifyListenersRoutedCreated(router);
135 private void notifyGlobalRpcAdded(final Class<? extends RpcService> type) {
136 for(ListenerRegistration<GlobalRpcRegistrationListener> listener : globalRpcListeners) {
138 listener.getInstance().onGlobalRpcRegistered(type);
139 } catch (Exception e) {
140 LOG.error("Unhandled exception during invoking listener {}", e);
146 private void notifyListenersRoutedCreated(final RpcRouter<?> router) {
148 for (ListenerRegistration<RouterInstantiationListener> listener : routerInstantiationListener) {
150 listener.getInstance().onRpcRouterCreated(router);
151 } catch (Exception e) {
152 LOG.error("Unhandled exception during invoking listener {}", e);
158 public ListenerRegistration<RouterInstantiationListener> registerRouterInstantiationListener(
159 final RouterInstantiationListener listener) {
160 ListenerRegistration<RouterInstantiationListener> reg = routerInstantiationListener.register(listener);
162 for (RpcRouter<?> router : rpcRouters.values()) {
163 listener.onRpcRouterCreated(router);
165 } catch (Exception e) {
166 LOG.error("Unhandled exception during invoking listener {}", e);
171 @SuppressWarnings("unchecked")
173 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
175 return (ListenerRegistration<L>) routeChangeListeners.register(listener);
178 public RuntimeCodeGenerator getRpcFactory() {
182 public void setRpcFactory(final RuntimeCodeGenerator rpcFactory) {
183 this.rpcFactory = rpcFactory;
186 public interface RouterInstantiationListener extends EventListener {
187 void onRpcRouterCreated(RpcRouter<?> router);
190 public ListenerRegistration<GlobalRpcRegistrationListener> registerGlobalRpcRegistrationListener(final GlobalRpcRegistrationListener listener) {
191 return globalRpcListeners.register(listener);
194 public interface GlobalRpcRegistrationListener extends EventListener {
195 void onGlobalRpcRegistered(Class<? extends RpcService> cls);
196 void onGlobalRpcUnregistered(Class<? extends RpcService> cls);
200 private class RouteChangeForwarder<T extends RpcService> implements RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
202 private final Class<T> type;
204 public RouteChangeForwarder(final Class<T> type) {
209 public void onRouteChange(final RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
210 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> announcements = new HashMap<>();
211 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getAnnouncements()
213 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
214 announcements.put(key, entry.getValue());
216 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> removals = new HashMap<>();
217 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getRemovals()
219 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
220 removals.put(key, entry.getValue());
222 RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> toPublish = RoutingUtils
223 .<RpcContextIdentifier, InstanceIdentifier<?>> change(announcements, removals);
224 for (ListenerRegistration<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> listener : routeChangeListeners) {
226 listener.getInstance().onRouteChange(toPublish);
227 } catch (Exception e) {
234 public static class RpcProxyRegistration<T extends RpcService> extends AbstractObjectRegistration<T> implements RpcRegistration<T> {
236 private final Class<T> serviceType;
237 private RpcProviderRegistryImpl registry;
239 public RpcProxyRegistration(final Class<T> type, final T service, final RpcProviderRegistryImpl registry) {
241 this.serviceType = type;
242 this.registry = registry;
246 public Class<T> getServiceType() {
251 protected void removeRegistration() {
252 if (registry != null) {
253 T publicProxy = registry.getRpcService(serviceType);
254 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
255 if (currentDelegate == getInstance()) {
256 RuntimeCodeHelper.setDelegate(publicProxy, null);