From f7873bd1a65592539a26e49ba84cd3c90cdca252 Mon Sep 17 00:00:00 2001 From: mpriyank Date: Tue, 7 May 2024 15:33:19 +0100 Subject: Conditional cps change events - introduced a parameter to control the cps core change event notifications. we should be able to disable these notifications without affecting other notification flows. - fixed the LayeredArchitectureTest as we are accessing the Anchor model in the events package now. Issue-ID: CPS-2213 Change-Id: Id875925bc14de1cc6e8fa3193c0df470e09fe43f Signed-off-by: mpriyank --- cps-application/src/main/resources/application.yml | 3 +++ .../cps/architecture/LayeredArchitectureTest.java | 5 +++-- .../cps/events/CpsDataUpdateEventsService.java | 8 ++++++-- .../events/CpsDataUpdateEventsServiceSpec.groovy | 24 ++++++++++++++++------ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index 68dd31b53..810068081 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -116,8 +116,11 @@ app: topic: ${DMI_DEVICE_HEARTBEAT_TOPIC:dmi-device-heartbeat} cps: data-updated: + change-event-notifications-enabled: ${CPS_CHANGE_EVENT_NOTIFICATIONS_ENABLED:true} topic: ${CPS_CHANGE_EVENT_TOPIC:cps-data-updated-events} + + notification: enabled: true async: diff --git a/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java b/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java index ec16ceefe..c18a3ed10 100644 --- a/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java +++ b/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,7 @@ public class LayeredArchitectureTest { private static final String CPS_UTILS_PACKAGE = "org.onap.cps.utils.."; private static final String NCMP_INIT_PACKAGE = "org.onap.cps.ncmp.init.."; private static final String CPS_CACHE_PACKAGE = "org.onap.cps.cache.."; + private static final String CPS_EVENTS_PACKAGE = "org.onap.cps.events.."; @ArchTest static final ArchRule restControllerShouldOnlyDependOnRestController = @@ -57,7 +58,7 @@ public class LayeredArchitectureTest { .or().resideInAPackage(SPI_SERVICE_PACKAGE).should().onlyHaveDependentClassesThat() .resideInAnyPackage(REST_CONTROLLER_PACKAGE, API_SERVICE_PACKAGE, SPI_SERVICE_PACKAGE, NCMP_REST_PACKAGE, NCMP_SERVICE_PACKAGE, YANG_SCHEMA_PACKAGE, NOTIFICATION_PACKAGE, CPS_UTILS_PACKAGE, NCMP_INIT_PACKAGE, - CPS_CACHE_PACKAGE)); + CPS_CACHE_PACKAGE, CPS_EVENTS_PACKAGE)); @ArchTest diff --git a/cps-service/src/main/java/org/onap/cps/events/CpsDataUpdateEventsService.java b/cps-service/src/main/java/org/onap/cps/events/CpsDataUpdateEventsService.java index d38432dfa..109783488 100644 --- a/cps-service/src/main/java/org/onap/cps/events/CpsDataUpdateEventsService.java +++ b/cps-service/src/main/java/org/onap/cps/events/CpsDataUpdateEventsService.java @@ -46,6 +46,9 @@ public class CpsDataUpdateEventsService { @Value("${app.cps.data-updated.topic:cps-data-updated-events}") private String topicName; + @Value("${app.cps.data-updated.change-event-notifications-enabled:true}") + private boolean cpsChangeEventNotificationsEnabled; + @Value("${notification.enabled:false}") private boolean notificationsEnabled; @@ -60,7 +63,7 @@ public class CpsDataUpdateEventsService { @Timed(value = "cps.dataupdate.events.publish", description = "Time taken to publish Data Update event") public void publishCpsDataUpdateEvent(final Anchor anchor, final String xpath, final Operation operation, final OffsetDateTime observedTimestamp) { - if (notificationsEnabled) { + if (notificationsEnabled && cpsChangeEventNotificationsEnabled) { final CpsDataUpdatedEvent cpsDataUpdatedEvent = createCpsDataUpdatedEvent(anchor, observedTimestamp, xpath, operation); final String updateEventId = anchor.getDataspaceName() + ":" + anchor.getName(); @@ -70,7 +73,8 @@ public class CpsDataUpdateEventsService { .extensions(extensions).build().asCloudEvent(); eventsPublisher.publishCloudEvent(topicName, updateEventId, cpsDataUpdatedEventAsCloudEvent); } else { - log.debug("Notifications disabled."); + log.debug("State of Overall Notifications : {} and Cps Change Event Notifications : {}", + notificationsEnabled, cpsChangeEventNotificationsEnabled); } } diff --git a/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy index 24b9ab5d7..11842645c 100644 --- a/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2024 TechMahindra Ltd. + * Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +30,6 @@ import io.cloudevents.CloudEvent import io.cloudevents.core.CloudEventUtils import io.cloudevents.jackson.PojoCloudEventDataMapper import org.onap.cps.events.model.CpsDataUpdatedEvent -import org.onap.cps.events.model.Data import org.onap.cps.spi.model.Anchor import org.onap.cps.utils.JsonObjectMapper import org.springframework.test.context.ContextConfiguration @@ -40,7 +40,6 @@ import java.time.OffsetDateTime @ContextConfiguration(classes = [ObjectMapper, JsonObjectMapper]) class CpsDataUpdateEventsServiceSpec extends Specification { def mockEventsPublisher = Mock(EventsPublisher) - def notificationsEnabled = true def objectMapper = new ObjectMapper(); def objectUnderTest = new CpsDataUpdateEventsService(mockEventsPublisher) @@ -52,6 +51,8 @@ class CpsDataUpdateEventsServiceSpec extends Specification { def observedTimestamp = OffsetDateTime.now() and: 'notificationsEnabled is #notificationsEnabled and it will be true as default' objectUnderTest.notificationsEnabled = true + and: 'cpsChangeEventNotificationsEnabled is also true' + objectUnderTest.cpsChangeEventNotificationsEnabled = true when: 'service is called to publish data update event' objectUnderTest.topicName = "cps-core-event" objectUnderTest.publishCpsDataUpdateEvent(anchor, xpath, operation, observedTimestamp) @@ -79,18 +80,27 @@ class CpsDataUpdateEventsServiceSpec extends Specification { 'non root node xpath and delete operation' | '/test/path' | DELETE || UPDATE } - def 'publish cps update event when notification service is disabled'() { + def 'publish cps update event when #scenario'() { given: 'an anchor, operation and observed timestamp' def anchor = new Anchor('anchor01', 'dataspace01', 'schema01'); def operation = CREATE def observedTimestamp = OffsetDateTime.now() - and: 'notificationsEnabled is false' - objectUnderTest.notificationsEnabled = false + and: 'notificationsEnabled is #notificationsEnabled' + objectUnderTest.notificationsEnabled = notificationsEnabled + and: 'cpsChangeEventNotificationsEnabled is #cpsChangeEventNotificationsEnabled' + objectUnderTest.cpsChangeEventNotificationsEnabled = cpsChangeEventNotificationsEnabled when: 'service is called to publish data update event' objectUnderTest.topicName = "cps-core-event" objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp) then: 'the event contains the required attributes' - 0 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) + expectedCallToPublisher * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) + where: 'below scenarios are present' + scenario | notificationsEnabled | cpsChangeEventNotificationsEnabled || expectedCallToPublisher + 'both notifications enabled' | true | true || 1 + 'both notifications disabled' | false | false || 0 + 'only CPS change event notification enabled' | false | true || 0 + 'only overall notification enabled' | true | false || 0 + } def 'publish cps update event when no timestamp provided'() { @@ -100,6 +110,8 @@ class CpsDataUpdateEventsServiceSpec extends Specification { def observedTimestamp = null and: 'notificationsEnabled is true' objectUnderTest.notificationsEnabled = true + and: 'cpsChangeEventNotificationsEnabled is true' + objectUnderTest.cpsChangeEventNotificationsEnabled = true when: 'service is called to publish data update event' objectUnderTest.topicName = "cps-core-event" objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp) -- cgit 1.2.3-korg