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