2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.codegen
12 import org.opendaylight.yangtools.yang.binding.BaseIdentity
13 import org.opendaylight.yangtools.yang.binding.RpcService
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
16 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
18 class RuntimeCodeHelper {
20 * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
22 * Note: This method uses reflection, but access to delegate field should be
23 * avoided and called only if neccessary.
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
33 * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
35 * Note: This method uses reflection, but setting delegate field should not occur too much
36 * to introduce any significant performance hits.
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)
45 throw new IllegalArgumentException("delegate class is not assignable to proxy");
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>;
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);