From 0f518a7ba2c8ceb3b4e9d038faca7c9b315db7d4 Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Tue, 12 May 2015 10:36:26 +0200 Subject: [PATCH] BUG-3183: Extend notification publisher API - offer methods of notificationPublishService now return ListenableFuture - added exception for notification rejected outcome Change-Id: Iff17e1edd754d49e6c53f97e357c34f5eac6a8ef Signed-off-by: Michal Rehak (cherry picked from commit 38c80b0fc0c559aabd0a8dce59dd99deab1c3418) --- .../api/NotificationPublishService.java | 26 +++++++++++++----- .../api/NotificationRejectedException.java | 27 +++++++++++++++++++ ...gDOMNotificationPublishServiceAdapter.java | 17 +++++++----- .../ForwardedNotificationAdapterTest.java | 19 +++++++++++-- 4 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationRejectedException.java diff --git a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationPublishService.java b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationPublishService.java index 87e37ffed1..5ec51269b7 100644 --- a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationPublishService.java +++ b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationPublishService.java @@ -7,6 +7,8 @@ */ package org.opendaylight.controller.md.sal.binding.api; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import java.util.concurrent.TimeUnit; import org.opendaylight.yangtools.yang.binding.Notification; @@ -26,6 +28,13 @@ import org.opendaylight.yangtools.yang.binding.Notification; * notification has or has not been seen. */ public interface NotificationPublishService extends BindingService { + + /** + * Well-known value indicating that the binding-aware implementation is currently not + * able to accept a notification. + */ + ListenableFuture REJECTED = Futures.immediateFailedFuture(new NotificationRejectedException("Rejected due to resource constraints.")); + /** * Publishes a notification to subscribed listeners. This initiates * the process of sending the notification, but delivery to the @@ -47,14 +56,16 @@ public interface NotificationPublishService extends BindingService { * listeners can happen asynchronously, potentially after a call to * this method returns. * - * This method is guaranteed not to block. + * Still guaranteed not to block. Returns Listenable Future which will complete once. * * @param notification * the notification to publish. - * @return true if the notification was accepted for processing, false otherwise + * @return A listenable future which will report completion when the service has finished + * propagating the notification to its immediate registrants, or {@value #REJECTED} if resource + * constraints prevent * @throws NullPointerException if the notification is null */ - boolean offerNotification(Notification notification); + ListenableFuture offerNotification(Notification notification); /** * Publishes a notification to subscribed listeners. This initiates @@ -68,11 +79,14 @@ public interface NotificationPublishService extends BindingService { * @param timeout how long to wait before giving up, in units of unit * @param unit a TimeUnit determining how to interpret the * timeout parameter - * @return true if the notification was accepted for processing, false otherwise + * @return A listenable future which will report completion when the service has finished + * propagating the notification to its immediate registrants, or {@value #REJECTED} if resource + * constraints prevent * @throws InterruptedException if interrupted while waiting * @throws NullPointerException if the notification or unit is null * @throws IllegalArgumentException if timeout is negative. */ - boolean offerNotification(Notification notification, int timeout, TimeUnit unit) - throws InterruptedException; + ListenableFuture offerNotification(Notification notification, int timeout, TimeUnit unit) + throws InterruptedException; + } diff --git a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationRejectedException.java b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationRejectedException.java new file mode 100644 index 0000000000..4423d79507 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/NotificationRejectedException.java @@ -0,0 +1,27 @@ +/* + * 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.api; + +/** + *

+ * This exception indicates that given notification can not be processed by corresponding mechanism. + * More info can be provided in message. + *

+ *

+ * Expected use: {@link NotificationPublishService} + *

+ */ +public class NotificationRejectedException extends Exception { + + private static final long serialVersionUID = 3722954527834860394L; + + public NotificationRejectedException(String message) { + super(message); + } +} diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java index 084f423807..1fe81ea3f4 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java @@ -52,16 +52,19 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification } @Override - public boolean offerNotification(final Notification notification) { - final ListenableFuture listenableFuture = domPublishService.offerNotification(toDomNotification(notification)); - return !DOMNotificationPublishService.REJECTED.equals(listenableFuture); + public ListenableFuture offerNotification(final Notification notification) { + ListenableFuture offerResult = domPublishService.offerNotification(toDomNotification(notification)); + return DOMNotificationPublishService.REJECTED.equals(offerResult) + ? NotificationPublishService.REJECTED + : offerResult; } @Override - public boolean offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException { - final ListenableFuture listenableFuture = - domPublishService.offerNotification(toDomNotification(notification), timeout, unit); - return !DOMNotificationPublishService.REJECTED.equals(listenableFuture); + public ListenableFuture offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException { + ListenableFuture offerResult = domPublishService.offerNotification(toDomNotification(notification), timeout, unit); + return DOMNotificationPublishService.REJECTED.equals(offerResult) + ? NotificationPublishService.REJECTED + : offerResult; } private DOMNotification toDomNotification(final Notification notification) { diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/impl/test/ForwardedNotificationAdapterTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/impl/test/ForwardedNotificationAdapterTest.java index c2cdc0caaa..cf7ad16ad3 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/impl/test/ForwardedNotificationAdapterTest.java +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/impl/test/ForwardedNotificationAdapterTest.java @@ -8,6 +8,7 @@ package org.opendaylight.controller.md.sal.binding.impl.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; @@ -15,8 +16,12 @@ import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Assert; import org.junit.Test; +import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged; @@ -26,9 +31,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controll import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.YangModuleInfo; import org.opendaylight.yangtools.yang.binding.util.BindingReflections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest { + private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class); + @Override protected Iterable getModuleInfos() throws Exception { return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class)); @@ -66,7 +75,12 @@ public class ForwardedNotificationAdapterTest extends AbstractNotificationBroker final TestNotifListener testNotifListener = new TestNotifListener(latch); final ListenerRegistration listenerRegistration = getNotificationService() .registerNotificationListener(testNotifListener); - assertTrue(getNotificationPublishService().offerNotification(testData)); + try { + getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS); + } catch (ExecutionException | TimeoutException e) { + LOG.error("Notification delivery failed", e); + Assert.fail("notification should be delivered"); + } latch.await(); assertTrue(testNotifListener.getReceivedNotifications().size() == 1); @@ -83,7 +97,8 @@ public class ForwardedNotificationAdapterTest extends AbstractNotificationBroker final TestNotifListener testNotifListener = new TestNotifListener(latch); final ListenerRegistration listenerRegistration = getNotificationService() .registerNotificationListener(testNotifListener); - assertTrue(getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS)); + assertNotSame(NotificationPublishService.REJECTED, + getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS)); latch.await(); assertTrue(testNotifListener.getReceivedNotifications().size() == 1); -- 2.36.6