2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
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;
27 public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
29 private BundleContext bundleContext;
31 public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
32 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
33 super(identifier, dependencyResolver);
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);
43 public void validate() {
45 Preconditions.checkNotNull(bundleContext);
46 // Add custom validation for module attributes here.
50 public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
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>());
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>());
73 private GeneratedClassLoadingStrategy getGlobalClassLoadingStrategy() {
74 ServiceReference<GeneratedClassLoadingStrategy> ref = bundleContext.getServiceReference(GeneratedClassLoadingStrategy.class);
75 return bundleContext.getService(ref);
78 private BindingIndependentMappingService tryToReuseGlobalMappingServiceInstance() {
79 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
80 if(serviceRef == null) {
83 return bundleContext.getService(serviceRef);
87 private BundleContext getBundleContext() {
91 public void setBundleContext(final BundleContext bundleContext) {
92 this.bundleContext = bundleContext;