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