Initial tapi notification implementation
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / utils / TopicManager.java
1 /*
2  * Copyright © 2021 Nokia, 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.utils;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
13 import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsListenerImpl;
14 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
15 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationAlarmServiceSerializer;
16 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
17 import org.opendaylight.transportpce.nbinotifications.serialization.TapiNotificationSerializer;
18 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class TopicManager {
25
26     private static final Logger LOG = LoggerFactory.getLogger(TopicManager.class);
27     private static TopicManager instance = new TopicManager();
28
29     private Map<String, Publisher<NotificationTapiService>> tapiPublisherMap = new HashMap<>();
30     private String publisherServer;
31     private JsonStringConverter<NotificationTapiService> tapiConverter;
32     private NbiNotificationsListenerImpl nbiNotificationsListener;
33     private Map<String, Publisher<NotificationAlarmService>> alarmPublisherMap = new HashMap<>();
34     private Map<String, Publisher<NotificationProcessService>> processPublisherMap = new HashMap<>();
35     private JsonStringConverter<NotificationProcessService> processConverter;
36     private JsonStringConverter<NotificationAlarmService> alarmConverter;
37     private int calledSetConverter = 0;
38
39     private TopicManager() {
40     }
41
42     public static TopicManager getInstance() {
43         return instance;
44     }
45
46     public void setNbiNotificationsListener(NbiNotificationsListenerImpl nbiNotificationsListener) {
47         this.nbiNotificationsListener = nbiNotificationsListener;
48     }
49
50     public void setProcessConverter(JsonStringConverter<NotificationProcessService> processConverter) {
51         this.processConverter = processConverter;
52     }
53
54     public void setAlarmConverter(JsonStringConverter<NotificationAlarmService> alarmConverter) {
55         this.alarmConverter = alarmConverter;
56     }
57
58     public void setTapiConverter(JsonStringConverter<NotificationTapiService> tapiConverter) {
59         this.tapiConverter = tapiConverter;
60         this.calledSetConverter++;
61     }
62
63     public void setPublisherServer(String publisherServer) {
64         this.publisherServer = publisherServer;
65     }
66
67     public void addProcessTopic(String topic) {
68         LOG.info("Adding process topic: {}", topic);
69         processPublisherMap.put(topic, new Publisher<>(topic, publisherServer, processConverter,
70             NotificationServiceSerializer.class));
71         if (this.nbiNotificationsListener != null) {
72             this.nbiNotificationsListener.setPublishersServiceMap(processPublisherMap);
73         }
74     }
75
76     public void addAlarmTopic(String topic) {
77         LOG.info("Adding alarm topic: {}", topic);
78         alarmPublisherMap.put(topic, new Publisher<>(topic, publisherServer, alarmConverter,
79                 NotificationAlarmServiceSerializer.class));
80         if (this.nbiNotificationsListener != null) {
81             this.nbiNotificationsListener.setPublishersAlarmMap(alarmPublisherMap);
82         }
83     }
84
85     public void addTapiTopic(String topic) {
86         if (tapiPublisherMap.containsKey(topic)) {
87             LOG.info("Tapi topic: {} already exists", topic);
88             return;
89         }
90         LOG.info("Adding new tapi topic: {}", topic);
91         tapiPublisherMap.put(topic, new Publisher<>(topic, publisherServer, tapiConverter,
92             TapiNotificationSerializer.class));
93         if (this.nbiNotificationsListener != null) {
94             this.nbiNotificationsListener.setTapiPublishersMap(tapiPublisherMap);
95         }
96     }
97
98     public Map<String, Publisher<NotificationTapiService>> getTapiTopicMap() {
99         return this.tapiPublisherMap;
100     }
101
102     public Map<String, Publisher<NotificationAlarmService>> getAlarmTopicMap() {
103         return this.alarmPublisherMap;
104     }
105
106     public Map<String, Publisher<NotificationProcessService>> getProcessTopicMap() {
107         return this.processPublisherMap;
108     }
109 }