2 * Copyright © 2020 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.nbinotifications.producer;
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.rev210628.NotificationService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class Publisher {
26 private static final Logger LOG = LoggerFactory.getLogger(Publisher.class);
28 private final String id;
29 private final Producer<String, NotificationService> producer;
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);
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);
45 @VisibleForTesting Publisher(String id, Producer<String, NotificationService> producer) {
46 this.producer = producer;
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));