Refine the RPC implementation registration
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / NbiNotificationsProvider.java
index ac4c6ef8e61660e4d9b4e32a496e9185fa2c98dc..7eda70e1e864e439de77a8fc414495aad2444021 100644 (file)
@@ -7,26 +7,36 @@
  */
 package org.opendaylight.transportpce.nbinotifications.impl;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import org.opendaylight.mdsal.binding.api.NotificationService;
 import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
-import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsListenerImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.CreateNotificationSubscriptionServiceImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.DeleteNotificationSubscriptionServiceImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationListImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationSubscriptionServiceDetailsImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationSubscriptionServiceListImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationsAlarmServiceImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetNotificationsProcessServiceImpl;
+import org.opendaylight.transportpce.nbinotifications.impl.rpc.GetSupportedNotificationTypesImpl;
+import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsHandler;
 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NbiNotificationsListener;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NbiNotificationsService;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
-import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.TapiNotificationService;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationAlarmService;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationProcessService;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationTapiService;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.Context1;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.context.NotificationContext;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
@@ -50,8 +60,9 @@ public class NbiNotificationsProvider {
     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
     private static Map<String, Publisher<NotificationProcessService>> publishersServiceMap =  new HashMap<>();
     private static Map<String, Publisher<NotificationAlarmService>> publishersAlarmMap =  new HashMap<>();
-    private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
-    private List<ObjectRegistration<NbiNotificationsImpl>> rpcRegistrations = new ArrayList<>();
+    private Registration listenerRegistration;
+    private Registration rpcRegistration;
+    private NetworkTransactionService networkTransactionService;
 
     @Activate
     public NbiNotificationsProvider(@Reference RpcProviderService rpcProviderService,
@@ -66,6 +77,7 @@ public class NbiNotificationsProvider {
     public NbiNotificationsProvider(String subscriberServer, String publisherServer,
             RpcProviderService rpcProviderService, NotificationService notificationService,
             BindingDOMCodecServices bindingDOMCodecServices, NetworkTransactionService networkTransactionService) {
+        this.networkTransactionService = networkTransactionService;
         List<String> publishersServiceList = List.of("PceListener", "ServiceHandlerOperations", "ServiceHandler",
                 "RendererListener");
         TopicManager topicManager = TopicManager.getInstance();
@@ -90,14 +102,22 @@ public class NbiNotificationsProvider {
         LOG.info("baozhi tapi converter: {}", converterTapiService);
         topicManager.setTapiConverter(converterTapiService);
 
-        NbiNotificationsImpl nbiImpl = new NbiNotificationsImpl(converterService, converterAlarmService,
-            converterTapiService, subscriberServer, networkTransactionService, topicManager);
-        rpcRegistrations.add(rpcProviderService.registerRpcImplementation(NbiNotificationsService.class, nbiImpl));
-        rpcRegistrations.add(rpcProviderService.registerRpcImplementation(TapiNotificationService.class, nbiImpl));
-        NbiNotificationsListenerImpl nbiNotificationsListener = new NbiNotificationsListenerImpl(
-                topicManager.getProcessTopicMap(), topicManager.getAlarmTopicMap(), topicManager.getTapiTopicMap());
-        listenerRegistration = notificationService.registerNotificationListener(nbiNotificationsListener);
-        topicManager.setNbiNotificationsListener(nbiNotificationsListener);
+        rpcRegistration = rpcProviderService.registerRpcImplementations(
+                new GetNotificationsProcessServiceImpl(converterService, subscriberServer),
+                new GetNotificationsAlarmServiceImpl(converterAlarmService, subscriberServer),
+                new GetSupportedNotificationTypesImpl(this),
+                new CreateNotificationSubscriptionServiceImpl(this, topicManager),
+                new DeleteNotificationSubscriptionServiceImpl(networkTransactionService, topicManager),
+                new GetNotificationSubscriptionServiceDetailsImpl(this),
+                new GetNotificationSubscriptionServiceListImpl(this),
+                new GetNotificationListImpl(converterTapiService, subscriberServer, networkTransactionService,
+                        topicManager));
+
+        NbiNotificationsHandler notificationsListener = new NbiNotificationsHandler(
+            topicManager.getProcessTopicMap(), topicManager.getAlarmTopicMap(), topicManager.getTapiTopicMap());
+        listenerRegistration = notificationService.registerCompositeListener(
+            notificationsListener.getCompositeListener());
+        topicManager.setNbiNotificationsListener(notificationsListener);
         LOG.info("NbiNotificationsProvider Session Initiated");
     }
 
@@ -112,8 +132,45 @@ public class NbiNotificationsProvider {
         for (Publisher<NotificationAlarmService> publisherAlarm : publishersAlarmMap.values()) {
             publisherAlarm.close();
         }
-        rpcRegistrations.forEach(reg -> reg.close());
+        rpcRegistration.close();
         listenerRegistration.close();
         LOG.info("NbiNotificationsProvider Closed");
     }
+
+    public NotificationContext getNotificationContext() {
+        LOG.info("Getting tapi notification context");
+        try {
+            Optional<NotificationContext> notificationContextOptional = this.networkTransactionService.read(
+                    LogicalDatastoreType.OPERATIONAL,
+                    InstanceIdentifier.builder(Context.class)
+                        .augmentation(Context1.class).child(NotificationContext.class)
+                        .build())
+                .get();
+            if (notificationContextOptional.isPresent()) {
+                return notificationContextOptional.orElseThrow();
+            }
+            LOG.debug("notification context is empty");
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Caught exception getting Notification Context", e);
+        }
+        LOG.error("Could not get TAPI notification context");
+        return null;
+    }
+
+    public boolean updateNotificationContext(NotificationContext notificationContext1) {
+        try {
+            this.networkTransactionService.merge(
+                    LogicalDatastoreType.OPERATIONAL,
+                    InstanceIdentifier.builder(Context.class)
+                        .augmentation(Context1.class).child(NotificationContext.class)
+                        .build(),
+                    notificationContext1);
+            this.networkTransactionService.commit().get();
+            return true;
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Could not update TAPI notification context");
+        }
+        return false;
+    }
+
 }