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 cabdd15513e3dd89144029c3b515c2d1ed80dbce..ec4bc76f554f3a8fb7bcff890fc1c047743b7057 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 2ccc4c13c4169a2228ad4bbba5151051cf260d9c..1eb421de7c7ef249a363cfd567eb1b4d4a4bc6df 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 2a31d34d016a3e86d116b9c5e9c1f57fb775bd1f..33eb9ee8aa4edb4691dbe4c8b485c38a6e0a01a0 100644 (file)
@@ -78,4 +78,8 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService
             return ImmutableSet.of(DOMNotificationService.class);
         }
     }
+
+    public DOMNotificationService getDomService() {
+        return domNotifService;
+    }
 }
index e0cc4d4fa03f5e48dddea168cfbd9e715a828771..07da99bb59159346348696c94548ba83d7836897 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;
     }
 }