From 0f72333c10ec4a277d102d3110e62e6945282a15 Mon Sep 17 00:00:00 2001 From: Gilles Thouenon Date: Thu, 16 Mar 2023 15:22:38 +0100 Subject: [PATCH] Remove nobinotifications-blueprint.xml file - convert NbiNotificationsProvider into a Component - adapt NbiNotificationsProviderTest accordingly JIRA: TRNSPRTPCE-736 Signed-off-by: Gilles Thouenon Change-Id: I298b7d1bf6f18f06a0bb3687f4df2363311629cf (cherry picked from commit 0e5a378adbacbe904865e220a01fded2f597ff07) --- .../tpce/module/TransportPCEImpl.java | 13 +-- nbinotifications/pom.xml | 8 ++ .../impl/NbiNotificationsProvider.java | 100 ++++++++++-------- .../blueprint/nobinotifications-blueprint.xml | 42 -------- .../impl/NbiNotificationsProviderTest.java | 10 +- 5 files changed, 68 insertions(+), 105 deletions(-) delete mode 100644 nbinotifications/src/main/resources/OSGI-INF/blueprint/nobinotifications-blueprint.xml diff --git a/lighty/src/main/java/io/lighty/controllers/tpce/module/TransportPCEImpl.java b/lighty/src/main/java/io/lighty/controllers/tpce/module/TransportPCEImpl.java index 19b5523a5..8eb97e47f 100644 --- a/lighty/src/main/java/io/lighty/controllers/tpce/module/TransportPCEImpl.java +++ b/lighty/src/main/java/io/lighty/controllers/tpce/module/TransportPCEImpl.java @@ -9,10 +9,6 @@ package io.lighty.controllers.tpce.module; import io.lighty.core.controller.api.AbstractLightyModule; import io.lighty.core.controller.api.LightyServices; - -import java.util.Arrays; -import java.util.List; - import org.opendaylight.transportpce.common.crossconnect.CrossConnect; import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl; import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121; @@ -108,12 +104,6 @@ public class TransportPCEImpl extends AbstractLightyModule implements TransportP private TapiProvider tapiProvider; // nbi-notifications beans private NbiNotificationsProvider nbiNotificationsProvider; - /** - * List of publisher topics. - */ - private final List publisherServiceList = Arrays.asList("PceListener", "ServiceHandlerOperations", - "ServiceHandler", "RendererListener"); - private final List publisherAlarmList = Arrays.asList("ServiceListener"); public TransportPCEImpl(LightyServices lightyServices, boolean activateNbiNotification, boolean activateTapi, String olmtimer1, String olmtimer2) { @@ -207,7 +197,7 @@ public class TransportPCEImpl extends AbstractLightyModule implements TransportP if (activateNbiNotification) { LOG.info("Creating nbi-notifications beans ..."); nbiNotificationsProvider = new NbiNotificationsProvider( - publisherServiceList, publisherAlarmList, null, null, lightyServices.getRpcProviderService(), + null, null, lightyServices.getRpcProviderService(), lightyServices.getNotificationService(), lightyServices.getAdapterContext().currentSerializer(), networkTransaction); } @@ -220,7 +210,6 @@ public class TransportPCEImpl extends AbstractLightyModule implements TransportP } if (nbiNotificationsProvider != null) { LOG.info("Initializing nbi-notifications provider ..."); - nbiNotificationsProvider.init(); } LOG.info("Init done."); return true; diff --git a/nbinotifications/pom.xml b/nbinotifications/pom.xml index 7c16ea13f..344e24991 100644 --- a/nbinotifications/pom.xml +++ b/nbinotifications/pom.xml @@ -98,6 +98,14 @@ kafka-clients ${kafka.version} + + org.osgi + org.osgi.service.component.annotations + + + org.osgi + org.osgi.service.metatype.annotations + diff --git a/nbinotifications/src/main/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProvider.java b/nbinotifications/src/main/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProvider.java index 863d689a3..ac4c6ef8e 100644 --- a/nbinotifications/src/main/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProvider.java +++ b/nbinotifications/src/main/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProvider.java @@ -7,6 +7,7 @@ */ package org.opendaylight.transportpce.nbinotifications.impl; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,72 +27,84 @@ import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapi 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.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Component(configurationPid = "org.opendaylight.transportpce.nbinotifications") public class NbiNotificationsProvider { + @ObjectClassDefinition + public @interface Configuration { + @AttributeDefinition + String suscriberServer() default ""; + @AttributeDefinition + String publisherServer() default ""; + } + private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class); private static Map> publishersServiceMap = new HashMap<>(); private static Map> publishersAlarmMap = new HashMap<>(); - private final RpcProviderService rpcService; - private final NotificationService notificationService; - private final JsonStringConverter converterService; - private final JsonStringConverter converterAlarmService; - private final JsonStringConverter converterTapiService; - private final String subscriberServer; - private ObjectRegistration rpcRegistration; private ListenerRegistration listenerRegistration; - private TopicManager topicManager = TopicManager.getInstance(); - private final NetworkTransactionService networkTransactionService; + private List> rpcRegistrations = new ArrayList<>(); + @Activate + public NbiNotificationsProvider(@Reference RpcProviderService rpcProviderService, + @Reference NotificationService notificationService, + @Reference BindingDOMCodecServices bindingDOMCodecServices, + @Reference NetworkTransactionService networkTransactionService, + final Configuration configuration) { + this(configuration.suscriberServer(), configuration.publisherServer(), rpcProviderService, notificationService, + bindingDOMCodecServices, networkTransactionService); + } - public NbiNotificationsProvider(List publishersService, List publishersAlarm, - String subscriberServer, String publisherServer, - RpcProviderService rpcProviderService, NotificationService notificationService, - BindingDOMCodecServices bindingDOMCodecServices, - NetworkTransactionService networkTransactionService) { - this.rpcService = rpcProviderService; - this.notificationService = notificationService; - this.topicManager.setPublisherServer(publisherServer); - converterService = new JsonStringConverter<>(bindingDOMCodecServices); - this.topicManager.setProcessConverter(converterService); - for (String publisherService: publishersService) { + public NbiNotificationsProvider(String subscriberServer, String publisherServer, + RpcProviderService rpcProviderService, NotificationService notificationService, + BindingDOMCodecServices bindingDOMCodecServices, NetworkTransactionService networkTransactionService) { + List publishersServiceList = List.of("PceListener", "ServiceHandlerOperations", "ServiceHandler", + "RendererListener"); + TopicManager topicManager = TopicManager.getInstance(); + topicManager.setPublisherServer(publisherServer); + JsonStringConverter converterService = + new JsonStringConverter<>(bindingDOMCodecServices); + topicManager.setProcessConverter(converterService); + for (String publisherService: publishersServiceList) { LOG.info("Creating publisher for the following class {}", publisherService); - this.topicManager.addProcessTopic(publisherService); + topicManager.addProcessTopic(publisherService); } - converterAlarmService = new JsonStringConverter<>(bindingDOMCodecServices); - this.topicManager.setAlarmConverter(converterAlarmService); - for (String publisherAlarm: publishersAlarm) { + JsonStringConverter converterAlarmService = + new JsonStringConverter<>(bindingDOMCodecServices); + topicManager.setAlarmConverter(converterAlarmService); + List publishersAlarmList = List.of("ServiceListener"); + for (String publisherAlarm: publishersAlarmList) { LOG.info("Creating publisher for the following class {}", publisherAlarm); - this.topicManager.addAlarmTopic(publisherAlarm); + topicManager.addAlarmTopic(publisherAlarm); } - this.subscriberServer = subscriberServer; - converterTapiService = new JsonStringConverter<>(bindingDOMCodecServices); + JsonStringConverter converterTapiService = + new JsonStringConverter<>(bindingDOMCodecServices); LOG.info("baozhi tapi converter: {}", converterTapiService); - this.topicManager.setTapiConverter(converterTapiService); - this.networkTransactionService = networkTransactionService; - } + topicManager.setTapiConverter(converterTapiService); - /** - * Method called when the blueprint container is created. - */ - public void init() { - LOG.info("NbiNotificationsProvider Session Initiated"); NbiNotificationsImpl nbiImpl = new NbiNotificationsImpl(converterService, converterAlarmService, - converterTapiService, subscriberServer, this.networkTransactionService, this.topicManager); - rpcRegistration = rpcService.registerRpcImplementation(NbiNotificationsService.class, nbiImpl); - rpcService.registerRpcImplementation(TapiNotificationService.class, nbiImpl); - NbiNotificationsListenerImpl nbiNotificationsListener = - new NbiNotificationsListenerImpl(this.topicManager.getProcessTopicMap(), - this.topicManager.getAlarmTopicMap(), this.topicManager.getTapiTopicMap()); + 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); - this.topicManager.setNbiNotificationsListener(nbiNotificationsListener); + topicManager.setNbiNotificationsListener(nbiNotificationsListener); + LOG.info("NbiNotificationsProvider Session Initiated"); } /** * Method called when the blueprint container is destroyed. */ + @Deactivate public void close() { for (Publisher publisher : publishersServiceMap.values()) { publisher.close(); @@ -99,9 +112,8 @@ public class NbiNotificationsProvider { for (Publisher publisherAlarm : publishersAlarmMap.values()) { publisherAlarm.close(); } - rpcRegistration.close(); + rpcRegistrations.forEach(reg -> reg.close()); listenerRegistration.close(); LOG.info("NbiNotificationsProvider Closed"); } - } diff --git a/nbinotifications/src/main/resources/OSGI-INF/blueprint/nobinotifications-blueprint.xml b/nbinotifications/src/main/resources/OSGI-INF/blueprint/nobinotifications-blueprint.xml deleted file mode 100644 index 5b0176c3a..000000000 --- a/nbinotifications/src/main/resources/OSGI-INF/blueprint/nobinotifications-blueprint.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - PceListener - ServiceHandlerOperations - ServiceHandler - RendererListener - - - - - ServiceListener - - - - - - - - - - diff --git a/nbinotifications/src/test/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProviderTest.java b/nbinotifications/src/test/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProviderTest.java index bbf2e62e0..68a93c328 100644 --- a/nbinotifications/src/test/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProviderTest.java +++ b/nbinotifications/src/test/java/org/opendaylight/transportpce/nbinotifications/impl/NbiNotificationsProviderTest.java @@ -11,7 +11,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import java.util.Arrays; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -28,7 +27,6 @@ public class NbiNotificationsProviderTest extends AbstractTest { @Mock RpcProviderService rpcProviderRegistry; - @Mock private NotificationService notificationService; @@ -40,11 +38,9 @@ public class NbiNotificationsProviderTest extends AbstractTest { @Test void initTest() { networkTransactionService = new NetworkTransactionImpl(getDataBroker()); - NbiNotificationsProvider provider = new NbiNotificationsProvider( - Arrays.asList("topic1", "topic2"), Arrays.asList("topic1", "topic2"), "localhost:8080", - "localhost:8080", rpcProviderRegistry, notificationService, - getDataStoreContextUtil().getBindingDOMCodecServices(), networkTransactionService); - provider.init(); + NbiNotificationsProvider provider = new NbiNotificationsProvider("localhost:8080", "localhost:8080", + rpcProviderRegistry, notificationService, getDataStoreContextUtil().getBindingDOMCodecServices(), + networkTransactionService); verify(rpcProviderRegistry, times(2)) .registerRpcImplementation(any(), any(NbiNotificationsImpl.class)); verify(notificationService, times(1)) -- 2.36.6