Merge "Startup archetype: Add basic unit tests for impl."
[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
19 /**
20  * A single notification event in the disruptor ringbuffer. These objects are reused,
21  * so they do have mutable state.
22  */
23 final class DOMNotificationRouterEvent {
24     public static final EventFactory<DOMNotificationRouterEvent> FACTORY = new EventFactory<DOMNotificationRouterEvent>() {
25         @Override
26         public DOMNotificationRouterEvent newInstance() {
27             return new DOMNotificationRouterEvent();
28         }
29     };
30
31     private Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers;
32     private DOMNotification notification;
33     private SettableFuture<Void> future;
34
35     private DOMNotificationRouterEvent() {
36         // Hidden on purpose, initialized in initialize()
37     }
38
39     ListenableFuture<Void> initialize(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
40         this.notification = Preconditions.checkNotNull(notification);
41         this.subscribers = Preconditions.checkNotNull(subscribers);
42         this.future = SettableFuture.create();
43         return this.future;
44     }
45
46     void deliverNotification() {
47         for (ListenerRegistration<? extends DOMNotificationListener> r : subscribers) {
48             final DOMNotificationListener l = r.getInstance();
49             if (l != null) {
50                 l.onNotification(notification);
51             }
52         }
53     }
54
55     void setFuture() {
56         future.set(null);
57     }
58
59 }