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 javassist.ClassPool;
16 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
17 import org.opendaylight.yangtools.concepts.Delegator;
18 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.RpcService;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
26 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
27 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
28 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceReference;
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 BundleContext bundleContext;
43 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
44 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
45 super(identifier, dependencyResolver);
48 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
49 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
50 RuntimeMappingModule oldModule, java.lang.AutoCloseable oldInstance) {
51 super(identifier, dependencyResolver, oldModule, oldInstance);
55 public void validate() {
57 Preconditions.checkNotNull(bundleContext);
58 // Add custom validation for module attributes here.
62 public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
67 public java.lang.AutoCloseable createInstance() {
69 RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
70 if(potential != null) {
73 RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
74 service.setPool(SingletonHolder.CLASS_POOL);
76 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
80 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
81 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
82 if(serviceRef == null) {
86 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
87 if (delegate == null) {
90 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
93 private BundleContext getBundleContext() {
97 public void setBundleContext(BundleContext bundleContext) {
98 this.bundleContext = bundleContext;
101 private static final class RuntimeGeneratedMappingServiceProxy implements //
102 BindingIndependentMappingService, //
103 Delegator<BindingIndependentMappingService>, //
106 private BindingIndependentMappingService delegate;
107 private ServiceReference<BindingIndependentMappingService> reference;
108 private BundleContext bundleContext;
110 public RuntimeGeneratedMappingServiceProxy(BundleContext bundleContext,
111 ServiceReference<BindingIndependentMappingService> serviceRef,
112 BindingIndependentMappingService delegate) {
113 this.bundleContext = Preconditions.checkNotNull(bundleContext);
114 this.reference = Preconditions.checkNotNull(serviceRef);
115 this.delegate = Preconditions.checkNotNull(delegate);
118 public CodecRegistry getCodecRegistry() {
119 return delegate.getCodecRegistry();
122 public CompositeNode toDataDom(DataObject data) {
123 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);
131 public InstanceIdentifier toDataDom(
132 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
133 return delegate.toDataDom(path);
136 public DataObject dataObjectFromDataDom(
137 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
138 CompositeNode result) throws DeserializationException {
139 return delegate.dataObjectFromDataDom(path, result);
142 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
143 throws DeserializationException {
144 return delegate.fromDataDom(entry);
147 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
148 return delegate.getRpcQNamesFor(service);
151 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
152 return delegate.dataObjectFromDataDom(inputClass, domInput);
156 public Optional<Class<? extends RpcService>> getRpcServiceClassFor(String namespace, String revision) {
157 return delegate.getRpcServiceClassFor(namespace, revision);
161 public void close() throws Exception {
162 if(delegate != null) {
164 bundleContext.ungetService(reference);
171 public BindingIndependentMappingService getDelegate() {