2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.controlloop.actorserviceprovider.topic;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.function.BiConsumer;
28 import org.onap.policy.common.utils.coder.StandardCoderObject;
29 import org.onap.policy.controlloop.actorserviceprovider.Util;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Forwarder that selectively forwards message to listeners based on the content of the
35 * message. Each forwarder is associated with a single set of selector keys. Listeners are
36 * then registered with that forwarder for a particular set of values for the given keys.
38 public class Forwarder {
39 private static final Logger logger = LoggerFactory.getLogger(Forwarder.class);
42 * Maps a set of field values to one or more listeners.
45 private final Map<List<String>, Map<BiConsumer<String, StandardCoderObject>, String>>
46 values2listeners = new ConcurrentHashMap<>();
50 * Keys used to extract the field values from the {@link StandardCoderObject}.
52 private final List<SelectorKey> keys;
55 * Constructs the object.
57 * @param keys keys used to extract the field's value from the
58 * {@link StandardCoderObject}
60 public Forwarder(List<SelectorKey> keys) {
65 * Registers a listener for messages containing the given field values.
67 * @param values field values of interest, in one-to-one correspondence with the keys
68 * @param listener listener to register
70 public void register(List<String> values, BiConsumer<String, StandardCoderObject> listener) {
71 if (keys.size() != values.size()) {
72 throw new IllegalArgumentException("key/value mismatch");
75 logger.info("register topic listener for key={} value={}", keys, values);
77 values2listeners.compute(values, (key, listeners) -> {
78 Map<BiConsumer<String, StandardCoderObject>, String> map = listeners;
80 map = new ConcurrentHashMap<>();
83 map.put(listener, "");
89 * Unregisters a listener for messages containing the given field values.
91 * @param values field values of interest, in one-to-one correspondence with the keys
92 * @param listener listener to unregister
94 public void unregister(List<String> values, BiConsumer<String, StandardCoderObject> listener) {
95 logger.info("unregister topic listener for key={} value={}", keys, values);
97 values2listeners.computeIfPresent(values, (key, listeners) -> {
98 listeners.remove(listener);
99 return (listeners.isEmpty() ? null : listeners);
104 * Processes a message, forwarding it to the appropriate listeners, if any.
106 * @param textMessage original text message that was received
107 * @param scoMessage decoded text message
109 public void onMessage(String textMessage, StandardCoderObject scoMessage) {
110 // extract the key values from the message
111 List<String> values = new ArrayList<>(keys.size());
112 for (SelectorKey key : keys) {
113 String value = key.extractField(scoMessage);
116 * No value for this field, so this message is not relevant to this
119 logger.info("message has no key={}", keys);
126 // get the listeners for this set of values
127 Map<BiConsumer<String, StandardCoderObject>, String> listeners = values2listeners.get(values);
128 if (listeners == null) {
129 // no listeners for this particular list of values
130 logger.info("no listener registered for key={} value={}", keys, values);
135 // forward the message to each listener
136 logger.info("forwarding message to listeners for key={} value={}", keys, values);
137 for (BiConsumer<String, StandardCoderObject> listener : listeners.keySet()) {
139 listener.accept(textMessage, scoMessage);
140 } catch (RuntimeException e) {
141 logger.warn("exception thrown by listener {}", Util.ident(listener), e);