From: Toine Siebelink Date: Mon, 14 Feb 2022 08:38:56 +0000 (+0000) Subject: Merge "Upgrade SDN-C" X-Git-Tag: 3.0.0~27 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=eaef84ed9d8dadadbe462d0d065c9bc802e84a74;hp=02a89e4739beb90b57da45661fe1c738929694d7;p=cps.git Merge "Upgrade SDN-C" --- diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java index 51b248295..5b89d9f4c 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -24,9 +24,9 @@ package org.onap.cps.spi.impl; import java.util.Collection; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import javax.transaction.Transactional; +import lombok.AllArgsConstructor; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; @@ -38,30 +38,19 @@ import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; -import org.onap.cps.spi.repository.FragmentRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.onap.cps.spi.repository.YangResourceRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; @Component +@AllArgsConstructor public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService { - @Autowired - private DataspaceRepository dataspaceRepository; - - @Autowired - private AnchorRepository anchorRepository; - - @Autowired - private SchemaSetRepository schemaSetRepository; - - @Autowired - private FragmentRepository fragmentRepository; - - @Autowired - private YangResourceRepository yangResourceRepository; + private final DataspaceRepository dataspaceRepository; + private final AnchorRepository anchorRepository; + private final SchemaSetRepository schemaSetRepository; + private final YangResourceRepository yangResourceRepository; @Override public void createDataspace(final String dataspaceName) { @@ -140,7 +129,6 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { final var anchorEntity = getAnchorEntity(dataspaceName, anchorName); - fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)); anchorRepository.delete(anchorEntity); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index 2d67d7c95..ed414fc30 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2020-2021 Bell Canada. + * Modifications Copyright (C) 2020-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. @@ -320,6 +320,14 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.save(parentEntity); } + @Override + @Transactional + public void deleteDataNodes(final String dataspaceName, final String anchorName) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName) + .ifPresent( + anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity))); + } @Override @Transactional diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index 3e39a05c5..86d5de6d0 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -134,10 +134,10 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Transactional // A retry is made to store the schema set if it fails because of duplicated yang resource exception that // can occur in case of specific concurrent requests. - @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 2, backoff = @Backoff(delay = 500)) + @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff = + @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2)) public void storeSchemaSet(final String dataspaceName, final String schemaSetName, final Map yangResourcesNameToContentMap) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final var yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap); final var schemaSetEntity = new SchemaSetEntity(); @@ -153,6 +153,10 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional + // A retry is made to store the schema set if it fails because of duplicated yang resource exception that + // can occur in case of specific concurrent requests. + @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff = + @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2)) public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName, final Map newYangResourcesModuleNameToContentMap, final List moduleReferences) { @@ -219,7 +223,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ convertedException.ifPresent( e -> log.warn( "Cannot persist duplicated yang resource. " - + "A total of 2 attempts to store the schema set are planned.", e)); + + "System will attempt this method up to 5 times.", e)); throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException; } } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy index 2218014ec..063bd5b5a 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy @@ -41,8 +41,7 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { static final String SET_DATA = '/data/anchor.sql' static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql' static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA' - static final Integer DELETED_ANCHOR_ID = 3001 - static final Long DELETED_FRAGMENT_ID = 4001 + static final Integer DELETED_ANCHOR_ID = 3002 @Sql(CLEAR_DATA) def 'Create and retrieve a new dataspace.'() { @@ -150,10 +149,9 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { @Sql([CLEAR_DATA, SET_DATA]) def 'Delete anchor'() { when: 'delete anchor action is invoked' - objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1) - then: 'anchor and associated data fragment are deleted' + objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2) + then: 'anchor is deleted' assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty() - assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty() } @Sql([CLEAR_DATA, SET_DATA]) diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy index 60d7774cc..41e7a419a 100755 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 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. @@ -35,7 +35,6 @@ import org.onap.cps.spi.model.DataNodeBuilder import org.onap.cps.utils.JsonObjectMapper import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.jdbc.Sql - import javax.validation.ConstraintViolationException import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS @@ -49,6 +48,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { static final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) static final String SET_DATA = '/data/fragment.sql' + static final int DATASPACE_1001_ID = 1001L + static final int ANCHOR_3003_ID = 3003L static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1' static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100' @@ -524,6 +525,20 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { 'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]' } + @Sql([CLEAR_DATA, SET_DATA]) + def 'Delete data node for an anchor.'() { + given: 'a data-node exists for an anchor' + assert fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID) + when: 'data nodes are deleted ' + objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3) + then: 'all data-nodes are deleted successfully' + assert !fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID) + } + + def fragmentsExistInDB(dataSpaceId, anchorId) { + !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty() + } + static Collection toDataNodes(xpaths) { return xpaths.collect { new DataNodeBuilder().withXpath(it).build() } } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceConcurrencySpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceConcurrencySpec.groovy new file mode 100644 index 000000000..085bb3340 --- /dev/null +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceConcurrencySpec.groovy @@ -0,0 +1,124 @@ +/* + * ============LICENSE_START======================================================= + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.cps.spi.impl + +import com.fasterxml.jackson.databind.ObjectMapper +import org.hibernate.exception.ConstraintViolationException +import org.mockito.InjectMocks +import org.mockito.Mock +import org.onap.cps.spi.CpsAdminPersistenceService +import org.onap.cps.spi.CpsModulePersistenceService +import org.onap.cps.spi.entities.DataspaceEntity +import org.onap.cps.spi.entities.YangResourceEntity +import org.onap.cps.spi.exceptions.DuplicatedYangResourceException +import org.onap.cps.spi.model.ExtendedModuleReference +import org.onap.cps.spi.model.ModuleReference +import org.onap.cps.spi.repository.AnchorRepository +import org.onap.cps.spi.repository.DataspaceRepository +import org.onap.cps.spi.repository.FragmentRepository +import org.onap.cps.spi.repository.SchemaSetRepository +import org.onap.cps.spi.repository.YangResourceRepository +import org.onap.cps.utils.JsonObjectMapper +import org.spockframework.spring.SpringBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.dao.DataIntegrityViolationException +import org.springframework.test.context.jdbc.Sql +import spock.lang.Shared +import spock.lang.Specification + +import java.sql.SQLException + +class CpsModulePersistenceServiceConcurrencySpec extends CpsPersistenceSpecBase { + + @Autowired + CpsModulePersistenceService objectUnderTest + + @Autowired + AnchorRepository anchorRepository + + @Autowired + SchemaSetRepository schemaSetRepository + + @Autowired + CpsAdminPersistenceService cpsAdminPersistenceService + + @SpringBean + YangResourceRepository yangResourceRepositoryMock = Mock() + + @SpringBean + DataspaceRepository dataspaceRepositoryMock = Mock() + + @SpringBean + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + + static final String DATASPACE_NAME = 'DATASPACE-001' + static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW' + static final String NEW_RESOURCE_NAME = 'some new resource' + static final String NEW_RESOURCE_CONTENT = 'module stores {\n' + + ' yang-version 1.1;\n' + + ' namespace "org:onap:ccsdk:sample";\n' + + '}' + + def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT] + + @Shared + yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539' + + @Shared + yangResourceChecksumDbConstraint = 'yang_resource_checksum_key' + + @Shared + sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum) + + @Shared + checksumIntegrityException = + new DataIntegrityViolationException("checksum integrity exception", + new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint)) + + def 'Store new schema set, retry mechanism'() { + given: 'no pre-existing schemaset in database' + dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity() + yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList() + when: 'a new schemaset is stored' + objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap) + then: ' duplicated yang resource exception is thrown ' + def e = thrown(DuplicatedYangResourceException) + and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)' + 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException } + } + + def 'Store schema set using modules, retry mechanism'() { + given: 'map of new modules, a list of existing modules, module reference' + def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }'] + def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12") + def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule] + and: 'no pre-existing schemaset in database' + dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity() + yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList() + when: 'a new schemaset is stored from a module' + objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference) + then: ' duplicated yang resource exception is thrown ' + def e = thrown(DuplicatedYangResourceException) + and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)' + 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException } + } +} diff --git a/cps-ri/src/test/java/org/onap/cps/TestApplication.java b/cps-ri/src/test/java/org/onap/cps/TestApplication.java index 0d1df456e..075a241fc 100644 --- a/cps-ri/src/test/java/org/onap/cps/TestApplication.java +++ b/cps-ri/src/test/java/org/onap/cps/TestApplication.java @@ -21,11 +21,13 @@ package org.onap.cps; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.retry.annotation.EnableRetry; /** * The @SpringBootApplication annotated class is required in order to run tests * marked with @SpringBootTest annotation. */ @SpringBootApplication(scanBasePackages = "org.onap.cps.spi") +@EnableRetry public class TestApplication { } diff --git a/cps-ri/src/test/resources/application.yml b/cps-ri/src/test/resources/application.yml index 73292bbfa..18e6ed624 100644 --- a/cps-ri/src/test/resources/application.yml +++ b/cps-ri/src/test/resources/application.yml @@ -18,10 +18,12 @@ spring: jpa: ddl-auto: create + show-sql: true properties: hibernate: enable_lazy_load_no_trans: true dialect: org.hibernate.dialect.PostgreSQLDialect + format_sql: true datasource: url: ${DB_URL} 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 f455e47ef..d2482d50a 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 d30a6571d..1013addbe 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 a23bc95f3..af06e5fc1 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, @@ -137,6 +132,14 @@ public class CpsDataServiceImpl implements CpsDataService { processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE); } + @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) { @@ -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 6054ce5d7..e7b639d48 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 97a14797b..5ad59df2a 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 processDataUpdatedEvent(final String dataspaceName, final String anchorName, - final OffsetDateTime observedTimestamp, + public Future 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 b8c472f27..fd658861c 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 listElementsCollection); + void addListElements(String dataspaceName, String anchorName, String parentNodeXpath, + Collection 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 leaves); + void updateDataLeaves(String dataspaceName, String anchorName, String xpath, Map 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 newListElements); + void replaceListContent(String dataspaceName, String anchorName, + String parentNodeXpath, Collection 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 queryDataNodes(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String cpsPath, @NonNull FetchDescendantsOption fetchDescendantsOption); + Collection 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 fe6e46086..bb122d1ae 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 a302eb5e7..785788be9 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 d18bcf55e..52389522f 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 67ed3d90f..5b13fa516 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 306e187a0..c20bdeea7 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) } } diff --git a/docs/admin-guide.rst b/docs/admin-guide.rst index 7689ee5b6..203151bf8 100644 --- a/docs/admin-guide.rst +++ b/docs/admin-guide.rst @@ -12,6 +12,123 @@ CPS Admin Guide .. toctree:: :maxdepth: 1 +Logging Configuration +===================== + +.. note:: + Default logging level of "logging.level.org.onap.cps" is set to "INFO". + +.. code-block:: bash + + logging: + level: + org: + springframework: INFO + onap: + cps: INFO + +CPS Log pattern +--------------- + +.. code-block:: java + + + { + "timestamp" : "%timestamp", // 2022-01-28 18:39:17.768 + "severity": "%level", // DEBUG + "service": "${springAppName}", // cps-application + "trace": "${TraceId}", // e17da1571e518c59 + "span": "${SpanId}", // e17da1571e518c59 + "pid": "${PID}", //11128 + "thread": "%thread", //tp1901272535-29 + "class": "%logger{40}", .// o.onap.cps.aop.CpsLoggingAspectService + "rest": "%message" // Execution time ... + } + + +Change logging level +-------------------- + +.. container:: ulist + +- Curl command 1. Check current log level of "logging.level.org.onap.cps" if it is set to it's default value (INFO) + +.. code-block:: java + + curl --location --request GET 'http://{cps-service-name:cps-management-port}/manage/loggers/org.onap.cps' \ + --header 'Content-Type: application/json; charset=utf-8' + + Response body : HTTP Status 200 + + { + "configuredLevel": "INFO", + "effectiveLevel": "INFO" + } + +- Curl command 2. Change logging level of "logging.level.org.onap.cps" to "DEBUG" + +.. note:: + Below-mentioned endpoint will change the log level at runtime. After executing the curl command "effectiveLevel" will set and applied immediately without restarting CPS service. + +.. code-block:: java + + curl --location --request POST 'http://{cps-service-name:cps-management-port}/manage/loggers/org.onap.cps' \ + --header 'Content-Type: application/json; charset=utf-8' \ + --data-raw '{ + "configuredLevel": "DEBUG" + }' + + Response body : HTTP Status 204 + +- Curl command 3. Verify if log level of "logging.level.org.onap.cps" is changed from 'INFO' to 'DEBUG' + +.. code-block:: java + + curl --location --request GET 'http://{cps-service-name:cps-management-port}/manage/loggers/org.onap.cps' \ + --header 'Content-Type: application/json; charset=utf-8' + + Response body : HTTP Status 200 + + { + "configuredLevel": "DEBUG", + "effectiveLevel": "DEBUG" + } + +Location of log files +--------------------- +By default, Spring Boot will only log to the console and will not write log files. + +.. image:: images/cps-service-console.JPG + :width: 700 + :alt: CPS service console + +Measure Execution Time of CPS Service +------------------------------------- + +.. note:: + Make sure effective log level of "logging.level.org.onap.cps" is 'DEBUG'. This can be verified by executing curl command 3. + +Execute CPS service that you want to calculate total elapsed time and log as shown below + +.. code-block:: xml + + 2022-01-28 18:39:17.679 DEBUG [cps-application,e17da1571e518c59,e17da1571e518c59] 11128 --- [tp1901272535-29] o.onap.cps.aop.CpsLoggingAspectService : Execution time of : DataspaceRepository.getByName() with argument[s] = [test42] having result = org.onap.cps.spi.entities.DataspaceEntity@68ded236 :: 205 ms + + 2022-01-28 18:39:17.726 DEBUG [cps-application,e17da1571e518c59,e17da1571e518c59] 11128 --- [tp1901272535-29] o.onap.cps.aop.CpsLoggingAspectService : Execution time of : AnchorRepository.getByDataspaceAndName() with argument[s] = [org.onap.cps.spi.entities.DataspaceEntity@68ded236, bookstore] having result = org.onap.cps.spi.entities.AnchorEntity@71c47fb1 :: 46 ms + + 2022-01-28 18:39:17.768 DEBUG [cps-application,e17da1571e518c59,e17da1571e518c59] 11128 --- [tp1901272535-29] o.onap.cps.aop.CpsLoggingAspectService : Execution time of : CpsAdminPersistenceServiceImpl.getAnchor() with argument[s] = [test42, bookstore] having result = Anchor(name=bookstore, dataspaceName=test42, schemaSetName=bookstore) :: 299 ms + + 2022-01-28 18:39:17.768 DEBUG [cps-application,e17da1571e518c59,e17da1571e518c59] 11128 --- [tp1901272535-29] o.onap.cps.aop.CpsLoggingAspectService : Execution time of : CpsAdminServiceImpl.getAnchor() with argument[s] = [test42, bookstore] having result = Anchor(name=bookstore, dataspaceName=test42, schemaSetName=bookstore) :: 305 ms + + 2022-01-28 18:39:17.843 DEBUG [cps-application,e17da1571e518c59,e17da1571e518c59] 11128 --- [tp1901272535-29] o.onap.cps.aop.CpsLoggingAspectService : Execution time of : AdminRestController.getAnchor() with argument[s] = [test42, bookstore] having result = <200 OK OK,class AnchorDetails { + name: bookstore + dataspaceName: test42 + schemaSetName: bookstore + },[]> :: 419 ms + +.. warning:: + Revert logging level of "logging.level.org.onap.cps" to 'INFO' again to prevent unnecessary logging and impacts on performance. + .. Below Label is used by documentation for other CPS components to link here, do not remove even if it gives a warning .. _cps_common_logging: diff --git a/docs/images/cps-service-console.JPG b/docs/images/cps-service-console.JPG new file mode 100644 index 000000000..964e5b67b Binary files /dev/null and b/docs/images/cps-service-console.JPG differ diff --git a/docs/release-notes.rst b/docs/release-notes.rst index 058f6baae..3fb0a7015 100755 --- a/docs/release-notes.rst +++ b/docs/release-notes.rst @@ -41,6 +41,7 @@ Bug Fixes - `CPS-783 `_ Remove cm handle does not completely remove all cm handle information - `CPS-841 `_ Upgrade log4j to 2.17.1 as recommended by ONAP SECCOM - `CPS-867 `_ Database port made configurable through env variable DB_PORT + - `CPS-856 `_ Retry mechanism not working for concurrent CmHandle registration Known Limitations, Issues and Workarounds -----------------------------------------