bc862886d7abc548efc680d8f029c89a6d98f8d6
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / RpcProviderRegistryImpl.java
1 package org.opendaylight.controller.sal.binding.impl;
2
3 import java.util.Map;
4 import java.util.Map.Entry;
5 import java.util.HashMap;
6 import java.util.Set;
7 import java.util.WeakHashMap;
8
9 import javax.swing.tree.ExpandVetoException;
10
11 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
12 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
14 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
18 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
19 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
20 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
21 import org.opendaylight.controller.sal.binding.spi.RpcContextIdentifier;
22 import org.opendaylight.controller.sal.binding.spi.RpcRouter;
23 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
27 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30
31 import static com.google.common.base.Preconditions.*;
32
33 public class RpcProviderRegistryImpl implements //
34         RpcProviderRegistry, //
35         RouteChangePublisher<RpcContextIdentifier, InstanceIdentifier<?>> {
36
37     private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL;
38
39     private final Map<Class<? extends RpcService>, RpcService> publicProxies = new WeakHashMap<>();
40     private final Map<Class<? extends RpcService>, RpcRouter<?>> rpcRouters = new WeakHashMap<>();
41     private final ListenerRegistry<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> routeChangeListeners = ListenerRegistry
42             .create();
43
44     @Override
45     public final <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
46             T implementation) throws IllegalStateException {
47         return getRpcRouter(type).addRoutedRpcImplementation(implementation);
48     }
49
50     @Override
51     public final <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
52             throws IllegalStateException {
53         RpcRouter<T> potentialRouter = (RpcRouter<T>) rpcRouters.get(type);
54         if (potentialRouter != null) {
55             checkState(potentialRouter.getDefaultService() == null,
56                     "Default service for routed RPC already registered.");
57             return potentialRouter.registerDefaultService(implementation);
58         }
59         T publicProxy = getRpcService(type);
60         RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
61         checkState(currentDelegate == null, "Rpc service is already registered");
62         RuntimeCodeHelper.setDelegate(publicProxy, implementation);
63         return new RpcProxyRegistration<T>(type, implementation, this);
64     }
65
66     @Override
67     public final <T extends RpcService> T getRpcService(Class<T> type) {
68
69         RpcService potentialProxy = publicProxies.get(type);
70         if (potentialProxy != null) {
71             return (T) potentialProxy;
72         }
73         T proxy = rpcFactory.getDirectProxyFor(type);
74         publicProxies.put(type, proxy);
75         return proxy;
76     }
77
78     private <T extends RpcService> RpcRouter<T> getRpcRouter(Class<T> type) {
79         RpcRouter<?> potentialRouter = rpcRouters.get(type);
80         if (potentialRouter != null) {
81             return (RpcRouter<T>) potentialRouter;
82         }
83         RpcRouter<T> router = rpcFactory.getRouterFor(type);
84         router.registerRouteChangeListener(new RouteChangeForwarder(type));
85         RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
86         rpcRouters.put(type, router);
87         return router;
88     }
89
90     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
91             L listener) {
92         return (ListenerRegistration<L>) routeChangeListeners.register(listener);
93     }
94
95     public RuntimeCodeGenerator getRpcFactory() {
96         return rpcFactory;
97     }
98
99     public void setRpcFactory(RuntimeCodeGenerator rpcFactory) {
100         this.rpcFactory = rpcFactory;
101     }
102
103     private class RouteChangeForwarder<T extends RpcService> implements
104             RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
105
106         private final Class<T> type;
107
108         public RouteChangeForwarder(Class<T> type) {
109             this.type = type;
110         }
111
112         @Override
113         public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
114             Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> announcements = new HashMap<>();
115             for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getAnnouncements()
116                     .entrySet()) {
117                 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
118                 announcements.put(key, entry.getValue());
119             }
120             Map<RpcContextIdentifier, Set<InstanceIdentifier<?>>> removals = new HashMap<>();
121             for (Entry<Class<? extends BaseIdentity>, Set<InstanceIdentifier<?>>> entry : change.getRemovals()
122                     .entrySet()) {
123                 RpcContextIdentifier key = RpcContextIdentifier.contextFor(type, entry.getKey());
124                 removals.put(key, entry.getValue());
125             }
126             RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> toPublish = RoutingUtils
127                     .<RpcContextIdentifier, InstanceIdentifier<?>> change(announcements, removals);
128             for (ListenerRegistration<RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> listener : routeChangeListeners) {
129                 try {
130                     listener.getInstance().onRouteChange(toPublish);
131                 } catch (Exception e) {
132                     e.printStackTrace();
133                 }
134             }
135         }
136     }
137
138     public static class RpcProxyRegistration<T extends RpcService> extends AbstractObjectRegistration<T> implements
139             RpcRegistration<T> {
140
141         private final Class<T> serviceType;
142         private RpcProviderRegistryImpl registry;
143
144         public RpcProxyRegistration(Class<T> type, T service, RpcProviderRegistryImpl registry) {
145             super(service);
146             serviceType = type;
147         }
148
149         @Override
150         public Class<T> getServiceType() {
151             return serviceType;
152         }
153
154         @Override
155         protected void removeRegistration() {
156             if (registry != null) {
157                 T publicProxy = registry.getRpcService(serviceType);
158                 RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy);
159                 if (currentDelegate == getInstance()) {
160                     RuntimeCodeHelper.setDelegate(publicProxy, null);
161                 }
162                 registry = null;
163             }
164         }
165     }
166 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.