2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.event.carrier.kafka;
25 import java.time.Duration;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Properties;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.kafka.clients.producer.internals.DefaultPartitioner;
33 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
34 import org.onap.policy.common.parameters.GroupValidationResult;
35 import org.onap.policy.common.parameters.ValidationStatus;
36 import org.onap.policy.common.parameters.annotations.Min;
37 import org.onap.policy.common.parameters.annotations.NotBlank;
40 * Apex parameters for Kafka as an event carrier technology.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class KafkaCarrierTechnologyParameters extends CarrierTechnologyParameters {
48 /** The label of this carrier technology. */
49 public static final String KAFKA_CARRIER_TECHNOLOGY_LABEL = "KAFKA";
51 /** The producer plugin class for the Kafka carrier technology. */
52 public static final String KAFKA_EVENT_PRODUCER_PLUGIN_CLASS = ApexKafkaProducer.class.getName();
54 /** The consumer plugin class for the Kafka carrier technology. */
55 public static final String KAFKA_EVENT_CONSUMER_PLUGIN_CLASS = ApexKafkaConsumer.class.getName();
57 // Repeated strings in messages
58 private static final String ENTRY = "entry ";
59 private static final String KAFKA_PROPERTIES = "kafkaProperties";
61 // Default parameter values
62 private static final String DEFAULT_ACKS = "all";
63 private static final String DEFAULT_BOOT_SERVERS = "localhost:9092";
64 private static final int DEFAULT_RETRIES = 0;
65 private static final int DEFAULT_BATCH_SIZE = 16384;
66 private static final int DEFAULT_LINGER_TIME = 1;
67 private static final long DEFAULT_BUFFER_MEMORY = 33554432;
68 private static final String DEFAULT_GROUP_ID = "default-group-id";
69 private static final boolean DEFAULT_ENABLE_AUTOCMIT = true;
70 private static final int DEFAULT_AUTO_COMMIT_TIME = 1000;
71 private static final int DEFAULT_SESSION_TIMEOUT = 30000;
72 private static final String DEFAULT_PROD_TOPIC = "apex-out";
73 private static final int DEFAULT_CONS_POLL_TIME = 100;
74 private static final String[] DEFAULT_CONS_TOPICLIST = {"apex-in"};
75 private static final String DEFAULT_STRING_SERZER = "org.apache.kafka.common.serialization.StringSerializer";
76 private static final String DEFAULT_STRING_DESZER = "org.apache.kafka.common.serialization.StringDeserializer";
77 private static final String DEFAULT_PARTITIONR_CLASS = DefaultPartitioner.class.getName();
79 // Parameter property map tokens
80 private static final String PROPERTY_BOOTSTRAP_SERVERS = "bootstrap.servers";
81 private static final String PROPERTY_ACKS = "acks";
82 private static final String PROPERTY_RETRIES = "retries";
83 private static final String PROPERTY_BATCH_SIZE = "batch.size";
84 private static final String PROPERTY_LINGER_TIME = "linger.ms";
85 private static final String PROPERTY_BUFFER_MEMORY = "buffer.memory";
86 private static final String PROPERTY_GROUP_ID = "group.id";
87 private static final String PROPERTY_ENABLE_AUTO_COMMIT = "enable.auto.commit";
88 private static final String PROPERTY_AUTO_COMMIT_TIME = "auto.commit.interval.ms";
89 private static final String PROPERTY_SESSION_TIMEOUT = "session.timeout.ms";
90 private static final String PROPERTY_KEY_SERIALIZER = "key.serializer";
91 private static final String PROPERTY_VALUE_SERIALIZER = "value.serializer";
92 private static final String PROPERTY_KEY_DESERIALIZER = "key.deserializer";
93 private static final String PROPERTY_VALUE_DESERIALIZER = "value.deserializer";
94 private static final String PROPERTY_PARTITIONER_CLASS = "partitioner.class";
96 // kafka carrier parameters
98 private String bootstrapServers = DEFAULT_BOOT_SERVERS;
100 private String acks = DEFAULT_ACKS;
102 private int retries = DEFAULT_RETRIES;
104 private int batchSize = DEFAULT_BATCH_SIZE;
106 private int lingerTime = DEFAULT_LINGER_TIME;
108 private long bufferMemory = DEFAULT_BUFFER_MEMORY;
110 private String groupId = DEFAULT_GROUP_ID;
111 private boolean enableAutoCommit = DEFAULT_ENABLE_AUTOCMIT;
113 private int autoCommitTime = DEFAULT_AUTO_COMMIT_TIME;
115 private int sessionTimeout = DEFAULT_SESSION_TIMEOUT;
117 private String producerTopic = DEFAULT_PROD_TOPIC;
119 private int consumerPollTime = DEFAULT_CONS_POLL_TIME;
120 private String[] consumerTopicList = DEFAULT_CONS_TOPICLIST;
122 private String keySerializer = DEFAULT_STRING_SERZER;
124 private String valueSerializer = DEFAULT_STRING_SERZER;
126 private String keyDeserializer = DEFAULT_STRING_DESZER;
128 private String valueDeserializer = DEFAULT_STRING_DESZER;
130 private String partitionerClass = DEFAULT_PARTITIONR_CLASS;
132 // All Kafka properties can be specified as an array of key-value pairs
133 private String[][] kafkaProperties = null;
138 * Constructor to create a kafka carrier technology parameters instance and register the instance with the parameter
141 public KafkaCarrierTechnologyParameters() {
144 // Set the carrier technology properties for the kafka carrier technology
145 this.setLabel(KAFKA_CARRIER_TECHNOLOGY_LABEL);
146 this.setEventProducerPluginClass(KAFKA_EVENT_PRODUCER_PLUGIN_CLASS);
147 this.setEventConsumerPluginClass(KAFKA_EVENT_CONSUMER_PLUGIN_CLASS);
151 * Gets the kafka producer properties.
153 * @return the kafka producer properties
155 public Properties getKafkaProducerProperties() {
156 final Properties retKafkaProps = new Properties();
158 // Add properties from the Kafka property array
159 if (kafkaProperties != null) {
160 for (int i = 0; i < kafkaProperties.length; i++) {
161 retKafkaProps.setProperty(kafkaProperties[i][0], kafkaProperties[i][1]);
166 putExplicitProperty(retKafkaProps, PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers, DEFAULT_BOOT_SERVERS);
167 putExplicitProperty(retKafkaProps, PROPERTY_ACKS, acks, DEFAULT_ACKS);
168 putExplicitProperty(retKafkaProps, PROPERTY_RETRIES, retries, DEFAULT_RETRIES);
169 putExplicitProperty(retKafkaProps, PROPERTY_BATCH_SIZE, batchSize, DEFAULT_BATCH_SIZE);
170 putExplicitProperty(retKafkaProps, PROPERTY_LINGER_TIME, lingerTime, DEFAULT_LINGER_TIME);
171 putExplicitProperty(retKafkaProps, PROPERTY_BUFFER_MEMORY, bufferMemory, DEFAULT_BUFFER_MEMORY);
172 putExplicitProperty(retKafkaProps, PROPERTY_KEY_SERIALIZER, keySerializer, DEFAULT_STRING_SERZER);
173 putExplicitProperty(retKafkaProps, PROPERTY_VALUE_SERIALIZER, valueSerializer, DEFAULT_STRING_SERZER);
174 putExplicitProperty(retKafkaProps, PROPERTY_PARTITIONER_CLASS, partitionerClass, DEFAULT_PARTITIONR_CLASS);
177 return retKafkaProps;
181 * Gets the kafka consumer properties.
183 * @return the kafka consumer properties
185 public Properties getKafkaConsumerProperties() {
186 final Properties retKafkaProps = new Properties();
188 // Add properties from the Kafka property array
189 if (kafkaProperties != null) {
190 for (int i = 0; i < kafkaProperties.length; i++) {
191 retKafkaProps.setProperty(kafkaProperties[i][0], kafkaProperties[i][1]);
196 putExplicitProperty(retKafkaProps, PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers, DEFAULT_BOOT_SERVERS);
197 putExplicitProperty(retKafkaProps, PROPERTY_GROUP_ID, groupId, DEFAULT_GROUP_ID);
198 putExplicitProperty(retKafkaProps, PROPERTY_ENABLE_AUTO_COMMIT, enableAutoCommit, DEFAULT_ENABLE_AUTOCMIT);
199 putExplicitProperty(retKafkaProps, PROPERTY_AUTO_COMMIT_TIME, autoCommitTime, DEFAULT_AUTO_COMMIT_TIME);
200 putExplicitProperty(retKafkaProps, PROPERTY_SESSION_TIMEOUT, sessionTimeout, DEFAULT_SESSION_TIMEOUT);
201 putExplicitProperty(retKafkaProps, PROPERTY_KEY_DESERIALIZER, keyDeserializer, DEFAULT_STRING_DESZER);
202 putExplicitProperty(retKafkaProps, PROPERTY_VALUE_DESERIALIZER, valueDeserializer, DEFAULT_STRING_DESZER);
205 return retKafkaProps;
209 * Gets the consumer topic list.
211 * @return the consumer topic list
213 public Collection<String> getConsumerTopicListAsCollection() {
214 return Arrays.asList(consumerTopicList);
218 * Gets the consumer poll duration.
219 * @return The poll duration
221 public Duration getConsumerPollDuration() {
222 return Duration.ofMillis(consumerPollTime);
229 public GroupValidationResult validate() {
230 final GroupValidationResult result = super.validate();
232 validateConsumerTopicList(result);
234 validateKafkaProperties(result);
240 * Validate the consumer topic list.
242 * @param result the result of the validation.
244 private void validateConsumerTopicList(final GroupValidationResult result) {
245 if (consumerTopicList == null || consumerTopicList.length == 0) {
246 result.setResult("consumerTopicList", ValidationStatus.INVALID,
247 "not specified, must be specified as a list of strings");
251 StringBuilder consumerTopicStringBuilder = new StringBuilder();
252 for (final String consumerTopic : consumerTopicList) {
253 if (StringUtils.isBlank(consumerTopic)) {
254 consumerTopicStringBuilder.append(consumerTopic + "/");
257 if (consumerTopicStringBuilder.length() > 0) {
258 result.setResult("consumerTopicList", ValidationStatus.INVALID,
259 "invalid consumer topic list entries found: /" + consumerTopicStringBuilder.toString());
264 * Validate the kafka properties.
266 * @param result the result of the validation.
268 private void validateKafkaProperties(final GroupValidationResult result) {
269 // Kafka properties are optional
270 if (kafkaProperties == null || kafkaProperties.length == 0) {
274 for (int i = 0; i < kafkaProperties.length; i++) {
275 if (kafkaProperties[i].length != 2) {
276 result.setResult(KAFKA_PROPERTIES, ValidationStatus.INVALID,
277 ENTRY + i + " invalid, kafka properties must be name-value pairs");
280 if (StringUtils.isBlank(kafkaProperties[i][0])) {
281 result.setResult(KAFKA_PROPERTIES, ValidationStatus.INVALID,
282 ENTRY + i + " invalid, key is null or blank");
285 // the value of a property has to be specified as empty in some cases, but should never be null.
286 if (null == kafkaProperties[i][1]) {
287 result.setResult(KAFKA_PROPERTIES, ValidationStatus.INVALID,
288 ENTRY + i + " invalid, value is null");
294 * Put a property into the properties if it is not already defined and is not the default value.
296 * @param returnKafkaProperties the properties to set the value in
297 * @param property the property to put
298 * @param value the value of the property to put
299 * @param defaultValue the default value of the property to put
301 private void putExplicitProperty(final Properties returnKafkaProperties, final String property,
302 final Object value, final Object defaultValue) {
304 // Check if the property is already in the properties
305 if (!returnKafkaProperties.containsKey(property)) {
306 // Not found, so add it
307 returnKafkaProperties.setProperty(property, value.toString());
309 // Found, only overwrite if the property does not have the default value
311 returnKafkaProperties.setProperty(property, defaultValue.toString());
312 } else if (!value.toString().contentEquals(defaultValue.toString())) {
313 returnKafkaProperties.setProperty(property, value.toString());