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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.