Refactor NBINotification & add ServiceListener tests
[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.serialization.NotificationAlarmServiceSerializer;
20 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsListener;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NbiNotificationsProvider {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
32     private static Map<String, Publisher<org.opendaylight.yang.gen.v1
33             .nbi.notifications.rev210628.NotificationService>> publishersServiceMap =  new HashMap<>();
34     private static Map<String, Publisher<NotificationAlarmService>> publishersAlarmMap =  new HashMap<>();
35
36     private final RpcProviderService rpcService;
37     private ObjectRegistration<NbiNotificationsService> rpcRegistration;
38     private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
39     private NotificationService notificationService;
40     private final JsonStringConverter<org.opendaylight.yang.gen.v1
41         .nbi.notifications.rev210628.NotificationService> converterService;
42     private final JsonStringConverter<NotificationAlarmService> converterAlarmService;
43     private final String subscriberServer;
44
45
46     public NbiNotificationsProvider(List<String> topicsService, List<String> topicsAlarm,
47             String subscriberServer, String publisherServer,
48             RpcProviderService rpcProviderService, NotificationService notificationService,
49             BindingDOMCodecServices bindingDOMCodecServices) {
50         this.rpcService = rpcProviderService;
51         this.notificationService = notificationService;
52         converterService =  new JsonStringConverter<>(bindingDOMCodecServices);
53         for (String topic: topicsService) {
54             LOG.info("Creating publisher for topic {}", topic);
55             publishersServiceMap.put(topic, new Publisher<>(topic, publisherServer, converterService,
56                     NotificationServiceSerializer.class));
57         }
58         converterAlarmService = new JsonStringConverter<>(bindingDOMCodecServices);
59         for (String topic: topicsAlarm) {
60             LOG.info("Creating publisher for topic {}", topic);
61             publishersAlarmMap.put(topic, new Publisher<>(topic, publisherServer, converterAlarmService,
62                     NotificationAlarmServiceSerializer.class));
63         }
64         this.subscriberServer = subscriberServer;
65     }
66
67     /**
68      * Method called when the blueprint container is created.
69      */
70     public void init() {
71         LOG.info("NbiNotificationsProvider Session Initiated");
72         rpcRegistration = rpcService.registerRpcImplementation(NbiNotificationsService.class,
73                 new NbiNotificationsImpl(converterService, converterAlarmService, subscriberServer));
74         listenerRegistration = notificationService.registerNotificationListener(
75                 new NbiNotificationsListenerImpl(publishersServiceMap, publishersAlarmMap));
76     }
77
78     /**
79      * Method called when the blueprint container is destroyed.
80      */
81     public void close() {
82         for (Publisher<org.opendaylight.yang.gen.v1
83                 .nbi.notifications.rev210628.NotificationService> publisher : publishersServiceMap.values()) {
84             publisher.close();
85         }
86         for (Publisher<NotificationAlarmService> publisherAlarm : publishersAlarmMap.values()) {
87             publisherAlarm.close();
88         }
89         rpcRegistration.close();
90         listenerRegistration.close();
91         LOG.info("NbiNotificationsProvider Closed");
92     }
93
94 }