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