1 package org.opendaylight.controller.sal.binding.codegen.impl;
3 import org.opendaylight.yangtools.yang.binding.RpcService;
4 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
5 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
6 import org.opendaylight.controller.sal.binding.spi.RpcRouter;
7 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
8 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
10 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*;
14 import java.util.HashMap;
16 import org.opendaylight.controller.sal.binding.spi.RpcRoutingTable;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 import org.opendaylight.yangtools.yang.binding.RpcImplementation;
19 import org.opendaylight.controller.md.sal.common.api.routing.MutableRoutingTable;
20 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
22 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableSet;
31 public class RpcRouterCodegenInstance<T extends RpcService> implements //
32 RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
34 private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
36 private T defaultService;
38 private final Class<T> serviceType;
40 private final T invocationProxy;
42 private final Set<Class<? extends BaseIdentity>> contexts;
44 private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
46 private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
48 public RpcRouterCodegenInstance(Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
49 Set<Class<? extends DataContainer>> inputs) {
50 this.listeners = ListenerRegistry.create();
51 this.serviceType = type;
52 this.invocationProxy = routerImpl;
53 this.contexts = ImmutableSet.copyOf(contexts);
54 Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
55 for (Class<? extends BaseIdentity> ctx : contexts) {
56 RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(ctx);
57 Map invokerView = table.getRoutes();
58 setRoutingTable((RpcService) invocationProxy, ctx, invokerView);
59 mutableRoutingTables.put(ctx, table);
60 table.registerRouteChangeListener(this);
62 this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
66 public Class<T> getServiceType() {
71 public T getInvocationProxy() {
72 return invocationProxy;
76 @SuppressWarnings("unchecked")
77 public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
78 return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
82 public T getDefaultService() {
83 return defaultService;
87 public Set<Class<? extends BaseIdentity>> getContexts() {
92 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
94 return listeners.registerWithType(listener);
98 public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
99 for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
101 listener.getInstance().onRouteChange(change);
102 } catch (Exception e) {
103 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
109 public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
110 return routingTables.get(context).getRoute(path);
114 public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
115 return new RoutedRpcRegistrationImpl(service);
119 public RpcRegistration<T> registerDefaultService(T service) {
120 // TODO Auto-generated method stub
124 private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
126 public RoutedRpcRegistrationImpl(T instance) {
131 public Class<T> getServiceType() {
136 public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
137 routingTables.get(context).updateRoute(path, getInstance());
141 public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
142 routingTables.get(context).removeRoute(path, getInstance());
147 public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
148 registerPath(context, instance);
152 public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
153 unregisterPath(context, instance);
157 protected void removeRegistration() {