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 private final String name;
50 @SuppressWarnings("unchecked")
51 public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
52 Set<Class<? extends DataContainer>> inputs) {
54 this.listeners = ListenerRegistry.create();
55 this.serviceType = type;
56 this.invocationProxy = routerImpl;
57 this.contexts = ImmutableSet.copyOf(contexts);
58 Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
59 for (Class<? extends BaseIdentity> ctx : contexts) {
60 RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
62 @SuppressWarnings("rawtypes")
63 Map invokerView = table.getRoutes();
65 setRoutingTable((RpcService) invocationProxy, ctx, invokerView);
66 mutableRoutingTables.put(ctx, table);
67 table.registerRouteChangeListener(this);
69 this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
73 public Class<T> getServiceType() {
78 public T getInvocationProxy() {
79 return invocationProxy;
83 @SuppressWarnings("unchecked")
84 public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
85 return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
89 public T getDefaultService() {
90 return defaultService;
94 public Set<Class<? extends BaseIdentity>> getContexts() {
99 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
101 return listeners.registerWithType(listener);
105 public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
106 for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
108 listener.getInstance().onRouteChange(change);
109 } catch (Exception e) {
110 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
116 public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
117 return routingTables.get(context).getRoute(path);
121 public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
122 return new RoutedRpcRegistrationImpl(service);
126 public RpcRegistration<T> registerDefaultService(T service) {
127 // TODO Auto-generated method stub
131 private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
133 public RoutedRpcRegistrationImpl(T instance) {
138 public Class<T> getServiceType() {
143 public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
144 routingTables.get(context).updateRoute(path, getInstance());
148 public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
149 routingTables.get(context).removeRoute(path, getInstance());
154 public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
155 registerPath(context, instance);
159 public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
160 unregisterPath(context, instance);
164 protected void removeRegistration() {