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