BUG-614: degrade NotifyTask to Runnable
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotifyTask.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.impl;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationListener;
11 import org.opendaylight.yangtools.yang.binding.Notification;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.google.common.base.Objects;
16 import com.google.common.base.Preconditions;
17
18 class NotifyTask implements Runnable {
19     private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class);
20
21     private final NotificationListener<?> listener;
22     private final Notification notification;
23
24     public NotifyTask(final NotificationListener<?> listener, final Notification notification) {
25         this.listener = Preconditions.checkNotNull(listener);
26         this.notification = Preconditions.checkNotNull(notification);
27     }
28
29     @SuppressWarnings("unchecked")
30     private <T extends Notification> NotificationListener<T> getListener() {
31         return (NotificationListener<T>)listener;
32     }
33
34     @Override
35     public void run() {
36         if (LOG.isDebugEnabled()) {
37             LOG.debug("Delivering notification {} to {}", notification, listener);
38         } else {
39             LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), listener);
40         }
41
42         try {
43             getListener().onNotification(notification);
44         } catch (final Exception e) {
45             LOG.error("Unhandled exception thrown by listener: {}", listener, e);
46         }
47
48         if (LOG.isDebugEnabled()) {
49             LOG.debug("Notification delivered {} to {}", notification, listener);
50         } else {
51             LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), listener);
52         }
53     }
54
55     @Override
56     public int hashCode() {
57         final int prime = 31;
58         int result = 1;
59         result = prime * result + ((listener== null) ? 0 : listener.hashCode());
60         result = prime * result + ((notification== null) ? 0 : notification.hashCode());
61         return result;
62     }
63
64     @Override
65     public boolean equals(final Object obj) {
66         if (this == obj)
67             return true;
68         if (obj == null)
69             return false;
70         if (getClass() != obj.getClass())
71             return false;
72         NotifyTask other = (NotifyTask) obj;
73         if (listener == null) {
74             if (other.listener != null)
75                 return false;
76         } else if (!listener.equals(other.listener))
77             return false;
78         if (notification == null) {
79             if (other.notification != null)
80                 return false;
81         } else if (!notification.equals(other.notification))
82             return false;
83         return true;
84     }
85
86     @Override
87     public String toString() {
88         return Objects.toStringHelper(this)
89                 .add("listener", listener)
90                 .add("notification", notification.getClass())
91                 .toString();
92     }
93 }