2 * Copyright © 2020 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.nbinotifications.impl;
10 import java.util.HashMap;
11 import java.util.List;
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;
49 @Component(configurationPid = "org.opendaylight.transportpce.nbinotifications")
50 public class NbiNotificationsProvider {
52 @ObjectClassDefinition
53 public @interface Configuration {
55 String suscriberServer() default "";
57 String publisherServer() default "";
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;
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);
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",
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);
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);
100 JsonStringConverter<NotificationTapiService> converterTapiService =
101 new JsonStringConverter<>(bindingDOMCodecServices);
102 LOG.info("baozhi tapi converter: {}", converterTapiService);
103 topicManager.setTapiConverter(converterTapiService);
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,
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");
125 * Method called when the blueprint container is destroyed.
128 public void close() {
129 for (Publisher<NotificationProcessService> publisher : publishersServiceMap.values()) {
132 for (Publisher<NotificationAlarmService> publisherAlarm : publishersAlarmMap.values()) {
133 publisherAlarm.close();
135 rpcRegistration.close();
136 listenerRegistration.close();
137 LOG.info("NbiNotificationsProvider Closed");
140 public NotificationContext getNotificationContext() {
141 LOG.info("Getting tapi notification context");
143 Optional<NotificationContext> notificationContextOptional = this.networkTransactionService.read(
144 LogicalDatastoreType.OPERATIONAL,
145 InstanceIdentifier.builder(Context.class)
146 .augmentation(Context1.class).child(NotificationContext.class)
149 if (notificationContextOptional.isPresent()) {
150 return notificationContextOptional.orElseThrow();
152 LOG.debug("notification context is empty");
153 } catch (InterruptedException | ExecutionException e) {
154 LOG.error("Caught exception getting Notification Context", e);
156 LOG.error("Could not get TAPI notification context");
160 public boolean updateNotificationContext(NotificationContext notificationContext1) {
162 this.networkTransactionService.merge(
163 LogicalDatastoreType.OPERATIONAL,
164 InstanceIdentifier.builder(Context.class)
165 .augmentation(Context1.class).child(NotificationContext.class)
167 notificationContext1);
168 this.networkTransactionService.commit().get();
170 } catch (InterruptedException | ExecutionException e) {
171 LOG.error("Could not update TAPI notification context");