Bug 9283 - BindingToNormalizedNodeCodecFactory in sal-binding-broker 92/64292/8
authorJakub Toth <jakubt4@gmail.com>
Mon, 16 Oct 2017 07:56:58 +0000 (09:56 +0200)
committerJakub Toth <jakub.toth@pantheon.tech>
Mon, 16 Oct 2017 12:22:43 +0000 (14:22 +0200)
use hardcoded dependencies on OSGI/karaf

  * remove hardcoded dependecies on OSGi
    * moved to blueprint

Change-Id: I0b012877bb7dd12131c6268070ea3a87c7d0ded1
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodec.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingToNormalizedNodeCodecFactory.java
opendaylight/md-sal/sal-binding-broker/src/main/resources/org/opendaylight/blueprint/binding-broker.xml

index 647148c..e4e2053 100644 (file)
@@ -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<? extends DataObject> binding) {
+    public YangInstanceIdentifier
+            toYangInstanceIdentifierBlocking(final InstanceIdentifier<? extends DataObject> 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<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalizedNode(
             final Entry<InstanceIdentifier<? extends DataObject>, 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<InstanceIdentifier<? extends DataObject>> toBinding(final YangInstanceIdentifier normalized)
-                    throws DeserializationException {
+            throws DeserializationException {
         try {
-            return Optional.<InstanceIdentifier<? extends DataObject>>fromNullable(this.codecRegistry.fromYangInstanceIdentifier(normalized));
+            return Optional.<InstanceIdentifier<? extends DataObject>>fromNullable(
+                    this.codecRegistry.fromYangInstanceIdentifier(normalized));
         } catch (final IllegalArgumentException e) {
             return Optional.absent();
         }
@@ -205,7 +207,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto
 
     public Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> toBinding(
             @Nonnull final Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> 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<InstanceIdentifier<? extends DataObject>, DataObject> binding = Entry.class.cast(this.codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue()));
+            final Entry<InstanceIdentifier<? extends DataObject>, 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 <T extends DataObject> Function<Optional<NormalizedNode<?, ?>>, Optional<T>>  deserializeFunction(final InstanceIdentifier<T> path) {
+    public <T extends DataObject> Function<Optional<NormalizedNode<?, ?>>, Optional<T>>
+            deserializeFunction(final InstanceIdentifier<T> path) {
         return this.codecRegistry.deserializeFunction(path);
     }
 
@@ -288,7 +293,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto
         return ret.build();
     }
 
-    protected ImmutableBiMap<Method, RpcDefinition> getRpcMethodToSchema(final Class<? extends RpcService> key) {
+    public ImmutableBiMap<Method, RpcDefinition> getRpcMethodToSchema(final Class<? extends RpcService> key) {
         final Module module = getModuleBlocking(key);
         final ImmutableBiMap.Builder<Method, RpcDefinition> 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<Class<?>> 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<? extends RpcService> key, final RpcDefinition rpcDef) throws NoSuchMethodException {
+    private Method findRpcMethod(final Class<? extends RpcService> 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<InstanceIdentifier<?>, BindingCodecTreeNode<?>> getSubtreeCodec(
-            final YangInstanceIdentifier domIdentifier) {
+    public Map.Entry<InstanceIdentifier<?>, 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<? extends Notification>) 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<Class<?>> decompose(final InstanceIdentifier<?> path) {
         final Set<Class<?>> clazzes = new HashSet<>();
-        for(final InstanceIdentifier.PathArgument arg : path.getPathArguments()) {
+        for (final InstanceIdentifier.PathArgument arg : path.getPathArguments()) {
             clazzes.add(arg.getType());
         }
         return clazzes;
     }
-
 }
index 6652014..97fcc07 100644 (file)
@@ -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<BindingToNormalizedNodeCodec> registerOSGiService(final BindingToNormalizedNodeCodec instance,
-            final BundleContext bundleContext) {
-        final Dictionary<String, String> 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 );
-    }
 }
index 6b89328..55d5894 100644 (file)
     </interfaces>
   </service>
 
-  <!-- We also register the BindingToNormalizedNodeCodec with its actual class name for
-       backwards compatibility for CSS users that inject the binding-dom-mapping-service -->
-  <bean id="mappingCodecServiceReg" class="org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodecFactory"
-         factory-method="registerOSGiService" destroy-method="unregister">
-    <argument ref="mappingCodec"/>
-    <argument ref="blueprintBundleContext"/>
-  </bean>
+  <service id="codec" ref="mappingCodec" interface="org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec">
+    <service-properties>
+      <entry key="config-module-namespace" value="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl"/>
+      <entry key="config-module-name" value="runtime-generated-mapping"/>
+      <entry key="config-instance-name" value="runtime-mapping-singleton"/>
+    </service-properties>
+  </service>
 
   <!-- Binding RPC Registry Service -->
 

©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.