2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.event.comm.client;
24 import java.util.List;
26 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
27 import org.onap.policy.common.endpoints.event.comm.TopicSink;
28 import org.onap.policy.common.utils.coder.Coder;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Client for sending messages to a Topic using TopicSink.
37 public class TopicSinkClient {
38 private static final Logger logger = LoggerFactory.getLogger(TopicSinkClient.class);
41 * Coder used to encode messages being sent to the topic.
43 private static final Coder CODER = new StandardCoder();
46 * Topic to which messages are published.
49 private final String topic;
52 * Where messages are published.
54 private final TopicSink sink;
57 * Constructs the object.
59 * @param topic topic to which messages should be published
60 * @throws TopicSinkClientException if the topic does not exist
62 public TopicSinkClient(final String topic) throws TopicSinkClientException {
65 final List<TopicSink> lst = getTopicSinks(topic);
67 throw new TopicSinkClientException("no sinks for topic: " + topic);
70 this.sink = lst.get(0);
74 * Sends a message to the topic, after encoding the message as json.
76 * @param message message to be encoded and sent
77 * @return {@code true} if the message was successfully sent/enqueued, {@code false} otherwise
79 public boolean send(final Object message) {
81 final String json = CODER.encode(message);
82 return sink.send(json);
84 } catch (RuntimeException | CoderException e) {
85 logger.warn("send to {} failed because of {}", topic, e.getMessage(), e);
90 // the remaining methods are wrappers that can be overridden by junit tests
93 * Gets the sinks for a given topic.
95 * @param topic the topic of interest
96 * @return the sinks for the topic
98 protected List<TopicSink> getTopicSinks(final String topic) {
99 return TopicEndpoint.manager.getTopicSinks(topic);