Add service listener to notify Kafka
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / NbiNotificationsProvider.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.impl;
9
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.binding.api.RpcProviderService;
15 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
16 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
17 import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsListenerImpl;
18 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
19 import org.opendaylight.transportpce.nbinotifications.producer.PublisherAlarm;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsListener;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsService;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NbiNotificationsProvider {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
30     private static Map<String, Publisher> publishersServiceMap =  new HashMap<>();
31     private static Map<String, PublisherAlarm> publishersAlarmMap =  new HashMap<>();
32
33     private final RpcProviderService rpcService;
34     private ObjectRegistration<NbiNotificationsService> rpcRegistration;
35     private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
36     private NotificationService notificationService;
37     private final JsonStringConverter<org.opendaylight.yang.gen.v1
38         .nbi.notifications.rev210628.NotificationService> converterService;
39     private final JsonStringConverter<org.opendaylight.yang.gen.v1
40             .nbi.notifications.rev210628.NotificationAlarmService> converterAlarmService;
41     private final String subscriberServer;
42
43
44     public NbiNotificationsProvider(List<String> topicsService, List<String> topicsAlarm,
45             String subscriberServer, String publisherServer,
46             RpcProviderService rpcProviderService, NotificationService notificationService,
47             BindingDOMCodecServices bindingDOMCodecServices) {
48         this.rpcService = rpcProviderService;
49         this.notificationService = notificationService;
50         converterService =  new JsonStringConverter<>(bindingDOMCodecServices);
51         for (String topic: topicsService) {
52             LOG.info("Creating publisher for topic {}", topic);
53             publishersServiceMap.put(topic, new Publisher(topic, publisherServer, converterService));
54         }
55         converterAlarmService = new JsonStringConverter<>(bindingDOMCodecServices);
56         for (String topic: topicsAlarm) {
57             LOG.info("Creating publisher for topic {}", topic);
58             publishersAlarmMap.put(topic, new PublisherAlarm(topic, publisherServer, converterAlarmService));
59         }
60         this.subscriberServer = subscriberServer;
61     }
62
63     /**
64      * Method called when the blueprint container is created.
65      */
66     public void init() {
67         LOG.info("NbiNotificationsProvider Session Initiated");
68         rpcRegistration = rpcService.registerRpcImplementation(NbiNotificationsService.class,
69                 new NbiNotificationsImpl(converterService, converterAlarmService, subscriberServer));
70         listenerRegistration = notificationService.registerNotificationListener(
71                 new NbiNotificationsListenerImpl(publishersServiceMap, publishersAlarmMap));
72     }
73
74     /**
75      * Method called when the blueprint container is destroyed.
76      */
77     public void close() {
78         for (Publisher publisher : publishersServiceMap.values()) {
79             publisher.close();
80         }
81         for (PublisherAlarm publisherAlarm : publishersAlarmMap.values()) {
82             publisherAlarm.close();
83         }
84         rpcRegistration.close();
85         listenerRegistration.close();
86         LOG.info("NbiNotificationsProvider Closed");
87     }
88
89 }