1 package org.opendaylight.controller.sal.binding.impl.connect.dom;
3 import java.lang.ref.WeakReference;
4 import java.util.Collections;
5 import java.util.HashSet;
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;
19 class DomToBindingNotificationForwarder implements NotificationProviderService.NotificationInterestListener,
20 NotificationListener {
22 private final ConcurrentMap<QName, WeakReference<Class<? extends Notification>>> notifications = new ConcurrentHashMap<>();
23 private final Set<QName> supportedNotifications = new HashSet<>();
25 private final BindingIndependentMappingService mappingService;
26 private final NotificationProviderService baNotifyService;
27 private final NotificationPublishService domNotificationService;
29 DomToBindingNotificationForwarder(final BindingIndependentMappingService mappingService, final NotificationProviderService baNotifyService,
30 final NotificationPublishService domNotificationService) {
31 this.mappingService = mappingService;
32 this.baNotifyService = baNotifyService;
33 this.domNotificationService = domNotificationService;
37 public Set<QName> getSupportedNotifications() {
38 return Collections.unmodifiableSet(supportedNotifications);
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,
51 if (baNotification instanceof Notification) {
52 baNotifyService.publish((Notification) baNotification);
59 public void onNotificationSubscribtion(final Class<? extends Notification> notificationType) {
60 QName qname = BindingReflections.findQName(notificationType);
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);