8b2db8b13cd09b175fc1269ebec693717b70aab5
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRouterCodegenInstance.java
1 package org.opendaylight.controller.sal.binding.codegen.impl;
2
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;
9
10 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*;
11
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.HashMap;
15
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;
27
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableSet;
30
31 public class RpcRouterCodegenInstance<T extends RpcService> implements //
32         RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
35
36     private T defaultService;
37
38     private final Class<T> serviceType;
39
40     private final T invocationProxy;
41
42     private final Set<Class<? extends BaseIdentity>> contexts;
43
44     private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
45
46     private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
47
48     private final String name;
49
50     @SuppressWarnings("unchecked")
51     public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
52             Set<Class<? extends DataContainer>> inputs) {
53         this.name = name;
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);
61             
62             @SuppressWarnings("rawtypes")
63             Map invokerView = table.getRoutes();
64             
65             setRoutingTable((RpcService) invocationProxy, ctx, invokerView);
66             mutableRoutingTables.put(ctx, table);
67             table.registerRouteChangeListener(this);
68         }
69         this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
70     }
71
72     @Override
73     public Class<T> getServiceType() {
74         return serviceType;
75     }
76
77     @Override
78     public T getInvocationProxy() {
79         return invocationProxy;
80     }
81
82     @Override
83     @SuppressWarnings("unchecked")
84     public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
85         return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
86     }
87
88     @Override
89     public T getDefaultService() {
90         return defaultService;
91     }
92
93     @Override
94     public Set<Class<? extends BaseIdentity>> getContexts() {
95         return contexts;
96     }
97
98     @Override
99     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
100             L listener) {
101         return listeners.registerWithType(listener);
102     }
103
104     @Override
105     public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
106         for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
107             try {
108                 listener.getInstance().onRouteChange(change);
109             } catch (Exception e) {
110                 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
111             }
112         }
113     }
114
115     @Override
116     public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
117         return routingTables.get(context).getRoute(path);
118     }
119
120     @Override
121     public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
122         return new RoutedRpcRegistrationImpl(service);
123     }
124
125     @Override
126     public RpcRegistration<T> registerDefaultService(T service) {
127         // TODO Auto-generated method stub
128         return null;
129     }
130
131     private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
132
133         public RoutedRpcRegistrationImpl(T instance) {
134             super(instance);
135         }
136
137         @Override
138         public Class<T> getServiceType() {
139             return serviceType;
140         }
141
142         @Override
143         public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
144             routingTables.get(context).updateRoute(path, getInstance());
145         }
146
147         @Override
148         public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
149             routingTables.get(context).removeRoute(path, getInstance());
150
151         }
152
153         @Override
154         public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
155             registerPath(context, instance);
156         }
157
158         @Override
159         public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
160             unregisterPath(context, instance);
161         }
162
163         @Override
164         protected void removeRegistration() {
165
166         }
167     }
168 }