Modify config Module impls to co-exist with blueprint
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / config / yang / config / concurrent_data_broker / DomConcurrentDataBrokerModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.config.concurrent_data_broker;
9
10 import org.opendaylight.controller.config.api.DependencyResolver;
11 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
13 import org.opendaylight.controller.md.sal.dom.spi.ForwardingDOMDataBroker;
14 import org.osgi.framework.BundleContext;
15
16 public class DomConcurrentDataBrokerModule extends AbstractDomConcurrentDataBrokerModule {
17     private BundleContext bundleContext;
18
19     public DomConcurrentDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final DependencyResolver dependencyResolver) {
20         super(identifier, dependencyResolver);
21     }
22
23     public DomConcurrentDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final DependencyResolver dependencyResolver, final DomConcurrentDataBrokerModule oldModule, final AutoCloseable oldInstance) {
24         super(identifier, dependencyResolver, oldModule, oldInstance);
25     }
26
27     @Override
28     public boolean canReuseInstance(AbstractDomConcurrentDataBrokerModule oldModule) {
29         return true;
30     }
31
32     @Override
33     public AutoCloseable createInstance() {
34         // The ConcurrentDOMDataBroker is provided via blueprint so wait for and return it here for
35         // backwards compatibility.
36         WaitingServiceTracker<DOMDataBroker> tracker = WaitingServiceTracker.create(
37                 DOMDataBroker.class, bundleContext, "(type=default)");
38         DOMDataBroker delegate = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
39         return new ForwardingConcurrentDOMBroker(delegate, tracker);
40     }
41
42     public void setBundleContext(final BundleContext bundleContext) {
43         this.bundleContext = bundleContext;
44     }
45
46     private static class ForwardingConcurrentDOMBroker extends ForwardingDOMDataBroker implements AutoCloseable {
47         private final DOMDataBroker delegate;
48         private final AutoCloseable closeable;
49
50         ForwardingConcurrentDOMBroker(DOMDataBroker delegate, AutoCloseable closeable) {
51             this.delegate = delegate;
52             this.closeable = closeable;
53         }
54
55         @Override
56         protected DOMDataBroker delegate() {
57             return delegate;
58         }
59
60         @Override
61         public void close() throws Exception {
62             // We don't close the delegate as the life-cycle is controlled via blueprint.
63             closeable.close();
64         }
65     }
66 }