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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Hashtable;
13 import java.util.Map.Entry;
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;
34 public final class RuntimeMappingModule extends
35 org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
37 private BundleContext bundleContext;
39 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
40 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
41 super(identifier, dependencyResolver);
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);
51 public void validate() {
53 Preconditions.checkNotNull(bundleContext);
54 // Add custom validation for module attributes here.
58 public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
63 public java.lang.AutoCloseable createInstance() {
65 RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
66 if(potential != null) {
69 RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
70 service.setPool(SingletonHolder.CLASS_POOL);
72 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
76 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
77 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
78 if(serviceRef == null) {
82 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
83 if (delegate == null) {
86 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
89 private BundleContext getBundleContext() {
93 public void setBundleContext(BundleContext bundleContext) {
94 this.bundleContext = bundleContext;
97 private static final class RuntimeGeneratedMappingServiceProxy implements //
98 BindingIndependentMappingService, //
99 Delegator<BindingIndependentMappingService>, //
102 private BindingIndependentMappingService delegate;
103 private ServiceReference<BindingIndependentMappingService> reference;
104 private BundleContext bundleContext;
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);
114 public CodecRegistry getCodecRegistry() {
115 return delegate.getCodecRegistry();
118 public CompositeNode toDataDom(DataObject data) {
119 return delegate.toDataDom(data);
122 public Entry<InstanceIdentifier, CompositeNode> toDataDom(
123 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
124 return delegate.toDataDom(entry);
127 public InstanceIdentifier toDataDom(
128 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
129 return delegate.toDataDom(path);
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);
138 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
139 throws DeserializationException {
140 return delegate.fromDataDom(entry);
143 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
144 return delegate.getRpcQNamesFor(service);
148 public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
149 return delegate.getRpcServiceClassFor(namespace,revision);
152 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
153 return delegate.dataObjectFromDataDom(inputClass, domInput);
157 public void close() throws Exception {
158 if(delegate != null) {
160 bundleContext.ungetService(reference);
167 public BindingIndependentMappingService getDelegate() {