2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
10 import java.util.Hashtable;
11 import java.util.Map.Entry;
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;
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
36 public final class RuntimeMappingModule extends
37 org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
39 private BundleContext bundleContext;
41 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
42 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
43 super(identifier, dependencyResolver);
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);
53 public void validate() {
55 Preconditions.checkNotNull(bundleContext);
56 // Add custom validation for module attributes here.
60 public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
65 public java.lang.AutoCloseable createInstance() {
67 RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
68 if(potential != null) {
71 RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
72 service.setPool(SingletonHolder.CLASS_POOL);
74 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
78 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
79 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
80 if(serviceRef == null) {
84 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
85 if (delegate == null) {
88 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
91 private BundleContext getBundleContext() {
95 public void setBundleContext(BundleContext bundleContext) {
96 this.bundleContext = bundleContext;
99 private static final class RuntimeGeneratedMappingServiceProxy implements //
100 BindingIndependentMappingService, //
101 Delegator<BindingIndependentMappingService>, //
104 private BindingIndependentMappingService delegate;
105 private ServiceReference<BindingIndependentMappingService> reference;
106 private BundleContext bundleContext;
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);
117 public CodecRegistry getCodecRegistry() {
118 return delegate.getCodecRegistry();
122 public CompositeNode toDataDom(DataObject data) {
123 return delegate.toDataDom(data);
127 public Entry<InstanceIdentifier, CompositeNode> toDataDom(
128 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
129 return delegate.toDataDom(entry);
133 public InstanceIdentifier toDataDom(
134 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
135 return delegate.toDataDom(path);
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);
146 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
147 throws DeserializationException {
148 return delegate.fromDataDom(entry);
152 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
153 return delegate.getRpcQNamesFor(service);
157 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
158 return delegate.dataObjectFromDataDom(inputClass, domInput);
162 public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
163 return delegate.getRpcServiceClassFor(namespace, revision);
167 public void close() throws Exception {
168 if(delegate != null) {
170 bundleContext.ungetService(reference);
177 public BindingIndependentMappingService getDelegate() {