From ae2bfb7032031cfd0397351389665c51c836a586 Mon Sep 17 00:00:00 2001 From: Jakub Toth Date: Mon, 16 Oct 2017 09:56:58 +0200 Subject: [PATCH] Bug 9283 - BindingToNormalizedNodeCodecFactory in sal-binding-broker use hardcoded dependencies on OSGI/karaf * remove hardcoded dependecies on OSGi * moved to blueprint Change-Id: I0b012877bb7dd12131c6268070ea3a87c7d0ded1 Signed-off-by: Jakub Toth --- .../impl/BindingToNormalizedNodeCodec.java | 65 ++++++++++--------- .../BindingToNormalizedNodeCodecFactory.java | 27 -------- .../opendaylight/blueprint/binding-broker.xml | 14 ++-- 3 files changed, 42 insertions(+), 64 deletions(-) diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodec.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodec.java index 647148c626..e4e205307c 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodec.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodec.java @@ -62,7 +62,8 @@ import org.opendaylight.yangtools.yang.model.api.meta.StatementSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFactory, BindingNormalizedNodeSerializer, SchemaContextListener, AutoCloseable { +public class BindingToNormalizedNodeCodec + implements BindingCodecTreeFactory, BindingNormalizedNodeSerializer, SchemaContextListener, AutoCloseable { private static final long WAIT_DURATION_SEC = 5; private static final Logger LOG = LoggerFactory.getLogger(BindingToNormalizedNodeCodec.class); @@ -85,22 +86,23 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto public BindingToNormalizedNodeCodec(final ClassLoadingStrategy classLoadingStrategy, final BindingNormalizedNodeCodecRegistry codecRegistry) { - this(classLoadingStrategy,codecRegistry,false); + this(classLoadingStrategy, codecRegistry, false); } public BindingToNormalizedNodeCodec(final ClassLoadingStrategy classLoadingStrategy, - final BindingNormalizedNodeCodecRegistry codecRegistry,final boolean waitForSchema) { - this.classLoadingStrategy = Preconditions.checkNotNull(classLoadingStrategy,"classLoadingStrategy"); - this.codecRegistry = Preconditions.checkNotNull(codecRegistry,"codecRegistry"); + final BindingNormalizedNodeCodecRegistry codecRegistry, final boolean waitForSchema) { + this.classLoadingStrategy = Preconditions.checkNotNull(classLoadingStrategy, "classLoadingStrategy"); + this.codecRegistry = Preconditions.checkNotNull(codecRegistry, "codecRegistry"); this.futureSchema = new FutureSchema(WAIT_DURATION_SEC, TimeUnit.SECONDS, waitForSchema); } - YangInstanceIdentifier toYangInstanceIdentifierBlocking(final InstanceIdentifier binding) { + public YangInstanceIdentifier + toYangInstanceIdentifierBlocking(final InstanceIdentifier binding) { try { return this.codecRegistry.toYangInstanceIdentifier(binding); } catch (final MissingSchemaException e) { - waitForSchema(decompose(binding),e); + waitForSchema(decompose(binding), e); return this.codecRegistry.toYangInstanceIdentifier(binding); } } @@ -124,9 +126,8 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto return this.codecRegistry.toYangInstanceIdentifier(binding); } - - YangInstanceIdentifier toYangInstanceIdentifierCached(final InstanceIdentifier binding) { - return this.iiCache .getUnchecked(binding); + public YangInstanceIdentifier toYangInstanceIdentifierCached(final InstanceIdentifier binding) { + return this.iiCache.getUnchecked(binding); } @Override @@ -144,10 +145,10 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto * @return DOM Map Entry with {@link YangInstanceIdentifier} as key and {@link NormalizedNode} * as value. */ - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public Entry> toNormalizedNode( final Entry, DataObject> binding) { - return toNormalizedNode((InstanceIdentifier) binding.getKey(),binding.getValue()); + return toNormalizedNode((InstanceIdentifier) binding.getKey(), binding.getValue()); } @Override @@ -191,9 +192,10 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto * */ public Optional> toBinding(final YangInstanceIdentifier normalized) - throws DeserializationException { + throws DeserializationException { try { - return Optional.>fromNullable(this.codecRegistry.fromYangInstanceIdentifier(normalized)); + return Optional.>fromNullable( + this.codecRegistry.fromYangInstanceIdentifier(normalized)); } catch (final IllegalArgumentException e) { return Optional.absent(); } @@ -205,7 +207,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto public Optional, DataObject>> toBinding( @Nonnull final Entry> normalized) - throws DeserializationException { + throws DeserializationException { try { /* * This cast is required, due to generics behaviour in openjdk / oracle javac @@ -221,7 +223,8 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto * */ @SuppressWarnings("unchecked") - final Entry, DataObject> binding = Entry.class.cast(this.codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue())); + final Entry, DataObject> binding = + Entry.class.cast(this.codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue())); return Optional.fromNullable(binding); } catch (final IllegalArgumentException e) { return Optional.absent(); @@ -231,12 +234,14 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto @Override public void onGlobalContextUpdated(final SchemaContext schemaContext) { this.legacyToNormalized = new DataNormalizer(schemaContext); - final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(this.classLoadingStrategy, schemaContext); + final BindingRuntimeContext runtimeContext = + BindingRuntimeContext.create(this.classLoadingStrategy, schemaContext); this.codecRegistry.onBindingRuntimeContextUpdated(runtimeContext); this.futureSchema.onRuntimeContextUpdated(runtimeContext); } - public Function>, Optional> deserializeFunction(final InstanceIdentifier path) { + public Function>, Optional> + deserializeFunction(final InstanceIdentifier path) { return this.codecRegistry.deserializeFunction(path); } @@ -288,7 +293,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto return ret.build(); } - protected ImmutableBiMap getRpcMethodToSchema(final Class key) { + public ImmutableBiMap getRpcMethodToSchema(final Class key) { final Module module = getModuleBlocking(key); final ImmutableBiMap.Builder ret = ImmutableBiMap.builder(); try { @@ -307,7 +312,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto final URI namespace = moduleName.getNamespace(); final Date revision = moduleName.getRevision(); Module module = runtimeContext().getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision); - if((module == null) && this.futureSchema.waitForSchema(namespace,revision)) { + if ((module == null) && this.futureSchema.waitForSchema(namespace, revision)) { module = runtimeContext().getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision); } Preconditions.checkState(module != null, "Schema for %s is not available.", modeledClass); @@ -317,16 +322,17 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto private void waitForSchema(final Collection> binding, final MissingSchemaException e) { LOG.warn("Blocking thread to wait for schema convergence updates for {} {}", this.futureSchema.getDuration(), this.futureSchema.getUnit()); - if(this.futureSchema.waitForSchema(binding)) { + if (this.futureSchema.waitForSchema(binding)) { return; } throw e; } - private Method findRpcMethod(final Class key, final RpcDefinition rpcDef) throws NoSuchMethodException { + private Method findRpcMethod(final Class key, final RpcDefinition rpcDef) + throws NoSuchMethodException { final String methodName = BindingMapping.getMethodName(rpcDef.getQName()); - if(rpcDef.getInput() != null && isExplicitStatement(rpcDef.getInput())) { + if ((rpcDef.getInput() != null) && isExplicitStatement(rpcDef.getInput())) { final Class inputClz = runtimeContext().getClassForSchema(rpcDef.getInput()); return key.getMethod(methodName, inputClz); } @@ -334,8 +340,8 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto } private static boolean isExplicitStatement(final ContainerSchemaNode node) { - return node instanceof EffectiveStatement - && ((EffectiveStatement) node).getDeclared().getStatementSource() == StatementSource.DECLARATION; + return (node instanceof EffectiveStatement) + && (((EffectiveStatement) node).getDeclared().getStatementSource() == StatementSource.DECLARATION); } private BindingRuntimeContext runtimeContext() { @@ -353,8 +359,8 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto } @Nonnull - protected Map.Entry, BindingCodecTreeNode> getSubtreeCodec( - final YangInstanceIdentifier domIdentifier) { + public Map.Entry, BindingCodecTreeNode> + getSubtreeCodec(final YangInstanceIdentifier domIdentifier) { final BindingCodecTree currentCodecTree = this.codecRegistry.getCodecContext(); final InstanceIdentifier bindingPath = this.codecRegistry.fromYangInstanceIdentifier(domIdentifier); @@ -377,7 +383,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto result.add((Class) runtimeContext().getClassForSchema(notification)); } catch (final IllegalStateException e) { // Ignore - LOG.warn("Class for {} is currently not known.",notification.getPath(),e); + LOG.warn("Class for {} is currently not known.", notification.getPath(), e); } } } @@ -386,10 +392,9 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto private static Collection> decompose(final InstanceIdentifier path) { final Set> clazzes = new HashSet<>(); - for(final InstanceIdentifier.PathArgument arg : path.getPathArguments()) { + for (final InstanceIdentifier.PathArgument arg : path.getPathArguments()) { clazzes.add(arg.getType()); } return clazzes; } - } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodecFactory.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodecFactory.java index 665201480e..97fcc07e98 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodecFactory.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodecFactory.java @@ -7,8 +7,6 @@ */ package org.opendaylight.controller.md.sal.binding.impl; -import java.util.Dictionary; -import java.util.Hashtable; import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder; import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator; import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry; @@ -16,8 +14,6 @@ import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy; import org.opendaylight.mdsal.dom.api.DOMSchemaService; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; /** * Factory class for creating and initializing the BindingToNormalizedNodeCodec instances. @@ -48,27 +44,4 @@ public class BindingToNormalizedNodeCodecFactory { final DOMSchemaService schemaService) { return schemaService.registerSchemaContextListener(instance); } - - /** - * This method is called via blueprint to register a BindingToNormalizedNodeCodec instance with the OSGI - * service registry. This is done in code instead of directly via blueprint because the BindingToNormalizedNodeCodec - * instance must be advertised with the actual class for backwards compatibility with CSS modules and blueprint - * will try to create a proxy wrapper which is problematic with BindingToNormalizedNodeCodec because it's final - * and has final methods which can't be proxied. - * - * @param instance the BindingToNormalizedNodeCodec instance - * @param bundleContext the BundleContext - * @return ServiceRegistration instance - */ - public static ServiceRegistration registerOSGiService(final BindingToNormalizedNodeCodec instance, - final BundleContext bundleContext) { - final Dictionary props = new Hashtable<>(); - - // Set the appropriate service properties so the corresponding CSS module is restarted if this - // blueprint container is restarted - props.put("config-module-namespace", "urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl"); - props.put("config-module-name", "runtime-generated-mapping"); - props.put("config-instance-name", "runtime-mapping-singleton"); - return bundleContext.registerService(BindingToNormalizedNodeCodec.class, instance, props ); - } } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/resources/org/opendaylight/blueprint/binding-broker.xml b/opendaylight/md-sal/sal-binding-broker/src/main/resources/org/opendaylight/blueprint/binding-broker.xml index 6b89328f1c..55d589428d 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/resources/org/opendaylight/blueprint/binding-broker.xml +++ b/opendaylight/md-sal/sal-binding-broker/src/main/resources/org/opendaylight/blueprint/binding-broker.xml @@ -26,13 +26,13 @@ - - - - - + + + + + + + -- 2.36.6