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

©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.