6574d408103e29049fc82a1089d05933102ad502
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.common.endpoints.event.comm.bus.internal;
20
21 import java.util.Map;
22 import org.onap.policy.common.endpoints.event.comm.Topic;
23 import org.onap.policy.common.endpoints.event.comm.bus.KafkaTopicSink;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This implementation publishes events for the associated KAFKA topic, inline with the calling
29  * thread.
30  */
31 public class InlineKafkaTopicSink extends InlineBusTopicSink implements KafkaTopicSink {
32
33     /**
34      * Logger.
35      */
36     private static Logger logger = LoggerFactory.getLogger(InlineKafkaTopicSink.class);
37
38     protected Map<String, String> additionalProps = null;
39
40     /**
41      * Argument-based KAFKA Topic Writer instantiation. BusTopicParams contains below mentioned
42      * attributes.
43      *
44      * <p>servers              list of KAFKA servers available for publishing
45      * topic                the topic to publish to
46      * partitionId          the partition key (optional, autogenerated if not provided)
47      * useHttps             does connection use HTTPS?
48      * @param busTopicParams contains attributes needed
49      * @throws IllegalArgumentException if invalid arguments are detected
50      */
51     public InlineKafkaTopicSink(BusTopicParams busTopicParams) {
52         super(busTopicParams);
53         this.additionalProps = busTopicParams.getAdditionalProps();
54     }
55
56     /**
57      * Instantiation of internal resources.
58      */
59     @Override
60     public void init() {
61
62         this.publisher = new BusPublisher.KafkaPublisherWrapper(BusTopicParams.builder()
63                 .servers(this.servers)
64                 .topic(this.effectiveTopic)
65                 .useHttps(this.useHttps)
66                 .additionalProps(this.additionalProps)
67                 .build());
68         logger.info("{}: KAFKA SINK created", this);
69     }
70
71     @Override
72     public String toString() {
73         return "InlineKafkaTopicSink [getTopicCommInfrastructure()=" + getTopicCommInfrastructure() + ", toString()="
74                         + super.toString() + "]";
75     }
76
77     @Override
78     public CommInfrastructure getTopicCommInfrastructure() {
79         return Topic.CommInfrastructure.KAFKA;
80     }
81 }