From 82a550f6b080cb50912d93f7b13ba0fc97a95470 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Mon, 11 Jul 2022 10:55:53 +0100 Subject: Query CmHandles using CPS path Added withCpsPath condition parameter Validated to prevent misuse and blocking of querying using private properties Updated OpenAPI with examples and links to documentation Moved methods related to cmHandle querying using cps path from InventoryPersistence to CmHandleQueries Renamed private method deleteSchemaSetAndListElementByCmHandleId to deleteCmHandleByCmHandleId Issue-ID: CPS-977 Change-Id: I83827215b7e58de74f8f62cd0140516d217d93f1 Signed-off-by: lukegleeson --- .../NetworkCmProxyCmHandlerQueryServiceImpl.java | 144 +++++++++--------- .../api/impl/NetworkCmProxyDataServiceImpl.java | 4 +- .../cps/ncmp/api/inventory/CmHandleQueries.java | 165 +++++++++++++++++++++ .../ncmp/api/inventory/InventoryPersistence.java | 65 -------- .../cps/ncmp/api/inventory/sync/SyncUtils.java | 25 ++-- 5 files changed, 256 insertions(+), 147 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java (limited to 'cps-ncmp-service/src/main') 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 a62a009ce..d784bcdea 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 @@ -21,6 +21,8 @@ package org.onap.cps.ncmp.api.impl; 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.validateCpsPathConditionProperties; import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties; import java.util.ArrayList; @@ -31,17 +33,22 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.cpspath.parser.PathParsingException; import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.inventory.CmHandleQueries; import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; +import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.CmHandleQueryServiceParameters; import org.onap.cps.spi.model.ConditionProperties; import org.onap.cps.spi.model.DataNode; +import org.onap.cps.utils.ValidQueryProperties; import org.springframework.stereotype.Service; @Service @@ -49,9 +56,8 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCmHandlerQueryService { - 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 static final Map NO_QUERY_TO_EXECUTE = null; + private final CmHandleQueries cmHandleQueries; private final InventoryPersistence inventoryPersistence; /** @@ -68,14 +74,10 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return getAllCmHandles(); } - final Map publicPropertyQueryResult - = executePublicPropertyQueries(cmHandleQueryServiceParameters); + final Map combinedQueryResult = executeInventoryQueries( + cmHandleQueryServiceParameters); - final Map combinedQueryResult = - combineWithModuleNameQuery(cmHandleQueryServiceParameters, publicPropertyQueryResult); - - return combinedQueryResult == NO_QUERY_EXECUTED - ? Collections.emptySet() : new HashSet<>(combinedQueryResult.values()); + return new HashSet<>(combineWithModuleNameQuery(cmHandleQueryServiceParameters, combinedQueryResult).values()); } /** @@ -92,52 +94,24 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return getAllCmHandleIds(); } - final Map publicPropertyQueryResult - = executePublicPropertyQueries(cmHandleQueryServiceParameters); + final Map combinedQueryResult = executeInventoryQueries( + cmHandleQueryServiceParameters); final Collection moduleNamesForQuery = getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters()); if (moduleNamesForQuery.isEmpty()) { - return publicPropertyQueryResult == NO_QUERY_EXECUTED - ? Collections.emptySet() : publicPropertyQueryResult.keySet(); + return combinedQueryResult.keySet(); } final Set moduleNameQueryResult = getNamesOfAnchorsWithGivenModules(moduleNamesForQuery); - if (publicPropertyQueryResult == NO_QUERY_EXECUTED) { + if (combinedQueryResult == NO_QUERY_TO_EXECUTE) { return moduleNameQueryResult; } - moduleNameQueryResult.retainAll(publicPropertyQueryResult.keySet()); + moduleNameQueryResult.retainAll(combinedQueryResult.keySet()); return moduleNameQueryResult; } - private Map executePublicPropertyQueries( - final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { - final Map publicPropertyQueryPairs = - getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters()); - if (publicPropertyQueryPairs.isEmpty()) { - return NO_QUERY_EXECUTED; - } - Map cmHandleIdToNcmpServiceCmHandles = null; - for (final Map.Entry entry : publicPropertyQueryPairs.entrySet()) { - final String cpsPath = "//public-properties[@name='" + entry.getKey() + "' and @value='" - + entry.getValue() + "']/ancestor::cm-handles"; - - final Collection dataNodes = inventoryPersistence.queryDataNodes(cpsPath); - if (cmHandleIdToNcmpServiceCmHandles == NO_QUERY_EXECUTED) { - cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes); - } else { - final Collection cmHandleIdsToRetain = dataNodes.parallelStream() - .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet()); - cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain); - } - if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) { - break; - } - } - return cmHandleIdToNcmpServiceCmHandles; - } - private Map combineWithModuleNameQuery( final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final Map previousQueryResult) { @@ -151,7 +125,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return Collections.emptyMap(); } final Map queryResult = new HashMap<>(cmHandleIdsByModuleName.size()); - if (previousQueryResult == NO_QUERY_EXECUTED) { + if (previousQueryResult == NO_QUERY_TO_EXECUTE) { cmHandleIdsByModuleName.forEach(cmHandleId -> queryResult.put(cmHandleId, createNcmpServiceCmHandle( inventoryPersistence.getDataNode("/dmi-registry/cm-handles[@id='" + cmHandleId + "']"))) @@ -163,19 +137,68 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return queryResult; } + private Map executeInventoryQueries( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + final Map cpsPath = getCpsPath(cmHandleQueryServiceParameters.getCmHandleQueryParameters()); + if (!validateCpsPathConditionProperties(cpsPath)) { + return Collections.emptyMap(); + } + final Map cpsPathQueryResult; + if (cpsPath.isEmpty()) { + cpsPathQueryResult = NO_QUERY_TO_EXECUTE; + } else { + try { + cpsPathQueryResult = cmHandleQueries.getCmHandleDataNodesByCpsPath( + cpsPath.get("cpsPath"), INCLUDE_ALL_DESCENDANTS) + .stream().map(this::createNcmpServiceCmHandle) + .collect(Collectors.toMap(NcmpServiceCmHandle::getCmHandleId, + Function.identity())); + } catch (final PathParsingException pathParsingException) { + throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(), + pathParsingException); + } + if (cpsPathQueryResult.isEmpty()) { + return Collections.emptyMap(); + } + } + + final Map publicPropertyQueryPairs = + getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters()); + final Map propertiesQueryResult = publicPropertyQueryPairs.isEmpty() + ? NO_QUERY_TO_EXECUTE : cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs); + + return cmHandleQueries.combineCmHandleQueries(cpsPathQueryResult, propertiesQueryResult); + } + private Set getNamesOfAnchorsWithGivenModules(final Collection moduleNamesForQuery) { final Collection anchors = inventoryPersistence.queryAnchors(moduleNamesForQuery); return anchors.parallelStream().map(Anchor::getName).collect(Collectors.toSet()); } - private Map collectDataNodesToNcmpServiceCmHandles( - final Collection dataNodes) { - final Map cmHandleIdToNcmpServiceCmHandle = new HashMap<>(); - dataNodes.forEach(dataNode -> { - final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode); - cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle); - }); - return cmHandleIdToNcmpServiceCmHandle; + private Collection getModuleNamesForQuery(final List conditionProperties) { + final List result = new ArrayList<>(); + getConditions(conditionProperties, ValidQueryProperties.HAS_ALL_MODULES.getQueryProperty()) + .parallelStream().forEach( + conditionProperty -> { + validateModuleNameConditionProperties(conditionProperty); + result.add(conditionProperty.get("moduleName")); + } + ); + return result; + } + + private Map getCpsPath(final List conditionProperties) { + final Map result = new HashMap<>(); + getConditions(conditionProperties, ValidQueryProperties.WITH_CPS_PATH.getQueryProperty()).forEach( + result::putAll); + return result; + } + + private Map getPublicPropertyPairs(final List conditionProperties) { + final Map result = new HashMap<>(); + getConditions(conditionProperties, + ValidQueryProperties.HAS_ALL_PROPERTIES.getQueryProperty()).forEach(result::putAll); + return result; } private List> getConditions(final List conditionProperties, @@ -188,23 +211,6 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return Collections.emptyList(); } - private Collection getModuleNamesForQuery(final List conditionProperties) { - final List result = new ArrayList<>(); - getConditions(conditionProperties, MODULE_QUERY_NAME).parallelStream().forEach( - conditionProperty -> { - validateModuleNameConditionProperties(conditionProperty); - result.add(conditionProperty.get("moduleName")); - } - ); - return result; - } - - private Map getPublicPropertyPairs(final List conditionProperties) { - final Map result = new HashMap<>(); - getConditions(conditionProperties, PROPERTY_QUERY_NAME).forEach(result::putAll); - return result; - } - private Set getAllCmHandles() { return inventoryPersistence.getDataNode("/dmi-registry") .getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet()); 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 8d32c1ade..28cbf2cc2 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 @@ -271,7 +271,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId); lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.DELETING); - deleteSchemaSetAndListElementByCmHandleId(cmHandleId); + deleteCmHandleByCmHandleId(cmHandleId); cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)); lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.DELETED); @@ -295,7 +295,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return cmHandleRegistrationResponses; } - private void deleteSchemaSetAndListElementByCmHandleId(final String cmHandleId) { + private void deleteCmHandleByCmHandleId(final String cmHandleId) { inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId); inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']"); } 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 new file mode 100644 index 000000000..92387bab3 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java @@ -0,0 +1,165 @@ +/* + * ============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.ncmp.api.inventory; + +import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle; +import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; +import org.onap.cps.spi.CpsDataPersistenceService; +import org.onap.cps.spi.FetchDescendantsOption; +import org.onap.cps.spi.model.DataNode; +import org.springframework.stereotype.Component; + +@RequiredArgsConstructor +@Component +public class CmHandleQueries { + + private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; + private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; + + private final CpsDataPersistenceService cpsDataPersistenceService; + private static final Map NO_QUERY_TO_EXECUTE = null; + private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles"; + + + /** + * Query CmHandles based on PublicProperties. + * + * @param publicPropertyQueryPairs public properties for query + * @return CmHandles which have these public properties + */ + public Map queryCmHandlePublicProperties( + final Map publicPropertyQueryPairs) { + if (publicPropertyQueryPairs.isEmpty()) { + return Collections.emptyMap(); + } + Map cmHandleIdToNcmpServiceCmHandles = null; + for (final Map.Entry publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) { + final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey() + + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]"; + + final Collection dataNodes = getCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS); + if (cmHandleIdToNcmpServiceCmHandles == null) { + cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes); + } else { + final Collection cmHandleIdsToRetain = dataNodes.parallelStream() + .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet()); + cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain); + } + if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) { + break; + } + } + return cmHandleIdToNcmpServiceCmHandles; + } + + /** + * Combine Maps of CmHandles. + * + * @param firstQuery first CmHandles Map + * @param secondQuery second CmHandles Map + * @return combined Map of CmHandles + */ + public Map combineCmHandleQueries( + final Map firstQuery, + final Map secondQuery) { + if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) { + return Collections.emptyMap(); + } else if (firstQuery == NO_QUERY_TO_EXECUTE) { + return secondQuery; + } else if (secondQuery == NO_QUERY_TO_EXECUTE) { + return firstQuery; + } else { + firstQuery.keySet().retainAll(secondQuery.keySet()); + return firstQuery; + } + } + + /** + * Method which returns cm handles by the cm handles state. + * + * @param cmHandleState cm handle state + * @return a list of cm handles + */ + public List getCmHandlesByState(final CmHandleState cmHandleState) { + return getCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]", + INCLUDE_ALL_DESCENDANTS); + } + + /** + * Method to return data nodes representing the cm handles. + * + * @param cpsPath cps path for which the cmHandle is requested + * @return a list of data nodes representing the cm handles. + */ + public List getCmHandleDataNodesByCpsPath(final String cpsPath, + final FetchDescendantsOption fetchDescendantsOption) { + return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption); + } + + /** + * Method which returns cm handles by the cm handle id and state. + * @param cmHandleId cm handle id + * @param cmHandleState cm handle state + * @return a list of cm handles + */ + public List getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) { + return getCmHandleDataNodesByCpsPath("//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\"" + + cmHandleState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS); + } + + /** + * Method which returns cm handles by the operational sync state of cm handle. + * @param dataStoreSyncState sync state + * @return a list of cm handles + */ + public List getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) { + return getCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\"" + + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS); + } + + private Map collectDataNodesToNcmpServiceCmHandles( + final Collection dataNodes) { + final Map cmHandleIdToNcmpServiceCmHandle = new HashMap<>(); + dataNodes.forEach(dataNode -> { + final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode); + cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle); + }); + return cmHandleIdToNcmpServiceCmHandle; + } + + private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) { + return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter + .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString())); + } +} + + 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 be26a58d5..14fc6d698 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 @@ -28,7 +28,6 @@ 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; @@ -60,8 +59,6 @@ public class InventoryPersistence { private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']"; - private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles"; - private final JsonObjectMapper jsonObjectMapper; private final CpsDataService cpsDataService; @@ -99,57 +96,6 @@ public class InventoryPersistence { cmHandleJsonData, OffsetDateTime.now()); } - /** - * Method which returns cm handles by the cm handles state. - * - * @param cmHandleState cm handle state - * @return a list of cm handles - */ - public List getCmHandlesByState(final CmHandleState cmHandleState) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, - NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\"" - + cmHandleState + ANCESTOR_CM_HANDLES, - FetchDescendantsOption.OMIT_DESCENDANTS); - } - - /** - * Method to return data nodes representing the cm handles. - * - * @param cpsPath cps path for which the cmHandle is requested - * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes - * @return a list of data nodes representing the cm handles. - */ - public List getCmHandleDataNodesByCpsPath(final String cpsPath, - final FetchDescendantsOption fetchDescendantsOption) { - return cpsDataPersistenceService.queryDataNodes( - NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption); - } - - /** - * Method which returns cm handles by the cm handle id and state. - * @param cmHandleId cm handle id - * @param cmHandleState cm handle state - * @return a list of cm handles - */ - public List getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, - NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\"" - + cmHandleState + ANCESTOR_CM_HANDLES, - FetchDescendantsOption.OMIT_DESCENDANTS); - } - - /** - * Method which returns cm handles by the operational sync state of cm handle. - * @param dataStoreSyncState sync state - * @return a list of cm handles - */ - public List getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, - NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores" - + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES, - FetchDescendantsOption.OMIT_DESCENDANTS); - } - /** * This method retrieves DMI service name, DMI properties and the state for a given cm handle. * @param cmHandleId the id of the cm handle @@ -218,17 +164,6 @@ public class InventoryPersistence { } } - /** - * 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. * 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 467fd8f60..2b7d3c99c 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 @@ -40,6 +40,7 @@ import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; 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.CompositeState; import org.onap.cps.ncmp.api.inventory.DataStoreSyncState; @@ -57,6 +58,8 @@ import org.springframework.stereotype.Service; public class SyncUtils { private final InventoryPersistence inventoryPersistence; + private final CmHandleQueries cmHandleQueries; + private final DmiDataOperations dmiDataOperations; private final JsonObjectMapper jsonObjectMapper; @@ -70,7 +73,7 @@ public class SyncUtils { */ public List getAdvisedCmHandles() { final List advisedCmHandlesAsDataNodeList = new ArrayList<>( - inventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED)); + cmHandleQueries.getCmHandlesByState(CmHandleState.ADVISED)); log.info("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodeList.size()); if (advisedCmHandlesAsDataNodeList.isEmpty()) { return Collections.emptyList(); @@ -87,16 +90,16 @@ public class SyncUtils { * return null if not found */ public YangModelCmHandle getAnUnSynchronizedReadyCmHandle() { - final List unSynchronizedCmHandles = inventoryPersistence - .getCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED); + final List unSynchronizedCmHandles = cmHandleQueries + .getCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED); if (unSynchronizedCmHandles.isEmpty()) { return null; } Collections.shuffle(unSynchronizedCmHandles); for (final DataNode cmHandle : unSynchronizedCmHandles) { final String cmHandleId = cmHandle.getLeaves().get("id").toString(); - final List readyCmHandles = inventoryPersistence - .getCmHandlesByIdAndState(cmHandleId, CmHandleState.READY); + final List readyCmHandles = cmHandleQueries + .getCmHandlesByIdAndState(cmHandleId, CmHandleState.READY); if (!readyCmHandles.isEmpty()) { return inventoryPersistence.getYangModelCmHandle(cmHandleId); } @@ -110,8 +113,8 @@ public class SyncUtils { * @return a random LOCKED yang model cm handle, return null if not found */ public List getModuleSyncFailedCmHandles() { - final List lockedCmHandlesAsDataNodeList = inventoryPersistence.getCmHandleDataNodesByCpsPath( - "//lock-reason[@reason=\"LOCKED_MODULE_SYNC_FAILED\"]/ancestor::cm-handles", + final List lockedCmHandlesAsDataNodeList = cmHandleQueries.getCmHandleDataNodesByCpsPath( + "//lock-reason[@reason=\"LOCKED_MODULE_SYNC_FAILED\"]", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList); } @@ -171,8 +174,8 @@ public class SyncUtils { */ public String getResourceData(final String cmHandleId) { final ResponseEntity resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi( - cmHandleId, DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, - UUID.randomUUID().toString()); + cmHandleId, DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, + UUID.randomUUID().toString()); if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) { return getFirstResource(resourceDataResponseEntity.getBody()); } @@ -188,8 +191,8 @@ public class SyncUtils { } private List convertCmHandlesDataNodesToYangModelCmHandles( - final List cmHandlesAsDataNodeList) { + final List cmHandlesAsDataNodeList) { return cmHandlesAsDataNodeList.stream().map(dataNode -> YangDataConverter.convertCmHandleToYangModel(dataNode, - dataNode.getLeaves().get("id").toString())).collect(Collectors.toList()); + dataNode.getLeaves().get("id").toString())).collect(Collectors.toList()); } } -- cgit 1.2.3-korg