bb5398cebe3c3e37f84175a8cefd96c0509d1d88
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.kafka;
23
24 import java.util.EnumMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.kafka.clients.producer.KafkaProducer;
29 import org.apache.kafka.clients.producer.Producer;
30 import org.apache.kafka.clients.producer.ProducerRecord;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
33 import org.onap.policy.apex.service.engine.event.PeeredReference;
34 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Concrete implementation of an Apex event producer that sends events using Kafka.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class ApexKafkaProducer extends ApexPluginsEventProducer {
46
47     // Get a reference to the logger
48     private static final Logger LOGGER = LoggerFactory.getLogger(ApexKafkaProducer.class);
49
50     // The Kafka parameters read from the parameter service
51     private KafkaCarrierTechnologyParameters kafkaProducerProperties;
52
53     // The Kafka Producer used to send events using Kafka
54     private Producer<String, Object> kafkaProducer;
55
56     @Override
57     public void init(final String producerName, final EventHandlerParameters producerParameters)
58             throws ApexEventException {
59         this.name = producerName;
60
61         // Check and get the Kafka Properties
62         if (!(producerParameters.getCarrierTechnologyParameters() instanceof KafkaCarrierTechnologyParameters)) {
63             String message = "specified producer properties are not applicable to a Kafka producer (" + this.name + ")";
64             LOGGER.warn(message);
65             throw new ApexEventException(message);
66         }
67         kafkaProducerProperties =
68                 (KafkaCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
69     }
70
71     /**
72      * {@inheritDoc}.
73      */
74     @Override
75     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
76             final Object event) {
77         super.sendEvent(executionId, executionProperties, eventName, event);
78
79         // Kafka producer must be started in the same thread as it is stopped, so we must start it here
80         if (kafkaProducer == null) {
81             // Kick off the Kafka producer
82             kafkaProducer = new KafkaProducer<>(kafkaProducerProperties.getKafkaProducerProperties());
83             if (LOGGER.isDebugEnabled()) {
84                 LOGGER.debug("event producer " + this.name + " is ready to send to topics: "
85                         + kafkaProducerProperties.getProducerTopic());
86             }
87         }
88
89         kafkaProducer.send(new ProducerRecord<String, Object>(kafkaProducerProperties.getProducerTopic(), name, event));
90         if (LOGGER.isTraceEnabled()) {
91             LOGGER.trace("event sent from engine using {} to topic {} : {} ", this.name,
92                     kafkaProducerProperties.getProducerTopic(), event);
93         }
94     }
95
96     /**
97      * {@inheritDoc}.
98      */
99     @Override
100     public void stop() {
101         if (kafkaProducer != null) {
102             kafkaProducer.flush();
103             kafkaProducer.close();
104         }
105     }
106 }