b0dd2a90f7b8d53a1a4c4432d0d7074a1438ff4e
[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 java.util.concurrent.Callable;
11
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;
16
17 import com.google.common.base.Objects;
18 import com.google.common.base.Preconditions;
19
20 class NotifyTask implements Callable<Object> {
21     private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class);
22
23     private final NotificationListener<?> listener;
24     private final Notification notification;
25
26     public NotifyTask(final NotificationListener<?> listener, final Notification notification) {
27         this.listener = Preconditions.checkNotNull(listener);
28         this.notification = Preconditions.checkNotNull(notification);
29     }
30
31     @SuppressWarnings("unchecked")
32     private <T extends Notification> NotificationListener<T> getListener() {
33         return (NotificationListener<T>)listener;
34     }
35
36     @Override
37     public Object call() {
38         if (LOG.isDebugEnabled()) {
39             LOG.debug("Delivering notification {} to {}", notification, listener);
40         } else {
41             LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), listener);
42         }
43
44         try {
45             getListener().onNotification(notification);
46         } catch (final Exception e) {
47             LOG.error("Unhandled exception thrown by listener: {}", listener, e);
48         }
49
50         if (LOG.isDebugEnabled()) {
51             LOG.debug("Notification delivered {} to {}", notification, listener);
52         } else {
53             LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), listener);
54         }
55
56         return null;
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + ((listener== null) ? 0 : listener.hashCode());
64         result = prime * result + ((notification== null) ? 0 : notification.hashCode());
65         return result;
66     }
67
68     @Override
69     public boolean equals(final Object obj) {
70         if (this == obj)
71             return true;
72         if (obj == null)
73             return false;
74         if (getClass() != obj.getClass())
75             return false;
76         NotifyTask other = (NotifyTask) obj;
77         if (listener == null) {
78             if (other.listener != null)
79                 return false;
80         } else if (!listener.equals(other.listener))
81             return false;
82         if (notification == null) {
83             if (other.notification != null)
84                 return false;
85         } else if (!notification.equals(other.notification))
86             return false;
87         return true;
88     }
89
90     @Override
91     public String toString() {
92         return Objects.toStringHelper(this)
93                 .add("listener", listener)
94                 .add("notification", notification.getClass())
95                 .toString();
96     }
97 }