From 2aab13b140cbb13fc7ab880b576c75f42a774415 Mon Sep 17 00:00:00 2001 From: emaclee Date: Tue, 28 Jun 2022 13:13:21 +0100 Subject: Move persistence related methods -created new method in inventory persistence service for getting module references Issue-ID: CPS-1117 Signed-off-by: emaclee Change-Id: Ic1f3c180a62a7211e19982d8f3570829db58370e --- .../NetworkCmProxyCmHandlerQueryServiceImpl.java | 33 ++----- .../api/impl/NetworkCmProxyDataServiceImpl.java | 33 +------ .../ncmp/api/inventory/InventoryPersistence.java | 105 +++++++++++++++++++++ .../cps/ncmp/api/inventory/sync/SyncUtils.java | 3 +- 4 files changed, 118 insertions(+), 56 deletions(-) (limited to 'cps-ncmp-service/src/main/java/org') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java index 000627bec..a62a009ce 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java @@ -20,10 +20,7 @@ package org.onap.cps.ncmp.api.impl; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR; import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle; -import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties; import java.util.ArrayList; @@ -39,9 +36,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; -import org.onap.cps.spi.CpsAdminPersistenceService; -import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.CmHandleQueryServiceParameters; import org.onap.cps.spi.model.ConditionProperties; @@ -56,8 +52,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm private static final String PROPERTY_QUERY_NAME = "hasAllProperties"; private static final String MODULE_QUERY_NAME = "hasAllModules"; private static final Map NO_QUERY_EXECUTED = null; - private final CpsDataPersistenceService cpsDataPersistenceService; - private final CpsAdminPersistenceService cpsAdminPersistenceService; + private final InventoryPersistence inventoryPersistence; /** * Query and return cm handles that match the given query parameters. @@ -128,7 +123,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm final String cpsPath = "//public-properties[@name='" + entry.getKey() + "' and @value='" + entry.getValue() + "']/ancestor::cm-handles"; - final Collection dataNodes = queryDataNodes(cpsPath); + final Collection dataNodes = inventoryPersistence.queryDataNodes(cpsPath); if (cmHandleIdToNcmpServiceCmHandles == NO_QUERY_EXECUTED) { cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes); } else { @@ -159,7 +154,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm if (previousQueryResult == NO_QUERY_EXECUTED) { cmHandleIdsByModuleName.forEach(cmHandleId -> queryResult.put(cmHandleId, createNcmpServiceCmHandle( - getDataNode("/dmi-registry/cm-handles[@id='" + cmHandleId + "']"))) + inventoryPersistence.getDataNode("/dmi-registry/cm-handles[@id='" + cmHandleId + "']"))) ); return queryResult; } @@ -169,8 +164,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm } private Set getNamesOfAnchorsWithGivenModules(final Collection moduleNamesForQuery) { - final Collection anchors = - cpsAdminPersistenceService.queryAnchors("NFP-Operational", moduleNamesForQuery); + final Collection anchors = inventoryPersistence.queryAnchors(moduleNamesForQuery); return anchors.parallelStream().map(Anchor::getName).collect(Collectors.toSet()); } @@ -212,23 +206,12 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm } private Set getAllCmHandles() { - return getDataNode("/dmi-registry").getChildDataNodes().stream() - .map(this::createNcmpServiceCmHandle).collect(Collectors.toSet()); + return inventoryPersistence.getDataNode("/dmi-registry") + .getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet()); } private Set getAllCmHandleIds() { - return cpsAdminPersistenceService.getAnchors("NFP-Operational") - .parallelStream().map(Anchor::getName).collect(Collectors.toSet()); - } - - private List queryDataNodes(final String cpsPath) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - cpsPath, INCLUDE_ALL_DESCENDANTS); - } - - private DataNode getDataNode(final String cpsPath) { - return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - cpsPath, INCLUDE_ALL_DESCENDANTS); + return inventoryPersistence.getAnchors().parallelStream().map(Anchor::getName).collect(Collectors.toSet()); } private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) { 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 044a5a44f..d827d465c 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 @@ -23,13 +23,7 @@ package org.onap.cps.ncmp.api.impl; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT; -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.ncmp.api.impl.operations.DmiRequestBody.OperationEnum; -import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED; import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters; import java.util.ArrayList; @@ -41,8 +35,6 @@ import java.util.Set; import java.util.stream.Collectors; 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.NetworkCmProxyCmHandlerQueryService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; @@ -61,7 +53,6 @@ import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.exceptions.DataValidationException; -import org.onap.cps.spi.exceptions.SchemaSetNotFoundException; import org.onap.cps.spi.model.CmHandleQueryServiceParameters; import org.onap.cps.spi.model.ModuleDefinition; import org.onap.cps.spi.model.ModuleReference; @@ -75,14 +66,10 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService { - private final CpsDataService cpsDataService; - private final JsonObjectMapper jsonObjectMapper; private final DmiDataOperations dmiDataOperations; - private final CpsModuleService cpsModuleService; - private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler; private final InventoryPersistence inventoryPersistence; @@ -147,11 +134,10 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService requestData, dataType); } - @Override public Collection getYangResourcesModuleReferences(final String cmHandleId) { CpsValidator.validateNameCharacters(cmHandleId); - return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); + return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId); } @Override @@ -279,9 +265,8 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService for (final String cmHandle : tobeRemovedCmHandles) { try { CpsValidator.validateNameCharacters(cmHandle); - deleteSchemaSetWithCascade(cmHandle); - cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP); + inventoryPersistence.deleteSchemaSetWithCascade(cmHandle); + inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandle + "']"); cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle)); } catch (final DataNodeNotFoundException dataNodeNotFoundException) { log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", @@ -303,21 +288,11 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return cmHandleRegistrationResponses; } - private void deleteSchemaSetWithCascade(final String schemaSetName) { - try { - 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); - } - } - private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) { try { 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); + inventoryPersistence.saveListElements(cmHandleJsonData); return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId()); } catch (final AlreadyDefinedException alreadyDefinedException) { return CmHandleRegistrationResponse.createFailureResponse( 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 d47da6c0c..19cf22598 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 @@ -22,23 +22,32 @@ 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.Collection; import java.util.List; 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 { @@ -47,6 +56,8 @@ public class InventoryPersistence { private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; + private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry"; + private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']"; private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles"; @@ -59,6 +70,8 @@ public class InventoryPersistence { private final CpsDataPersistenceService cpsDataPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; + /** * Get the Cm Handle Composite State from the data node. * @@ -157,6 +170,80 @@ public class InventoryPersistence { return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); } + /** + * Method to return module references by cmHandleId. + * + * @param cmHandleId cm handle ID + * @return a collection of module references (moduleName and revision) + */ + public Collection getYangResourcesModuleReferences(final String cmHandleId) { + CpsValidator.validateNameCharacters(cmHandleId); + return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId); + } + + /** + * Method to save list elements. + * + * @param cmHandleJsonData cmHandle JSON data + */ + public void saveListElements(final String cmHandleJsonData) { + cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, + cmHandleJsonData, NO_TIMESTAMP); + } + + /** + * 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); + } + + /** + * 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); + } + } + + /** + * Query data nodes via cps path. + * + * @param cpsPath cps path + * @return List of data nodes + */ + public List queryDataNodes(final String cpsPath) { + return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + cpsPath, INCLUDE_ALL_DESCENDANTS); + } + + /** + * Get data node via xpath. + * + * @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); + } + + /** + * Get data node of given cm handle. + * + * @param cmHandle cm handle + * @return data node + */ private DataNode getCmHandleDataNode(final String cmHandle) { return cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, @@ -164,4 +251,22 @@ public class InventoryPersistence { FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); } + /** + * Query anchors via module names. + * + * @param moduleNamesForQuery module names + * @return Collection of anchors + */ + public Collection queryAnchors(final Collection moduleNamesForQuery) { + return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery); + } + + /** + * Method to get all anchors. + * + * @return Collection of anchors + */ + public Collection getAnchors() { + return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME); + } } \ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java index 046a116ef..2b80b9d53 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java @@ -22,7 +22,6 @@ package org.onap.cps.ncmp.api.inventory.sync; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.ImmutableMap; import java.security.SecureRandom; import java.time.Duration; import java.time.OffsetDateTime; @@ -190,6 +189,6 @@ public class SyncUtils { final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString); final Iterator> overallJsonTreeMap = overallJsonNode.fields(); final Map.Entry firstElement = overallJsonTreeMap.next(); - return jsonObjectMapper.asJsonString(ImmutableMap.of(firstElement.getKey(), firstElement.getValue())); + return jsonObjectMapper.asJsonString(Map.of(firstElement.getKey(), firstElement.getValue())); } } -- cgit 1.2.3-korg