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