Bug 3148 - Fixed binding.api.NotificationListener support 90/20090/2
authorTony Tkacik <ttkacik@cisco.com>
Thu, 7 May 2015 17:24:15 +0000 (19:24 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 26 May 2015 08:43:24 +0000 (08:43 +0000)
Change-Id: I5017e502a23f9cdb7d35dc393e84db7c2989f491
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
(cherry picked from commit 9ca4deb2c87a332baefb4b58e264d544362b30f5)

opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/binding/impl/NotificationBrokerImplModule.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/compat/FunctionalNotificationListenerAdapter.java [new file with mode: 0644]
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/compat/HeliumNotificationProviderServiceWithInterestListeners.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationServiceAdapter.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazySerializedDOMNotification.java

index cabdd15..ec4bc76 100644 (file)
@@ -13,6 +13,7 @@ import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProvi
 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceWithInterestListeners;
 import org.opendaylight.controller.md.sal.binding.compat.HydrogenNotificationBrokerImpl;
 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
+import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
@@ -61,12 +62,18 @@ public final class NotificationBrokerImplModule extends
 
     private static AutoCloseable createHeliumAdapter(final NotificationPublishService publishService,
             final NotificationService listenService) {
-        if(publishService instanceof BindingDOMNotificationPublishServiceAdapter) {
-            final BindingDOMNotificationPublishServiceAdapter casted = (BindingDOMNotificationPublishServiceAdapter) publishService;
-            final DOMNotificationPublishService domService = casted.getDomPublishService();
-            if(domService instanceof DOMNotificationSubscriptionListenerRegistry) {
-                final DOMNotificationSubscriptionListenerRegistry subsRegistry = (DOMNotificationSubscriptionListenerRegistry) domService;
-                return new HeliumNotificationProviderServiceWithInterestListeners(publishService, listenService, casted.getCodecRegistry(), subsRegistry);
+        if (listenService instanceof BindingDOMNotificationServiceAdapter
+                && publishService instanceof BindingDOMNotificationPublishServiceAdapter) {
+            final BindingDOMNotificationPublishServiceAdapter castedPublish =
+                    (BindingDOMNotificationPublishServiceAdapter) publishService;
+            final BindingDOMNotificationServiceAdapter castedListen =
+                    (BindingDOMNotificationServiceAdapter) listenService;
+            final DOMNotificationPublishService domPublishService = castedPublish.getDomPublishService();
+            if (domPublishService instanceof DOMNotificationSubscriptionListenerRegistry) {
+                final DOMNotificationSubscriptionListenerRegistry subsRegistry =
+                        (DOMNotificationSubscriptionListenerRegistry) domPublishService;
+                return new HeliumNotificationProviderServiceWithInterestListeners(castedPublish, castedListen,
+                        subsRegistry);
             }
         }
         return new HeliumNotificationProviderServiceAdapter(publishService, listenService);
diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/compat/FunctionalNotificationListenerAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/compat/FunctionalNotificationListenerAdapter.java
new file mode 100644 (file)
index 0000000..94c2197
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.compat;
+
+import javax.annotation.Nonnull;
+import org.opendaylight.controller.md.sal.binding.impl.LazySerializedDOMNotification;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
+import org.opendaylight.controller.sal.binding.api.NotificationListener;
+import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+final class FunctionalNotificationListenerAdapter<N extends Notification> implements DOMNotificationListener {
+
+    private final BindingNormalizedNodeSerializer codec;
+    private final NotificationListener<N> delegate;
+    private final Class<N> type;
+
+    public FunctionalNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final Class<N> type, final NotificationListener<N> delegate) {
+        this.codec = codec;
+        this.type = type;
+        this.delegate = delegate;
+    }
+
+    @Override
+    public void onNotification(@Nonnull final DOMNotification notification) {
+        delegate.onNotification( type.cast(deserialize(notification)));
+    }
+
+    private Notification deserialize(final DOMNotification notification) {
+        if(notification instanceof LazySerializedDOMNotification) {
+            return ((LazySerializedDOMNotification) notification).getBindingData();
+        }
+        return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
+    }
+}
index 2ccc4c1..1eb421d 100644 (file)
@@ -11,14 +11,18 @@ import com.google.common.collect.Sets;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Set;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
+import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
+import org.opendaylight.controller.sal.binding.api.NotificationListener;
+import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.util.ListenerRegistry;
 import org.opendaylight.yangtools.yang.binding.Notification;
+import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -29,13 +33,15 @@ public class HeliumNotificationProviderServiceWithInterestListeners extends Heli
 
     private final ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create();
     private final ListenerRegistration<Listener> domListener;
+    private final DOMNotificationService domService;
     private final BindingToNormalizedNodeCodec codec;
 
     public HeliumNotificationProviderServiceWithInterestListeners(
-            final NotificationPublishService publishService, final NotificationService listenService, final BindingToNormalizedNodeCodec codec, final DOMNotificationSubscriptionListenerRegistry registry) {
+            final BindingDOMNotificationPublishServiceAdapter publishService, final BindingDOMNotificationServiceAdapter listenService, final DOMNotificationSubscriptionListenerRegistry registry) {
         super(publishService, listenService);
-        this.codec = codec;
+        this.codec = publishService.getCodecRegistry();
         this.domListener = registry.registerSubscriptionListener(new Listener());
+        this.domService = listenService.getDomService();
     }
 
     @Override
@@ -64,6 +70,22 @@ public class HeliumNotificationProviderServiceWithInterestListeners extends Heli
         }
     }
 
+    @Override
+    public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
+            final Class<T> type, final NotificationListener<T> listener) {
+
+        final FunctionalNotificationListenerAdapter<T> adapter = new FunctionalNotificationListenerAdapter<>(codec, type, listener);
+        final SchemaPath domType = SchemaPath.create(true, BindingReflections.findQName(type));
+        final ListenerRegistration<?> domReg = domService.registerNotificationListener(adapter, domType);
+        return new AbstractListenerRegistration<NotificationListener<T>>(listener) {
+            @Override
+            protected void removeRegistration() {
+                domReg.close();
+            }
+
+        };
+    }
+
     private void notifyListener(final NotificationInterestListener listener, final Set<Class<? extends Notification>> baEvent) {
         for(final Class<? extends Notification> event: baEvent) {
             listener.onNotificationSubscribtion(event);
index e0cc4d4..07da99b 100644 (file)
@@ -21,7 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  * of notification actually accessed data from notification.
  *
  */
-class LazySerializedDOMNotification implements DOMNotification {
+public final class LazySerializedDOMNotification implements DOMNotification {
 
     private final BindingNormalizedNodeSerializer codec;
     private final Notification data;
@@ -54,7 +54,7 @@ class LazySerializedDOMNotification implements DOMNotification {
         return domBody;
     }
 
-    protected Notification getBindingData() {
+    public Notification getBindingData() {
         return data;
     }
 }

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