2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2020,2023 Bell Canada. All rights reserved.
8 * Modifications Copyright (C) 2022-2024 Nordix Foundation.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.common.endpoints.event.comm.bus.internal;
26 import com.att.nsa.cambria.client.CambriaClientBuilders;
27 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
28 import com.att.nsa.cambria.client.CambriaConsumer;
29 import io.opentelemetry.api.trace.Span;
30 import io.opentelemetry.api.trace.SpanContext;
31 import io.opentelemetry.api.trace.TraceFlags;
32 import io.opentelemetry.api.trace.TraceState;
33 import io.opentelemetry.context.Context;
34 import io.opentelemetry.instrumentation.kafkaclients.v2_6.TracingConsumerInterceptor;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.nio.charset.StandardCharsets;
38 import java.security.GeneralSecurityException;
39 import java.time.Duration;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Properties;
44 import java.util.concurrent.CountDownLatch;
45 import java.util.concurrent.TimeUnit;
48 import lombok.NoArgsConstructor;
49 import org.apache.kafka.clients.consumer.ConsumerConfig;
50 import org.apache.kafka.clients.consumer.ConsumerRecord;
51 import org.apache.kafka.clients.consumer.ConsumerRecords;
52 import org.apache.kafka.clients.consumer.KafkaConsumer;
53 import org.apache.kafka.clients.consumer.OffsetAndMetadata;
54 import org.apache.kafka.common.TopicPartition;
55 import org.apache.kafka.common.header.Headers;
56 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * Wrapper around libraries to consume from message bus.
63 public interface BusConsumer {
68 * @return list of messages
69 * @throws IOException when error encountered by underlying libraries
71 public Iterable<String> fetch() throws IOException;
74 * close underlying library consumer.
79 * Consumer that handles fetch() failures by sleeping.
81 abstract class FetchingBusConsumer implements BusConsumer {
82 private static final Logger logger = LoggerFactory.getLogger(FetchingBusConsumer.class);
87 protected int fetchTimeout;
90 * Time to sleep on a fetch failure.
93 private final int sleepTime;
96 * Counted down when {@link #close()} is invoked.
98 private final CountDownLatch closeCondition = new CountDownLatch(1);
102 * Constructs the object.
104 * @param busTopicParams parameters for the bus topic
106 protected FetchingBusConsumer(BusTopicParams busTopicParams) {
107 this.fetchTimeout = busTopicParams.getFetchTimeout();
109 if (this.fetchTimeout <= 0) {
110 this.sleepTime = PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH;
112 // don't sleep too long, even if fetch timeout is large
113 this.sleepTime = Math.min(this.fetchTimeout, PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH);
118 * Causes the thread to sleep; invoked after fetch() fails. If the consumer is closed,
119 * or the thread is interrupted, then this will return immediately.
121 protected void sleepAfterFetchFailure() {
123 logger.info("{}: backoff for {}ms", this, sleepTime);
124 if (this.closeCondition.await(this.sleepTime, TimeUnit.MILLISECONDS)) {
125 logger.info("{}: closed while handling fetch error", this);
128 } catch (InterruptedException e) {
129 logger.warn("{}: interrupted while handling fetch error", this, e);
130 Thread.currentThread().interrupt();
135 public void close() {
136 this.closeCondition.countDown();
141 * Cambria based consumer.
143 public static class CambriaConsumerWrapper extends FetchingBusConsumer {
148 private static Logger logger = LoggerFactory.getLogger(CambriaConsumerWrapper.class);
151 * Used to build the consumer.
153 private final ConsumerBuilder builder;
158 private final CambriaConsumer consumer;
161 * Cambria Consumer Wrapper.
162 * BusTopicParam object contains the following parameters
163 * servers - messaging bus hosts.
164 * topic - topic for messages
166 * apiSecret - API Secret
167 * consumerGroup - Consumer Group
168 * consumerInstance - Consumer Instance
169 * fetchTimeout - Fetch Timeout
170 * fetchLimit - Fetch Limit
172 * @param busTopicParams - The parameters for the bus topic
174 public CambriaConsumerWrapper(BusTopicParams busTopicParams) {
175 super(busTopicParams);
177 this.builder = new CambriaClientBuilders.ConsumerBuilder();
179 builder.knownAs(busTopicParams.getConsumerGroup(), busTopicParams.getConsumerInstance())
180 .usingHosts(busTopicParams.getServers()).onTopic(busTopicParams.getTopic())
181 .waitAtServer(fetchTimeout).receivingAtMost(busTopicParams.getFetchLimit());
183 // Set read timeout to fetch timeout + 30 seconds (TBD: this should be configurable)
184 builder.withSocketTimeout(fetchTimeout + 30000);
186 if (busTopicParams.isUseHttps()) {
187 builder.usingHttps();
189 if (busTopicParams.isAllowSelfSignedCerts()) {
190 builder.allowSelfSignedCertificates();
194 if (busTopicParams.isApiKeyValid() && busTopicParams.isApiSecretValid()) {
195 builder.authenticatedBy(busTopicParams.getApiKey(), busTopicParams.getApiSecret());
198 if (busTopicParams.isUserNameValid() && busTopicParams.isPasswordValid()) {
199 builder.authenticatedByHttp(busTopicParams.getUserName(), busTopicParams.getPassword());
203 this.consumer = builder.build();
204 } catch (MalformedURLException | GeneralSecurityException e) {
205 throw new IllegalArgumentException(e);
210 public Iterable<String> fetch() throws IOException {
212 return this.consumer.fetch();
213 } catch (final IOException e) { //NOSONAR
214 logger.error("{}: cannot fetch because of {}", this, e.getMessage());
215 sleepAfterFetchFailure();
221 public void close() {
223 this.consumer.close();
227 public String toString() {
228 return "CambriaConsumerWrapper [fetchTimeout=" + fetchTimeout + "]";
233 * Kafka based consumer.
235 class KafkaConsumerWrapper extends FetchingBusConsumer {
240 private static final Logger logger = LoggerFactory.getLogger(KafkaConsumerWrapper.class);
242 private static final String KEY_DESERIALIZER = "org.apache.kafka.common.serialization.StringDeserializer";
247 protected KafkaConsumer<String, String> consumer;
248 protected Properties kafkaProps;
250 protected boolean allowTracing;
253 * Kafka Consumer Wrapper.
254 * BusTopicParam - object contains the following parameters
255 * servers - messaging bus hosts.
258 * @param busTopicParams - The parameters for the bus topic
260 public KafkaConsumerWrapper(BusTopicParams busTopicParams) {
261 super(busTopicParams);
263 if (busTopicParams.isTopicInvalid()) {
264 throw new IllegalArgumentException("No topic for Kafka");
267 //Setup Properties for consumer
268 kafkaProps = new Properties();
269 kafkaProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
270 busTopicParams.getServers().get(0));
272 if (busTopicParams.isAdditionalPropsValid()) {
273 kafkaProps.putAll(busTopicParams.getAdditionalProps());
276 if (kafkaProps.get(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG) == null) {
277 kafkaProps.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, KEY_DESERIALIZER);
279 if (kafkaProps.get(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG) == null) {
280 kafkaProps.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KEY_DESERIALIZER);
282 if (kafkaProps.get(ConsumerConfig.GROUP_ID_CONFIG) == null) {
283 kafkaProps.setProperty(ConsumerConfig.GROUP_ID_CONFIG, busTopicParams.getConsumerGroup());
285 if (busTopicParams.isAllowTracing()) {
286 this.allowTracing = true;
287 kafkaProps.setProperty(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG,
288 TracingConsumerInterceptor.class.getName());
291 consumer = new KafkaConsumer<>(kafkaProps);
292 //Subscribe to the topic
293 consumer.subscribe(List.of(busTopicParams.getTopic()));
297 public Iterable<String> fetch() {
298 ConsumerRecords<String, String> records = this.consumer.poll(Duration.ofMillis(fetchTimeout));
299 if (records == null || records.count() <= 0) {
300 return Collections.emptyList();
302 List<String> messages = new ArrayList<>(records.count());
305 createParentTraceContext(records);
308 for (TopicPartition partition : records.partitions()) {
309 List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);
310 for (ConsumerRecord<String, String> partitionRecord : partitionRecords) {
311 messages.add(partitionRecord.value());
313 long lastOffset = partitionRecords.get(partitionRecords.size() - 1).offset();
314 consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(lastOffset + 1)));
316 } catch (Exception e) {
317 logger.error("{}: cannot fetch, throwing exception after sleep...", this);
318 sleepAfterFetchFailure();
324 private void createParentTraceContext(ConsumerRecords<String, String> records) {
325 TraceParentInfo traceParentInfo = new TraceParentInfo();
326 for (ConsumerRecord<String, String> consumerRecord : records) {
328 Headers consumerRecordHeaders = consumerRecord.headers();
329 traceParentInfo = processTraceParentHeader(consumerRecordHeaders);
332 SpanContext spanContext = SpanContext.createFromRemoteParent(
333 traceParentInfo.getTraceId(), traceParentInfo.getSpanId(),
334 TraceFlags.getSampled(), TraceState.builder().build());
336 Context.current().with(Span.wrap(spanContext)).makeCurrent();
339 private TraceParentInfo processTraceParentHeader(Headers headers) {
340 TraceParentInfo traceParentInfo = new TraceParentInfo();
341 if (headers.lastHeader("traceparent") != null) {
342 traceParentInfo.setParentTraceId(new String(headers.lastHeader(
343 "traceparent").value(), StandardCharsets.UTF_8));
345 String[] parts = traceParentInfo.getParentTraceId().split("-");
346 traceParentInfo.setTraceId(parts[1]);
347 traceParentInfo.setSpanId(parts[2]);
350 return traceParentInfo;
355 private static class TraceParentInfo {
356 private String parentTraceId;
357 private String traceId;
358 private String spanId;
362 public void close() {
364 this.consumer.close();
365 logger.info("Kafka Consumer exited {}", this);
369 public String toString() {
370 return "KafkaConsumerWrapper [fetchTimeout=" + fetchTimeout + "]";