Merge "Fixed netconf monitoring."
[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     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);
61         }
62         this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
63     }
64
65     @Override
66     public Class<T> getServiceType() {
67         return serviceType;
68     }
69
70     @Override
71     public T getInvocationProxy() {
72         return invocationProxy;
73     }
74
75     @Override
76     @SuppressWarnings("unchecked")
77     public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
78         return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
79     }
80
81     @Override
82     public T getDefaultService() {
83         return defaultService;
84     }
85
86     @Override
87     public Set<Class<? extends BaseIdentity>> getContexts() {
88         return contexts;
89     }
90
91     @Override
92     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
93             L listener) {
94         return listeners.registerWithType(listener);
95     }
96
97     @Override
98     public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
99         for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
100             try {
101                 listener.getInstance().onRouteChange(change);
102             } catch (Exception e) {
103                 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
104             }
105         }
106     }
107
108     @Override
109     public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
110         return routingTables.get(context).getRoute(path);
111     }
112
113     @Override
114     public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
115         return new RoutedRpcRegistrationImpl(service);
116     }
117
118     @Override
119     public RpcRegistration<T> registerDefaultService(T service) {
120         // TODO Auto-generated method stub
121         return null;
122     }
123
124     private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
125
126         public RoutedRpcRegistrationImpl(T instance) {
127             super(instance);
128         }
129
130         @Override
131         public Class<T> getServiceType() {
132             return serviceType;
133         }
134
135         @Override
136         public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
137             routingTables.get(context).updateRoute(path, getInstance());
138         }
139
140         @Override
141         public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
142             routingTables.get(context).removeRoute(path, getInstance());
143
144         }
145
146         @Override
147         public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
148             registerPath(context, instance);
149         }
150
151         @Override
152         public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
153             unregisterPath(context, instance);
154         }
155
156         @Override
157         protected void removeRegistration() {
158
159         }
160     }
161 }