Fix bugs 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.List;
11 import java.util.Map;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
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.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
19 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
20 import org.opendaylight.transportpce.nbinotifications.impl.rpc.CreateNotificationSubscriptionServiceImpl;
21 import org.opendaylight.transportpce.nbinotifications.impl.rpc.DeleteNotificationSubscriptionServiceImpl;
22 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationListImpl;
23 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationSubscriptionServiceDetailsImpl;
24 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationSubscriptionServiceListImpl;
25 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationsAlarmServiceImpl;
26 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationsProcessServiceImpl;
27 import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetSupportedNotificationTypesImpl;
28 import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsHandler;
29 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
30 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationAlarmService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationProcessService;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationTapiService;
34 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
35 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.Context1;
36 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.context.NotificationContext;
37 import org.opendaylight.yangtools.concepts.Registration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Deactivate;
42 import org.osgi.service.component.annotations.Reference;
43 import org.osgi.service.metatype.annotations.AttributeDefinition;
44 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @Component(configurationPid = "org.opendaylight.transportpce.nbinotifications")
49 public class NbiNotificationsProvider {
50
51     @ObjectClassDefinition
52     public @interface Configuration {
53         @AttributeDefinition
54         String suscriberServer() default "";
55         @AttributeDefinition
56         String publisherServer() default "";
57     }
58
59     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
60     private Map<String, Publisher<NotificationProcessService>> publishersServiceMap;
61     private Map<String, Publisher<NotificationAlarmService>> publishersAlarmMap;
62     private Registration listenerRegistration;
63     private Registration rpcRegistration;
64     private NetworkTransactionService networkTransactionService;
65
66     @Activate
67     public NbiNotificationsProvider(@Reference RpcProviderService rpcProviderService,
68             @Reference NotificationService notificationService,
69             @Reference BindingDOMCodecServices bindingDOMCodecServices,
70             @Reference NetworkTransactionService networkTransactionService,
71             final Configuration configuration) {
72         this(configuration.suscriberServer(), configuration.publisherServer(), rpcProviderService, notificationService,
73                 bindingDOMCodecServices, networkTransactionService);
74     }
75
76     public NbiNotificationsProvider(String subscriberServer, String publisherServer,
77             RpcProviderService rpcProviderService, NotificationService notificationService,
78             BindingDOMCodecServices bindingDOMCodecServices, NetworkTransactionService networkTransactionService) {
79         this.networkTransactionService = networkTransactionService;
80         List<String> publishersServiceList = List.of("PceListener", "ServiceHandlerOperations", "ServiceHandler",
81                 "RendererListener");
82         TopicManager topicManager = TopicManager.getInstance();
83         topicManager.setPublisherServer(publisherServer);
84         JsonStringConverter<NotificationProcessService> converterService =
85             new JsonStringConverter<>(bindingDOMCodecServices);
86         topicManager.setProcessConverter(converterService);
87         for (String publisherService: publishersServiceList) {
88             LOG.info("Creating publisher for the following class {}", publisherService);
89             topicManager.addProcessTopic(publisherService);
90         }
91         JsonStringConverter<NotificationAlarmService> converterAlarmService =
92                 new JsonStringConverter<>(bindingDOMCodecServices);
93         topicManager.setAlarmConverter(converterAlarmService);
94         List<String> publishersAlarmList = List.of("ServiceListener");
95         for (String publisherAlarm: publishersAlarmList) {
96             LOG.info("Creating publisher for the following class {}", publisherAlarm);
97             topicManager.addAlarmTopic(publisherAlarm);
98         }
99         JsonStringConverter<NotificationTapiService> converterTapiService =
100                 new JsonStringConverter<>(bindingDOMCodecServices);
101         LOG.info("tapi converter: {}", converterTapiService);
102         topicManager.setTapiConverter(converterTapiService);
103
104         rpcRegistration = rpcProviderService.registerRpcImplementations(
105                 new GetNotificationsProcessServiceImpl(converterService, subscriberServer),
106                 new GetNotificationsAlarmServiceImpl(converterAlarmService, subscriberServer),
107                 new GetSupportedNotificationTypesImpl(this),
108                 new CreateNotificationSubscriptionServiceImpl(this, topicManager),
109                 new DeleteNotificationSubscriptionServiceImpl(networkTransactionService, topicManager),
110                 new GetNotificationSubscriptionServiceDetailsImpl(this),
111                 new GetNotificationSubscriptionServiceListImpl(this),
112                 new GetNotificationListImpl(converterTapiService, subscriberServer, networkTransactionService,
113                         topicManager));
114
115         NbiNotificationsHandler notificationsListener = new NbiNotificationsHandler(
116             topicManager.getProcessTopicMap(), topicManager.getAlarmTopicMap(), topicManager.getTapiTopicMap());
117         listenerRegistration = notificationService.registerCompositeListener(
118             notificationsListener.getCompositeListener());
119         topicManager.setNbiNotificationsListener(notificationsListener);
120         publishersServiceMap = topicManager.getProcessTopicMap();
121         publishersAlarmMap = topicManager.getAlarmTopicMap();
122         LOG.info("NbiNotificationsProvider Session Initiated");
123     }
124
125     /**
126      * Method called when the blueprint container is destroyed.
127      */
128     @Deactivate
129     public void close() {
130         for (Publisher<NotificationProcessService> publisher : publishersServiceMap.values()) {
131             publisher.close();
132         }
133         for (Publisher<NotificationAlarmService> publisherAlarm : publishersAlarmMap.values()) {
134             publisherAlarm.close();
135         }
136         rpcRegistration.close();
137         listenerRegistration.close();
138         LOG.info("NbiNotificationsProvider Closed");
139     }
140
141     public NotificationContext getNotificationContext() {
142         LOG.info("Getting tapi notification context");
143         try {
144             Optional<NotificationContext> notificationContextOptional = this.networkTransactionService.read(
145                     LogicalDatastoreType.OPERATIONAL,
146                     InstanceIdentifier.builder(Context.class)
147                         .augmentation(Context1.class).child(NotificationContext.class)
148                         .build())
149                 .get();
150             if (notificationContextOptional.isPresent()) {
151                 return notificationContextOptional.orElseThrow();
152             }
153             LOG.debug("notification context is empty");
154         } catch (InterruptedException | ExecutionException e) {
155             LOG.error("Caught exception getting Notification Context", e);
156         }
157         LOG.error("Could not get TAPI notification context");
158         return null;
159     }
160
161     public boolean updateNotificationContext(NotificationContext notificationContext1) {
162         try {
163             this.networkTransactionService.merge(
164                     LogicalDatastoreType.OPERATIONAL,
165                     InstanceIdentifier.builder(Context.class)
166                         .augmentation(Context1.class).child(NotificationContext.class)
167                         .build(),
168                     notificationContext1);
169             this.networkTransactionService.commit().get();
170             return true;
171         } catch (InterruptedException | ExecutionException e) {
172             LOG.error("Could not update TAPI notification context");
173         }
174         return false;
175     }
176
177 }