From: Raghurama Bhat Date: Tue, 8 Apr 2014 20:48:25 +0000 (-0700) Subject: Bug 591: Minimize the use of Xtend in sal-binding-broker-impl X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~231 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=f21eb777e0e23586aaa3880028b556a48dfd4823;ds=sidebyside Bug 591: Minimize the use of Xtend in sal-binding-broker-impl Change-Id: I3b21ccdf42685c7b10bf66fadf99ae82692149cf Signed-off-by: Raghurama Bhat --- diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java new file mode 100644 index 0000000000..ea8b6c0972 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.codegen; + +import com.google.common.base.Objects; +import java.lang.reflect.Field; +import java.util.Map; +import org.eclipse.xtext.xbase.lib.Exceptions; +import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification; +import org.opendaylight.yangtools.yang.binding.BaseIdentity; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.RpcService; + +@SuppressWarnings("all") +public class RuntimeCodeHelper { + /** + * Helper method to return delegate from ManagedDirectedProxy with use of reflection. + * + * Note: This method uses reflection, but access to delegate field should be + * avoided and called only if neccessary. + */ + public static T getDelegate(final RpcService proxy) { + try { + Class _class = proxy.getClass(); + final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD); + boolean _equals = Objects.equal(field, null); + if (_equals) { + UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to get delegate from proxy"); + throw _unsupportedOperationException; + } + try { + Object _get = field.get(proxy); + return ((T) _get); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } catch (Throwable _e_1) { + throw Exceptions.sneakyThrow(_e_1); + } + } + + /** + * Helper method to set delegate to ManagedDirectedProxy with use of reflection. + * + * Note: This method uses reflection, but setting delegate field should not occur too much + * to introduce any significant performance hits. + */ + public static void setDelegate(final RpcService proxy, final RpcService delegate) { + try { + Class _class = proxy.getClass(); + final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD); + boolean _equals = Objects.equal(field, null); + if (_equals) { + UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to set delegate to proxy"); + throw _unsupportedOperationException; + } + boolean _or = false; + boolean _equals_1 = Objects.equal(delegate, null); + if (_equals_1) { + _or = true; + } else { + Class _type = field.getType(); + Class _class_1 = delegate.getClass(); + boolean _isAssignableFrom = _type.isAssignableFrom(_class_1); + _or = (_equals_1 || _isAssignableFrom); + } + if (_or) { + field.set(proxy, delegate); + } else { + IllegalArgumentException _illegalArgumentException = new IllegalArgumentException("delegate class is not assignable to proxy"); + throw _illegalArgumentException; + } + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + /** + * Helper method to set delegate to ManagedDirectedProxy with use of reflection. + * + * Note: This method uses reflection, but setting delegate field should not occur too much + * to introduce any significant performance hits. + */ + public static void setDelegate(final Object proxy, final Object delegate) { + try { + Class _class = proxy.getClass(); + final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD); + boolean _equals = Objects.equal(field, null); + if (_equals) { + UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to set delegate to proxy"); + throw _unsupportedOperationException; + } + boolean _or = false; + boolean _equals_1 = Objects.equal(delegate, null); + if (_equals_1) { + _or = true; + } else { + Class _type = field.getType(); + Class _class_1 = delegate.getClass(); + boolean _isAssignableFrom = _type.isAssignableFrom(_class_1); + _or = (_equals_1 || _isAssignableFrom); + } + if (_or) { + field.set(proxy, delegate); + } else { + IllegalArgumentException _illegalArgumentException = new IllegalArgumentException("delegate class is not assignable to proxy"); + throw _illegalArgumentException; + } + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + public static Map,? extends RpcService> getRoutingTable(final RpcService target, final Class tableClass) { + try { + Class _class = target.getClass(); + String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass); + final Field field = _class.getField(_routingTableField); + boolean _equals = Objects.equal(field, null); + if (_equals) { + UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException( + "Unable to get routing table. Table field does not exists"); + throw _unsupportedOperationException; + } + try { + Object _get = field.get(target); + return ((Map,? extends RpcService>) _get); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } catch (Throwable _e_1) { + throw Exceptions.sneakyThrow(_e_1); + } + } + + public static void setRoutingTable(final RpcService target, final Class tableClass, final Map,? extends RpcService> routingTable) { + try { + Class _class = target.getClass(); + String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass); + final Field field = _class.getField(_routingTableField); + boolean _equals = Objects.equal(field, null); + if (_equals) { + UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException( + "Unable to set routing table. Table field does not exists"); + throw _unsupportedOperationException; + } + field.set(target, routingTable); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.xtend deleted file mode 100644 index dff0d215b2..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.xtend +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.codegen - -import java.util.Map - -import org.opendaylight.yangtools.yang.binding.BaseIdentity -import org.opendaylight.yangtools.yang.binding.RpcService -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier - -import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.* - -class RuntimeCodeHelper { - /** - * Helper method to return delegate from ManagedDirectedProxy with use of reflection. - * - * Note: This method uses reflection, but access to delegate field should be - * avoided and called only if neccessary. - * - */ - public static def getDelegate(RpcService proxy) { - val field = proxy.class.getField(DELEGATE_FIELD) - if (field == null) throw new UnsupportedOperationException("Unable to get delegate from proxy"); - return field.get(proxy) as T - } - - /** - * Helper method to set delegate to ManagedDirectedProxy with use of reflection. - * - * Note: This method uses reflection, but setting delegate field should not occur too much - * to introduce any significant performance hits. - * - */ - public static def void setDelegate(RpcService proxy, RpcService delegate) { - val field = proxy.class.getField(DELEGATE_FIELD) - if (field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy"); - if (delegate == null || field.type.isAssignableFrom(delegate.class)) { - field.set(proxy, delegate) - } else - throw new IllegalArgumentException("delegate class is not assignable to proxy"); - } - - /** - * Helper method to set delegate to ManagedDirectedProxy with use of reflection. - * - * Note: This method uses reflection, but setting delegate field should not occur too much - * to introduce any significant performance hits. - * - */ - public static def void setDelegate(Object proxy, Object delegate) { - val field = proxy.class.getField(DELEGATE_FIELD) - if (field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy"); - if (delegate == null || field.type.isAssignableFrom(delegate.class)) { - field.set(proxy, delegate) - } else - throw new IllegalArgumentException("delegate class is not assignable to proxy"); - } - - - public static def Map, ? extends RpcService> getRoutingTable(RpcService target, - Class tableClass) { - val field = target.class.getField(tableClass.routingTableField) - if (field == null) throw new UnsupportedOperationException( - "Unable to get routing table. Table field does not exists"); - return field.get(target) as Map, ? extends RpcService>; - } - - public static def void setRoutingTable(RpcService target, Class tableClass, - Map, ? extends RpcService> routingTable) { - val field = target.class.getField(tableClass.routingTableField) - if (field == null) throw new UnsupportedOperationException( - "Unable to set routing table. Table field does not exists"); - field.set(target,routingTable); - } - -} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.java new file mode 100644 index 0000000000..0bc11d9b35 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + + +package org.opendaylight.controller.sal.binding.codegen; + +import java.lang.reflect.Method; +import org.opendaylight.yangtools.yang.binding.Notification; + +@SuppressWarnings("all") +public class YangtoolsMappingHelper { + public static boolean isNotificationCallback(final Method it) { + return it.getName().startsWith("on") && (it.getParameterTypes().length == 1) && + Notification.class.isAssignableFrom(it.getParameterTypes()[0]); + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.xtend deleted file mode 100644 index 18d3e26346..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/YangtoolsMappingHelper.xtend +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.codegen - -import java.lang.reflect.Method -import org.opendaylight.yangtools.yang.binding.Notification - -public static class YangtoolsMappingHelper { - - public static def boolean isNotificationCallback(Method it) { - return name.startsWith("on") && parameterTypes.size === 1 && - Notification.isAssignableFrom(parameterTypes.get(0)) - } -} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.java new file mode 100644 index 0000000000..cab4fe90e0 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + + +package org.opendaylight.controller.sal.binding.codegen.impl; + +import org.eclipse.xtext.xbase.lib.Exceptions; + +@SuppressWarnings("all") +public class BrokerImplClassLoader extends ClassLoader { + private final ClassLoader spiClassLoader; + + public BrokerImplClassLoader(final ClassLoader model, final ClassLoader spi) { + super(model); + this.spiClassLoader = spi; + } + + public Class loadClass(final String name) throws ClassNotFoundException { + try { + return super.loadClass(name); + } catch (ClassNotFoundException e) { + return this.spiClassLoader.loadClass(name); + } + } +} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.xtend deleted file mode 100644 index 6481c9d1b4..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/BrokerImplClassLoader.xtend +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.codegen.impl - -import java.lang.ClassLoader - -class BrokerImplClassLoader extends ClassLoader { - - val ClassLoader spiClassLoader - - public new(ClassLoader model, ClassLoader spi) { - super(model) - spiClassLoader = spi; - } - - override public loadClass(String name) throws ClassNotFoundException { - try { - return super.loadClass(name); - } catch (ClassNotFoundException e) { - return spiClassLoader.loadClass(name); - } - } - -} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.java new file mode 100644 index 0000000000..3b6a2539de --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.sal.binding.impl; + +import org.opendaylight.yangtools.yang.binding.RpcService; +import org.osgi.framework.ServiceRegistration; + +@SuppressWarnings("all") +public class RpcProxyContext { + public RpcProxyContext(final Class proxyClass) { + this.proxyClass = proxyClass; + } + + protected final Class proxyClass; + + protected RpcService _proxy; + + public RpcService getProxy() { + return this._proxy; + } + + public void setProxy(final RpcService proxy) { + this._proxy = proxy; + } + + protected ServiceRegistration _registration; + + public ServiceRegistration getRegistration() { + return this._registration; + } + + public void setRegistration(final ServiceRegistration registration) { + this._registration = registration; + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.xtend deleted file mode 100644 index 07494599f8..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProxyContext.xtend +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.impl - -import org.opendaylight.yangtools.yang.binding.RpcService -import org.osgi.framework.ServiceRegistration - -class RpcProxyContext { - - new(Class proxyClass) { - this.proxyClass = proxyClass - } - - protected val Class proxyClass; - - @Property - protected var RpcService proxy; - - @Property - protected var ServiceRegistration registration; -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.java new file mode 100644 index 0000000000..b0316635e2 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.impl.util; + +import java.util.Iterator; +import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +@SuppressWarnings("all") +public class BindingAwareDataReaderRouter extends AbstractDataReadRouter,DataObject> { + protected DataObject merge(final InstanceIdentifier path, final Iterable data) { + return data.iterator().next(); + } +} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.xtend deleted file mode 100644 index 5b775806d5..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/BindingAwareDataReaderRouter.xtend +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.impl.util - -import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier -import org.opendaylight.yangtools.yang.binding.DataObject - -class BindingAwareDataReaderRouter extends AbstractDataReadRouter, DataObject> { - - override protected merge(InstanceIdentifier path, Iterable data) { - return data.iterator.next; - } - -} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.java new file mode 100644 index 0000000000..aa6a9a21e5 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.sal.binding.impl.util; + +import com.google.common.collect.Multimap; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map.Entry; +import org.opendaylight.yangtools.concepts.Path; + +@SuppressWarnings("all") +public class MapUtils { + public static

, V extends Object> Collection> getAllChildren(final Multimap map, final P path) { + HashSet> _hashSet = new HashSet>(); + final HashSet> ret = _hashSet; + final Collection> entries = map.entries(); + for (final Entry entry : entries) { + { + final P currentPath = entry.getKey(); + if (path.contains(currentPath)) { + ret.add(entry); + } else if (currentPath.contains(path)){ + ret.add(entry); + } + } + } + return ret; + } +} + diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.xtend deleted file mode 100644 index c60686d209..0000000000 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.xtend +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.impl.util - -import com.google.common.collect.Multimap -import java.util.Collection -import java.util.HashSet -import java.util.Map.Entry -import org.opendaylight.yangtools.concepts.Path - -class MapUtils { - - public static def

, V> Collection> getAllChildren( - Multimap map, P path) { - val ret = new HashSet(); - val entries = map.entries; - - for (entry : entries) { - val currentPath = entry.key; - // If the registered reader processes nested elements - if (path.contains(currentPath)) { - ret.add(entry); - } else if(currentPath.contains(path)) { - // If the registered reader is parent of entry - ret.add(entry); - } - } - - return ret; - } -}