Eliminate some more references to SchemaService
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingToNormalizedNodeCodecFactory.java
index 180f7b8f45a698bc5e847cd020074a8ae28263bb..d4f28a2c63a7e2ab4706d283cbfd0dfafc8ccb26 100644 (file)
@@ -7,51 +7,86 @@
  */
 package org.opendaylight.controller.md.sal.binding.impl;
 
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
-import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
-import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
+import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
+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 global BindingToNormalizedNodeCodec instance.
+ * Factory class for creating and initializing the BindingToNormalizedNodeCodec instances.
  *
  * @author Thomas Pantelis
  */
 public class BindingToNormalizedNodeCodecFactory {
-    private static final AtomicBoolean INSTANCE_CREATED = new AtomicBoolean();
-    private static volatile BindingToNormalizedNodeCodec instance;
-
     /**
-     * Returns the global BindingToNormalizedNodeCodec instance, creating if necessary. The returned instance
-     * is registered with tthe SchemaService as a SchemaContextListener.
+     * This method is deprecated in favor of newInstance/registerInstance.
      *
      * @param classLoadingStrategy
      * @param schemaService
-     * @return the BindingToNormalizedNodeCodec instance
+     * @return BindingToNormalizedNodeCodec instance
      */
-    public static BindingToNormalizedNodeCodec getOrCreateInstance(ClassLoadingStrategy classLoadingStrategy,
-            SchemaService schemaService) {
-        if(!INSTANCE_CREATED.compareAndSet(false, true)) {
-            return instance;
-        }
-
-        BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
+    @Deprecated
+    public static BindingToNormalizedNodeCodec getOrCreateInstance(final ClassLoadingStrategy classLoadingStrategy,
+                            final DOMSchemaService schemaService) {
+        final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
                 StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
-        BindingToNormalizedNodeCodec localInstance = new BindingToNormalizedNodeCodec(
-                classLoadingStrategy, codecRegistry, true);
+        final BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(
+                               classLoadingStrategy, codecRegistry, true);
+        schemaService.registerSchemaContextListener(instance);
+        return instance;
+    }
 
-        schemaService.registerSchemaContextListener(localInstance);
+    /**
+     * Creates a new BindingToNormalizedNodeCodec instance.
+     *
+     * @param classLoadingStrategy
+     * @return the BindingToNormalizedNodeCodec instance
+     */
+    public static BindingToNormalizedNodeCodec newInstance(final ClassLoadingStrategy classLoadingStrategy) {
+        final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
+                StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
+        return new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry, true);
+    }
 
-        // Publish the BindingToNormalizedNodeCodec instance after we've registered it as a
-        // SchemaContextListener to avoid a race condition by publishing it too early when it isn't
-        // fully initialized.
-        instance = localInstance;
-        return instance;
+    /**
+     * Registers the given instance with the SchemaService as a SchemaContextListener.
+     *
+     * @param instance the BindingToNormalizedNodeCodec instance
+     * @param schemaService the SchemaService.
+     * @return the ListenerRegistration
+     */
+    public static ListenerRegistration<SchemaContextListener> registerInstance(final BindingToNormalizedNodeCodec instance,
+            final DOMSchemaService schemaService) {
+        return schemaService.registerSchemaContextListener(instance);
     }
 
-    public static BindingToNormalizedNodeCodec getInstance() {
-        return 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 );
     }
 }