1aec614ed05c160f4b161a78f8ead5c44250b7a7
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / AbstractCommonSubscriber.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.rfc8040.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17 import org.checkerframework.checker.lock.qual.GuardedBy;
18 import org.checkerframework.checker.lock.qual.Holding;
19 import org.opendaylight.restconf.nb.rfc8040.streams.StreamSessionHandler;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Features of subscribing part of both notifications.
26  */
27 abstract class AbstractCommonSubscriber extends AbstractQueryParams implements BaseListenerInterface {
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractCommonSubscriber.class);
29
30     @GuardedBy("this")
31     private final Set<StreamSessionHandler> subscribers = new HashSet<>();
32     @GuardedBy("this")
33     private Registration registration;
34
35     @Override
36     public final synchronized boolean hasSubscribers() {
37         return !this.subscribers.isEmpty();
38     }
39
40     @Override
41     public final synchronized Set<StreamSessionHandler> getSubscribers() {
42         return new HashSet<>(this.subscribers);
43     }
44
45     @Override
46     public final synchronized void close() throws InterruptedException, ExecutionException {
47         if (this.registration != null) {
48             this.registration.close();
49             this.registration = null;
50         }
51         deleteDataInDS().get();
52         this.subscribers.clear();
53     }
54
55     @Override
56     public synchronized void addSubscriber(final StreamSessionHandler subscriber) {
57         final boolean isConnected = subscriber.isConnected();
58         Preconditions.checkState(isConnected);
59         LOG.debug("Subscriber {} is added.", subscriber);
60         subscribers.add(subscriber);
61     }
62
63     @Override
64     public synchronized void removeSubscriber(final StreamSessionHandler subscriber) {
65         final boolean isConnected = subscriber.isConnected();
66         Preconditions.checkState(isConnected);
67         LOG.debug("Subscriber {} is removed", subscriber);
68         subscribers.remove(subscriber);
69         if (!hasSubscribers()) {
70             ListenersBroker.getInstance().removeAndCloseListener(this);
71         }
72     }
73
74     /**
75      * Sets {@link Registration} registration.
76      *
77      * @param registration a listener registration registration.
78      */
79     @Holding("this")
80     final void setRegistration(final Registration registration) {
81         this.registration = requireNonNull(registration);
82     }
83
84     /**
85      * Checks if {@link Registration} registration exists.
86      *
87      * @return {@code true} if exists, {@code false} otherwise.
88      */
89     @Holding("this")
90     final boolean isListening() {
91         return registration != null;
92     }
93
94     /**
95      * Post data to subscribed SSE session handlers.
96      *
97      * @param data Data of incoming notifications.
98      */
99     synchronized void post(final String data) {
100         final Iterator<StreamSessionHandler> iterator = subscribers.iterator();
101         while (iterator.hasNext()) {
102             final StreamSessionHandler subscriber = iterator.next();
103             final boolean isConnected = subscriber.isConnected();
104             if (isConnected) {
105                 subscriber.sendDataMessage(data);
106                 LOG.debug("Data was sent to subscriber {} on connection {}:", this, subscriber);
107             } else {
108                 // removal is probably not necessary, because it will be removed explicitly soon after invocation of
109                 // onWebSocketClosed(..) in handler; but just to be sure ...
110                 iterator.remove();
111                 LOG.debug("Subscriber for {} was removed - web-socket session is not open.", this);
112             }
113         }
114     }
115 }