Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / adapt / kafka / KafkaEventProducer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.testsuites.integration.uservice.adapt.kafka;
23
24 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
25 import java.time.Duration;
26 import org.apache.kafka.clients.producer.Producer;
27 import org.apache.kafka.clients.producer.ProducerRecord;
28 import org.apache.kafka.common.serialization.StringSerializer;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The Class KafkaEventProducer.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class KafkaEventProducer implements Runnable {
40     // Get a reference to the logger
41     private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventProducer.class);
42
43     private final String topic;
44     private final SharedKafkaTestResource sharedKafkaTestResource;
45     private final int eventCount;
46     private final boolean xmlEvents;
47     private final long eventInterval;
48     private long eventsSentCount = 0;
49
50     private final Thread producerThread;
51     private boolean sendEventsFlag = false;
52     private volatile boolean stopFlag = false;
53
54     /**
55      * Instantiates a new kafka event producer.
56      *
57      * @param topic the topic
58      * @param sharedKafkaTestResource the kafka server address
59      * @param eventCount the event count
60      * @param xmlEvents the xml events
61      * @param eventInterval the event interval
62      */
63     public KafkaEventProducer(final String topic, final SharedKafkaTestResource sharedKafkaTestResource,
64         final int eventCount, final boolean xmlEvents, final long eventInterval) {
65         this.topic = topic;
66         this.sharedKafkaTestResource = sharedKafkaTestResource;
67         this.eventCount = eventCount;
68         this.xmlEvents = xmlEvents;
69         this.eventInterval = eventInterval;
70
71         producerThread = new Thread(this);
72         producerThread.start();
73     }
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public void run() {
80         final Producer<String, String> producer = sharedKafkaTestResource.getKafkaTestUtils()
81             .getKafkaProducer(StringSerializer.class, StringSerializer.class);
82
83         while (producerThread.isAlive() && !stopFlag) {
84             ThreadUtilities.sleep(50);
85
86             if (sendEventsFlag) {
87                 sendEventsToTopic(producer);
88                 sendEventsFlag = false;
89             }
90         }
91
92         producer.close(Duration.ofMillis(1000));
93     }
94
95     /**
96      * Send events.
97      */
98     public void sendEvents() {
99         sendEventsFlag = true;
100     }
101
102     /**
103      * Send events to topic.
104      *
105      * @param producer the producer
106      */
107     private void sendEventsToTopic(final Producer<String, String> producer) {
108         LOGGER.debug("{} : sending events to Kafka server, event count {}, xmlEvents {}",
109             KafkaEventProducer.class.getName(), eventCount, xmlEvents);
110
111         for (int i = 0; i < eventCount; i++) {
112             LOGGER.debug("{} : waiting {} milliseconds before sending next event", KafkaEventProducer.class.getName(),
113                 eventInterval);
114             ThreadUtilities.sleep(eventInterval);
115
116             String eventString = null;
117             if (xmlEvents) {
118                 eventString = EventGenerator.xmlEvent();
119             } else {
120                 eventString = EventGenerator.jsonEvent();
121             }
122             producer.send(new ProducerRecord<String, String>(topic, "Event" + i + "Of" + eventCount, eventString));
123             producer.flush();
124             eventsSentCount++;
125             LOGGER.debug("****** Sent event No. {} ******\n{}", eventsSentCount, eventString);
126         }
127         LOGGER.debug("{}: completed", KafkaEventProducer.class.getName());
128     }
129
130     /**
131      * Gets the events sent count.
132      *
133      * @return the events sent count
134      */
135     public long getEventsSentCount() {
136         return eventsSentCount;
137     }
138
139     /**
140      * Shutdown.
141      */
142     public void shutdown() {
143         LOGGER.debug("{} : stopping", KafkaEventProducer.class.getName());
144
145         stopFlag = true;
146
147         while (producerThread.isAlive()) {
148             ThreadUtilities.sleep(10);
149         }
150
151         LOGGER.debug("{} : stopped", KafkaEventProducer.class.getName());
152     }
153
154     public boolean isAlive() {
155         return producerThread.isAlive();
156     }
157 }