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