Merge "Fix warnings reported in toaster"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRouterCodegenInstance.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.setRoutingTable;
11
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
17 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
21 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
22 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
23 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
26 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.ImmutableSet;
35
36 public class RpcRouterCodegenInstance<T extends RpcService> implements //
37         RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
38
39     private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
40
41     private T defaultService;
42
43     private final Class<T> serviceType;
44
45     private final T invocationProxy;
46
47     private final Set<Class<? extends BaseIdentity>> contexts;
48
49     private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
50
51     private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
52
53     private final String name;
54
55     @SuppressWarnings("unchecked")
56     public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
57             Set<Class<? extends DataContainer>> inputs) {
58         this.name = name;
59         this.listeners = ListenerRegistry.create();
60         this.serviceType = type;
61         this.invocationProxy = routerImpl;
62         this.contexts = ImmutableSet.copyOf(contexts);
63         Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
64         for (Class<? extends BaseIdentity> ctx : contexts) {
65             RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
66
67             @SuppressWarnings("rawtypes")
68             Map invokerView = table.getRoutes();
69
70             setRoutingTable(invocationProxy, ctx, invokerView);
71             mutableRoutingTables.put(ctx, table);
72             table.registerRouteChangeListener(this);
73         }
74         this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
75     }
76
77     @Override
78     public Class<T> getServiceType() {
79         return serviceType;
80     }
81
82     @Override
83     public T getInvocationProxy() {
84         return invocationProxy;
85     }
86
87     @Override
88     @SuppressWarnings("unchecked")
89     public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
90         return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
91     }
92
93     @Override
94     public T getDefaultService() {
95         return defaultService;
96     }
97
98     @Override
99     public Set<Class<? extends BaseIdentity>> getContexts() {
100         return contexts;
101     }
102
103     @Override
104     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
105             L listener) {
106         return listeners.registerWithType(listener);
107     }
108
109     @Override
110     public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
111         for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
112             try {
113                 listener.getInstance().onRouteChange(change);
114             } catch (Exception e) {
115                 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
116             }
117         }
118     }
119
120     @Override
121     public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
122         return routingTables.get(context).getRoute(path);
123     }
124
125     @Override
126     public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
127         return new RoutedRpcRegistrationImpl(service);
128     }
129
130     @Override
131     public RpcRegistration<T> registerDefaultService(T service) {
132         // TODO Auto-generated method stub
133         RuntimeCodeHelper.setDelegate(invocationProxy, service);
134         return null;
135     }
136
137     private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
138
139         public RoutedRpcRegistrationImpl(T instance) {
140             super(instance);
141         }
142
143         @Override
144         public Class<T> getServiceType() {
145             return serviceType;
146         }
147
148         @Override
149         public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
150             routingTables.get(context).updateRoute(path, getInstance());
151         }
152
153         @Override
154         public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
155             routingTables.get(context).removeRoute(path, getInstance());
156         }
157
158         @Override
159         public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
160             registerPath(context, instance);
161         }
162
163         @Override
164         public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
165             unregisterPath(context, instance);
166         }
167
168         @Override
169         protected void removeRegistration() {
170
171         }
172     }
173 }