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