0367cab3822b2fc5cda94122e1efb538eb74ab78
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMNotificationRouterEvent.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import com.lmax.disruptor.EventFactory;
14 import java.util.Collection;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * A single notification event in the disruptor ringbuffer. These objects are reused,
23  * so they do have mutable state.
24  */
25 final class DOMNotificationRouterEvent {
26     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouterEvent.class);
27     public static final EventFactory<DOMNotificationRouterEvent> FACTORY = new EventFactory<DOMNotificationRouterEvent>() {
28         @Override
29         public DOMNotificationRouterEvent newInstance() {
30             return new DOMNotificationRouterEvent();
31         }
32     };
33
34     private Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers;
35     private DOMNotification notification;
36     private SettableFuture<Void> future;
37
38     private DOMNotificationRouterEvent() {
39         // Hidden on purpose, initialized in initialize()
40     }
41
42     ListenableFuture<Void> initialize(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
43         this.notification = Preconditions.checkNotNull(notification);
44         this.subscribers = Preconditions.checkNotNull(subscribers);
45         this.future = SettableFuture.create();
46         return this.future;
47     }
48
49     void deliverNotification() {
50         LOG.trace("Start delivery of notification {}", notification);
51         for (ListenerRegistration<? extends DOMNotificationListener> r : subscribers) {
52             final DOMNotificationListener listener = r.getInstance();
53             if (listener != null) {
54                 try {
55                     LOG.trace("Notifying listener {}", listener);
56                     listener.onNotification(notification);
57                     LOG.trace("Listener notification completed");
58                 } catch (Exception e) {
59                     LOG.error("Delivery of notification {} caused an error in listener {}", notification, listener, e);
60                 }
61             }
62         }
63         LOG.trace("Delivery completed");
64     }
65
66     void setFuture() {
67         future.set(null);
68         notification = null;
69         subscribers = null;
70         future = null;
71     }
72 }