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) {
72 final RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(SingletonHolder.CLASS_POOL);
73 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
77 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
78 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
79 if(serviceRef == null) {
83 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
84 if (delegate == null) {
87 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
90 private BundleContext getBundleContext() {
94 public void setBundleContext(BundleContext bundleContext) {
95 this.bundleContext = bundleContext;
98 private static final class RuntimeGeneratedMappingServiceProxy implements //
99 BindingIndependentMappingService, //
100 Delegator<BindingIndependentMappingService>, //
103 private BindingIndependentMappingService delegate;
104 private ServiceReference<BindingIndependentMappingService> reference;
105 private BundleContext bundleContext;
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);
116 public CodecRegistry getCodecRegistry() {
117 return delegate.getCodecRegistry();
121 public CompositeNode toDataDom(DataObject data) {
122 return delegate.toDataDom(data);
126 public Entry<InstanceIdentifier, CompositeNode> toDataDom(
127 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
128 return delegate.toDataDom(entry);
132 public InstanceIdentifier toDataDom(
133 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
134 return delegate.toDataDom(path);
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);
145 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
146 throws DeserializationException {
147 return delegate.fromDataDom(entry);
151 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
152 return delegate.getRpcQNamesFor(service);
156 public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
157 return delegate.getRpcServiceClassFor(namespace,revision);
161 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
162 return delegate.dataObjectFromDataDom(inputClass, domInput);
166 public void close() throws Exception {
167 if(delegate != null) {
169 bundleContext.ungetService(reference);
176 public BindingIndependentMappingService getDelegate() {