2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.comm;
23 import java.util.Collection;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Listener for PDP Status messages expected to be received from multiple PDPs. The
38 * listener "completes" once a message has been seen from all of the PDPs.
40 public abstract class MultiPdpStatusListener implements TypedMessageListener<PdpStatus> {
41 private static final Logger logger = LoggerFactory.getLogger(MultiPdpStatusListener.class);
44 * This is decremented once a message has been received from every PDP.
46 private final CountDownLatch allSeen = new CountDownLatch(1);
49 * IDs for which no message has been received yet.
51 private final Set<String> unseenIds = ConcurrentHashMap.newKeySet();
54 * Constructs the object.
56 * @param id ID for which to wait
58 public MultiPdpStatusListener(String id) {
63 * Constructs the object.
65 * @param ids IDs for which to wait
67 public MultiPdpStatusListener(Collection<String> ids) {
72 unseenIds.addAll(ids);
77 * Gets the set of IDs for which messages have not yet been received.
79 * @return the IDs that have not been seen yet
81 public SortedSet<String> getUnseenIds() {
82 return new TreeSet<>(unseenIds);
86 * Waits for messages to be received for all PDPs, or until a timeout is reached.
88 * @param timeout the maximum time to wait
89 * @param unit the time unit of the timeout argument
90 * @return {@code true} if messages were received for all PDPs, {@code false} if the
91 * timeout was reached first
92 * @throws InterruptedException if the current thread is interrupted while waiting
94 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
95 return allSeen.await(timeout, unit);
99 * After giving the event to the subclass via
100 * {@link #handleEvent(CommInfrastructure, String, PdpStatus)}, this triggers
101 * completion of {@link #await(long, TimeUnit)}, if all PDPs have received a message.
104 public final void onTopicEvent(CommInfrastructure infra, String topic, PdpStatus message) {
107 id = handleEvent(infra, topic, message);
108 } catch (RuntimeException e) {
109 logger.warn("handleEvent failed due to: {}", e.getMessage(), e);
116 unseenIds.remove(id);
118 if (unseenIds.isEmpty()) {
124 * Indicates that a message was received for a PDP.
126 * @param infra infrastructure with which the message was received
127 * @param topic topic on which the message was received
128 * @param message message that was received
129 * @return the ID extracted from the message, or {@code null} if it cannot be extracted
131 protected abstract String handleEvent(CommInfrastructure infra, String topic, PdpStatus message);