Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX bean.
[controller.git] / opendaylight / md-sal / sal-binding-broker / 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 (delegate == null || 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         /**
49      * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
50      * 
51      * Note: This method uses reflection, but setting delegate field should not occur too much
52      * to introduce any significant performance hits.
53      * 
54      */
55     public static def void setDelegate(Object proxy, Object delegate) {
56         val field = proxy.class.getField(DELEGATE_FIELD)
57         if (field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy");
58         if (delegate == null || field.type.isAssignableFrom(delegate.class)) {
59             field.set(proxy, delegate)
60         } else
61             throw new IllegalArgumentException("delegate class is not assignable to proxy");
62     }
63     
64
65     public static def Map<InstanceIdentifier<?>, ? extends RpcService> getRoutingTable(RpcService target,
66         Class<? extends BaseIdentity> tableClass) {
67         val field = target.class.getField(tableClass.routingTableField)
68         if (field == null) throw new UnsupportedOperationException(
69             "Unable to get routing table. Table field does not exists");
70         return field.get(target) as Map<InstanceIdentifier<? extends Object>, ? extends RpcService>;
71     }
72
73     public static def void setRoutingTable(RpcService target, Class<? extends BaseIdentity> tableClass,
74         Map<InstanceIdentifier<?>, ? extends RpcService> routingTable) {
75          val field = target.class.getField(tableClass.routingTableField)
76         if (field == null) throw new UnsupportedOperationException(
77             "Unable to set routing table. Table field does not exists");
78         field.set(target,routingTable);
79     }
80
81 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.