3188b8d45671ae74073e60281ab32f76821c579c
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.client;
23
24
25 import java.util.List;
26
27 import lombok.Getter;
28
29 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
30 import org.onap.policy.common.endpoints.event.comm.TopicSink;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Client for sending messages to a Topic using TopicSink.
39  */
40 public class TopicSinkClient {
41     private static final Logger logger = LoggerFactory.getLogger(TopicSinkClient.class);
42
43     /**
44      * Coder used to encode messages being sent to the topic.
45      */
46     private static final Coder CODER = new StandardCoder();
47
48     /**
49      * Topic to which messages are published.
50      */
51     @Getter
52     private final String topic;
53
54     /**
55      * Where messages are published.
56      */
57     private final TopicSink sink;
58
59     /**
60      * Constructs the object.
61      *
62      * @param topic topic to which messages should be published
63      * @throws TopicSinkClientException if the topic does not exist
64      */
65     public TopicSinkClient(final String topic) throws TopicSinkClientException {
66         this.topic = topic;
67
68         final List<TopicSink> lst = getTopicSinks(topic);
69         if (lst.isEmpty()) {
70             throw new TopicSinkClientException("no sinks for topic: " + topic);
71         }
72
73         this.sink = lst.get(0);
74     }
75
76     /**
77      * Sends a message to the topic, after encoding the message as json.
78      *
79      * @param message message to be encoded and sent
80      * @return {@code true} if the message was successfully sent/enqueued, {@code false} otherwise
81      */
82     public boolean send(final Object message) {
83         try {
84             final String json = CODER.encode(message);
85             return sink.send(json);
86
87         } catch (RuntimeException | CoderException e) {
88             logger.warn("send to {} failed because of {}", topic, e.getMessage(), e);
89             return false;
90         }
91     }
92
93     // the remaining methods are wrappers that can be overridden by junit tests
94
95     /**
96      * Gets the sinks for a given topic.
97      *
98      * @param topic the topic of interest
99      * @return the sinks for the topic
100      */
101     protected List<TopicSink> getTopicSinks(final String topic) {
102         return TopicEndpoint.manager.getTopicSinks(topic);
103     }
104 }