Add service listener to notify Kafka
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / producer / PublisherAlarm.java
1 /*
2  * Copyright © 2021 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.NotificationAlarmServiceSerializer;
20 import org.opendaylight.transportpce.nbinotifications.utils.NbiNotificationsUtils;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class PublisherAlarm {
26     private static final Logger LOG = LoggerFactory.getLogger(PublisherAlarm.class);
27
28     private final String id;
29     private final Producer<String, NotificationAlarmService> producer;
30
31     public PublisherAlarm(String id, String publisherServer, JsonStringConverter<NotificationAlarmService> 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 , NotificationAlarmServiceSerializer.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
46     PublisherAlarm(String id, Producer<String, NotificationAlarmService> producer) {
47         this.producer = producer;
48         this.id = id;
49     }
50
51     public void close() {
52         producer.close();
53     }
54
55     public void sendEvent(NotificationAlarmService notificationAlarmService) {
56         LOG.info("SendEvent request to topic '{}' ", notificationAlarmService.getConnectionType().getName());
57         producer.send(new ProducerRecord<>("alarm" + notificationAlarmService.getConnectionType().getName(),
58                 id, notificationAlarmService));
59         producer.flush();
60     }
61 }