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 java.util.EventListener;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
17 import java.util.WeakHashMap;
19 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
20 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
22 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
27 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
28 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
29 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
30 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
31 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
34 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.RpcService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class RpcProviderRegistryImpl implements //
41 RpcProviderRegistry, //
42 RouteChangePublisher<RpcContextIdentifier, InstanceIdentifier<?>> {
44 private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL;
46 // publicProxies is a cache of proxy objects where each value in the map corresponds to a specific RpcService
47 private final Map<Class<? extends RpcService>, RpcService> publicProxies = new WeakHashMap<>();
48 private final Map<Class<? extends RpcService>, RpcRouter<?>> rpcRouters = new WeakHashMap<>();
49 private final ListenerRegistry<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> routeChangeListeners = ListenerRegistry
51 private final ListenerRegistry<RouterInstantiationListener> routerInstantiationListener = ListenerRegistry.create();
53 private final static Logger LOG = LoggerFactory.getLogger(RpcProviderRegistryImpl.class);
55 private final String name;
57 private final ListenerRegistry<GlobalRpcRegistrationListener> globalRpcListeners = ListenerRegistry.create();
59 public String getName() {
63 public RpcProviderRegistryImpl(final String name) {
69 public final <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
70 final T implementation) throws IllegalStateException {
71 return getRpcRouter(type).addRoutedRpcImplementation(implementation);
75 public final <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
76 throws IllegalStateException {
77 @SuppressWarnings("unchecked")
78 RpcRouter<T> potentialRouter = (RpcRouter<T>) rpcRouters.get(type);
79 if (potentialRouter != null) {
80 checkState(potentialRouter.getDefaultService() == null,
81 "Default service for routed RPC already registered.");
82 return potentialRouter.registerDefaultService(implementation);
84 T publicProxy = getRpcService(type);
85 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
86 checkState(currentDelegate == null, "Rpc service is already registered");
87 LOG.debug("Registering {} as global implementation of {} in {}", implementation, type.getSimpleName(), this);
88 RuntimeCodeHelper.setDelegate(publicProxy, implementation);
89 notifyGlobalRpcAdded(type);
90 return new RpcProxyRegistration<T>(type, implementation, this);
93 @SuppressWarnings("unchecked")
95 public final <T extends RpcService> T getRpcService(final Class<T> type) {
97 T potentialProxy = (T) publicProxies.get(type);
98 if (potentialProxy != null) {
99 return potentialProxy;
101 synchronized (this) {
103 * Potential proxy could be instantiated by other thread while we
104 * were waiting for the lock.
107 potentialProxy = (T) publicProxies.get(type);
108 if (potentialProxy != null) {
109 return potentialProxy;
111 T proxy = rpcFactory.getDirectProxyFor(type);
112 LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this);
113 publicProxies.put(type, proxy);
118 @SuppressWarnings({ "unchecked", "rawtypes" })
119 public <T extends RpcService> RpcRouter<T> getRpcRouter(final Class<T> type) {
120 RpcRouter<?> potentialRouter = rpcRouters.get(type);
121 if (potentialRouter != null) {
122 return (RpcRouter<T>) potentialRouter;
124 synchronized (this) {
126 * Potential Router could be instantiated by other thread while we
127 * were waiting for the lock.
129 potentialRouter = rpcRouters.get(type);
130 if (potentialRouter != null) {
131 return (RpcRouter<T>) potentialRouter;
133 RpcRouter<T> router = rpcFactory.getRouterFor(type, name);
134 router.registerRouteChangeListener(new RouteChangeForwarder(type));
135 LOG.debug("Registering router {} as global implementation of {} in {}", router, type.getSimpleName(), this);
136 RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
137 rpcRouters.put(type, router);
138 notifyListenersRoutedCreated(router);
143 private void notifyGlobalRpcAdded(final Class<? extends RpcService> type) {
144 for(ListenerRegistration<GlobalRpcRegistrationListener> listener : globalRpcListeners) {
146 listener.getInstance().onGlobalRpcRegistered(type);
147 } catch (Exception e) {
148 LOG.error("Unhandled exception during invoking listener {}", e);
154 private void notifyListenersRoutedCreated(final RpcRouter<?> router) {
156 for (ListenerRegistration<RouterInstantiationListener> listener : routerInstantiationListener) {
158 listener.getInstance().onRpcRouterCreated(router);
159 } catch (Exception e) {
160 LOG.error("Unhandled exception during invoking listener {}", e);
166 public ListenerRegistration<RouterInstantiationListener> registerRouterInstantiationListener(
167 final RouterInstantiationListener listener) {
168 ListenerRegistration<RouterInstantiationListener> reg = routerInstantiationListener.register(listener);
170 for (RpcRouter<?> router : rpcRouters.values()) {
171 listener.onRpcRouterCreated(router);
173 } catch (Exception e) {
174 LOG.error("Unhandled exception during invoking listener {}", e);
179 @SuppressWarnings("unchecked")
181 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
183 return (ListenerRegistration<L>) routeChangeListeners.register(listener);
186 public RuntimeCodeGenerator getRpcFactory() {
190 public void setRpcFactory(final RuntimeCodeGenerator rpcFactory) {
191 this.rpcFactory = rpcFactory;
194 public interface RouterInstantiationListener extends EventListener {
195 void onRpcRouterCreated(RpcRouter<?> router);
198 public ListenerRegistration<GlobalRpcRegistrationListener> registerGlobalRpcRegistrationListener(final GlobalRpcRegistrationListener listener) {
199 return globalRpcListeners.register(listener);
202 public interface GlobalRpcRegistrationListener extends EventListener {
203 void onGlobalRpcRegistered(Class<? extends RpcService> cls);
204 void onGlobalRpcUnregistered(Class<? extends RpcService> cls);
208 private class RouteChangeForwarder<T extends RpcService> implements
209 RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
211 private final Class<T> type;
213 public RouteChangeForwarder(final Class<T> type) {
218 public void onRouteChange(final RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
219 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> announcements = new HashMap<>();
220 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getAnnouncements()
222 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
223 announcements.put(key, entry.getValue());
225 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> removals = new HashMap<>();
226 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getRemovals()
228 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
229 removals.put(key, entry.getValue());
231 RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> toPublish = RoutingUtils
232 .<RpcContextIdentifier, InstanceIdentifier<?>> change(announcements, removals);
233 for (ListenerRegistration<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> listener : routeChangeListeners) {
235 listener.getInstance().onRouteChange(toPublish);
236 } catch (Exception e) {
243 public static class RpcProxyRegistration<T extends RpcService> extends AbstractObjectRegistration<T> implements
246 private final Class<T> serviceType;
247 private RpcProviderRegistryImpl registry;
249 public RpcProxyRegistration(final Class<T> type, final T service, final RpcProviderRegistryImpl registry) {
251 this.serviceType = type;
252 this.registry = registry;
256 public Class<T> getServiceType() {
261 protected void removeRegistration() {
262 if (registry != null) {
263 T publicProxy = registry.getRpcService(serviceType);
264 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
265 if (currentDelegate == getInstance()) {
266 RuntimeCodeHelper.setDelegate(publicProxy, null);