Conditional cps change events 54/137854/3
authormpriyank <priyank.maheshwari@est.tech>
Tue, 7 May 2024 14:33:19 +0000 (15:33 +0100)
committermpriyank <priyank.maheshwari@est.tech>
Wed, 8 May 2024 13:09:39 +0000 (14:09 +0100)
- 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 <priyank.maheshwari@est.tech>
cps-application/src/main/resources/application.yml
cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java
cps-service/src/main/java/org/onap/cps/events/CpsDataUpdateEventsService.java
cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy

index 68dd31b..8100680 100644 (file)
@@ -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:
index ec16cee..c18a3ed 100644 (file)
@@ -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
index d38432d..1097834 100644 (file)
@@ -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);
         }
     }
 
index 24b9ab5..1184264 100644 (file)
@@ -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)