Merge "BUG-509: create data tree SPI package"
[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 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
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 static final Logger LOG = LoggerFactory.getLogger(RuntimeMappingModule.class);
42
43     private BundleContext bundleContext;
44
45     public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
46             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
47         super(identifier, dependencyResolver);
48     }
49
50     public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
51             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
52             RuntimeMappingModule oldModule, java.lang.AutoCloseable oldInstance) {
53         super(identifier, dependencyResolver, oldModule, oldInstance);
54     }
55
56     @Override
57     public void validate() {
58         super.validate();
59         Preconditions.checkNotNull(bundleContext);
60         // Add custom validation for module attributes here.
61     }
62
63     @Override
64     public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
65         return true;
66     }
67
68     @Override
69     public java.lang.AutoCloseable createInstance() {
70
71         RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
72         if(potential != null) {
73             return potential;
74         }
75
76         final RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(SingletonHolder.CLASS_POOL);
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         @Override
120         public CodecRegistry getCodecRegistry() {
121             return delegate.getCodecRegistry();
122         }
123
124         @Override
125         public CompositeNode toDataDom(DataObject data) {
126             return delegate.toDataDom(data);
127         }
128
129         @Override
130         public Entry<InstanceIdentifier, CompositeNode> toDataDom(
131                 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
132             return delegate.toDataDom(entry);
133         }
134
135         @Override
136         public InstanceIdentifier toDataDom(
137                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
138             return delegate.toDataDom(path);
139         }
140
141         @Override
142         public DataObject dataObjectFromDataDom(
143                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
144                 CompositeNode result) throws DeserializationException {
145             return delegate.dataObjectFromDataDom(path, result);
146         }
147
148         @Override
149         public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
150                 throws DeserializationException {
151             return delegate.fromDataDom(entry);
152         }
153
154         @Override
155         public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
156             return delegate.getRpcQNamesFor(service);
157         }
158
159         @Override
160         public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
161             return delegate.getRpcServiceClassFor(namespace,revision);
162         }
163
164         @Override
165         public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
166             return delegate.dataObjectFromDataDom(inputClass, domInput);
167         }
168
169         @Override
170         public void close() {
171             if(delegate != null) {
172                 delegate = null;
173
174                 try {
175                     bundleContext.ungetService(reference);
176                 } catch (IllegalStateException e) {
177                     // Indicates the BundleContext is no longer valid which can happen normally on shutdown.
178                     LOG.debug( "Error unregistering service", e );
179                 }
180
181                 bundleContext= null;
182                 reference = null;
183             }
184         }
185
186         @Override
187         public BindingIndependentMappingService getDelegate() {
188             return delegate;
189         }
190     }
191 }