Extract dom-serializer-api|impl from binding-broker-impl
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / RuntimeMappingModule.java
1 /**
2  * Generated file
3
4  * Generated from: yang module name: opendaylight-sal-binding-broker-impl  yang module local name: runtime-generated-mapping
5  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
6  * Generated at: Wed Nov 20 18:20:19 CET 2013
7  *
8  * Do not modify this file unless it is present under src/main directory
9  */
10 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
11
12 import java.util.Map.Entry;
13 import java.util.Set;
14
15 import javassist.ClassPool;
16
17 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
18 import org.opendaylight.yangtools.concepts.Delegator;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
27 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
28 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceReference;
31
32 import com.google.common.base.Preconditions;
33
34 /**
35 *
36 */
37 public final class RuntimeMappingModule extends
38         org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
39
40     private BundleContext bundleContext;
41
42     public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
43             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
44         super(identifier, dependencyResolver);
45     }
46
47     public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
48             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
49             RuntimeMappingModule oldModule, java.lang.AutoCloseable oldInstance) {
50         super(identifier, dependencyResolver, oldModule, oldInstance);
51     }
52
53     @Override
54     public void validate() {
55         super.validate();
56         Preconditions.checkNotNull(bundleContext);
57         // Add custom validation for module attributes here.
58     }
59
60     @Override
61     public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
62         return true;
63     }
64
65     @Override
66     public java.lang.AutoCloseable createInstance() {
67         
68         RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
69         if(potential != null) {
70             return potential;
71         }
72         RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
73         service.setPool(SingletonHolder.CLASS_POOL);
74         service.init();
75         return service;
76     }
77
78     private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
79         ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
80         if(serviceRef == null) {
81             return null;
82         }
83         return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef);
84     }
85
86     private BundleContext getBundleContext() {
87         return bundleContext;
88     }
89
90     public void setBundleContext(BundleContext bundleContext) {
91         this.bundleContext = bundleContext;
92     }
93
94     private static final class RuntimeGeneratedMappingServiceProxy implements //
95     BindingIndependentMappingService, //
96     Delegator<BindingIndependentMappingService>, //
97     AutoCloseable {
98         
99         private BindingIndependentMappingService delegate;
100         private ServiceReference<BindingIndependentMappingService> reference;
101         private BundleContext bundleContext;
102
103         public RuntimeGeneratedMappingServiceProxy(BundleContext bundleContext,
104                 ServiceReference<BindingIndependentMappingService> serviceRef) {
105             this.bundleContext = bundleContext;
106             this.reference = serviceRef;
107             this.delegate = bundleContext.getService(serviceRef);
108         }
109
110         public CodecRegistry getCodecRegistry() {
111             return delegate.getCodecRegistry();
112         }
113
114         public CompositeNode toDataDom(DataObject data) {
115             return delegate.toDataDom(data);
116         }
117
118         public Entry<InstanceIdentifier, CompositeNode> toDataDom(
119                 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
120             return delegate.toDataDom(entry);
121         }
122
123         public InstanceIdentifier toDataDom(
124                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
125             return delegate.toDataDom(path);
126         }
127
128         public DataObject dataObjectFromDataDom(
129                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
130                 CompositeNode result) throws DeserializationException {
131             return delegate.dataObjectFromDataDom(path, result);
132         }
133
134         public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
135                 throws DeserializationException {
136             return delegate.fromDataDom(entry);
137         }
138
139         public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
140             return delegate.getRpcQNamesFor(service);
141         }
142
143         public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
144             return delegate.dataObjectFromDataDom(inputClass, domInput);
145         }
146         
147         @Override
148         public void close() throws Exception {
149             if(delegate != null) {
150                 delegate = null;
151                 bundleContext.ungetService(reference);
152                 bundleContext= null;
153                 reference = null;
154             }
155         }
156
157         @Override
158         public BindingIndependentMappingService getDelegate() {
159             return delegate;
160         }
161     }
162 }