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.Preconditions;
37 public final class RuntimeMappingModule extends
38 org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModule {
40 private BundleContext bundleContext;
42 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
43 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
44 super(identifier, dependencyResolver);
47 public RuntimeMappingModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
48 org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
49 RuntimeMappingModule oldModule, java.lang.AutoCloseable oldInstance) {
50 super(identifier, dependencyResolver, oldModule, oldInstance);
54 public void validate() {
56 Preconditions.checkNotNull(bundleContext);
57 // Add custom validation for module attributes here.
61 public boolean canReuseInstance(AbstractRuntimeMappingModule oldModule) {
66 public java.lang.AutoCloseable createInstance() {
68 RuntimeGeneratedMappingServiceProxy potential = tryToReuseGlobalInstance();
69 if(potential != null) {
72 RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
73 service.setPool(SingletonHolder.CLASS_POOL);
75 bundleContext.registerService(SchemaServiceListener.class, service, new Hashtable<String,String>());
79 private RuntimeGeneratedMappingServiceProxy tryToReuseGlobalInstance() {
80 ServiceReference<BindingIndependentMappingService> serviceRef = getBundleContext().getServiceReference(BindingIndependentMappingService.class);
81 if(serviceRef == null) {
85 BindingIndependentMappingService delegate = bundleContext.getService(serviceRef);
86 if (delegate == null) {
89 return new RuntimeGeneratedMappingServiceProxy(getBundleContext(),serviceRef,delegate);
92 private BundleContext getBundleContext() {
96 public void setBundleContext(BundleContext bundleContext) {
97 this.bundleContext = bundleContext;
100 private static final class RuntimeGeneratedMappingServiceProxy implements //
101 BindingIndependentMappingService, //
102 Delegator<BindingIndependentMappingService>, //
105 private BindingIndependentMappingService delegate;
106 private ServiceReference<BindingIndependentMappingService> reference;
107 private BundleContext bundleContext;
109 public RuntimeGeneratedMappingServiceProxy(BundleContext bundleContext,
110 ServiceReference<BindingIndependentMappingService> serviceRef,
111 BindingIndependentMappingService delegate) {
112 this.bundleContext = Preconditions.checkNotNull(bundleContext);
113 this.reference = Preconditions.checkNotNull(serviceRef);
114 this.delegate = Preconditions.checkNotNull(delegate);
117 public CodecRegistry getCodecRegistry() {
118 return delegate.getCodecRegistry();
121 public CompositeNode toDataDom(DataObject data) {
122 return delegate.toDataDom(data);
125 public Entry<InstanceIdentifier, CompositeNode> toDataDom(
126 Entry<org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject>, DataObject> entry) {
127 return delegate.toDataDom(entry);
130 public InstanceIdentifier toDataDom(
131 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path) {
132 return delegate.toDataDom(path);
135 public DataObject dataObjectFromDataDom(
136 org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> path,
137 CompositeNode result) throws DeserializationException {
138 return delegate.dataObjectFromDataDom(path, result);
141 public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> fromDataDom(InstanceIdentifier entry)
142 throws DeserializationException {
143 return delegate.fromDataDom(entry);
146 public Set<QName> getRpcQNamesFor(Class<? extends RpcService> service) {
147 return delegate.getRpcQNamesFor(service);
150 public DataContainer dataObjectFromDataDom(Class<? extends DataContainer> inputClass, CompositeNode domInput) {
151 return delegate.dataObjectFromDataDom(inputClass, domInput);
155 public void close() throws Exception {
156 if(delegate != null) {
158 bundleContext.ungetService(reference);
165 public BindingIndependentMappingService getDelegate() {