Modify config Module impls to co-exist with blueprint
[controller.git] / opendaylight / md-sal / sal-dom-broker-config / src / main / java / org / opendaylight / controller / config / yang / md / sal / dom / impl / DomBrokerImplModule.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.md.sal.dom.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.MutableClassToInstanceMap;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
22 import org.opendaylight.controller.sal.core.api.BrokerService;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
25 import org.opendaylight.controller.sal.dom.broker.GlobalBundleScanningSchemaServiceImpl;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public final class DomBrokerImplModule extends org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomBrokerImplModule{
31     private static final Logger LOG = LoggerFactory.getLogger(DomBrokerImplModule.class);
32
33     private BundleContext bundleContext;
34
35     public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
36         super(identifier, dependencyResolver);
37     }
38
39     public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final DomBrokerImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
40         super(identifier, dependencyResolver, oldModule, oldInstance);
41     }
42
43     @Override
44     public void validate() {
45         super.validate();
46         final long depth = getNotificationQueueDepth().getValue();
47         Preconditions.checkArgument(Long.lowestOneBit(depth) == Long.highestOneBit(depth), "Queue depth %s is not power-of-two", depth);
48     }
49
50     @Override
51     public java.lang.AutoCloseable createInstance() {
52         // The services are provided via blueprint so retrieve then from the OSGi service registry for
53         // backwards compatibility.
54
55         final List<AutoCloseable> closeables = new ArrayList<>();
56         DOMNotificationService domNotificationService = newTracker(
57                 DOMNotificationService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES);
58
59         DOMNotificationPublishService domNotificationPublishService = newTracker(
60                 DOMNotificationPublishService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES);
61
62         DOMRpcService domRpcService = newTracker(
63                 DOMRpcService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES);
64
65         DOMRpcProviderService domRpcProvider = newTracker(
66                 DOMRpcProviderService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES);
67
68         DOMMountPointService mountService = newTracker(DOMMountPointService.class, closeables).
69                 waitForService(WaitingServiceTracker.FIVE_MINUTES);
70
71         final DOMDataBroker dataBroker = getAsyncDataBrokerDependency();
72
73         final ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
74
75         services.putInstance(DOMNotificationService.class, domNotificationService);
76         services.putInstance(DOMNotificationPublishService.class, domNotificationPublishService);
77
78         final SchemaService schemaService = getSchemaServiceImpl();
79         services.putInstance(SchemaService.class, schemaService);
80
81         services.putInstance(DOMDataBroker.class, dataBroker);
82
83         services.putInstance(DOMRpcService.class, domRpcService);
84         services.putInstance(DOMRpcProviderService.class, domRpcProvider);
85
86         services.putInstance(DOMMountPointService.class, mountService);
87
88         BrokerImpl broker = new BrokerImpl(domRpcService, domRpcProvider, services);
89         broker.setDeactivator(new AutoCloseable() {
90             @Override
91             public void close() {
92                 for(AutoCloseable ac: closeables) {
93                     try {
94                         ac.close();
95                     } catch(Exception e) {
96                         LOG.warn("Exception while closing {}", ac, e);
97                     }
98                 }
99             }
100         });
101
102         return broker;
103     }
104
105     private <T> WaitingServiceTracker<T> newTracker(Class<T> serviceInterface, List<AutoCloseable> closeables) {
106         WaitingServiceTracker<T> tracker = WaitingServiceTracker.create(serviceInterface, bundleContext);
107         closeables.add(tracker);
108         return tracker;
109     }
110
111     private SchemaService getSchemaServiceImpl() {
112         final SchemaService schemaService;
113         if(getRootSchemaService() != null) {
114             schemaService = getRootSchemaServiceDependency();
115         } else {
116             schemaService = GlobalBundleScanningSchemaServiceImpl.getInstance();
117         }
118         return schemaService;
119     }
120
121     public void setBundleContext(final BundleContext bundleContext) {
122         this.bundleContext = bundleContext;
123     }
124 }