Merge "Fix raw references to Promise"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / RuntimeMappingModule.java
index 99b7ed8acf777a61d4a73c37cbf80500241274ba..0ad043489269df0f6443e37f8aa20d0f60fbccca 100644 (file)
@@ -1,38 +1,41 @@
-/**
- * Generated file
-
- * Generated from: yang module name: opendaylight-sal-binding-broker-impl  yang module local name: runtime-generated-mapping
- * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
- * Generated at: Wed Nov 20 18:20:19 CET 2013
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
- * Do not modify this file unless it is present under src/main directory
+ * 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.config.yang.md.sal.binding.impl;
 
+import com.google.common.base.Preconditions;
+import java.util.Hashtable;
 import javassist.ClassPool;
-
+import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
-import org.opendaylight.controller.sal.binding.dom.serializer.impl.RuntimeGeneratedMappingServiceImpl;
+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.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
+import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.osgi.framework.BundleContext;
-
-import com.google.common.base.Preconditions;
+import org.osgi.framework.ServiceReference;
 
 /**
 *
 */
-public final class RuntimeMappingModule extends
-        org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
+public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
 
     private BundleContext bundleContext;
 
-    public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
-            org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
+    public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
+            final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
     }
 
-    public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
-            org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
-            RuntimeMappingModule oldModule, java.lang.AutoCloseable oldInstance) {
+    public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
+            final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
+            final RuntimeMappingModule oldModule, final java.lang.AutoCloseable oldInstance) {
         super(identifier, dependencyResolver, oldModule, oldInstance);
     }
 
@@ -44,23 +47,48 @@ public final class RuntimeMappingModule extends
     }
 
     @Override
-    public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
+    public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
         return true;
     }
 
     @Override
     public java.lang.AutoCloseable createInstance() {
-        RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
-        service.setPool(SingletonHolder.CLASS_POOL);
-        service.start(getBundleContext());
-        return service;
+        final GeneratedClassLoadingStrategy classLoading = getGlobalClassLoadingStrategy();
+        final BindingIndependentMappingService legacyMapping = getGlobalLegacyMappingService(classLoading);
+        BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
+        BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(classLoading, legacyMapping, codecRegistry);
+        bundleContext.registerService(SchemaContextListener.class, instance, new Hashtable<String,String>());
+        return instance;
+    }
+
+    private BindingIndependentMappingService getGlobalLegacyMappingService(final GeneratedClassLoadingStrategy classLoading) {
+        BindingIndependentMappingService potential = tryToReuseGlobalMappingServiceInstance();
+        if(potential == null) {
+            potential =  new RuntimeGeneratedMappingServiceImpl(ClassPool.getDefault(),classLoading);
+            bundleContext.registerService(SchemaContextListener.class, (SchemaContextListener) potential, new Hashtable<String,String>());
+        }
+        return potential;
+    }
+
+    private GeneratedClassLoadingStrategy getGlobalClassLoadingStrategy() {
+        ServiceReference<GeneratedClassLoadingStrategy> ref = bundleContext.getServiceReference(GeneratedClassLoadingStrategy.class);
+        return bundleContext.getService(ref);
+    }
+
+    private BindingIndependentMappingService tryToReuseGlobalMappingServiceInstance() {
+        ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
+        if(serviceRef == null) {
+            return null;
+        }
+        return bundleContext.getService(serviceRef);
+
     }
 
     private BundleContext getBundleContext() {
         return bundleContext;
     }
 
-    public void setBundleContext(BundleContext bundleContext) {
+    public void setBundleContext(final BundleContext bundleContext) {
         this.bundleContext = bundleContext;
     }
 }