87ce27d501159a0736b9b73d31545e660716a878
[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 org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
13 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
14 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
15 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
16 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20
21 /**
22  *
23 **/
24 public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
25
26     private BundleContext bundleContext;
27
28     public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
29             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
30         super(identifier, dependencyResolver);
31     }
32
33     public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
34             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
35             final RuntimeMappingModule oldModule, final java.lang.AutoCloseable oldInstance) {
36         super(identifier, dependencyResolver, oldModule, oldInstance);
37     }
38
39     @Override
40     public void validate() {
41         super.validate();
42         Preconditions.checkNotNull(bundleContext);
43         // Add custom validation for module attributes here.
44     }
45
46     @Override
47     public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
48         return true;
49     }
50
51     @Override
52     public java.lang.AutoCloseable createInstance() {
53         final GeneratedClassLoadingStrategy classLoading = getGlobalClassLoadingStrategy();
54         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
55         final BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(classLoading, codecRegistry,getWaitForSchema());
56         bundleContext.registerService(SchemaContextListener.class, instance, new Hashtable<String,String>());
57         return instance;
58     }
59
60     private GeneratedClassLoadingStrategy getGlobalClassLoadingStrategy() {
61         final ServiceReference<GeneratedClassLoadingStrategy> ref = bundleContext.getServiceReference(GeneratedClassLoadingStrategy.class);
62         return bundleContext.getService(ref);
63     }
64
65     public void setBundleContext(final BundleContext bundleContext) {
66         this.bundleContext = bundleContext;
67     }
68 }