Decouple config and netconf subsystems.
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / config / yang / messagebus / app / impl / MessageBusAppImplModule.java
1 /*
2  * Copyright (c) 2015 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.messagebus.app.impl;
9
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import org.opendaylight.controller.config.api.DependencyResolver;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
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.messagebus.app.impl.EventSourceTopology;
20 //import org.opendaylight.controller.messagebus.eventsources.netconf.NetconfEventSourceManager;
21 import org.opendaylight.controller.messagebus.spi.EventSource;
22 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
23 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
27 import org.osgi.framework.BundleContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Preconditions;
32
33 public class MessageBusAppImplModule extends org.opendaylight.controller.config.yang.messagebus.app.impl.AbstractMessageBusAppImplModule {
34     private static final Logger LOGGER = LoggerFactory.getLogger(MessageBusAppImplModule.class);
35
36     private BundleContext bundleContext;
37
38     public BundleContext getBundleContext() {
39         return bundleContext;
40     }
41
42     public void setBundleContext(final BundleContext bundleContext) {
43         this.bundleContext = bundleContext;
44     }
45
46     public MessageBusAppImplModule(final ModuleIdentifier identifier, final DependencyResolver dependencyResolver) {
47         super(identifier, dependencyResolver);
48     }
49
50     public MessageBusAppImplModule(final ModuleIdentifier identifier, final DependencyResolver dependencyResolver,
51             final MessageBusAppImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
52         super(identifier, dependencyResolver, oldModule, oldInstance);
53     }
54
55     @Override
56     protected void customValidation() {
57     }
58
59     @Override
60     public java.lang.AutoCloseable createInstance() {
61
62         final ProviderContext bindingCtx = getBindingBrokerDependency().registerProvider(new Providers.BindingAware());
63         final ProviderSession domCtx = getDomBrokerDependency().registerProvider(new Providers.BindingIndependent());
64         final DataBroker dataBroker = bindingCtx.getSALService(DataBroker.class);
65         final DOMNotificationPublishService domPublish = domCtx.getService(DOMNotificationPublishService.class);
66         final DOMMountPointService domMount = domCtx.getService(DOMMountPointService.class);
67         final RpcProviderRegistry rpcRegistry = bindingCtx.getSALService(RpcProviderRegistry.class);
68         final MountPointService mountPointService = bindingCtx.getSALService(MountPointService.class);
69         final EventSourceRegistryWrapper eventSourceRegistryWrapper = new EventSourceRegistryWrapper(new EventSourceTopology(dataBroker, rpcRegistry));
70 //        final NetconfEventSourceManager netconfEventSourceManager
71 //                = NetconfEventSourceManager.create(dataBroker,
72 //                        domPublish,
73 //                        domMount,
74 //                        mountPointService,
75 //                        eventSourceRegistryWrapper,
76 //                        getNamespaceToStream());
77 //        eventSourceRegistryWrapper.addAutoCloseable(netconfEventSourceManager);
78         LOGGER.info("Messagebus initialized");
79         return eventSourceRegistryWrapper;
80
81     }
82
83     //TODO: separate NetconfEventSource into separate bundle, remove this wrapper, return EventSourceTopology directly as EventSourceRegistry
84     private class EventSourceRegistryWrapper implements EventSourceRegistry{
85
86         private final EventSourceRegistry baseEventSourceRegistry;
87         private final Set<AutoCloseable> autoCloseables = new HashSet<>();
88
89         public EventSourceRegistryWrapper(EventSourceRegistry baseEventSourceRegistry) {
90             this.baseEventSourceRegistry = baseEventSourceRegistry;
91         }
92
93         public void addAutoCloseable(AutoCloseable ac){
94             Preconditions.checkNotNull(ac);
95             autoCloseables.add(ac);
96         }
97
98         @Override
99         public void close() throws Exception {
100             for(AutoCloseable ac : autoCloseables){
101                 ac.close();
102             }
103             baseEventSourceRegistry.close();
104         }
105
106         @Override
107         public <T extends EventSource> EventSourceRegistration<T> registerEventSource(T eventSource) {
108             return this.baseEventSourceRegistry.registerEventSource(eventSource);
109         }
110
111     }
112 }