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;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.Optional;
33 import com.google.common.base.Preconditions;
38 public final class RuntimeMappingModule extends
39 org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
41 private static final Logger LOG = LoggerFactory.getLogger(RuntimeMappingModule.class);
43 private BundleContext bundleContext;
45 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
46 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
47 super(identifier, dependencyResolver);
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);
57 public void validate() {
59 Preconditions.checkNotNull(bundleContext);
60 // Add custom validation for module attributes here.
64 public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
69 public java.lang.AutoCloseable createInstance() {
71 RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
72 if(potential != null) {
76 final RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(SingletonHolder.CLASS_POOL);
77 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
81 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
82 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
83 if(serviceRef == null) {
87 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
88 if (delegate == null) {
91 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
94 private BundleContext getBundleContext() {
98 public void setBundleContext(BundleContext bundleContext) {
99 this.bundleContext = bundleContext;
102 private static final class RuntimeGeneratedMappingServiceProxy implements //
103 BindingIndependentMappingService, //
104 Delegator<BindingIndependentMappingService>, //
107 private BindingIndependentMappingService delegate;
108 private ServiceReference<BindingIndependentMappingService> reference;
109 private BundleContext bundleContext;
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);
120 public CodecRegistry getCodecRegistry() {
121 return delegate.getCodecRegistry();
125 public CompositeNode toDataDom(DataObject data) {
126 return delegate.toDataDom(data);
130 public Entry<InstanceIdentifier, CompositeNode> toDataDom(
131 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
132 return delegate.toDataDom(entry);
136 public InstanceIdentifier toDataDom(
137 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
138 return delegate.toDataDom(path);
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);
149 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
150 throws DeserializationException {
151 return delegate.fromDataDom(entry);
155 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
156 return delegate.getRpcQNamesFor(service);
160 public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
161 return delegate.getRpcServiceClassFor(namespace,revision);
165 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
166 return delegate.dataObjectFromDataDom(inputClass, domInput);
170 public void close() {
171 if(delegate != null) {
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 );
187 public BindingIndependentMappingService getDelegate() {