6bdb3c8fbed555b188c17fd9789fb4d7af9a93c7
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-binding-broker-impl / src / main / java / org / opendaylight / controller / sal / binding / codegen / RuntimeCodeHelper.xtend
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
9
10 import java.util.Map
11
12 import org.opendaylight.yangtools.yang.binding.BaseIdentity
13 import org.opendaylight.yangtools.yang.binding.RpcService
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
15
16 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
17
18 class RuntimeCodeHelper {
19     /**
20      * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
21      * 
22      * Note: This method uses reflection, but access to delegate field should be 
23      * avoided and called only if neccessary.
24      * 
25      */
26     public static def <T extends RpcService> getDelegate(RpcService proxy) {
27         val field = proxy.class.getField(DELEGATE_FIELD)
28         if (field == null) throw new UnsupportedOperationException("Unable to get delegate from proxy");
29         return field.get(proxy) as T
30     }
31
32     /**
33      * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
34      * 
35      * Note: This method uses reflection, but setting delegate field should not occur too much
36      * to introduce any significant performance hits.
37      * 
38      */
39     public static def void setDelegate(RpcService proxy, RpcService delegate) {
40         val field = proxy.class.getField(DELEGATE_FIELD)
41         if (field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy");
42         if (field.type.isAssignableFrom(delegate.class)) {
43             field.set(proxy, delegate)
44         } else
45             throw new IllegalArgumentException("delegate class is not assignable to proxy");
46     }
47
48     public static def Map<InstanceIdentifier, ? extends RpcService> getRoutingTable(RpcService target,
49         Class<? extends BaseIdentity> tableClass) {
50         val field = target.class.getField(tableClass.routingTableField)
51         if (field == null) throw new UnsupportedOperationException(
52             "Unable to get routing table. Table field does not exists");
53         return field.get(target) as Map<InstanceIdentifier, ? extends RpcService>;
54     }
55
56     public static def void setRoutingTable(RpcService target, Class<? extends BaseIdentity> tableClass,
57         Map<InstanceIdentifier, ? extends RpcService> routingTable) {
58          val field = target.class.getField(tableClass.routingTableField)
59         if (field == null) throw new UnsupportedOperationException(
60             "Unable to set routing table. Table field does not exists");
61         field.set(target,routingTable);
62         
63     }
64
65 }