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