Merge changes I114cbac1,I45c2e7cd
[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 MountPointService bindingMount = bindingCtx.getSALService(MountPointService.class);
68         final RpcProviderRegistry rpcRegistry = bindingCtx.getSALService(RpcProviderRegistry.class);
69
70         final EventSourceRegistryWrapper eventSourceRegistryWrapper = new EventSourceRegistryWrapper(new EventSourceTopology(dataBroker, rpcRegistry));
71         final NetconfEventSourceManager netconfEventSourceManager = NetconfEventSourceManager.create(dataBroker, domPublish,domMount, bindingMount, eventSourceRegistryWrapper, getNamespaceToStream());
72         eventSourceRegistryWrapper.addAutoCloseable(netconfEventSourceManager);
73         LOGGER.info("Messagebus initialized");
74         return eventSourceRegistryWrapper;
75
76     }
77
78     //TODO: separate NetconfEventSource into separate bundle, remove this wrapper, return EventSourceTopology directly as EventSourceRegistry
79     private class EventSourceRegistryWrapper implements EventSourceRegistry{
80
81         private final EventSourceRegistry baseEventSourceRegistry;
82         private final Set<AutoCloseable> autoCloseables = new HashSet<>();
83
84         public EventSourceRegistryWrapper(EventSourceRegistry baseEventSourceRegistry) {
85             this.baseEventSourceRegistry = baseEventSourceRegistry;
86         }
87
88         public void addAutoCloseable(AutoCloseable ac){
89             Preconditions.checkNotNull(ac);
90             autoCloseables.add(ac);
91         }
92
93         @Override
94         public void close() throws Exception {
95             for(AutoCloseable ac : autoCloseables){
96                 ac.close();
97             }
98             baseEventSourceRegistry.close();
99         }
100
101         @Override
102         public <T extends EventSource> EventSourceRegistration<T> registerEventSource(T eventSource) {
103             return this.baseEventSourceRegistry.registerEventSource(eventSource);
104         }
105
106     }
107 }