Fix for Bug 144, Bug 147, 148 - improved codec
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRouterCodegenInstance.xtend
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.spi.RpcRouter
5 import org.opendaylight.yangtools.yang.binding.BaseIdentity
6 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
7 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*
8 import java.util.Set
9 import java.util.HashMap
10 import org.opendaylight.controller.sal.binding.spi.RpcRoutingTable
11 import org.opendaylight.yangtools.yang.binding.DataContainer
12 import org.opendaylight.yangtools.yang.binding.RpcImplementation
13
14 class RpcRouterCodegenInstance<T extends RpcService> implements RpcRouter<T> {
15
16     @Property
17     val T invocationProxy
18
19     @Property
20     val RpcImplementation invokerDelegate;
21
22     @Property
23     val Class<T> serviceType
24
25     @Property
26     val Set<Class<? extends BaseIdentity>> contexts
27
28     @Property
29     val Set<Class<? extends DataContainer>> supportedInputs;
30
31     val routingTables = new HashMap<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, ? extends RpcService>>;
32
33     @Property
34     var T defaultService
35
36     new(Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
37         Set<Class<? extends DataContainer>> inputs) {
38         _serviceType = type
39         _invocationProxy = routerImpl
40         _invokerDelegate = routerImpl as RpcImplementation
41         _contexts = contexts
42         _supportedInputs = inputs;
43
44         for (ctx : contexts) {
45             val table = XtendHelper.createRoutingTable(ctx)
46             invocationProxy.setRoutingTable(ctx, table.routes);
47             routingTables.put(ctx, table);
48         }
49     }
50
51     override <C extends BaseIdentity> getRoutingTable(Class<C> table) {
52         routingTables.get(table) as RpcRoutingTable<C,T>
53     }
54
55     override getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
56         val table = getRoutingTable(context);
57         return table.getRoute(path);
58     }
59
60     override <T extends DataContainer> invoke(Class<T> type, T input) {
61         return invokerDelegate.invoke(type, input);
62     }
63
64 }