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 java.util.EventListener;
12 import java.util.Map.Entry;
13 import java.util.HashMap;
15 import java.util.WeakHashMap;
17 import javax.swing.tree.ExpandVetoException;
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.RpcProviderRegistry;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
26 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
27 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
28 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
29 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
30 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
31 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
32 import org.opendaylight.yangtools.concepts.Identifiable;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
35 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.RpcService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import static com.google.common.base.Preconditions.*;
43 public class RpcProviderRegistryImpl implements //
44 RpcProviderRegistry, //
45 RouteChangePublisher<RpcContextIdentifier, InstanceIdentifier<?>> {
47 private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL;
49 private final Map<Class<? extends RpcService>, RpcService> publicProxies = new WeakHashMap<>();
50 private final Map<Class<? extends RpcService>, RpcRouter<?>> rpcRouters = new WeakHashMap<>();
51 private final ListenerRegistry<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> routeChangeListeners = ListenerRegistry
53 private final ListenerRegistry<RouterInstantiationListener> routerInstantiationListener = ListenerRegistry.create();
55 private final static Logger LOG = LoggerFactory.getLogger(RpcProviderRegistryImpl.class);
57 private final String name;
59 private ListenerRegistry<GlobalRpcRegistrationListener> globalRpcListeners = ListenerRegistry.create();
61 public String getName() {
65 public RpcProviderRegistryImpl(String name) {
71 public final <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
72 T implementation) throws IllegalStateException {
73 return getRpcRouter(type).addRoutedRpcImplementation(implementation);
77 public final <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
78 throws IllegalStateException {
79 @SuppressWarnings("unchecked")
80 RpcRouter<T> potentialRouter = (RpcRouter<T>) rpcRouters.get(type);
81 if (potentialRouter != null) {
82 checkState(potentialRouter.getDefaultService() == null,
83 "Default service for routed RPC already registered.");
84 return potentialRouter.registerDefaultService(implementation);
86 T publicProxy = getRpcService(type);
87 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
88 checkState(currentDelegate == null, "Rpc service is already registered");
89 LOG.debug("Registering {} as global implementation of {} in {}", implementation, type.getSimpleName(), this);
90 RuntimeCodeHelper.setDelegate(publicProxy, implementation);
91 notifyGlobalRpcAdded(type);
92 return new RpcProxyRegistration<T>(type, implementation, this);
95 @SuppressWarnings("unchecked")
97 public final <T extends RpcService> T getRpcService(Class<T> type) {
99 T potentialProxy = (T) publicProxies.get(type);
100 if (potentialProxy != null) {
101 return potentialProxy;
103 synchronized (this) {
105 * Potential proxy could be instantiated by other thread while we
106 * were waiting for the lock.
109 potentialProxy = (T) publicProxies.get(type);
110 if (potentialProxy != null) {
111 return (T) potentialProxy;
113 T proxy = rpcFactory.getDirectProxyFor(type);
114 LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this);
115 publicProxies.put(type, proxy);
120 @SuppressWarnings("unchecked")
121 public <T extends RpcService> RpcRouter<T> getRpcRouter(Class<T> type) {
122 RpcRouter<?> potentialRouter = rpcRouters.get(type);
123 if (potentialRouter != null) {
124 return (RpcRouter<T>) potentialRouter;
126 synchronized (this) {
128 * Potential Router could be instantiated by other thread while we
129 * were waiting for the lock.
131 potentialRouter = rpcRouters.get(type);
132 if (potentialRouter != null) {
133 return (RpcRouter<T>) potentialRouter;
135 RpcRouter<T> router = rpcFactory.getRouterFor(type, name);
136 router.registerRouteChangeListener(new RouteChangeForwarder(type));
137 LOG.debug("Registering router {} as global implementation of {} in {}", router, type.getSimpleName(), this);
138 RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
139 rpcRouters.put(type, router);
140 notifyListenersRoutedCreated(router);
145 private void notifyGlobalRpcAdded(Class<? extends RpcService> type) {
146 for(ListenerRegistration<GlobalRpcRegistrationListener> listener : globalRpcListeners) {
148 listener.getInstance().onGlobalRpcRegistered(type);
149 } catch (Exception e) {
150 LOG.error("Unhandled exception during invoking listener {}", e);
156 private void notifyListenersRoutedCreated(RpcRouter<?> router) {
158 for (ListenerRegistration<RouterInstantiationListener> listener : routerInstantiationListener) {
160 listener.getInstance().onRpcRouterCreated(router);
161 } catch (Exception e) {
162 LOG.error("Unhandled exception during invoking listener {}", e);
168 public ListenerRegistration<RouterInstantiationListener> registerRouterInstantiationListener(
169 RouterInstantiationListener listener) {
170 ListenerRegistration<RouterInstantiationListener> reg = routerInstantiationListener.register(listener);
172 for (RpcRouter<?> router : rpcRouters.values()) {
173 listener.onRpcRouterCreated(router);
175 } catch (Exception e) {
176 LOG.error("Unhandled exception during invoking listener {}", e);
182 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
184 return (ListenerRegistration<L>) routeChangeListeners.register(listener);
187 public RuntimeCodeGenerator getRpcFactory() {
191 public void setRpcFactory(RuntimeCodeGenerator rpcFactory) {
192 this.rpcFactory = rpcFactory;
195 public interface RouterInstantiationListener extends EventListener {
196 void onRpcRouterCreated(RpcRouter<?> router);
199 public ListenerRegistration<GlobalRpcRegistrationListener> registerGlobalRpcRegistrationListener(GlobalRpcRegistrationListener listener) {
200 return globalRpcListeners.register(listener);
203 public interface GlobalRpcRegistrationListener extends EventListener {
204 void onGlobalRpcRegistered(Class<? extends RpcService> cls);
205 void onGlobalRpcUnregistered(Class<? extends RpcService> cls);
209 private class RouteChangeForwarder<T extends RpcService> implements
210 RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
212 private final Class<T> type;
214 public RouteChangeForwarder(Class<T> type) {
219 public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
220 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> announcements = new HashMap<>();
221 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getAnnouncements()
223 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
224 announcements.put(key, entry.getValue());
226 Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> removals = new HashMap<>();
227 for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getRemovals()
229 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
230 removals.put(key, entry.getValue());
232 RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> toPublish = RoutingUtils
233 .<RpcContextIdentifier, InstanceIdentifier<?>> change(announcements, removals);
234 for (ListenerRegistration<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> listener : routeChangeListeners) {
236 listener.getInstance().onRouteChange(toPublish);
237 } catch (Exception e) {
244 public static class RpcProxyRegistration<T extends RpcService> extends AbstractObjectRegistration<T> implements
247 private final Class<T> serviceType;
248 private RpcProviderRegistryImpl registry;
250 public RpcProxyRegistration(Class<T> type, T service, RpcProviderRegistryImpl 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);