Expose default module instances into OSGi
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / NotificationBrokerImplModule.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.binding.impl;
9
10 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
11 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
12 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceAdapter;
13 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceWithInterestListeners;
14 import org.opendaylight.controller.md.sal.binding.compat.HydrogenNotificationBrokerImpl;
15 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
16 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
18 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
19 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
20
21 /**
22 *
23 */
24 public final class NotificationBrokerImplModule extends
25         org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractNotificationBrokerImplModule {
26
27     public NotificationBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
28             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
29         super(identifier, dependencyResolver);
30     }
31
32     public NotificationBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
33             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
34             final NotificationBrokerImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
35         super(identifier, dependencyResolver, oldModule, oldInstance);
36     }
37
38     @Override
39     public void validate() {
40         super.validate();
41         // Add custom validation for module attributes here.
42     }
43
44     @Override
45     public java.lang.AutoCloseable createInstance() {
46
47         final NotificationPublishService notificationPublishService = getNotificationPublishAdapterDependency();
48         final NotificationService notificationService = getNotificationAdapterDependency();
49
50         if(notificationPublishService != null & notificationService != null) {
51             return createHeliumAdapter(notificationPublishService,notificationService);
52         }
53
54         /*
55          *  FIXME: Switch to new broker (which has different threading model)
56          *  once this change is communicated with downstream users or
57          *  we will have adapter implementation which will honor Helium
58          *  threading model for notifications.
59          */
60         return new HydrogenNotificationBrokerImpl(SingletonHolder.getDefaultNotificationExecutor());
61     }
62
63     private static AutoCloseable createHeliumAdapter(final NotificationPublishService publishService,
64             final NotificationService listenService) {
65         if (listenService instanceof BindingDOMNotificationServiceAdapter
66                 && publishService instanceof BindingDOMNotificationPublishServiceAdapter) {
67             final BindingDOMNotificationPublishServiceAdapter castedPublish =
68                     (BindingDOMNotificationPublishServiceAdapter) publishService;
69             final BindingDOMNotificationServiceAdapter castedListen =
70                     (BindingDOMNotificationServiceAdapter) listenService;
71             final DOMNotificationPublishService domPublishService = castedPublish.getDomPublishService();
72             if (domPublishService instanceof DOMNotificationSubscriptionListenerRegistry) {
73                 final DOMNotificationSubscriptionListenerRegistry subsRegistry =
74                         (DOMNotificationSubscriptionListenerRegistry) domPublishService;
75                 return new HeliumNotificationProviderServiceWithInterestListeners(castedPublish, castedListen,
76                         subsRegistry);
77             }
78         }
79         return new HeliumNotificationProviderServiceAdapter(publishService, listenService);
80     }
81 }