Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / RuntimeCodeHelper.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;
9
10 import com.google.common.base.Objects;
11 import java.lang.reflect.Field;
12 import java.util.Map;
13 import org.eclipse.xtext.xbase.lib.Exceptions;
14 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification;
15 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.binding.RpcService;
18
19 @SuppressWarnings("all")
20 public class RuntimeCodeHelper {
21   /**
22    * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
23    *
24    * Note: This method uses reflection, but access to delegate field should be
25    * avoided and called only if neccessary.
26    */
27   public static <T extends RpcService> T getDelegate(final RpcService proxy) {
28     try {
29       Class<? extends RpcService> _class = proxy.getClass();
30       final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD);
31       boolean _equals = Objects.equal(field, null);
32       if (_equals) {
33         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to get delegate from proxy");
34         throw _unsupportedOperationException;
35       }
36       try {
37         Object _get = field.get(proxy);
38         return ((T) _get);
39       } catch (Throwable _e) {
40         throw Exceptions.sneakyThrow(_e);
41       }
42     } catch (Throwable _e_1) {
43       throw Exceptions.sneakyThrow(_e_1);
44     }
45   }
46
47   /**
48    * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
49    *
50    * Note: This method uses reflection, but setting delegate field should not occur too much
51    * to introduce any significant performance hits.
52    */
53   public static void setDelegate(final RpcService proxy, final RpcService delegate) {
54     try {
55       Class<? extends RpcService> _class = proxy.getClass();
56       final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD);
57       boolean _equals = Objects.equal(field, null);
58       if (_equals) {
59         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to set delegate to proxy");
60         throw _unsupportedOperationException;
61       }
62       boolean _or = false;
63       boolean _equals_1 = Objects.equal(delegate, null);
64       if (_equals_1) {
65         _or = true;
66       } else {
67         Class<? extends Object> _type = field.getType();
68         Class<? extends RpcService> _class_1 = delegate.getClass();
69         boolean _isAssignableFrom = _type.isAssignableFrom(_class_1);
70         _or = (_equals_1 || _isAssignableFrom);
71       }
72       if (_or) {
73         field.set(proxy, delegate);
74       } else {
75         IllegalArgumentException _illegalArgumentException = new IllegalArgumentException("delegate class is not assignable to proxy");
76         throw _illegalArgumentException;
77       }
78     } catch (Throwable _e) {
79       throw Exceptions.sneakyThrow(_e);
80     }
81   }
82
83   /**
84    * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
85    *
86    * Note: This method uses reflection, but setting delegate field should not occur too much
87    * to introduce any significant performance hits.
88    */
89   public static void setDelegate(final Object proxy, final Object delegate) {
90     try {
91       Class<? extends Object> _class = proxy.getClass();
92       final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD);
93       boolean _equals = Objects.equal(field, null);
94       if (_equals) {
95         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to set delegate to proxy");
96         throw _unsupportedOperationException;
97       }
98       boolean _or = false;
99       boolean _equals_1 = Objects.equal(delegate, null);
100       if (_equals_1) {
101         _or = true;
102       } else {
103         Class<? extends Object> _type = field.getType();
104         Class<? extends Object> _class_1 = delegate.getClass();
105         boolean _isAssignableFrom = _type.isAssignableFrom(_class_1);
106         _or = (_equals_1 || _isAssignableFrom);
107       }
108       if (_or) {
109         field.set(proxy, delegate);
110       } else {
111         IllegalArgumentException _illegalArgumentException = new IllegalArgumentException("delegate class is not assignable to proxy");
112         throw _illegalArgumentException;
113       }
114     } catch (Throwable _e) {
115       throw Exceptions.sneakyThrow(_e);
116     }
117   }
118
119   public static Map<InstanceIdentifier<? extends Object>,? extends RpcService> getRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass) {
120     try {
121       Class<? extends RpcService> _class = target.getClass();
122       String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass);
123       final Field field = _class.getField(_routingTableField);
124       boolean _equals = Objects.equal(field, null);
125       if (_equals) {
126         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException(
127           "Unable to get routing table. Table field does not exists");
128         throw _unsupportedOperationException;
129       }
130       try {
131         Object _get = field.get(target);
132         return ((Map<InstanceIdentifier<? extends Object>,? extends RpcService>) _get);
133       } catch (Throwable _e) {
134         throw Exceptions.sneakyThrow(_e);
135       }
136     } catch (Throwable _e_1) {
137       throw Exceptions.sneakyThrow(_e_1);
138     }
139   }
140
141   public static void setRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass, final Map<InstanceIdentifier<? extends Object>,? extends RpcService> routingTable) {
142     try {
143       Class<? extends RpcService> _class = target.getClass();
144       String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass);
145       final Field field = _class.getField(_routingTableField);
146       boolean _equals = Objects.equal(field, null);
147       if (_equals) {
148         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException(
149           "Unable to set routing table. Table field does not exists");
150         throw _unsupportedOperationException;
151       }
152       field.set(target, routingTable);
153     } catch (Throwable _e) {
154       throw Exceptions.sneakyThrow(_e);
155     }
156   }
157 }