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