Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / RuntimeMappingModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Hashtable;
12 import javassist.ClassPool;
13 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
14 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
15 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
16 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
17 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
18 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
19 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23
24 /**
25 *
26 */
27 public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
28
29     private BundleContext bundleContext;
30
31     public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
32             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
33         super(identifier, dependencyResolver);
34     }
35
36     public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
37             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
38             final RuntimeMappingModule oldModule, final java.lang.AutoCloseable oldInstance) {
39         super(identifier, dependencyResolver, oldModule, oldInstance);
40     }
41
42     @Override
43     public void validate() {
44         super.validate();
45         Preconditions.checkNotNull(bundleContext);
46         // Add custom validation for module attributes here.
47     }
48
49     @Override
50     public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
51         return true;
52     }
53
54     @Override
55     public java.lang.AutoCloseable createInstance() {
56         final GeneratedClassLoadingStrategy classLoading = getGlobalClassLoadingStrategy();
57         final BindingIndependentMappingService legacyMapping = getGlobalLegacyMappingService(classLoading);
58         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
59         BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(classLoading, legacyMapping, codecRegistry);
60         bundleContext.registerService(SchemaContextListener.class, instance, new Hashtable<String,String>());
61         return instance;
62     }
63
64     private BindingIndependentMappingService getGlobalLegacyMappingService(final GeneratedClassLoadingStrategy classLoading) {
65         BindingIndependentMappingService potential = tryToReuseGlobalMappingServiceInstance();
66         if(potential == null) {
67             potential =  new RuntimeGeneratedMappingServiceImpl(ClassPool.getDefault(),classLoading);
68             bundleContext.registerService(SchemaContextListener.class, (SchemaContextListener) potential, new Hashtable<String,String>());
69         }
70         return potential;
71     }
72
73     private GeneratedClassLoadingStrategy getGlobalClassLoadingStrategy() {
74         ServiceReference<GeneratedClassLoadingStrategy> ref = bundleContext.getServiceReference(GeneratedClassLoadingStrategy.class);
75         return bundleContext.getService(ref);
76     }
77
78     private BindingIndependentMappingService tryToReuseGlobalMappingServiceInstance() {
79         ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
80         if(serviceRef == null) {
81             return null;
82         }
83         return bundleContext.getService(serviceRef);
84
85     }
86
87     private BundleContext getBundleContext() {
88         return bundleContext;
89     }
90
91     public void setBundleContext(final BundleContext bundleContext) {
92         this.bundleContext = bundleContext;
93     }
94 }