Add service listener to notify Kafka
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / listener / NbiNotificationsListenerImpl.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.listener;
9
10 import java.util.Map;
11 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
12 import org.opendaylight.transportpce.nbinotifications.producer.PublisherAlarm;
13 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsListener;
14 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmServiceBuilder;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationServiceBuilder;
16 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationAlarmService;
17 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class NbiNotificationsListenerImpl implements NbiNotificationsListener {
22     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsListenerImpl.class);
23     private Map<String, Publisher> publishersServiceMap;
24     private Map<String, PublisherAlarm> publishersAlarmMap;
25
26     public NbiNotificationsListenerImpl(Map<String, Publisher> publishersServiceMap,
27                                         Map<String, PublisherAlarm> publishersAlarmMap) {
28         this.publishersServiceMap = publishersServiceMap;
29         this.publishersAlarmMap = publishersAlarmMap;
30     }
31
32     @Override
33     public void onPublishNotificationService(PublishNotificationService notification) {
34         LOG.info("Receiving request for publishing notification service");
35         String topic = notification.getTopic();
36         if (!publishersServiceMap.containsKey(topic)) {
37             LOG.error("Unknown topic {}", topic);
38             return;
39         }
40         Publisher publisher = publishersServiceMap.get(topic);
41         publisher.sendEvent(new NotificationServiceBuilder().setCommonId(notification.getCommonId())
42                 .setConnectionType(notification.getConnectionType()).setMessage(notification.getMessage())
43                 .setOperationalState(notification.getOperationalState())
44                 .setResponseFailed(notification.getResponseFailed())
45                 .setServiceAEnd(notification.getServiceAEnd())
46                 .setServiceName(notification.getServiceName())
47                 .setServiceZEnd(notification.getServiceZEnd()).build());
48     }
49
50     @Override
51     public void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
52         LOG.info("Receiving request for publishing notification alarm service");
53         String topic = notification.getTopic();
54         if (!publishersAlarmMap.containsKey(topic)) {
55             LOG.error("Unknown topic {}", topic);
56             return;
57         }
58         PublisherAlarm publisherAlarm = publishersAlarmMap.get(topic);
59         publisherAlarm.sendEvent(new NotificationAlarmServiceBuilder().setConnectionType(notification
60                 .getConnectionType())
61                 .setMessage(notification.getMessage())
62                 .setOperationalState(notification.getOperationalState())
63                 .setServiceName(notification.getServiceName())
64                 .build());
65     }
66 }