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