Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[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
72         final RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(SingletonHolder.CLASS_POOL);
73         bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
74         return service;
75     }
76
77     private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
78         ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
79         if(serviceRef == null) {
80             return null;
81         }
82
83         BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
84         if (delegate == null) {
85             return null;
86         }
87         return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
88     }
89
90     private BundleContext getBundleContext() {
91         return bundleContext;
92     }
93
94     public void setBundleContext(BundleContext bundleContext) {
95         this.bundleContext = bundleContext;
96     }
97
98     private static final class RuntimeGeneratedMappingServiceProxy implements //
99     BindingIndependentMappingService, //
100     Delegator<BindingIndependentMappingService>, //
101     AutoCloseable {
102
103         private BindingIndependentMappingService delegate;
104         private ServiceReference<BindingIndependentMappingService> reference;
105         private BundleContext bundleContext;
106
107         public RuntimeGeneratedMappingServiceProxy(BundleContext bundleContext,
108                 ServiceReference<BindingIndependentMappingService> serviceRef,
109                 BindingIndependentMappingService delegate) {
110             this.bundleContext = Preconditions.checkNotNull(bundleContext);
111             this.reference = Preconditions.checkNotNull(serviceRef);
112             this.delegate = Preconditions.checkNotNull(delegate);
113         }
114
115         @Override
116         public CodecRegistry getCodecRegistry() {
117             return delegate.getCodecRegistry();
118         }
119
120         @Override
121         public CompositeNode toDataDom(DataObject data) {
122             return delegate.toDataDom(data);
123         }
124
125         @Override
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         @Override
132         public InstanceIdentifier toDataDom(
133                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
134             return delegate.toDataDom(path);
135         }
136
137         @Override
138         public DataObject dataObjectFromDataDom(
139                 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
140                 CompositeNode result) throws DeserializationException {
141             return delegate.dataObjectFromDataDom(path, result);
142         }
143
144         @Override
145         public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
146                 throws DeserializationException {
147             return delegate.fromDataDom(entry);
148         }
149
150         @Override
151         public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
152             return delegate.getRpcQNamesFor(service);
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 DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
162             return delegate.dataObjectFromDataDom(inputClass, domInput);
163         }
164
165         @Override
166         public void close() throws Exception {
167             if(delegate != null) {
168                 delegate = null;
169                 bundleContext.ungetService(reference);
170                 bundleContext= null;
171                 reference = null;
172             }
173         }
174
175         @Override
176         public BindingIndependentMappingService getDelegate() {
177             return delegate;
178         }
179     }
180 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.