Migration to TAPI 2.4 Step2
[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.NbiNotificationsHandler;
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.rev230728.NotificationAlarmService;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationProcessService;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.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 NbiNotificationsHandler 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
38     private TopicManager() {
39     }
40
41     public static TopicManager getInstance() {
42         return instance;
43     }
44
45     public void setNbiNotificationsListener(NbiNotificationsHandler nbiNotificationsListener) {
46         this.nbiNotificationsListener = nbiNotificationsListener;
47     }
48
49     public void setProcessConverter(JsonStringConverter<NotificationProcessService> processConverter) {
50         this.processConverter = processConverter;
51     }
52
53     public void setAlarmConverter(JsonStringConverter<NotificationAlarmService> alarmConverter) {
54         this.alarmConverter = alarmConverter;
55     }
56
57     public void setTapiConverter(JsonStringConverter<NotificationTapiService> tapiConverter) {
58         this.tapiConverter = tapiConverter;
59     }
60
61     public void setPublisherServer(String publisherServer) {
62         this.publisherServer = publisherServer;
63     }
64
65     public void addProcessTopic(String topic) {
66         LOG.info("Adding process topic: {}", topic);
67         processPublisherMap.put(topic, new Publisher<>(topic, publisherServer, processConverter,
68             NotificationServiceSerializer.class));
69         if (this.nbiNotificationsListener != null) {
70             this.nbiNotificationsListener.setPublishersServiceMap(processPublisherMap);
71         }
72     }
73
74     public void addAlarmTopic(String topic) {
75         LOG.info("Adding alarm topic: {}", topic);
76         alarmPublisherMap.put(topic, new Publisher<>(topic, publisherServer, alarmConverter,
77                 NotificationAlarmServiceSerializer.class));
78         if (this.nbiNotificationsListener != null) {
79             this.nbiNotificationsListener.setPublishersAlarmMap(alarmPublisherMap);
80         }
81     }
82
83     public void addTapiTopic(String topic) {
84         if (tapiPublisherMap.containsKey(topic)) {
85             LOG.info("Tapi topic: {} already exists", topic);
86             return;
87         }
88         LOG.info("Adding new tapi topic: {}", topic);
89         tapiPublisherMap.put(topic, new Publisher<>(topic, publisherServer, tapiConverter,
90             TapiNotificationSerializer.class));
91         if (this.nbiNotificationsListener != null) {
92             this.nbiNotificationsListener.setTapiPublishersMap(tapiPublisherMap);
93         }
94     }
95
96     public void deleteTapiTopic(String topic) {
97         if (!tapiPublisherMap.containsKey(topic)) {
98             LOG.info("Tapi topic: {} doesnt exist", topic);
99             return;
100         }
101         LOG.info("Deleting tapi topic: {}", topic);
102         tapiPublisherMap.remove(topic);
103         if (this.nbiNotificationsListener != null) {
104             this.nbiNotificationsListener.setTapiPublishersMap(tapiPublisherMap);
105         }
106     }
107
108     public Map<String, Publisher<NotificationTapiService>> getTapiTopicMap() {
109         return this.tapiPublisherMap;
110     }
111
112     public Map<String, Publisher<NotificationAlarmService>> getAlarmTopicMap() {
113         return this.alarmPublisherMap;
114     }
115
116     public Map<String, Publisher<NotificationProcessService>> getProcessTopicMap() {
117         return this.processPublisherMap;
118     }
119 }