diff options
Diffstat (limited to 'cps-service')
11 files changed, 187 insertions, 144 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java index f455e47efd..d2482d50a6 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2021 Bell Canada + * Modifications Copyright (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ package org.onap.cps.api; import java.time.OffsetDateTime; -import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; @@ -40,8 +39,7 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String jsonData, - OffsetDateTime observedTimestamp); + void saveData(String dataspaceName, String anchorName, String jsonData, OffsetDateTime observedTimestamp); /** * Persists child data fragment under existing data node for the given anchor and dataspace. @@ -52,8 +50,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void saveData(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Persists child data fragment representing one or more list elements under existing data node for the @@ -65,9 +63,8 @@ public interface CpsDataService { * @param jsonData json data representing list element(s) * @param observedTimestamp observedTimestamp */ - void saveListElements(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void saveListElements(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Retrieves datanode by XPath for given dataspace and anchor. @@ -79,8 +76,8 @@ public interface CpsDataService { * (recursively) as well * @return data node object */ - DataNode getDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull FetchDescendantsOption fetchDescendantsOption); + DataNode getDataNode(String dataspaceName, String anchorName, String xpath, + FetchDescendantsOption fetchDescendantsOption); /** * Updates data node for given dataspace and anchor using xpath to parent node. @@ -91,8 +88,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void updateNodeLeaves(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void updateNodeLeaves(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Replaces existing data node content including descendants. @@ -103,8 +100,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void replaceNodeTree(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void replaceNodeTree(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Replaces list content by removing all existing elements and inserting the given new elements as json @@ -116,8 +113,8 @@ public interface CpsDataService { * @param jsonData json data representing the new list elements * @param observedTimestamp observedTimestamp */ - void replaceListContent(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void replaceListContent(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Deletes data node for given anchor and dataspace. @@ -127,8 +124,17 @@ public interface CpsDataService { * @param dataNodeXpath data node xpath * @param observedTimestamp observed timestamp */ - void deleteDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String dataNodeXpath, - OffsetDateTime observedTimestamp); + void deleteDataNode(String dataspaceName, String anchorName, String dataNodeXpath, + OffsetDateTime observedTimestamp); + + /** + * Deletes all data nodes for a given anchor in a dataspace. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @param observedTimestamp observed timestamp + */ + void deleteDataNodes(String dataspaceName, String anchorName, OffsetDateTime observedTimestamp); /** * Deletes a list or a list-element under given anchor and dataspace. @@ -138,8 +144,8 @@ public interface CpsDataService { * @param listElementXpath list element xpath * @param observedTimestamp observedTimestamp */ - void deleteListOrListElement(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String listElementXpath, OffsetDateTime observedTimestamp); + void deleteListOrListElement(String dataspaceName, String anchorName, String listElementXpath, + OffsetDateTime observedTimestamp); /** * Updates leaves of DataNode for given dataspace and anchor using xpath, along with the leaves of each Child Data diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java index d30a6571d8..1013addbe1 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java @@ -22,19 +22,24 @@ package org.onap.cps.api.impl; +import java.time.OffsetDateTime; import java.util.Collection; import java.util.stream.Collectors; +import lombok.AllArgsConstructor; import org.onap.cps.api.CpsAdminService; +import org.onap.cps.api.CpsDataService; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.model.Anchor; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component("CpsAdminServiceImpl") +@AllArgsConstructor(onConstructor = @__(@Lazy)) public class CpsAdminServiceImpl implements CpsAdminService { - @Autowired - private CpsAdminPersistenceService cpsAdminPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; + @Lazy + private final CpsDataService cpsDataService; @Override public void createDataspace(final String dataspaceName) { @@ -68,6 +73,7 @@ public class CpsAdminServiceImpl implements CpsAdminService { @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { + cpsDataService.deleteDataNodes(dataspaceName, anchorName, OffsetDateTime.now()); cpsAdminPersistenceService.deleteAnchor(dataspaceName, anchorName); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java index a23bc95f3e..af06e5fc13 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java @@ -24,6 +24,7 @@ package org.onap.cps.api.impl; import java.time.OffsetDateTime; import java.util.Collection; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; @@ -32,31 +33,25 @@ import org.onap.cps.notification.Operation; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.DataValidationException; +import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.DataNodeBuilder; import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @Slf4j +@AllArgsConstructor public class CpsDataServiceImpl implements CpsDataService { private static final String ROOT_NODE_XPATH = "/"; - @Autowired - private CpsDataPersistenceService cpsDataPersistenceService; - - @Autowired - private CpsAdminService cpsAdminService; - - @Autowired - private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; - - @Autowired - private NotificationService notificationService; + private final CpsDataPersistenceService cpsDataPersistenceService; + private final CpsAdminService cpsAdminService; + private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; + private final NotificationService notificationService; @Override public void saveData(final String dataspaceName, final String anchorName, final String jsonData, @@ -138,6 +133,14 @@ public class CpsDataServiceImpl implements CpsDataService { } @Override + public void deleteDataNodes(final String dataspaceName, final String anchorName, + final OffsetDateTime observedTimestamp) { + final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName); + processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp); + } + + @Override public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath, final OffsetDateTime observedTimestamp) { cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath); @@ -185,9 +188,16 @@ public class CpsDataServiceImpl implements CpsDataService { private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, final OffsetDateTime observedTimestamp, final String xpath, final Operation operation) { + final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp); + } + + private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation, + final OffsetDateTime observedTimestamp) { try { - notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, xpath, operation); + notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation); } catch (final Exception exception) { + //If async message can't be queued for notification service, the initial request should not failed. log.error("Failed to send message to notification service", exception); } } diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java index 6054ce5d76..e7b639d48e 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java +++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java @@ -25,7 +25,7 @@ import java.net.URISyntaxException; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; -import org.onap.cps.api.CpsAdminService; +import lombok.AllArgsConstructor; import org.onap.cps.api.CpsDataService; import org.onap.cps.event.model.Content; import org.onap.cps.event.model.CpsDataUpdatedEvent; @@ -38,6 +38,7 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component +@AllArgsConstructor(onConstructor = @__(@Lazy)) public class CpsDataUpdatedEventFactory { private static final URI EVENT_SCHEMA; @@ -56,29 +57,22 @@ public class CpsDataUpdatedEventFactory { } } + @Lazy private final CpsDataService cpsDataService; - private final CpsAdminService cpsAdminService; - - public CpsDataUpdatedEventFactory(@Lazy final CpsDataService cpsDataService, - final CpsAdminService cpsAdminService) { - this.cpsDataService = cpsDataService; - this.cpsAdminService = cpsAdminService; - } /** * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used. * - * @param dataspaceName dataspaceName - * @param anchorName anchorName + * @param anchor anchor * @param observedTimestamp observedTimestamp * @param operation operation * @return CpsDataUpdatedEvent */ - public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName, + public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, final Operation operation) { - final var dataNode = cpsDataService - .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); - final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + final var dataNode = (operation == Operation.DELETE) ? null : + cpsDataService.getDataNode(anchor.getDataspaceName(), anchor.getName(), + "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation); } @@ -105,10 +99,12 @@ public class CpsDataUpdatedEventFactory { content.withAnchorName(anchor.getName()); content.withDataspaceName(anchor.getDataspaceName()); content.withSchemaSetName(anchor.getSchemaSetName()); - content.withData(createData(dataNode)); content.withOperation(Content.Operation.fromValue(operation.name())); content.withObservedTimestamp( DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp)); + if (dataNode != null) { + content.withData(createData(dataNode)); + } return content; } } diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java index 97a14797b5..5ad59df2aa 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java @@ -29,6 +29,7 @@ import java.util.concurrent.Future; import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.spi.model.Anchor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -80,22 +81,20 @@ public class NotificationService { /** * Process Data Updated Event and publishes the notification. * - * @param dataspaceName dataspace name - * @param anchorName anchor name + * @param anchor anchor * @param observedTimestamp observedTimestamp * @param xpath xpath of changed data node * @param operation operation * @return future */ @Async("notificationExecutor") - public Future<Void> processDataUpdatedEvent(final String dataspaceName, final String anchorName, - final OffsetDateTime observedTimestamp, + public Future<Void> processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, final String xpath, final Operation operation) { - log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName); + log.debug("process data updated event for anchor '{}'", anchor); try { - if (shouldSendNotification(dataspaceName)) { + if (shouldSendNotification(anchor.getDataspaceName())) { final var cpsDataUpdatedEvent = - cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName, + cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, observedTimestamp, getRootNodeOperation(xpath, operation)); log.debug("data updated event to be published {}", cpsDataUpdatedEvent); notificationPublisher.sendNotification(cpsDataUpdatedEvent); @@ -105,7 +104,7 @@ public class NotificationService { CPS operation should not fail if sending event fails for any reason. */ notificationErrorHandler.onException("Failed to process cps-data-updated-event.", - exception, dataspaceName, anchorName); + exception, anchor, xpath, operation); } return CompletableFuture.completedFuture(null); } diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java index b8c472f277..fd658861c2 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +24,6 @@ package org.onap.cps.spi; import java.util.Collection; import java.util.Map; -import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.spi.model.DataNode; /* @@ -40,8 +40,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param dataNode data node */ - void storeDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull DataNode dataNode); + void storeDataNode(String dataspaceName, String anchorName, DataNode dataNode); /** * Add a child to a Fragment. @@ -51,8 +50,7 @@ public interface CpsDataPersistenceService { * @param parentXpath parent xpath * @param dataNode dataNode */ - void addChildDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentXpath, - @NonNull DataNode dataNode); + void addChildDataNode(String dataspaceName, String anchorName, String parentXpath, DataNode dataNode); /** * Adds list child elements to a Fragment. @@ -63,8 +61,8 @@ public interface CpsDataPersistenceService { * @param listElementsCollection collection of data nodes representing list elements */ - void addListElements(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull Collection<DataNode> listElementsCollection); + void addListElements(String dataspaceName, String anchorName, String parentNodeXpath, + Collection<DataNode> listElementsCollection); /** * Retrieves datanode by XPath for given dataspace and anchor. @@ -76,8 +74,8 @@ public interface CpsDataPersistenceService { * (recursively) as well * @return data node object */ - DataNode getDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull FetchDescendantsOption fetchDescendantsOption); + DataNode getDataNode(String dataspaceName, String anchorName, String xpath, + FetchDescendantsOption fetchDescendantsOption); /** @@ -88,8 +86,7 @@ public interface CpsDataPersistenceService { * @param xpath xpath * @param leaves the leaves as a map where key is a leaf name and a value is a leaf value */ - void updateDataLeaves(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull Map<String, Object> leaves); + void updateDataLeaves(String dataspaceName, String anchorName, String xpath, Map<String, Object> leaves); /** * Replaces existing data node content including descendants. @@ -98,7 +95,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param dataNode data node */ - void replaceDataNodeTree(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull DataNode dataNode); + void replaceDataNodeTree(String dataspaceName, String anchorName, DataNode dataNode); /** * Replaces list content by removing all existing elements and inserting the given new elements @@ -109,8 +106,8 @@ public interface CpsDataPersistenceService { * @param parentNodeXpath parent node xpath * @param newListElements collection of data nodes representing the new list content */ - void replaceListContent(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String parentNodeXpath, @NonNull Collection<DataNode> newListElements); + void replaceListContent(String dataspaceName, String anchorName, + String parentNodeXpath, Collection<DataNode> newListElements); /** * Deletes any dataNode, yang container or yang list or yang list element. @@ -119,8 +116,15 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param targetXpath xpath to list or list element (include [@key=value] to delete a single list element) */ - void deleteDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String targetXpath); + void deleteDataNode(String dataspaceName, String anchorName, String targetXpath); + + /** + * Deletes all dataNodes in a given anchor. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + */ + void deleteDataNodes(String dataspaceName, String anchorName); /** * Deletes existing a single list element or the whole list. @@ -129,8 +133,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param targetXpath xpath to list or list element (include [@key=value] to delete a single list element) */ - void deleteListDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String targetXpath); + void deleteListDataNode(String dataspaceName, String anchorName, String targetXpath); /** * Get a datanode by cps path. @@ -142,7 +145,7 @@ public interface CpsDataPersistenceService { * included in the output * @return the data nodes found i.e. 0 or more data nodes */ - Collection<DataNode> queryDataNodes(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String cpsPath, @NonNull FetchDescendantsOption fetchDescendantsOption); + Collection<DataNode> queryDataNodes(String dataspaceName, String anchorName, + String cpsPath, FetchDescendantsOption fetchDescendantsOption); } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy index fe6e460862..bb122d1ae2 100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy @@ -22,17 +22,16 @@ package org.onap.cps.api.impl +import org.onap.cps.api.CpsDataService import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.model.Anchor import spock.lang.Specification +import java.time.OffsetDateTime class CpsAdminServiceImplSpec extends Specification { def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService) - def objectUnderTest = new CpsAdminServiceImpl() - - def setup() { - objectUnderTest.cpsAdminPersistenceService = mockCpsAdminPersistenceService - } + def mockCpsDataService = Mock(CpsDataService) + def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService) def 'Create dataspace method invokes persistence service.'() { when: 'create dataspace method is invoked' @@ -75,7 +74,9 @@ class CpsAdminServiceImplSpec extends Specification { def 'Delete anchor.'() { when: 'delete anchor is invoked' objectUnderTest.deleteAnchor('someDataspace','someAnchor') - then: 'associated persistence service method is invoked with same parameters' + then: 'delete data nodes is invoked on the data service with expected parameters' + 1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime ) + and: 'the persistence service method is invoked with same parameters to delete anchor' 1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor') } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy index a302eb5e75..785788be90 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy @@ -24,7 +24,6 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils import org.onap.cps.api.CpsAdminService -import org.onap.cps.api.CpsModuleService import org.onap.cps.notification.NotificationService import org.onap.cps.notification.Operation import org.onap.cps.spi.CpsDataPersistenceService @@ -44,18 +43,17 @@ class CpsDataServiceImplSpec extends Specification { def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache) def mockNotificationService = Mock(NotificationService) - def objectUnderTest = new CpsDataServiceImpl() + def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService, + mockYangTextSchemaSourceSetCache, mockNotificationService) def setup() { - objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService - objectUnderTest.cpsAdminService = mockCpsAdminService - objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache - objectUnderTest.notificationService = mockNotificationService + mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor } def dataspaceName = 'some dataspace' def anchorName = 'some anchor' def schemaSetName = 'some schema set' + def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build() def observedTimestamp = OffsetDateTime.now() def 'Saving json data.'() { @@ -68,7 +66,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, { dataNode -> dataNode.xpath == '/test-tree' }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.CREATE) } def 'Saving child data fragment under existing node.'() { @@ -81,7 +79,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree', { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.CREATE) } def 'Saving list element data fragment under existing node.'() { @@ -101,7 +99,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) } def 'Saving empty list element data fragment.'() { @@ -133,7 +131,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap() @@ -166,7 +164,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, "/bookstore/categories[@code='01']", ['name':'Romance', 'code': '01']) and: 'the data updated event is sent to the notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/bookstore', Operation.UPDATE) } def 'Replace data node: #scenario.'() { @@ -178,7 +176,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, { dataNode -> dataNode.xpath == expectedNodeXpath }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' @@ -202,7 +200,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) } def 'Replace whole list content with empty list element.'() { @@ -223,7 +221,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, '/test-tree/branch') and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree/branch', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree/branch', Operation.DELETE) } def 'Delete data node under anchor and dataspace.'() { @@ -234,12 +232,22 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with the correct parameters' 1 * mockCpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, '/data-node') and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/data-node', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/data-node', Operation.DELETE) + } + + def 'Delete all data nodes for a given anchor and dataspace.'() { + given: 'schema set for given anchor and dataspace references test tree model' + setupSchemaSetMocks('test-tree.yang') + when: 'delete data node method is invoked with correct parameters' + objectUnderTest.deleteDataNodes(dataspaceName, anchorName, observedTimestamp) + then: 'the persistence service method is invoked with the correct parameters' + 1 * mockCpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.DELETE) + } def setupSchemaSetMocks(String... yangResources) { - def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build() - mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet) mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources) diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy index d18bcf55ef..52389522f8 100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy @@ -37,23 +37,17 @@ class E2ENetworkSliceSpec extends Specification { def mockDataStoreService = Mock(CpsDataPersistenceService)
def mockCpsAdminService = Mock(CpsAdminService)
def mockNotificationService = Mock(NotificationService)
- def cpsDataServiceImpl = new CpsDataServiceImpl()
def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
def cpsModuleServiceImpl = new CpsModuleServiceImpl(mockModuleStoreService,
mockYangTextSchemaSourceSetCache,mockCpsAdminService )
+ def cpsDataServiceImpl = new CpsDataServiceImpl(mockDataStoreService, mockCpsAdminService,
+ mockYangTextSchemaSourceSetCache, mockNotificationService)
def dataspaceName = 'someDataspace'
def anchorName = 'someAnchor'
def schemaSetName = 'someSchemaSet'
def noTimestamp = null
- def setup() {
- cpsDataServiceImpl.cpsDataPersistenceService = mockDataStoreService
- cpsDataServiceImpl.cpsAdminService = mockCpsAdminService
- cpsDataServiceImpl.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
- cpsDataServiceImpl.notificationService = mockNotificationService
- }
-
def 'E2E model can be parsed by CPS.'() {
given: 'Valid yang resource as name-to-content map'
def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy index 67ed3d90fa..5b13fa516a 100644 --- a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy @@ -36,27 +36,22 @@ import spock.lang.Specification class CpsDataUpdateEventFactorySpec extends Specification { def mockCpsDataService = Mock(CpsDataService) - def mockCpsAdminService = Mock(CpsAdminService) - def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService) + def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService) - def myDataspaceName = 'my-dataspace' - def myAnchorName = 'my-anchorname' - def mySchemasetName = 'my-schemaset-name' def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ' def 'Create a CPS data updated event successfully: #scenario'() { - given: 'cps admin service is able to return anchor details' - mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >> - new Anchor(myAnchorName, myDataspaceName, mySchemasetName) + given: 'an anchor which has been updated' + def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name') and: 'cps data service returns the data node details' def xpath = '/' def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build() mockCpsDataService.getDataNode( - myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + 'my-dataspace', 'my-anchorname', xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode when: 'CPS data updated event is created' - def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, - myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE) + def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor, + DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE) then: 'CPS data updated event is created with correct envelope' with(cpsDataUpdatedEvent) { type == 'org.onap.cps.data-updated-event' @@ -72,10 +67,10 @@ class CpsDataUpdateEventFactorySpec extends Specification { assert observedTimestamp == inputObservedTimestamp else assert OffsetDateTime.now().minusSeconds(20).isBefore( - DateTimeUtility.toOffsetDateTime(observedTimestamp)) - assert anchorName == myAnchorName - assert dataspaceName == myDataspaceName - assert schemaSetName == mySchemasetName + DateTimeUtility.toOffsetDateTime(observedTimestamp)) + assert anchorName == 'my-anchorname' + assert dataspaceName == 'my-dataspace' + assert schemaSetName == 'my-schemaset-name' assert operation == Content.Operation.CREATE assert data == new Data().withAdditionalProperty('leafName', 'leafValue') } @@ -86,6 +81,33 @@ class CpsDataUpdateEventFactorySpec extends Specification { 'missing observed timestamp' | null } + def 'Create a delete CPS data updated event successfully'() { + given: 'an anchor which has been deleted' + def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name') + def deletionTimestamp = '2021-01-01T23:00:00.345-0400' + when: 'a delete root data node event is created' + def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor, + DateTimeUtility.toOffsetDateTime(deletionTimestamp), Operation.DELETE) + then: 'CPS data updated event is created with correct envelope' + with(cpsDataUpdatedEvent) { + type == 'org.onap.cps.data-updated-event' + source == new URI('urn:cps:org.onap.cps') + schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1') + StringUtils.hasText(id) + content != null + } + and: 'correct content' + with(cpsDataUpdatedEvent.content) { + assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format" + assert observedTimestamp == deletionTimestamp + assert anchorName == 'my-anchorname' + assert dataspaceName == 'my-dataspace' + assert schemaSetName == 'my-schemaset-name' + assert operation == Content.Operation.DELETE + assert data == null + } + } + def isExpectedDateTimeFormat(String observedTimestamp) { try { DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp) diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy index 306e187a00..c20bdeea7c 100644 --- a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy @@ -23,6 +23,7 @@ package org.onap.cps.notification import java.time.OffsetDateTime import org.onap.cps.config.AsyncConfig import org.onap.cps.event.model.CpsDataUpdatedEvent +import org.onap.cps.spi.model.Anchor import org.spockframework.spring.SpringBean import org.spockframework.spring.SpringSpy import org.springframework.beans.factory.annotation.Autowired @@ -53,15 +54,14 @@ class NotificationServiceSpec extends Specification { NotificationService objectUnderTest @Shared - def myDataspacePublishedName = 'my-dataspace-published' - def myAnchorName = 'my-anchorname' + def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name') def myObservedTimestamp = OffsetDateTime.now() def 'Skip sending notification when disabled.'() { given: 'notification is disabled' spyNotificationProperties.isEnabled() >> false when: 'dataUpdatedEvent is received' - objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', Operation.CREATE) + objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) then: 'the notification is not sent' 0 * mockNotificationPublisher.sendNotification(_) } @@ -69,13 +69,14 @@ class NotificationServiceSpec extends Specification { def 'Send notification when enabled: #scenario.'() { given: 'notification is enabled' spyNotificationProperties.isEnabled() >> true + and: 'an anchor is in dataspace where #scenario' + def anchor = new Anchor('my-anchorname', dataspaceName, 'my-schemaset-name') and: 'event factory can create event successfully' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp, - Operation.CREATE) >> + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received' - def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp, + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) @@ -86,7 +87,7 @@ class NotificationServiceSpec extends Specification { where: scenario | dataspaceName || expectedSendNotificationCount 'dataspace name does not match filter' | 'does-not-match-pattern' || 0 - 'dataspace name matches filter' | myDataspacePublishedName || 1 + 'dataspace name matches filter' | 'my-dataspace-published' || 1 } def 'Send UPDATE operation when non-root data nodes are changed.'() { @@ -94,10 +95,10 @@ class NotificationServiceSpec extends Specification { spyNotificationProperties.isEnabled() >> true and: 'event factory creates event if operation is UPDATE' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.UPDATE) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received for non-root xpath' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/non-root-node', + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/non-root-node', operation) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) @@ -114,11 +115,10 @@ class NotificationServiceSpec extends Specification { spyNotificationProperties.isEnabled() >> true and: 'event factory creates event if operation is #operation' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, - operation) >> cpsDataUpdatedEvent + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, operation) >> + cpsDataUpdatedEvent when: 'dataUpdatedEvent is received for root xpath' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', - operation) + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', operation) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' @@ -134,19 +134,17 @@ class NotificationServiceSpec extends Specification { given: 'notification is enabled' spyNotificationProperties.isEnabled() >> true and: 'event factory can not create event successfully' - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, - myObservedTimestamp, Operation.CREATE) >> + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> { throw new Exception("Could not create event") } when: 'event is sent for processing' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, - myObservedTimestamp, '/', Operation.CREATE) + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' future.isDone() and: 'error is handled and not thrown to caller' notThrown Exception - 1 * spyNotificationErrorHandler.onException(_, _, _, _) + 1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE) } } |