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.common.endpoints.listeners;
23 import java.util.concurrent.ConcurrentHashMap;
24 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
25 import org.onap.policy.common.utils.coder.StandardCoderObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Dispatches standard objects to listeners, based on the message type extracted from the
31 * message. Only one listener may be registered for a given type.
33 public class MessageTypeDispatcher extends JsonListener {
34 private static final Logger logger = LoggerFactory.getLogger(MessageTypeDispatcher.class);
37 * Name of the message field, which may be hierarchical.
39 private final Object[] messageFieldNames;
42 * Name of the message field, joined with "." - for logging.
44 private final String fullMessageFieldName;
47 * Maps a message type to its listener.
49 private final ConcurrentHashMap<String, ScoListener<?>> type2listener = new ConcurrentHashMap<>();
52 * Constructs the object.
54 * @param messageFieldNames name of the message field, which may be hierarchical
56 public MessageTypeDispatcher(String... messageFieldNames) {
57 this.messageFieldNames = messageFieldNames;
58 this.fullMessageFieldName = String.join(".", messageFieldNames);
62 * Registers a listener for a certain type of message.
64 * @param type type of message of interest to the listener
65 * @param listener listener to register
67 public <T> void register(String type, ScoListener<T> listener) {
68 type2listener.put(type, listener);
72 * Unregisters the listener associated with the specified message type.
74 * @param type type of message whose listener is to be unregistered
76 public void unregister(String type) {
77 type2listener.remove(type);
81 public void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco) {
82 // extract the message type
83 final String type = sco.getString(messageFieldNames);
85 logger.warn("unable to extract {}: {}", fullMessageFieldName, sco);
89 // dispatch the message
90 ScoListener<?> listener = type2listener.get(type);
91 if (listener == null) {
92 logger.info("discarding event of type {}", type);
96 listener.onTopicEvent(infra, topic, sco);