841ea550bc49a414ae148f15ed0e366c7dd63a84
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / connect / dom / DomToBindingNotificationForwarder.java
1 package org.opendaylight.controller.sal.binding.impl.connect.dom;
2
3 import java.lang.ref.WeakReference;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Set;
7 import java.util.concurrent.ConcurrentHashMap;
8 import java.util.concurrent.ConcurrentMap;
9 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
10 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
11 import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
12 import org.opendaylight.yangtools.yang.binding.DataContainer;
13 import org.opendaylight.yangtools.yang.binding.Notification;
14 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
18
19 class DomToBindingNotificationForwarder implements NotificationProviderService.NotificationInterestListener,
20     NotificationListener {
21
22     private final ConcurrentMap<QName, WeakReference<Class<? extends Notification>>> notifications = new ConcurrentHashMap<>();
23     private final Set<QName> supportedNotifications = new HashSet<>();
24
25     private final BindingIndependentMappingService mappingService;
26     private final NotificationProviderService baNotifyService;
27     private final NotificationPublishService domNotificationService;
28
29     DomToBindingNotificationForwarder(final BindingIndependentMappingService mappingService, final NotificationProviderService baNotifyService,
30         final NotificationPublishService domNotificationService) {
31         this.mappingService = mappingService;
32         this.baNotifyService = baNotifyService;
33         this.domNotificationService = domNotificationService;
34     }
35
36     @Override
37     public Set<QName> getSupportedNotifications() {
38         return Collections.unmodifiableSet(supportedNotifications);
39     }
40
41     @Override
42     public void onNotification(final CompositeNode notification) {
43         QName qname = notification.getNodeType();
44         WeakReference<Class<? extends Notification>> potential = notifications.get(qname);
45         if (potential != null) {
46             Class<? extends Notification> potentialClass = potential.get();
47             if (potentialClass != null) {
48                 final DataContainer baNotification = mappingService.dataObjectFromDataDom(potentialClass,
49                     notification);
50
51                 if (baNotification instanceof Notification) {
52                     baNotifyService.publish((Notification) baNotification);
53                 }
54             }
55         }
56     }
57
58     @Override
59     public void onNotificationSubscribtion(final Class<? extends Notification> notificationType) {
60         QName qname = BindingReflections.findQName(notificationType);
61         if (qname != null) {
62             WeakReference<Class<? extends Notification>> already = notifications.putIfAbsent(qname,
63                 new WeakReference<Class<? extends Notification>>(notificationType));
64             if (already == null) {
65                 domNotificationService.addNotificationListener(qname, this);
66                 supportedNotifications.add(qname);
67             }
68         }
69     }
70 }

©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.