From: Toine Siebelink Date: Mon, 22 Aug 2022 14:38:37 +0000 (+0000) Subject: Merge "Performance Improvement: Insert Yang Resources" X-Git-Tag: 3.1.0~31 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=7c1ade44e04bc6fa41141caaaa81f6b9954f7c88;hp=a6633c02eefedcf85639c7d756661f3e756c6634;p=cps.git Merge "Performance Improvement: Insert Yang Resources" --- diff --git a/INFO.yaml b/INFO.yaml index 88e186e75..a411e3775 100755 --- a/INFO.yaml +++ b/INFO.yaml @@ -62,6 +62,11 @@ committers: company: 'Ericsson Software Technology' id: 'JosephKeenan' timezone: 'Europe/Dublin' + - name: 'Sourabh Sharma' + email: 'sourabh.sourabh@est.tech' + company: 'Ericsson Software Technology' + id: 'sourabh_sourabh' + timezone: 'Europe/Dublin' repositories: - cps tsc: diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index ea1b93ba4..17551344f 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -107,8 +107,8 @@ notification: enabled-dataspaces: ${NOTIFICATION_DATASPACE_FILTER_PATTERNS:""} async: executor: - core-pool-size: 10 - max-pool-size: 100 + core-pool-size: 2 + max-pool-size: 10 queue-capacity: 500 wait-for-tasks-to-complete-on-shutdown: true thread-name-prefix: Async- diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java index 92387bab3..3dbb4a147 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java @@ -91,7 +91,7 @@ public class CmHandleQueries { final Map firstQuery, final Map secondQuery) { if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) { - return Collections.emptyMap(); + return NO_QUERY_TO_EXECUTE; } else if (firstQuery == NO_QUERY_TO_EXECUTE) { return secondQuery; } else if (secondQuery == NO_QUERY_TO_EXECUTE) { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy index 10a5d6246..7d7258dc4 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy @@ -82,7 +82,7 @@ class CmHandleQueriesSpec extends Specification { 'the first query contains entries and second query is null' | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | null || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] 'the second query contains entries and first query is null' | null | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] 'both queries are empty' | [:] | [:] || [:] - 'both queries are null' | null | null || [:] + 'both queries are null' | null | null || null } def 'Get Cm Handles By State'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdogSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdogSpec.groovy index 863977a64..81268cbc0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdogSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdogSpec.groovy @@ -21,12 +21,11 @@ package org.onap.cps.ncmp.api.inventory.sync -import org.mockito.Mock + import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.inventory.CmHandleState import org.onap.cps.ncmp.api.inventory.CompositeState -import org.onap.cps.ncmp.api.inventory.DataStoreSyncState import org.onap.cps.ncmp.api.inventory.InventoryPersistence import org.onap.cps.ncmp.api.inventory.LockReasonCategory import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/SyncUtilsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/SyncUtilsSpec.groovy index 82e9d33ae..fb4ca3933 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/SyncUtilsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/SyncUtilsSpec.groovy @@ -62,13 +62,25 @@ class SyncUtilsSpec extends Specification{ @Shared def dataNode = new DataNode(leaves: ['id': 'cm-handle-123']) + @Shared + def dataNodeAdditionalProperties = new DataNode(leaves: ['name': 'dmiProp1', 'value': 'dmiValue1']) + + def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() { given: 'the inventory persistence service returns a collection of data nodes' mockCmHandleQueries.getCmHandlesByState(CmHandleState.ADVISED) >> dataNodeCollection + and: 'we have some additional (dmi, private) properties' + dataNodeAdditionalProperties.xpath = dataNode.xpath + '/additional-properties[@name="dmiProp1"]' + dataNode.childDataNodes = [dataNodeAdditionalProperties] when: 'get advised cm handles are fetched' def yangModelCmHandles = objectUnderTest.getAdvisedCmHandles() then: 'the returned data node collection is the correct size' yangModelCmHandles.size() == expectedDataNodeSize + and: 'if there is a data node the additional (dmi, private) properties are included' + if (expectedDataNodeSize > 0) { + assert yangModelCmHandles[0].dmiProperties[0].name == 'dmiProp1' + assert yangModelCmHandles[0].dmiProperties[0].value == 'dmiValue1' + } and: 'yang model collection contains the correct data' yangModelCmHandles.stream().map(yangModel -> yangModel.id).collect(Collectors.toSet()) == dataNodeCollection.stream().map(dataNode -> dataNode.leaves.get("id")).collect(Collectors.toSet()) 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 647d6cdef..e9e945a49 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 @@ -30,7 +30,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -40,13 +39,14 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.transaction.Transactional; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.hibernate.exception.ConstraintViolationException; 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.SchemaSetEntity; import org.onap.cps.spi.entities.YangResourceEntity; import org.onap.cps.spi.entities.YangResourceModuleReference; @@ -72,7 +72,7 @@ import org.springframework.stereotype.Component; @Slf4j @Component -@AllArgsConstructor +@RequiredArgsConstructor public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService { private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key"; @@ -80,15 +80,15 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE); - private YangResourceRepository yangResourceRepository; + private final YangResourceRepository yangResourceRepository; - private SchemaSetRepository schemaSetRepository; + private final SchemaSetRepository schemaSetRepository; - private DataspaceRepository dataspaceRepository; + private final DataspaceRepository dataspaceRepository; - private CpsAdminPersistenceService cpsAdminPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; - private ModuleReferenceRepository moduleReferenceRepository; + private final ModuleReferenceRepository moduleReferenceRepository; @Override public Map getYangSchemaResources(final String dataspaceName, final String schemaSetName) { @@ -164,13 +164,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ final Map newModuleNameToContentMap, final Collection moduleReferences) { storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap); - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List listOfYangResourceIds = new ArrayList<>(); - moduleReferences.forEach(moduleReference -> - listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision( - moduleReference.getModuleName(), moduleReference.getRevision()))); + final List listOfYangResourceIds = + yangResourceRepository.getResourceIdsByModuleReferences(moduleReferences); yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java index 37202498f..f07f7f8d5 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java @@ -20,12 +20,14 @@ package org.onap.cps.spi.repository; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; +import javax.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; @@ -43,8 +45,9 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps private final JsonObjectMapper jsonObjectMapper; @Override + @Transactional public List findByAnchorAndCpsPath(final int anchorId, final CpsPathQuery cpsPathQuery) { - final var sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); + final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); final Map queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); sqlStringBuilder.append(" AND xpath SIMILAR TO :xpathRegex"); @@ -57,13 +60,23 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps } addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters); - final var query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); + final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); setQueryParameters(query, queryParameters); - return query.getResultList(); + return getFragmentEntitiesAsStream(query); + } + + private List getFragmentEntitiesAsStream(final Query query) { + final List fragmentEntities = new ArrayList<>(); + query.getResultStream().forEach(fragmentEntity -> { + fragmentEntities.add((FragmentEntity) fragmentEntity); + entityManager.detach(fragmentEntity); + }); + + return fragmentEntities; } private static String getSimilarToXpathSqlRegex(final CpsPathQuery cpsPathQuery) { - final var xpathRegexBuilder = new StringBuilder(); + final StringBuilder xpathRegexBuilder = new StringBuilder(); if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) { xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); } else { @@ -96,7 +109,7 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps .append(" OR attributes @> jsonb_build_object(:textLeafName, json_build_array(:textValue))"); queryParameters.put("textLeafName", cpsPathQuery.getTextFunctionConditionLeafName()); queryParameters.put("textValue", cpsPathQuery.getTextFunctionConditionValue()); - final var textValueAsInt = getTextValueAsInt(cpsPathQuery); + final Integer textValueAsInt = getTextValueAsInt(cpsPathQuery); if (textValueAsInt != null) { sqlStringBuilder.append(" OR attributes @> jsonb_build_object(:textLeafName, :textValueAsInt)"); sqlStringBuilder diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java new file mode 100644 index 000000000..335c971f9 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.repository; + +import java.util.Collection; +import java.util.List; +import org.onap.cps.spi.model.ModuleReference; + +public interface YangResourceNativeRepository { + + List getResourceIdsByModuleReferences(Collection moduleReferences); + +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java new file mode 100644 index 000000000..e21fecb2b --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.repository; + +import java.util.Collection; +import java.util.List; +import java.util.StringJoiner; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import org.hibernate.type.StandardBasicTypes; +import org.onap.cps.spi.model.ModuleReference; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository { + + @PersistenceContext + private EntityManager entityManager; + + @Override + @Transactional + public List getResourceIdsByModuleReferences(final Collection moduleReferences) { + final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) + .unwrap(org.hibernate.query.NativeQuery.class) + .addScalar("id", StandardBasicTypes.LONG); + return query.getResultList(); + } + + private String getCombinedSelectSqlQuery(final Collection moduleReferences) { + final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL "); + moduleReferences.stream().forEach(moduleReference -> { + sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'", + moduleReference.getModuleName(), + moduleReference.getRevision())); + }); + return sqlQueryJoiner.toString(); + } +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index 98306d868..6ca4fff4f 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -34,7 +34,7 @@ import org.springframework.stereotype.Repository; @Repository public interface YangResourceRepository extends JpaRepository, - SchemaSetYangResourceRepository { + YangResourceNativeRepository, SchemaSetYangResourceRepository { List findAllByChecksumIn(@NotNull Set checksum); @@ -91,10 +91,6 @@ public interface YangResourceRepository extends JpaRepository findAllModuleReferencesByDataspaceAndModuleNames( @Param("dataspaceName") String dataspaceName, @Param("moduleNames") Collection moduleNames); - - @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true) - Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision); - @Modifying @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS " + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) 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 0772a8c9f..7bdc2c166 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 @@ -65,7 +65,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData); cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, CREATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, CREATE, observedTimestamp); } @Override @@ -74,7 +74,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, CREATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, CREATE, observedTimestamp); } @Override @@ -85,7 +85,7 @@ public class CpsDataServiceImpl implements CpsDataService { buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath, listElementDataNodeCollection); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -102,7 +102,7 @@ public class CpsDataServiceImpl implements CpsDataService { final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves()); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -117,7 +117,7 @@ public class CpsDataServiceImpl implements CpsDataService { for (final DataNode dataNodeUpdate : dataNodeUpdates) { processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate); } - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -147,7 +147,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -164,7 +164,7 @@ public class CpsDataServiceImpl implements CpsDataService { final Collection dataNodes, final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -172,16 +172,15 @@ public class CpsDataServiceImpl implements CpsDataService { final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, DELETE); + processDataUpdatedEventAsync(dataspaceName, anchorName, dataNodeXpath, DELETE, observedTimestamp); } @Override public void deleteDataNodes(final String dataspaceName, final String anchorName, final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); - final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, DELETE, observedTimestamp); cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName); - processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, DELETE, observedTimestamp); } @Override @@ -189,7 +188,7 @@ public class CpsDataServiceImpl implements CpsDataService { final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, DELETE); + processDataUpdatedEventAsync(dataspaceName, anchorName, listNodeXpath, DELETE, observedTimestamp); } private DataNode buildDataNode(final String dataspaceName, final String anchorName, @@ -230,20 +229,10 @@ public class CpsDataServiceImpl implements CpsDataService { } - private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, - final OffsetDateTime observedTimestamp, final String xpath, - final Operation operation) { - final Anchor 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) { + private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, final String xpath, + final Operation operation, final OffsetDateTime observedTimestamp) { try { - notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, - operation); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName, xpath, operation, observedTimestamp); } 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/NotificationService.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java index 30bb85142..7da3a6123 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 @@ -32,6 +32,7 @@ import java.util.stream.Collectors; import javax.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsAdminService; import org.onap.cps.spi.model.Anchor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -45,6 +46,7 @@ public class NotificationService { private final NotificationPublisher notificationPublisher; private final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory; private final NotificationErrorHandler notificationErrorHandler; + private final CpsAdminService cpsAdminService; private List dataspacePatterns; @PostConstruct @@ -68,21 +70,24 @@ public class NotificationService { /** * Process Data Updated Event and publishes the notification. * - * @param anchor anchor - * @param observedTimestamp observedTimestamp + * @param dataspaceName dataspaceName + * @param anchorName anchorName * @param xpath xpath of changed data node * @param operation operation + * @param observedTimestamp observedTimestamp * @return future */ @Async("notificationExecutor") - public Future processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, - final String xpath, final Operation operation) { + public Future processDataUpdatedEvent(final String dataspaceName, final String anchorName, + final String xpath, final Operation operation, final OffsetDateTime observedTimestamp) { + + final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); log.debug("process data updated event for anchor '{}'", anchor); try { - if (shouldSendNotification(anchor.getDataspaceName())) { + if (shouldSendNotification(dataspaceName)) { final var cpsDataUpdatedEvent = - cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, - observedTimestamp, getRootNodeOperation(xpath, operation)); + cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, + observedTimestamp, getRootNodeOperation(xpath, operation)); log.debug("data updated event to be published {}", cpsDataUpdatedEvent); notificationPublisher.sendNotification(cpsDataUpdatedEvent); } @@ -91,7 +96,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, anchor, xpath, operation); + exception, anchor, xpath, operation); } return CompletableFuture.completedFuture(null); } 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 8b9d54529..6c995fa85 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 @@ -67,7 +67,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(anchor, observedTimestamp, '/', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, observedTimestamp) } def 'Saving json data with invalid #scenario.'() { @@ -76,9 +76,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.storeDataNode(_, _, _) + 0 * mockCpsDataPersistenceService.storeDataNode(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -96,7 +96,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(anchor, observedTimestamp, '/test-tree', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.CREATE, observedTimestamp) } def 'Saving child data fragment under existing node with invalid #scenario.'() { @@ -105,9 +105,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.addChildDataNode(_, _, _,_) + 0 * mockCpsDataPersistenceService.addChildDataNode(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -132,7 +132,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.UPDATE, observedTimestamp) } def 'Saving empty list element data fragment.'() { @@ -151,7 +151,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'add list elements persistence method is not invoked' - 0 * mockCpsDataPersistenceService.addListElements(_, _, _, _) + 0 * mockCpsDataPersistenceService.addListElements(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -176,7 +176,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'get data node persistence service is not invoked' - 0 * mockCpsDataPersistenceService.getDataNode(_, _, _, _) + 0 * mockCpsDataPersistenceService.getDataNode(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -192,7 +192,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(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, parentNodeXpath, Operation.UPDATE, observedTimestamp) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap() @@ -205,9 +205,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.updateDataLeaves(_, _, _, _) + 0 * mockCpsDataPersistenceService.updateDataLeaves(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -241,7 +241,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(anchor, observedTimestamp, '/bookstore', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/bookstore', Operation.UPDATE, observedTimestamp) } def 'Update Bookstore node leaves with invalid #scenario' () { @@ -251,9 +251,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.updateDataLeaves(_, _, _, _) + 0 * mockCpsDataPersistenceService.updateDataLeaves(*_) and: 'the data updated event is not sent to the notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -271,7 +271,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(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, parentNodeXpath, Operation.UPDATE, observedTimestamp) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' @@ -284,9 +284,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.replaceDataNodeTree(_, _,_) + 0 * mockCpsDataPersistenceService.replaceDataNodeTree(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -311,7 +311,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.UPDATE, observedTimestamp) } def 'Replace whole list content with empty list element.'() { @@ -330,9 +330,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.replaceListContent(_, _,_) + 0 * mockCpsDataPersistenceService.replaceListContent(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -348,7 +348,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(anchor, observedTimestamp, '/test-tree/branch', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree/branch', Operation.DELETE, observedTimestamp) } @@ -358,9 +358,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.deleteListDataNode(_, _, _) + 0 * mockCpsDataPersistenceService.deleteListDataNode(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -376,7 +376,7 @@ 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(anchor, observedTimestamp, '/data-node', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/data-node', Operation.DELETE, observedTimestamp) } def 'Delete data node with an invalid #scenario.'() { @@ -385,9 +385,9 @@ class CpsDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'the persistence service method is not invoked' - 0 * mockCpsDataPersistenceService.deleteDataNode(_, _, _) + 0 * mockCpsDataPersistenceService.deleteDataNode(*_) and: 'data updated event is not sent to notification service' - 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _) + 0 * mockNotificationService.processDataUpdatedEvent(*_) where: 'the following parameters are used' scenario | dataspaceName | anchorName 'dataspace name' | 'dataspace names with spaces' | 'anchorName' @@ -400,11 +400,10 @@ class CpsDataServiceImplSpec extends Specification { 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' + then: 'data updated event is sent to notification service before the delete' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.DELETE, observedTimestamp) + and: '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) { 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 8263c31f0..a996195c0 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 @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (c) 2021-2022 Bell Canada. + * Modifications Copyright (C) 2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +21,7 @@ package org.onap.cps.notification -import java.time.OffsetDateTime +import org.onap.cps.api.CpsAdminService import org.onap.cps.config.AsyncConfig import org.onap.cps.event.model.CpsDataUpdatedEvent import org.onap.cps.spi.model.Anchor @@ -33,6 +34,7 @@ import org.springframework.test.context.ContextConfiguration import spock.lang.Shared import spock.lang.Specification +import java.time.OffsetDateTime import java.util.concurrent.TimeUnit @SpringBootTest @@ -48,19 +50,29 @@ class NotificationServiceSpec extends Specification { NotificationErrorHandler spyNotificationErrorHandler @SpringSpy NotificationProperties spyNotificationProperties + @SpringBean + CpsAdminService mockCpsAdminService = Mock() @Autowired NotificationService objectUnderTest + @Shared + def dataspaceName = 'my-dataspace-published' + @Shared + def anchorName = 'my-anchorname' @Shared def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name') def myObservedTimestamp = OffsetDateTime.now() + def setup() { + mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor + } + def 'Skip sending notification when disabled.'() { given: 'notification is disabled' spyNotificationProperties.isEnabled() >> false when: 'dataUpdatedEvent is received' - objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) + objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, myObservedTimestamp) then: 'the notification is not sent' 0 * mockNotificationPublisher.sendNotification(_) } @@ -75,8 +87,8 @@ class NotificationServiceSpec extends Specification { mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received' - def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, - '/', Operation.CREATE) + def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, + '/', Operation.CREATE, myObservedTimestamp) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' @@ -97,7 +109,7 @@ class NotificationServiceSpec extends Specification { mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, expectedOperationInEvent) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received for #xpath' - def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, xpath, operation) + def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, xpath, operation, myObservedTimestamp) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' @@ -127,7 +139,7 @@ class NotificationServiceSpec extends Specification { mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> { throw new Exception("Could not create event") } when: 'event is sent for processing' - def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) + def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, myObservedTimestamp) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' diff --git a/docs/cps-events.rst b/docs/cps-events.rst index a28d4b052..34b5a5f66 100644 --- a/docs/cps-events.rst +++ b/docs/cps-events.rst @@ -8,12 +8,6 @@ CPS Events ########## -CPS Core -******** -.. - Cps core events yet to be written - - CPS-NCMP ******** @@ -23,16 +17,7 @@ Lifecycle Management (LCM) Event Overview -------- -Lifecycle management events are published as cm handle state transitions from one state to another. - - -LCM events and state handler ----------------------------- -The LCM events are triggered under the state handler which has the following responsibilities: - -#. Updating and persisting cm handle state based on the target state of the cm handle - -#. Create and calls to publish the LCM event based on the cm handle state transition that occured +LCM events for CM Handles are published when a CM Handle is created, deleted or another change in the cm handle state occurs. **3 possible event types:** @@ -40,8 +25,6 @@ The LCM events are triggered under the state handler which has the following res * Update * Delete - - LCM Event Schema ---------------- The current published LCM event is based on the following schema: