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