From 31634068044299fbc4298df2a6e698024da71dff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joakim=20T=C3=B6rnqvist?= Date: Mon, 19 Feb 2024 11:35:54 +0000 Subject: [PATCH] Refactor RendererServiceOperationsImpl notify MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Refactor three private methods in RendererServiceOperationsImpl into a separate class implementing the interface 'Notification' in package: org.opendaylight.transportpce.renderer.provisiondevice.notification The constructor in RendererServiceOperationsImpl requires an instance of 'Notification' instead of 'NotificationPublishService'. Meaning RendererServiceOperationsImpl is using NotificationPublishService indirectly as opposed to directly. The rest of the changes is a ripple effect due to the constructor being updated. JIRA: TRNSPRTPCE-616 Change-Id: Ia7e45116d44581fe2a54dc0b58843840a007d5fb Signed-off-by: Joakim Törnqvist --- .../tpce/module/TransportPCEImpl.java | 13 ++- .../RendererServiceOperationsImpl.java | 86 ++++---------- .../notification/NotificationSender.java | 110 ++++++++++++++++++ ...ndererServiceOperationsImplDeleteTest.java | 4 +- .../RendererServiceOperationsImplTest.java | 3 +- 5 files changed, 147 insertions(+), 69 deletions(-) create mode 100644 renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/notification/NotificationSender.java 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 d6e44ad9a..333236f4c 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 @@ -55,6 +55,7 @@ import org.opendaylight.transportpce.renderer.provisiondevice.OtnDeviceRendererS import org.opendaylight.transportpce.renderer.provisiondevice.OtnDeviceRendererServiceImpl; import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations; import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperationsImpl; +import org.opendaylight.transportpce.renderer.provisiondevice.notification.NotificationSender; import org.opendaylight.transportpce.renderer.rpcs.DeviceRendererRPCImpl; import org.opendaylight.transportpce.renderer.rpcs.TransportPCEServicePathRPCImpl; import org.opendaylight.transportpce.servicehandler.catalog.CatalogDataStoreOperationsImpl; @@ -165,8 +166,9 @@ public class TransportPCEImpl extends AbstractLightyModule implements TransportP RendererServiceOperations rendererServiceOperations = new RendererServiceOperationsImpl( deviceRendererService, otnDeviceRendererService, olmPowerServiceRpc, lgServBDB, - lgServBNPS, - portMapping); + new NotificationSender(lgServBNPS), + portMapping + ); ServiceDataStoreOperations serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(lgServBDB); RendererNotificationHandler rendererNotificationHandler = @@ -275,8 +277,11 @@ public class TransportPCEImpl extends AbstractLightyModule implements TransportP otnDeviceRendererService, olmPowerServiceRpc, lightyServices.getBindingDataBroker(), - lightyServices.getBindingNotificationPublishService(), - portMapping), + new NotificationSender( + lightyServices.getBindingNotificationPublishService() + ), + portMapping + ), lightyServices.getRpcProviderService()) .getRegisteredRpc()); } diff --git a/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImpl.java b/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImpl.java index b051192ad..651aefeb0 100644 --- a/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImpl.java +++ b/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImpl.java @@ -24,7 +24,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import org.opendaylight.mdsal.binding.api.DataBroker; -import org.opendaylight.mdsal.binding.api.NotificationPublishService; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.transportpce.common.ResponseCodes; import org.opendaylight.transportpce.common.StringConstants; @@ -33,6 +32,7 @@ import org.opendaylight.transportpce.common.mapping.PortMapping; import org.opendaylight.transportpce.common.service.ServiceTypes; import org.opendaylight.transportpce.renderer.ModelMappingUtils; import org.opendaylight.transportpce.renderer.ServicePathInputData; +import org.opendaylight.transportpce.renderer.provisiondevice.notification.Notification; import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServicePathDirection; import org.opendaylight.transportpce.renderer.provisiondevice.tasks.DeviceRenderingRollbackTask; import org.opendaylight.transportpce.renderer.provisiondevice.tasks.DeviceRenderingTask; @@ -50,8 +50,6 @@ import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.TransportpceOlmService; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output.Measurements; -import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSp; -import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSpBuilder; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteInput; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutput; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput; @@ -75,7 +73,6 @@ import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.olm.get.pm.input.ResourceIdentifierBuilder; import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.optical.renderer.nodes.Nodes; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.common.Uint32; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -104,7 +101,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations private final OtnDeviceRendererService otnDeviceRenderer; private final TransportpceOlmService olmService; private final DataBroker dataBroker; - private final NotificationPublishService notificationPublishService; + private final Notification notification; private final PortMapping portMapping; private ListeningExecutorService executor; @@ -113,13 +110,13 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations @Reference OtnDeviceRendererService otnDeviceRenderer, @Reference TransportpceOlmService olmService, @Reference DataBroker dataBroker, - @Reference NotificationPublishService notificationPublishService, + @Reference Notification notification, @Reference PortMapping portMapping) { this.deviceRenderer = deviceRenderer; this.otnDeviceRenderer = otnDeviceRenderer; this.olmService = olmService; this.dataBroker = dataBroker; - this.notificationPublishService = notificationPublishService; + this.notification = notification; this.portMapping = portMapping; this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS)); LOG.debug("RendererServiceOperationsImpl instantiated"); @@ -902,9 +899,13 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations String serviceName, RpcStatusEx rpcStatusEx, String message) { - send( - buildNotification(servicePathNotificationTypes, serviceName, rpcStatusEx, message, - null, null, null, null)); + + notification.send( + servicePathNotificationTypes, + serviceName, + rpcStatusEx, + message + ); } /** @@ -924,60 +925,19 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations Link notifLink, Set supportedLinks, String serviceType) { - send( - buildNotification(servicePathNotificationTypes, serviceName, rpcStatusEx, message, - pathDescription, notifLink, supportedLinks, serviceType)); - } - /** - * Build notification containing path description information. - * @param servicePathNotificationTypes ServicePathNotificationTypes - * @param serviceName String - * @param rpcStatusEx RpcStatusEx - * @param message String - * @param pathDescription PathDescription - * @return notification with RendererRpcResultSp type. - */ - private RendererRpcResultSp buildNotification( - ServicePathNotificationTypes servicePathNotificationTypes, - String serviceName, - RpcStatusEx rpcStatusEx, - String message, - PathDescription pathDescription, - Link notifLink, - Set supportedLinks, - String serviceType) { - RendererRpcResultSpBuilder builder = - new RendererRpcResultSpBuilder() - .setNotificationType(servicePathNotificationTypes).setServiceName(serviceName).setStatus(rpcStatusEx) - .setStatusMessage(message) - .setServiceType(serviceType); - if (pathDescription != null) { - builder - .setAToZDirection(pathDescription.getAToZDirection()) - .setZToADirection(pathDescription.getZToADirection()); - } - if (notifLink != null) { - builder.setLink(notifLink); - } - if (supportedLinks != null) { - builder.setLinkId(supportedLinks); - } - return builder.build(); - } - - /** - * Send renderer notification. - * @param notification Notification - */ - private void send(Notification notification) { - try { - LOG.info("Sending notification {}", notification); - notificationPublishService.putNotification(notification); - } catch (InterruptedException e) { - LOG.info("notification offer rejected: ", e); - Thread.currentThread().interrupt(); - } + notification.send( + notification.buildNotification( + servicePathNotificationTypes, + serviceName, + rpcStatusEx, + message, + pathDescription, + notifLink, + supportedLinks, + serviceType + ) + ); } private Link createLinkForNotif(List otnLinkTerminationPoints) { diff --git a/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/notification/NotificationSender.java b/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/notification/NotificationSender.java new file mode 100644 index 000000000..2856df7c6 --- /dev/null +++ b/renderer/src/main/java/org/opendaylight/transportpce/renderer/provisiondevice/notification/NotificationSender.java @@ -0,0 +1,110 @@ +/* + * Copyright © 2024 Smartoptics and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.transportpce.renderer.provisiondevice.notification; + +import java.util.Set; +import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSp; +import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSpBuilder; +import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.renderer.rpc.result.sp.Link; +import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.PathDescription; +import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx; +import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.ServicePathNotificationTypes; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component +public class NotificationSender implements Notification { + + private final NotificationPublishService notificationPublishService; + + private static final Logger LOG = LoggerFactory.getLogger(NotificationSender.class); + + @Activate + public NotificationSender(@Reference NotificationPublishService notificationPublishService) { + this.notificationPublishService = notificationPublishService; + } + + /** + * Send renderer notification. + */ + public void send(ServicePathNotificationTypes servicePathNotificationTypes, + String serviceName, + RpcStatusEx rpcStatusEx, + String message) { + + send( + buildNotification( + servicePathNotificationTypes, + serviceName, + rpcStatusEx, + message, + null, + null, + null, + null + ) + ); + } + + /** + * Send renderer notification. + * @param notification Notification + */ + public void send(org.opendaylight.yangtools.yang.binding.Notification notification) { + try { + LOG.info("Sending notification {}", notification); + notificationPublishService.putNotification(notification); + } catch (InterruptedException e) { + LOG.info("notification offer rejected: ", e); + Thread.currentThread().interrupt(); + } + } + + /** + * Build notification containing path description information. + * @param servicePathNotificationTypes ServicePathNotificationTypes + * @param serviceName String + * @param rpcStatusEx RpcStatusEx + * @param message String + * @param pathDescription PathDescription + * @return notification with RendererRpcResultSp type. + */ + @Override + public RendererRpcResultSp buildNotification(ServicePathNotificationTypes servicePathNotificationTypes, + String serviceName, + RpcStatusEx rpcStatusEx, + String message, + PathDescription pathDescription, + Link notifLink, + Set supportedLinks, + String serviceType) { + + RendererRpcResultSpBuilder builder = + new RendererRpcResultSpBuilder() + .setNotificationType(servicePathNotificationTypes).setServiceName(serviceName).setStatus(rpcStatusEx) + .setStatusMessage(message) + .setServiceType(serviceType); + if (pathDescription != null) { + builder + .setAToZDirection(pathDescription.getAToZDirection()) + .setZToADirection(pathDescription.getZToADirection()); + } + if (notifLink != null) { + builder.setLink(notifLink); + } + if (supportedLinks != null) { + builder.setLinkId(supportedLinks); + } + return builder.build(); + } +} diff --git a/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplDeleteTest.java b/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplDeleteTest.java index 0a20bde92..cf8eb6345 100644 --- a/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplDeleteTest.java +++ b/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplDeleteTest.java @@ -37,6 +37,7 @@ import org.opendaylight.transportpce.common.crossconnect.CrossConnect; import org.opendaylight.transportpce.common.device.DeviceTransactionManager; import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl; import org.opendaylight.transportpce.common.mapping.PortMapping; +import org.opendaylight.transportpce.renderer.provisiondevice.notification.NotificationSender; import org.opendaylight.transportpce.renderer.stub.OlmServiceStub; import org.opendaylight.transportpce.renderer.utils.NotificationPublishServiceMock; import org.opendaylight.transportpce.renderer.utils.ServiceDeleteDataUtils; @@ -91,7 +92,8 @@ public class RendererServiceOperationsImplDeleteTest extends AbstractTest { this.olmService = spy(this.olmService); NotificationPublishService notificationPublishService = new NotificationPublishServiceMock(); this.rendererServiceOperations = new RendererServiceOperationsImpl(deviceRenderer, - otnDeviceRendererService, olmService, getDataBroker(), notificationPublishService, portMapping); + otnDeviceRendererService, olmService, getDataBroker(), new NotificationSender(notificationPublishService), + portMapping); } diff --git a/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplTest.java b/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplTest.java index 63e347868..cb17336b6 100644 --- a/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplTest.java +++ b/renderer/src/test/java/org/opendaylight/transportpce/renderer/provisiondevice/RendererServiceOperationsImplTest.java @@ -39,6 +39,7 @@ import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfa import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121; import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221; import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710; +import org.opendaylight.transportpce.renderer.provisiondevice.notification.NotificationSender; import org.opendaylight.transportpce.renderer.stub.OlmServiceStub; import org.opendaylight.transportpce.renderer.utils.NotificationPublishServiceMock; import org.opendaylight.transportpce.renderer.utils.ServiceDataUtils; @@ -92,7 +93,7 @@ public class RendererServiceOperationsImplTest extends AbstractTest { NotificationPublishService notificationPublishService = new NotificationPublishServiceMock(); this.olmService = spy(this.olmService); this.rendererServiceOperations = new RendererServiceOperationsImpl(deviceRenderer, otnDeviceRendererService, - this.olmService, getDataBroker(), notificationPublishService, portMapping); + this.olmService, getDataBroker(), new NotificationSender(notificationPublishService), portMapping); } @Test -- 2.36.6