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 org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
28 import org.onap.policy.common.endpoints.utils.PropertyUtils.TriConsumer;
29 import org.onap.policy.common.utils.coder.StandardCoderObject;
30 import org.onap.policy.controlloop.actorserviceprovider.Util;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Forwarder that selectively forwards message to listeners based on the content of the
36 * message. Each forwarder is associated with a single set of selector keys. Listeners are
37 * then registered with that forwarder for a particular set of values for the given keys.
39 public class Forwarder {
40 private static final Logger logger = LoggerFactory.getLogger(Forwarder.class);
43 * Maps a set of field values to one or more listeners.
46 private final Map<List<String>, Map<TriConsumer<CommInfrastructure, String, StandardCoderObject>, String>>
47 values2listeners = new ConcurrentHashMap<>();
51 * Keys used to extract the field values from the {@link StandardCoderObject}.
53 private final List<SelectorKey> keys;
56 * Constructs the object.
58 * @param keys keys used to extract the field's value from the
59 * {@link StandardCoderObject}
61 public Forwarder(List<SelectorKey> keys) {
66 * Registers a listener for messages containing the given field values.
68 * @param values field values of interest, in one-to-one correspondence with the keys
69 * @param listener listener to register
71 public void register(List<String> values, TriConsumer<CommInfrastructure, String, StandardCoderObject> listener) {
72 if (keys.size() != values.size()) {
73 throw new IllegalArgumentException("key/value mismatch");
76 values2listeners.compute(values, (key, listeners) -> {
77 Map<TriConsumer<CommInfrastructure, String, StandardCoderObject>, String> map = listeners;
79 map = new ConcurrentHashMap<>();
82 map.put(listener, "");
88 * Unregisters a listener for messages containing the given field values.
90 * @param values field values of interest, in one-to-one correspondence with the keys
91 * @param listener listener to unregister
93 public void unregister(List<String> values, TriConsumer<CommInfrastructure, String, StandardCoderObject> listener) {
94 values2listeners.computeIfPresent(values, (key, listeners) -> {
95 listeners.remove(listener);
96 return (listeners.isEmpty() ? null : listeners);
101 * Processes a message, forwarding it to the appropriate listeners, if any.
103 * @param infra communication infrastructure on which the response was received
104 * @param textMessage original text message that was received
105 * @param scoMessage decoded text message
107 public void onMessage(CommInfrastructure infra, String textMessage, StandardCoderObject scoMessage) {
108 // extract the key values from the message
109 List<String> values = new ArrayList<>(keys.size());
110 for (SelectorKey key : keys) {
111 String value = key.extractField(scoMessage);
114 * No value for this field, so this message is not relevant to this
123 // get the listeners for this set of values
124 Map<TriConsumer<CommInfrastructure, String, StandardCoderObject>, String> listeners =
125 values2listeners.get(values);
126 if (listeners == null) {
127 // no listeners for this particular list of values
132 // forward the message to each listener
133 for (TriConsumer<CommInfrastructure, String, StandardCoderObject> listener : listeners.keySet()) {
135 listener.accept(infra, textMessage, scoMessage);
136 } catch (RuntimeException e) {
137 logger.warn("exception thrown by listener {}", Util.ident(listener), e);