2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.impl;
10 import java.util.concurrent.Callable;
12 import org.opendaylight.controller.sal.binding.api.NotificationListener;
13 import org.opendaylight.yangtools.yang.binding.Notification;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import com.google.common.base.Objects;
18 import com.google.common.base.Preconditions;
20 class NotifyTask implements Callable<Object> {
21 private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class);
23 private final NotificationListener<?> listener;
24 private final Notification notification;
26 public NotifyTask(final NotificationListener<?> listener, final Notification notification) {
27 this.listener = Preconditions.checkNotNull(listener);
28 this.notification = Preconditions.checkNotNull(notification);
31 @SuppressWarnings("unchecked")
32 private <T extends Notification> NotificationListener<T> getListener() {
33 return (NotificationListener<T>)listener;
37 public Object call() {
38 if (LOG.isDebugEnabled()) {
39 LOG.debug("Delivering notification {} to {}", notification, listener);
41 LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), listener);
45 getListener().onNotification(notification);
46 } catch (final Exception e) {
47 LOG.error("Unhandled exception thrown by listener: {}", listener, e);
50 if (LOG.isDebugEnabled()) {
51 LOG.debug("Notification delivered {} to {}", notification, listener);
53 LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), listener);
60 public int hashCode() {
63 result = prime * result + ((listener== null) ? 0 : listener.hashCode());
64 result = prime * result + ((notification== null) ? 0 : notification.hashCode());
69 public boolean equals(final Object obj) {
74 if (getClass() != obj.getClass())
76 NotifyTask other = (NotifyTask) obj;
77 if (listener == null) {
78 if (other.listener != null)
80 } else if (!listener.equals(other.listener))
82 if (notification == null) {
83 if (other.notification != null)
85 } else if (!notification.equals(other.notification))
91 public String toString() {
92 return Objects.toStringHelper(this)
93 .add("listener", listener)
94 .add("notification", notification.getClass())