diff options
Diffstat (limited to 'cps-ncmp-service')
12 files changed, 332 insertions, 174 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 209ade9559..3f440d65bd 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -59,7 +59,7 @@ import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.spi.FetchDescendantsOption; -import org.onap.cps.spi.exceptions.AlreadyDefinedException; +import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch; import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.exceptions.DataValidationException; @@ -365,12 +365,12 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService try { lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle); return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds); - } catch (final AlreadyDefinedException alreadyDefinedException) { - return List.of(CmHandleRegistrationResponse.createFailureResponse( - String.join(",", cmHandleIds), RegistrationError.CM_HANDLE_ALREADY_EXIST)); + } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) { + return CmHandleRegistrationResponse.createFailureResponses( + alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(), + RegistrationError.CM_HANDLE_ALREADY_EXIST); } catch (final Exception exception) { - return List.of(CmHandleRegistrationResponse.createFailureResponse(String.join(",", cmHandleIds), - exception)); + return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception); } } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java index d457f2601b..d5b459b025 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java @@ -56,7 +56,7 @@ public class DmiRestClient { } catch (final HttpStatusCodeException httpStatusCodeException) { final String exceptionMessage = "Unable to " + operation.toString() + " resource data."; throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(), - httpStatusCodeException.getRawStatusCode()); + httpStatusCodeException.getRawStatusCode()); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java index 45e2754222..f842ddbc1a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -46,8 +47,10 @@ import org.onap.cps.utils.CpsValidator; @Setter @NoArgsConstructor @JsonInclude(Include.NON_NULL) +@EqualsAndHashCode(onlyExplicitlyIncluded = true) public class YangModelCmHandle { + @EqualsAndHashCode.Include private String id; @JsonProperty("dmi-service-name") diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java index 9174dc7a7c..bfc3a9ac06 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java @@ -1,7 +1,6 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022 Nordix Foundation - * 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. @@ -21,57 +20,15 @@ package org.onap.cps.ncmp.api.inventory; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP; -import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED; -import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; - -import java.time.OffsetDateTime; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.api.CpsDataService; -import org.onap.cps.api.CpsModuleService; -import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; -import org.onap.cps.spi.CpsAdminPersistenceService; -import org.onap.cps.spi.CpsDataPersistenceService; -import org.onap.cps.spi.FetchDescendantsOption; -import org.onap.cps.spi.exceptions.SchemaSetNotFoundException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.ModuleDefinition; import org.onap.cps.spi.model.ModuleReference; -import org.onap.cps.utils.CpsValidator; -import org.onap.cps.utils.JsonObjectMapper; -import org.springframework.stereotype.Component; - -@Slf4j -@RequiredArgsConstructor -@Component -public class InventoryPersistence { - - private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; - - private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; - - private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry"; - - private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']"; - private final JsonObjectMapper jsonObjectMapper; - - private final CpsDataService cpsDataService; - - private final CpsModuleService cpsModuleService; - - private final CpsDataPersistenceService cpsDataPersistenceService; - - private final CpsAdminPersistenceService cpsAdminPersistenceService; +public interface InventoryPersistence { /** * Get the Cm Handle Composite State from the data node. @@ -79,50 +36,30 @@ public class InventoryPersistence { * @param cmHandleId cm handle id * @return the cm handle composite state */ - public CompositeState getCmHandleState(final String cmHandleId) { - final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state", - FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); - return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build(); - } + CompositeState getCmHandleState(String cmHandleId); /** * Save the cm handles state. * - * @param cmHandleId cm handle id + * @param cmHandleId cm handle id * @param compositeState composite state */ - public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) { - final String cmHandleJsonData = String.format("{\"state\":%s}", - jsonObjectMapper.asJsonString(compositeState)); - cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId), - cmHandleJsonData, OffsetDateTime.now()); - } + void saveCmHandleState(String cmHandleId, CompositeState compositeState); /** * Save all cm handles states in batch. * * @param cmHandleStatePerCmHandleId contains cm handle id and updated state */ - public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) { - final Map<String, String> cmHandlesJsonDataMap = new HashMap<>(); - cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put( - String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId), - String.format("{\"state\":%s}", jsonObjectMapper.asJsonString(compositeState)))); - cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - cmHandlesJsonDataMap, OffsetDateTime.now()); - } + void saveCmHandleStateBatch(Map<String, CompositeState> cmHandleStatePerCmHandleId); /** * This method retrieves DMI service name, DMI properties and the state for a given cm handle. + * * @param cmHandleId the id of the cm handle * @return yang model cm handle */ - public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) { - CpsValidator.validateNameCharacters(cmHandleId); - return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId); - } + YangModelCmHandle getYangModelCmHandle(String cmHandleId); /** * Method to return module definitions by cmHandleId. @@ -130,9 +67,7 @@ public class InventoryPersistence { * @param cmHandleId cm handle ID * @return a collection of module definitions (moduleName, revision and yang resource content) */ - public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) { - return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); - } + Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(String cmHandleId); /** * Method to return module references by cmHandleId. @@ -140,60 +75,35 @@ public class InventoryPersistence { * @param cmHandleId cm handle ID * @return a collection of module references (moduleName and revision) */ - public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) { - CpsValidator.validateNameCharacters(cmHandleId); - return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); - } + Collection<ModuleReference> getYangResourcesModuleReferences(String cmHandleId); /** * Method to save cmHandle. * * @param yangModelCmHandle cmHandle represented as Yang Model */ - public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) { - final String cmHandleJsonData = - String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)); - cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, - cmHandleJsonData, NO_TIMESTAMP); - } + void saveCmHandle(YangModelCmHandle yangModelCmHandle); /** * Method to save batch of cm handles. * * @param yangModelCmHandles cm handle represented as Yang Models */ - public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) { - final List<String> cmHandlesJsonData = new ArrayList<>(); - yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add( - String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)))); - cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP); - } + void saveCmHandleBatch(Collection<YangModelCmHandle> yangModelCmHandles); /** * Method to delete a list or a list element. * * @param listElementXpath list element xPath */ - public void deleteListOrListElement(final String listElementXpath) { - cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - listElementXpath, NO_TIMESTAMP); - } + void deleteListOrListElement(String listElementXpath); /** * Method to delete a schema set. * * @param schemaSetName schema set name */ - public void deleteSchemaSetWithCascade(final String schemaSetName) { - try { - CpsValidator.validateNameCharacters(schemaSetName); - cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, - CASCADE_DELETE_ALLOWED); - } catch (final SchemaSetNotFoundException schemaSetNotFoundException) { - log.warn("Schema set {} does not exist or already deleted", schemaSetName); - } - } + void deleteSchemaSetWithCascade(String schemaSetName); /** * Get data node via xpath. @@ -201,10 +111,7 @@ public class InventoryPersistence { * @param xpath xpath * @return data node */ - public DataNode getDataNode(final String xpath) { - return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - xpath, INCLUDE_ALL_DESCENDANTS); - } + DataNode getDataNode(String xpath); /** * Get data node of given cm handle. @@ -212,9 +119,7 @@ public class InventoryPersistence { * @param cmHandleId cmHandle ID * @return data node */ - public DataNode getCmHandleDataNode(final String cmHandleId) { - return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId)); - } + DataNode getCmHandleDataNode(String cmHandleId); /** * Query anchors via module names. @@ -222,37 +127,27 @@ public class InventoryPersistence { * @param moduleNamesForQuery module names * @return Collection of anchors */ - public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) { - return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery); - } + Collection<Anchor> queryAnchors(Collection<String> moduleNamesForQuery); /** * Method to get all anchors. * * @return Collection of anchors */ - public Collection<Anchor> getAnchors() { - return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME); - } + Collection<Anchor> getAnchors(); /** * Replaces list content by removing all existing elements and inserting the given new elements as data nodes. * - * @param parentNodeXpath parent node xpath - * @param dataNodes datanodes representing the updated data + * @param parentNodeXpath parent node xpath + * @param dataNodes datanodes representing the updated data */ - public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) { - cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - parentNodeXpath, dataNodes, NO_TIMESTAMP); - } + void replaceListContent(String parentNodeXpath, Collection<DataNode> dataNodes); /** * Deletes data node for given anchor and dataspace. * * @param dataNodeXpath data node xpath */ - public void deleteDataNode(final String dataNodeXpath) { - cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath, - NO_TIMESTAMP); - } -}
\ No newline at end of file + void deleteDataNode(String dataNodeXpath); +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java new file mode 100644 index 0000000000..99edfdb0f1 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java @@ -0,0 +1,186 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * 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. + * 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.ncmp.api.inventory; + +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP; +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED; +import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; + +import java.time.OffsetDateTime; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsDataService; +import org.onap.cps.api.CpsModuleService; +import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.spi.CpsAdminPersistenceService; +import org.onap.cps.spi.CpsDataPersistenceService; +import org.onap.cps.spi.FetchDescendantsOption; +import org.onap.cps.spi.exceptions.SchemaSetNotFoundException; +import org.onap.cps.spi.model.Anchor; +import org.onap.cps.spi.model.DataNode; +import org.onap.cps.spi.model.ModuleDefinition; +import org.onap.cps.spi.model.ModuleReference; +import org.onap.cps.utils.CpsValidator; +import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.stereotype.Component; + +@Slf4j +@RequiredArgsConstructor +@Component +public class InventoryPersistenceImpl implements InventoryPersistence { + + private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; + + private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; + + private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry"; + + private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']"; + + private final JsonObjectMapper jsonObjectMapper; + + private final CpsDataService cpsDataService; + + private final CpsModuleService cpsModuleService; + + private final CpsDataPersistenceService cpsDataPersistenceService; + + private final CpsAdminPersistenceService cpsAdminPersistenceService; + + @Override + public CompositeState getCmHandleState(final String cmHandleId) { + final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state", + FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build(); + } + + @Override + public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) { + final String cmHandleJsonData = String.format("{\"state\":%s}", + jsonObjectMapper.asJsonString(compositeState)); + cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId), + cmHandleJsonData, OffsetDateTime.now()); + } + + @Override + public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) { + final Map<String, String> cmHandlesJsonDataMap = new HashMap<>(); + cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put( + String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId), + String.format("{\"state\":%s}", jsonObjectMapper.asJsonString(compositeState)))); + cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + cmHandlesJsonDataMap, OffsetDateTime.now()); + } + + @Override + public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) { + CpsValidator.validateNameCharacters(cmHandleId); + return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId); + } + + @Override + public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) { + return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); + } + + @Override + public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) { + CpsValidator.validateNameCharacters(cmHandleId); + return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); + } + + @Override + public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) { + final String cmHandleJsonData = + String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)); + cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, + cmHandleJsonData, NO_TIMESTAMP); + } + + @Override + public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) { + final List<String> cmHandlesJsonData = new ArrayList<>(); + yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add( + String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)))); + cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP); + } + + @Override + public void deleteListOrListElement(final String listElementXpath) { + cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + listElementXpath, NO_TIMESTAMP); + } + + @Override + public void deleteSchemaSetWithCascade(final String schemaSetName) { + try { + CpsValidator.validateNameCharacters(schemaSetName); + cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, + CASCADE_DELETE_ALLOWED); + } catch (final SchemaSetNotFoundException schemaSetNotFoundException) { + log.warn("Schema set {} does not exist or already deleted", schemaSetName); + } + } + + @Override + public DataNode getDataNode(final String xpath) { + return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + xpath, INCLUDE_ALL_DESCENDANTS); + } + + @Override + public DataNode getCmHandleDataNode(final String cmHandleId) { + return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId)); + } + + @Override + public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) { + return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery); + } + + @Override + public Collection<Anchor> getAnchors() { + return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME); + } + + @Override + public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) { + cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + parentNodeXpath, dataNodes, NO_TIMESTAMP); + } + + @Override + public void deleteDataNode(final String dataNodeXpath) { + cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath, + NO_TIMESTAMP); + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncTasks.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncTasks.java index ada3dc6744..f914547a50 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncTasks.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncTasks.java @@ -71,6 +71,7 @@ public class ModuleSyncTasks { moduleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle); cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY); } catch (final Exception e) { + log.warn("Processing module sync batch failed."); syncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage()); setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason()); @@ -81,6 +82,7 @@ public class ModuleSyncTasks { lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandelStatePerCmHandle); } finally { batchCounter.getAndDecrement(); + log.info("Processing module sync batch finished. {} batch(es) active.", batchCounter.get()); } return COMPLETED_FUTURE; } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java index 73954c36b3..64d111f993 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java @@ -62,16 +62,22 @@ public class ModuleSyncWatchdog { */ @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:5000}") public void moduleSyncAdvisedCmHandles() { + log.info("Processing module sync watchdog waking up."); populateWorkQueueIfNeeded(); final int asyncTaskParallelismLevel = asyncTaskExecutor.getAsyncTaskParallelismLevel(); - while (!moduleSyncWorkQueue.isEmpty() && batchCounter.get() <= asyncTaskParallelismLevel) { - batchCounter.getAndIncrement(); - final Collection<DataNode> nextBatch = prepareNextBatch(); - asyncTaskExecutor.executeTask(() -> - moduleSyncTasks.performModuleSync(nextBatch, batchCounter), - ASYNC_TASK_TIMEOUT_IN_MILLISECONDS - ); - preventBusyWait(); + while (!moduleSyncWorkQueue.isEmpty()) { + if (batchCounter.get() <= asyncTaskParallelismLevel) { + final Collection<DataNode> nextBatch = prepareNextBatch(); + log.debug("Processing module sync batch of {}. {} batch(es) active.", + nextBatch.size(), batchCounter.get()); + asyncTaskExecutor.executeTask(() -> + moduleSyncTasks.performModuleSync(nextBatch, batchCounter), + ASYNC_TASK_TIMEOUT_IN_MILLISECONDS + ); + batchCounter.getAndIncrement(); + } else { + preventBusyWait(); + } } } @@ -80,6 +86,7 @@ public class ModuleSyncWatchdog { */ @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}") public void resetPreviouslyFailedCmHandles() { + log.info("Processing module sync retry-watchdog waking up."); final List<YangModelCmHandle> failedCmHandles = syncUtils.getModuleSyncFailedCmHandles(); moduleSyncTasks.resetFailedCmHandles(failedCmHandles); } @@ -95,6 +102,7 @@ public class ModuleSyncWatchdog { private void populateWorkQueueIfNeeded() { if (moduleSyncWorkQueue.isEmpty()) { final List<DataNode> advisedCmHandles = syncUtils.getAdvisedCmHandles(); + log.info("Processing module sync fetched {} advised cm handles from DB", advisedCmHandles.size()); for (final DataNode advisedCmHandle : advisedCmHandles) { if (!moduleSyncWorkQueue.offer(advisedCmHandle)) { log.warn("Unable to add cm handle {} to the work queue", advisedCmHandle.getLeaves().get("id")); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponse.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponse.java index b7faf09a9e..d5b27b61f6 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponse.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponse.java @@ -21,14 +21,20 @@ package org.onap.cps.ncmp.api.models; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.Builder; import lombok.Data; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Data @Builder +@Slf4j public class CmHandleRegistrationResponse { private final String cmHandle; @@ -36,16 +42,19 @@ public class CmHandleRegistrationResponse { private RegistrationError registrationError; private String errorText; + private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']"); + /** * Creates a failure response based on exception. * - * @param cmHandle cmHandle + * @param cmHandleId cmHandleId * @param exception exception * @return CmHandleRegistrationResponse */ - public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) { + public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId, + final Exception exception) { return CmHandleRegistrationResponse.builder() - .cmHandle(cmHandle) + .cmHandle(cmHandleId) .status(Status.FAILURE) .registrationError(RegistrationError.UNKNOWN_ERROR) .errorText(exception.getMessage()).build(); @@ -54,19 +63,55 @@ public class CmHandleRegistrationResponse { /** * Creates a failure response based on registration error. * - * @param cmHandle cmHandle + * @param cmHandleId cmHandleId * @param registrationError registrationError * @return CmHandleRegistrationResponse */ - public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, + public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId, final RegistrationError registrationError) { - return CmHandleRegistrationResponse.builder().cmHandle(cmHandle) + return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId) .status(Status.FAILURE) .registrationError(registrationError) .errorText(registrationError.errorText) .build(); } + /** + * Creates a failure response based on registration error. + * + * @param failedXpaths list of failed Xpaths + * @param registrationError enum describing the type of registration error + * @return CmHandleRegistrationResponse + */ + public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths, + final RegistrationError registrationError) { + final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size()); + for (final String xpath : failedXpaths) { + final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath); + if (matcher.find()) { + cmHandleRegistrationResponses.add( + CmHandleRegistrationResponse.createFailureResponse(matcher.group(1), registrationError)); + } else { + log.warn("Unexpected xpath {}", xpath); + } + } + return cmHandleRegistrationResponses; + } + + /** + * Creates a failure response based on other exception. + * + * @param cmHandleIds list of failed cmHandleIds + * @param exception exception caught during the registration + * @return CmHandleRegistrationResponse + */ + public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds, + final Exception exception) { + return cmHandleIds.stream() + .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception)) + .collect(Collectors.toList()); + } + public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) { return CmHandleRegistrationResponse.builder().cmHandle(cmHandle) .status(Status.SUCCESS).build(); @@ -92,4 +137,4 @@ public class CmHandleRegistrationResponse { public final String errorText; } -}
\ No newline at end of file +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy index 86a32a1a59..0b58d44191 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy @@ -28,14 +28,13 @@ import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler import org.onap.cps.ncmp.api.impl.exception.DmiRequestException import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations -import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.inventory.CmHandleQueries import org.onap.cps.ncmp.api.inventory.CmHandleState import org.onap.cps.ncmp.api.inventory.InventoryPersistence import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse import org.onap.cps.ncmp.api.models.DmiPluginRegistration import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle -import org.onap.cps.spi.exceptions.AlreadyDefinedException +import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataValidationException import org.onap.cps.spi.exceptions.SchemaSetNotFoundException @@ -185,20 +184,18 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'), new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')]) and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd' - mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(*_) >> { throw new RuntimeException("Failed") } + def xpath = "somePathWithId[@id='cmhandle2']" + mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(*_) >> { throw new AlreadyDefinedExceptionBatch([xpath]) } when: 'registration is updated to create cm-handles' def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration) then: 'a response is received for all cm-handles' response.getCreatedCmHandles().size() == 1 and: 'all cm-handles creation fails' - with(response.getCreatedCmHandles().get(0)) { + response.getCreatedCmHandles().each { + assert it.cmHandle == 'cmhandle2' assert it.status == Status.FAILURE - assert it.registrationError == UNKNOWN_ERROR - assert it.errorText == 'Failed' - def sortedCmHandles = it.cmHandle.split(',').sort() - assert sortedCmHandles[0] == 'cmhandle1' - assert sortedCmHandles[1] == 'cmhandle2' - assert sortedCmHandles[2] == 'cmhandle3' + assert it.registrationError == CM_HANDLE_ALREADY_EXIST + assert it.errorText == 'cm-handle already exists' } } @@ -219,10 +216,10 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { assert it.errorText == expectedErrorText } where: - scenario | cmHandleId | exception || expectedError | expectedErrorText - 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedException('', new RuntimeException()) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists' - 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id' - 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed' + scenario | cmHandleId | exception || expectedError | expectedErrorText + 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedExceptionBatch(["path[@id='${cmHandleId}']".toString()]) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists' + 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id' + 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed' } def 'Update CM-Handle: Update Operation Response is added to the response'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy index 19c8ae81ce..0d459fd0fa 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy @@ -44,7 +44,7 @@ import java.time.format.DateTimeFormatter import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS -class InventoryPersistenceSpec extends Specification { +class InventoryPersistenceImplSpec extends Specification { def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) @@ -56,7 +56,7 @@ class InventoryPersistenceSpec extends Specification { def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService) - def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsModuleService, + def objectUnderTest = new InventoryPersistenceImpl(spiedJsonObjectMapper, mockCpsDataService, mockCpsModuleService, mockCpsDataPersistenceService, mockCpsAdminPersistenceService) def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") 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 e5240c0e66..dd989bf676 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 @@ -52,20 +52,19 @@ class ModuleSyncWatchdogSpec extends Specification { def 'Module sync advised cm handles with #scenario.'() { given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles' mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles) - and: 'the executor has #parallelismLevel available threads' - spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> parallelismLevel + and: 'the executor has enough available threads' + spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3 when: ' module sync is started' objectUnderTest.moduleSyncAdvisedCmHandles() then: 'it performs #expectedNumberOfTaskExecutions tasks' expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_) where: ' the following parameter are used' - scenario | parallelismLevel | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions - 'less then 1 batch' | 9 | 1 || 1 - 'exactly 1 batch' | 9 | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1 - '2 batches' | 9 | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2 - 'queue capacity' | 9 | testQueueCapacity || 3 - 'over queue capacity' | 9 | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3 - 'not enough threads' | 2 | testQueueCapacity || 2 + scenario | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions + 'less then 1 batch' | 1 || 1 + 'exactly 1 batch' | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1 + '2 batches' | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2 + 'queue capacity' | testQueueCapacity || 3 + 'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3 } def 'Reset failed cm handles.'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponseSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponseSpec.groovy index 4476998d82..dba29343e9 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponseSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/models/CmHandleRegistrationResponseSpec.groovy @@ -24,6 +24,8 @@ import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationErr import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status import spock.lang.Specification +import java.util.stream.Collectors + class CmHandleRegistrationResponseSpec extends Specification { def 'Successful cm-handle Registration Response'() { @@ -68,4 +70,25 @@ class CmHandleRegistrationResponseSpec extends Specification { 'cm-handle id is invalid' | 'cm handle' | RegistrationError.CM_HANDLE_INVALID_ID } + def 'Failed cm-handle Registration with multiple responses.'() { + when: 'cm-handle failure response is created for 2 xpaths' + def cmHandleRegistrationResponses = + CmHandleRegistrationResponse.createFailureResponses(["somePathWithId[@id='123']","somePathWithId[@id='456']"], RegistrationError.CM_HANDLE_ALREADY_EXIST) + then: 'the response has the correct cm handle ids' + assert cmHandleRegistrationResponses.size() == 2 + assert cmHandleRegistrationResponses.stream().map(it -> it.cmHandle).collect(Collectors.toList()) + .containsAll(['123','456']) + } + + def 'Failed cm-handle Registration with multiple responses with an unexpected xpath.'() { + when: 'cm-handle failure response is created for one valid and one unexpected xpath' + def cmHandleRegistrationResponses = + CmHandleRegistrationResponse.createFailureResponses(["somePathWithId[@id='123']","valid/xpath/without-id[@key='123']"], RegistrationError.CM_HANDLE_ALREADY_EXIST) + then: 'the response has only one entry' + assert cmHandleRegistrationResponses.size() == 1 + } + + + + } |