21295a9b6e5e383c0515c295560a3b410cab0af7
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / producer / Publisher.java
1 /*
2  * Copyright © 2020 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.nbinotifications.producer;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.util.Properties;
12 import org.apache.kafka.clients.producer.KafkaProducer;
13 import org.apache.kafka.clients.producer.Producer;
14 import org.apache.kafka.clients.producer.ProducerConfig;
15 import org.apache.kafka.clients.producer.ProducerRecord;
16 import org.apache.kafka.common.serialization.StringSerializer;
17 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
18 import org.opendaylight.transportpce.nbinotifications.serialization.ConfigConstants;
19 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
20 import org.opendaylight.transportpce.nbinotifications.utils.NbiNotificationsUtils;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NotificationService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class Publisher {
26     private static final Logger LOG = LoggerFactory.getLogger(Publisher.class);
27
28     private final String id;
29     private final Producer<String, NotificationService> producer;
30
31     public Publisher(String id, String publisherServer, JsonStringConverter<NotificationService> serializer) {
32         Properties properties = NbiNotificationsUtils.loadProperties("publisher.properties");
33         properties.put(ProducerConfig.CLIENT_ID_CONFIG, id);
34         if (publisherServer != null && !publisherServer.isBlank()) {
35             properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, publisherServer);
36         }
37         properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
38         properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG , NotificationServiceSerializer.class);
39         properties.put(ConfigConstants.CONVERTER , serializer);
40         LOG.info("Creationg publisher for id {} with properties {}", id, properties);
41         producer = new KafkaProducer<>(properties);
42         this.id = id;
43     }
44
45     @VisibleForTesting Publisher(String id, Producer<String, NotificationService> producer) {
46         this.producer = producer;
47         this.id = id;
48     }
49
50     public void close() {
51         producer.close();
52     }
53
54     public void sendEvent(NotificationService notificationService) {
55         LOG.info("SendEvent request to topic '{}' ", notificationService.getConnectionType().getName());
56         producer.send(new ProducerRecord<>(notificationService.getConnectionType().getName(), id, notificationService));
57         producer.flush();
58     }
59 }