diff options
Diffstat (limited to 'cps-ncmp-service/src/main/java/org')
13 files changed, 543 insertions, 368 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index 45dba211a3..0ea0674281 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -33,6 +33,7 @@ import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters; 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.model.ModuleDefinition; import org.onap.cps.spi.model.ModuleReference; @@ -67,6 +68,18 @@ public interface NetworkCmProxyDataService { String requestId); /** + * Get resource data for operational. + * + * @param cmHandleId cm handle identifier + * @param resourceIdentifier resource identifier + * @Link FetchDescendantsOption fetch descendants option + * @return {@code Object} resource data + */ + Object getResourceDataOperational(String cmHandleId, + String resourceIdentifier, + FetchDescendantsOption fetchDescendantsOption); + + /** * Get resource data for data store pass-through running * using dmi. * 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 5b072f35ed..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 @@ -23,6 +23,7 @@ package org.onap.cps.ncmp.api.impl; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum; import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters; @@ -57,7 +58,8 @@ import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationErr 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.exceptions.AlreadyDefinedException; +import org.onap.cps.spi.FetchDescendantsOption; +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; @@ -116,24 +118,32 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String topicParamInQuery, final String requestId) { final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId, - resourceIdentifier, - optionsParamInQuery, - DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, - requestId, topicParamInQuery); + resourceIdentifier, + optionsParamInQuery, + DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, + requestId, topicParamInQuery); return responseEntity.getBody(); } @Override + public Object getResourceDataOperational(final String cmHandleId, + final String resourceIdentifier, + final FetchDescendantsOption fetchDescendantsOption) { + return cpsDataService.getDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, resourceIdentifier, + fetchDescendantsOption); + } + + @Override public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, final String requestId) { final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId, - resourceIdentifier, - optionsParamInQuery, - DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, - requestId, topicParamInQuery); + resourceIdentifier, + optionsParamInQuery, + DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, + requestId, topicParamInQuery); return responseEntity.getBody(); } @@ -145,7 +155,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String dataType) { CpsValidator.validateNameCharacters(cmHandleId); return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation, - requestData, dataType); + requestData, dataType); } @Override @@ -196,29 +206,29 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService * Set the data sync enabled flag, along with the data sync state * based on the data sync enabled boolean for the cm handle id provided. * - * @param cmHandleId cm handle id + * @param cmHandleId cm handle id * @param dataSyncEnabled data sync enabled flag */ @Override public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) { CpsValidator.validateNameCharacters(cmHandleId); final CompositeState compositeState = inventoryPersistence - .getCmHandleState(cmHandleId); + .getCmHandleState(cmHandleId); if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) { log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled); } else if (compositeState.getCmHandleState() != CmHandleState.READY) { throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: " - + compositeState.getCmHandleState()); + + compositeState.getCmHandleState()); } else { final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores() - .getOperationalDataStore().getDataStoreSyncState(); + .getOperationalDataStore().getDataStoreSyncState(); if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) { - cpsDataService.deleteDataNode("NFP-Operational", cmHandleId, - "/netconf-state", OffsetDateTime.now()); + cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, + "/netconf-state", OffsetDateTime.now()); } CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState); inventoryPersistence.saveCmHandleState(cmHandleId, - compositeState); + compositeState); } } @@ -233,9 +243,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final Set<NcmpServiceCmHandle> ncmpServiceCmHandles = cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier); final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size()); - ncmpServiceCmHandles.forEach(cmHandle -> { - cmHandleIds.add(cmHandle.getCmHandleId()); - }); + ncmpServiceCmHandles.forEach(cmHandle -> cmHandleIds.add(cmHandle.getCmHandleId())); return cmHandleIds; } @@ -262,7 +270,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) { CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = - inventoryPersistence.getYangModelCmHandle(cmHandleId); + inventoryPersistence.getYangModelCmHandle(cmHandleId); final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties(); final Map<String, String> cmHandlePublicProperties = new HashMap<>(); YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties); @@ -290,14 +298,18 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules( final DmiPluginRegistration dmiPluginRegistration) { List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(); + final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(); try { - cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream() - .map(cmHandle -> - YangModelCmHandle.toYangModelCmHandle( - dmiPluginRegistration.getDmiPlugin(), - dmiPluginRegistration.getDmiDataPlugin(), - dmiPluginRegistration.getDmiModelPlugin(), - cmHandle)).map(this::registerNewCmHandle).collect(Collectors.toList()); + dmiPluginRegistration.getCreatedCmHandles() + .forEach(cmHandle -> { + final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle( + dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getDmiDataPlugin(), + dmiPluginRegistration.getDmiModelPlugin(), + cmHandle); + cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED); + }); + cmHandleRegistrationResponses = registerNewCmHandles(cmHandleStatePerCmHandle); } catch (final DataValidationException dataValidationException) { cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration .getCreatedCmHandles().stream() @@ -346,15 +358,19 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']"); } - private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) { + private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState> + cmHandleStatePerCmHandle) { + final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId) + .collect(Collectors.toList()); try { - lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.ADVISED); - return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId()); - } catch (final AlreadyDefinedException alreadyDefinedException) { - return CmHandleRegistrationResponse.createFailureResponse( - yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST); + lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle); + return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds); + } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) { + return CmHandleRegistrationResponse.createFailureResponses( + alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(), + RegistrationError.CM_HANDLE_ALREADY_EXIST); } catch (final Exception exception) { - return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), 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/exception/InvalidTopicException.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java deleted file mode 100644 index b56ca7b8c2..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============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.impl.exception; - -import lombok.Getter; - -public class InvalidTopicException extends RuntimeException { - - @Getter - final String details; - - /** - * Constructor. - * - * @param message the error message - * @param details the error details - */ - public InvalidTopicException(final String message, final String details) { - super(message); - this.details = details; - } -} 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/CmHandleQueries.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java index 569e91e2c9..daabbb56fa 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 @@ -20,37 +20,14 @@ 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 static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -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<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null; - private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles"; +public interface CmHandleQueries { /** * Query CmHandles based on PublicProperties. @@ -58,52 +35,17 @@ public class CmHandleQueries { * @param publicPropertyQueryPairs public properties for query * @return CmHandles which have these public properties */ - public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties( - final Map<String, String> publicPropertyQueryPairs) { - if (publicPropertyQueryPairs.isEmpty()) { - return Collections.emptyMap(); - } - Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null; - for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) { - final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey() - + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]"; - - final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS); - if (cmHandleIdToNcmpServiceCmHandles == null) { - cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes); - } else { - final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream() - .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet()); - cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain); - } - if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) { - break; - } - } - return cmHandleIdToNcmpServiceCmHandles; - } + Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(Map<String, String> publicPropertyQueryPairs); /** * Combine Maps of CmHandles. * - * @param firstQuery first CmHandles Map + * @param firstQuery first CmHandles Map * @param secondQuery second CmHandles Map * @return combined Map of CmHandles */ - public Map<String, NcmpServiceCmHandle> combineCmHandleQueries( - final Map<String, NcmpServiceCmHandle> firstQuery, - final Map<String, NcmpServiceCmHandle> secondQuery) { - if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) { - return NO_QUERY_TO_EXECUTE; - } 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; - } - } + Map<String, NcmpServiceCmHandle> combineCmHandleQueries(Map<String, NcmpServiceCmHandle> firstQuery, + Map<String, NcmpServiceCmHandle> secondQuery); /** * Method which returns cm handles by the cm handles state. @@ -111,10 +53,7 @@ public class CmHandleQueries { * @param cmHandleState cm handle state * @return a list of cm handles */ - public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) { - return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]", - INCLUDE_ALL_DESCENDANTS); - } + List<DataNode> queryCmHandlesByState(CmHandleState cmHandleState); /** * Method to return data nodes representing the cm handles. @@ -122,49 +61,24 @@ public class CmHandleQueries { * @param cpsPath cps path for which the cmHandle is requested * @return a list of data nodes representing the cm handles. */ - public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath, - final FetchDescendantsOption fetchDescendantsOption) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption); - } + List<DataNode> queryCmHandleDataNodesByCpsPath(String cpsPath, FetchDescendantsOption fetchDescendantsOption); /** * Method to check the state of a cm handle with given id. * - * @param cmHandleId cm handle id + * @param cmHandleId cm handle id * @param requiredCmHandleState the required state of the cm handle * @return a boolean, true if the state is equal to the required state */ - public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) { - final DataNode stateDataNode = getCmHandleState(cmHandleId); - final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state"); - return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState); - } + boolean cmHandleHasState(String cmHandleId, CmHandleState requiredCmHandleState); /** * 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<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) { - return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\"" - + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS); - } - - private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles( - final Collection<DataNode> dataNodes) { - final Map<String, NcmpServiceCmHandle> 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())); - } + List<DataNode> queryCmHandlesByOperationalSyncState(DataStoreSyncState dataStoreSyncState); /** * Get all cm handles by DMI plugin identifier. @@ -172,34 +86,5 @@ public class CmHandleQueries { * @param dmiPluginIdentifier DMI plugin identifier * @return set of cm handles */ - public Set<NcmpServiceCmHandle> getCmHandlesByDmiPluginIdentifier(final String dmiPluginIdentifier) { - final Map<String, DataNode> cmHandleAsDataNodePerCmHandleId = new HashMap<>(); - for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) { - for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty( - dmiPluginIdentifier, - modelledDmiServiceLeaf.getLeafName())) { - cmHandleAsDataNodePerCmHandleId.put( - cmHandleAsDataNode.getLeaves().get("id").toString(), cmHandleAsDataNode); - } - } - final Set<NcmpServiceCmHandle> ncmpServiceCmHandles = new HashSet<>(cmHandleAsDataNodePerCmHandleId.size()); - cmHandleAsDataNodePerCmHandleId.values().forEach( - cmHandleAsDataNode -> ncmpServiceCmHandles.add(createNcmpServiceCmHandle(cmHandleAsDataNode))); - return ncmpServiceCmHandles; - } - - private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier, - final String dmiProperty) { - return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - "/dmi-registry/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']", - OMIT_DESCENDANTS); - } - - private DataNode getCmHandleState(final String cmHandleId) { - final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state"; - return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - xpath, OMIT_DESCENDANTS); - } + Set<NcmpServiceCmHandle> getCmHandlesByDmiPluginIdentifier(String dmiPluginIdentifier); } - - diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java new file mode 100644 index 0000000000..e9e2fcacff --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java @@ -0,0 +1,168 @@ +/* + * ============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 static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +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 CmHandleQueriesImpl implements 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<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null; + private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles"; + + + @Override + public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties( + final Map<String, String> publicPropertyQueryPairs) { + if (publicPropertyQueryPairs.isEmpty()) { + return Collections.emptyMap(); + } + Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null; + for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) { + final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey() + + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]"; + + final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS); + if (cmHandleIdToNcmpServiceCmHandles == null) { + cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes); + } else { + final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream() + .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet()); + cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain); + } + if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) { + break; + } + } + return cmHandleIdToNcmpServiceCmHandles; + } + + @Override + public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(final Map<String, NcmpServiceCmHandle> firstQuery, + final Map<String, NcmpServiceCmHandle> secondQuery) { + if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) { + return NO_QUERY_TO_EXECUTE; + } 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; + } + } + + @Override + public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) { + return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]", + INCLUDE_ALL_DESCENDANTS); + } + + @Override + public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath, + final FetchDescendantsOption fetchDescendantsOption) { + return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption); + } + + @Override + public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) { + final DataNode stateDataNode = getCmHandleState(cmHandleId); + final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state"); + return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState); + } + + @Override + public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) { + return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\"" + + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS); + } + + private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles( + final Collection<DataNode> dataNodes) { + final Map<String, NcmpServiceCmHandle> 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())); + } + + @Override + public Set<NcmpServiceCmHandle> getCmHandlesByDmiPluginIdentifier(final String dmiPluginIdentifier) { + final Map<String, DataNode> cmHandleAsDataNodePerCmHandleId = new HashMap<>(); + for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) { + for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty( + dmiPluginIdentifier, + modelledDmiServiceLeaf.getLeafName())) { + cmHandleAsDataNodePerCmHandleId.put( + cmHandleAsDataNode.getLeaves().get("id").toString(), cmHandleAsDataNode); + } + } + final Set<NcmpServiceCmHandle> ncmpServiceCmHandles = new HashSet<>(cmHandleAsDataNodePerCmHandleId.size()); + cmHandleAsDataNodePerCmHandleId.values().forEach( + cmHandleAsDataNode -> ncmpServiceCmHandles.add(createNcmpServiceCmHandle(cmHandleAsDataNode))); + return ncmpServiceCmHandles; + } + + private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier, + final String dmiProperty) { + return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + "/dmi-registry/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']", + OMIT_DESCENDANTS); + } + + private DataNode getCmHandleState(final String cmHandleId) { + final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state"; + return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + xpath, OMIT_DESCENDANTS); + } +} + + 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/ModuleSyncService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java index 7f61c476d5..7efce1ad52 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java @@ -23,9 +23,8 @@ package org.onap.cps.ncmp.api.inventory.sync; import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; import java.util.Collection; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAdminService; @@ -54,33 +53,28 @@ public class ModuleSyncService { */ public void syncAndCreateSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) { - final Collection<ModuleReference> moduleReferencesFromCmHandle = + final Collection<ModuleReference> allModuleReferencesFromCmHandle = dmiModelOperations.getModuleReferences(yangModelCmHandle); final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService - .identifyNewModuleReferences(moduleReferencesFromCmHandle); - - final Collection<ModuleReference> existingModuleReferencesFromCmHandle = - moduleReferencesFromCmHandle.stream().filter(moduleReferenceFromCmHandle -> - !identifiedNewModuleReferencesFromCmHandle.contains(moduleReferenceFromCmHandle) - ).collect(Collectors.toList()); + .identifyNewModuleReferences(allModuleReferencesFromCmHandle); final Map<String, String> newModuleNameToContentMap; if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) { - newModuleNameToContentMap = new HashMap<>(); + newModuleNameToContentMap = Collections.emptyMap(); } else { newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, identifiedNewModuleReferencesFromCmHandle); } - createSchemaSetAndAnchor(yangModelCmHandle, newModuleNameToContentMap, existingModuleReferencesFromCmHandle); + createSchemaSetAndAnchor(yangModelCmHandle, newModuleNameToContentMap, allModuleReferencesFromCmHandle); } private void createSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle, final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> existingModuleReferencesFromCmHandle) { + final Collection<ModuleReference> allModuleReferencesFromCmHandle) { final String schemaSetAndAnchorName = yangModelCmHandle.getId(); cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName, - newModuleNameToContentMap, existingModuleReferencesFromCmHandle); + newModuleNameToContentMap, allModuleReferencesFromCmHandle); cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName, schemaSetAndAnchorName); } 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 597e2ba8e5..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()); @@ -78,9 +79,10 @@ public class ModuleSyncTasks { } log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name()); } - updateCmHandlesStateBatch(cmHandelStatePerCmHandle); + lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandelStatePerCmHandle); } finally { batchCounter.getAndDecrement(); + log.info("Processing module sync batch finished. {} batch(es) active.", batchCounter.get()); } return COMPLETED_FUTURE; } @@ -98,11 +100,11 @@ public class ModuleSyncTasks { final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState); if (isReadyForRetry) { log.debug("Reset cm handle {} state to ADVISED to be re-attempted by module-sync watchdog", - failedCmHandle.getId()); + failedCmHandle.getId()); cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED); } } - updateCmHandlesStateBatch(cmHandleStatePerCmHandle); + lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle); return COMPLETED_FUTURE; } @@ -111,11 +113,4 @@ public class ModuleSyncTasks { advisedCmHandle.getCompositeState().setLockReason(lockReason); } - private void updateCmHandlesStateBatch(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) { - // To be refactored as part of CPS-1231; Use state-save-batch capability (depends sub-task12, 13) - for (final Map.Entry<YangModelCmHandle, CmHandleState> entry : cmHandleStatePerCmHandle.entrySet()) { - lcmEventsCmHandleStateHandler.updateCmHandleState(entry.getKey(), entry.getValue()); - } - } - } 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 1da2aa9430..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,12 +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; @@ -34,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(); @@ -52,24 +63,65 @@ 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(); } + public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) { + return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse) + .collect(Collectors.toList()); + } + public enum Status { SUCCESS, FAILURE; } @@ -85,4 +137,4 @@ public class CmHandleRegistrationResponse { public final String errorText; } -}
\ No newline at end of file +} |