Do not use RpcService in nbinotifications module
[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.common.network.NetworkTransactionService;
18 import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsListenerImpl;
19 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
20 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NbiNotificationsListener;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.concepts.Registration;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30 import org.osgi.service.component.annotations.Reference;
31 import org.osgi.service.metatype.annotations.AttributeDefinition;
32 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Component(configurationPid = "org.opendaylight.transportpce.nbinotifications")
37 public class NbiNotificationsProvider {
38
39     @ObjectClassDefinition
40     public @interface Configuration {
41         @AttributeDefinition
42         String suscriberServer() default "";
43         @AttributeDefinition
44         String publisherServer() default "";
45     }
46
47     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
48     private static Map<String, Publisher<NotificationProcessService>> publishersServiceMap =  new HashMap<>();
49     private static Map<String, Publisher<NotificationAlarmService>> publishersAlarmMap =  new HashMap<>();
50     private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
51     private Registration rpcRegistration;
52
53     @Activate
54     public NbiNotificationsProvider(@Reference RpcProviderService rpcProviderService,
55             @Reference NotificationService notificationService,
56             @Reference BindingDOMCodecServices bindingDOMCodecServices,
57             @Reference NetworkTransactionService networkTransactionService,
58             final Configuration configuration) {
59         this(configuration.suscriberServer(), configuration.publisherServer(), rpcProviderService, notificationService,
60                 bindingDOMCodecServices, networkTransactionService);
61     }
62
63     public NbiNotificationsProvider(String subscriberServer, String publisherServer,
64             RpcProviderService rpcProviderService, NotificationService notificationService,
65             BindingDOMCodecServices bindingDOMCodecServices, NetworkTransactionService networkTransactionService) {
66         List<String> publishersServiceList = List.of("PceListener", "ServiceHandlerOperations", "ServiceHandler",
67                 "RendererListener");
68         TopicManager topicManager = TopicManager.getInstance();
69         topicManager.setPublisherServer(publisherServer);
70         JsonStringConverter<NotificationProcessService> converterService =
71             new JsonStringConverter<>(bindingDOMCodecServices);
72         topicManager.setProcessConverter(converterService);
73         for (String publisherService: publishersServiceList) {
74             LOG.info("Creating publisher for the following class {}", publisherService);
75             topicManager.addProcessTopic(publisherService);
76         }
77         JsonStringConverter<NotificationAlarmService> converterAlarmService =
78                 new JsonStringConverter<>(bindingDOMCodecServices);
79         topicManager.setAlarmConverter(converterAlarmService);
80         List<String> publishersAlarmList = List.of("ServiceListener");
81         for (String publisherAlarm: publishersAlarmList) {
82             LOG.info("Creating publisher for the following class {}", publisherAlarm);
83             topicManager.addAlarmTopic(publisherAlarm);
84         }
85         JsonStringConverter<NotificationTapiService> converterTapiService =
86                 new JsonStringConverter<>(bindingDOMCodecServices);
87         LOG.info("baozhi tapi converter: {}", converterTapiService);
88         topicManager.setTapiConverter(converterTapiService);
89
90         NbiNotificationsImpl nbiImpl = new NbiNotificationsImpl(converterService, converterAlarmService,
91             converterTapiService, subscriberServer, networkTransactionService, topicManager);
92         rpcRegistration = rpcProviderService.registerRpcImplementations(nbiImpl.registerRPCs());
93         NbiNotificationsListenerImpl nbiNotificationsListener = new NbiNotificationsListenerImpl(
94                 topicManager.getProcessTopicMap(), topicManager.getAlarmTopicMap(), topicManager.getTapiTopicMap());
95         listenerRegistration = notificationService.registerNotificationListener(nbiNotificationsListener);
96         topicManager.setNbiNotificationsListener(nbiNotificationsListener);
97         LOG.info("NbiNotificationsProvider Session Initiated");
98     }
99
100     /**
101      * Method called when the blueprint container is destroyed.
102      */
103     @Deactivate
104     public void close() {
105         for (Publisher<NotificationProcessService> publisher : publishersServiceMap.values()) {
106             publisher.close();
107         }
108         for (Publisher<NotificationAlarmService> publisherAlarm : publishersAlarmMap.values()) {
109             publisherAlarm.close();
110         }
111         rpcRegistration.close();
112         listenerRegistration.close();
113         LOG.info("NbiNotificationsProvider Closed");
114     }
115 }