Bulk-add copyright headers to java files
[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 java.util.Hashtable;
11 import java.util.Map.Entry;
12 import java.util.Set;
13
14 import javassist.ClassPool;
15
16 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
17 import org.opendaylight.yangtools.concepts.Delegator;
18 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.RpcService;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
26 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
27 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
28 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
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         bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
76         return service;
77     }
78
79     private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
80         ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
81         if(serviceRef == null) {
82             return null;
83         }
84
85         BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
86         if (delegate == null) {
87             return null;
88         }
89         return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
90     }
91
92     private BundleContext getBundleContext() {
93         return bundleContext;
94     }
95
96     public void setBundleContext(BundleContext bundleContext) {
97         this.bundleContext = bundleContext;
98     }
99
100     private static final class RuntimeGeneratedMappingServiceProxy implements //
101     BindingIndependentMappingService, //
102     Delegator<BindingIndependentMappingService>, //
103     AutoCloseable {
104         
105         private BindingIndependentMappingService delegate;
106         private ServiceReference<BindingIndependentMappingService> reference;
107         private BundleContext bundleContext;
108
109         public RuntimeGeneratedMappingServiceProxy(BundleContext bundleContext,
110                 ServiceReference<BindingIndependentMappingService> serviceRef,
111                 BindingIndependentMappingService delegate) {
112             this.bundleContext = Preconditions.checkNotNull(bundleContext);
113             this.reference = Preconditions.checkNotNull(serviceRef);
114             this.delegate = Preconditions.checkNotNull(delegate);
115         }
116
117         public CodecRegistry getCodecRegistry() {
118             return delegate.getCodecRegistry();
119         }
120
121         public CompositeNode toDataDom(DataObject data) {
122             return delegate.toDataDom(data);
123         }
124
125         public Entry<InstanceIdentifier, CompositeNode> toDataDom(
126                 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
127             return delegate.toDataDom(entry);
128         }
129
130         public InstanceIdentifier toDataDom(
131                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
132             return delegate.toDataDom(path);
133         }
134
135         public DataObject dataObjectFromDataDom(
136                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
137                 CompositeNode result) throws DeserializationException {
138             return delegate.dataObjectFromDataDom(path, result);
139         }
140
141         public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
142                 throws DeserializationException {
143             return delegate.fromDataDom(entry);
144         }
145
146         public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
147             return delegate.getRpcQNamesFor(service);
148         }
149
150         public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
151             return delegate.dataObjectFromDataDom(inputClass, domInput);
152         }
153         
154         @Override
155         public void close() throws Exception {
156             if(delegate != null) {
157                 delegate = null;
158                 bundleContext.ungetService(reference);
159                 bundleContext= null;
160                 reference = null;
161             }
162         }
163
164         @Override
165         public BindingIndependentMappingService getDelegate() {
166             return delegate;
167         }
168     }
169 }