/*
- * ============LICENSE_START=======================================================
- * Copyright (C) 2022 Nordix Foundation
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022-2023 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.
package org.onap.cps.ncmp.api.inventory.sync;
-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.operations.DatastoreType.PASSTHROUGH_OPERATIONAL;
-import java.security.SecureRandom;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.time.Duration;
import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.api.CpsDataService;
-import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever;
+import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
+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.spi.CpsDataPersistenceService;
+import org.onap.cps.ncmp.api.inventory.CompositeState;
+import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
+import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.utils.JsonObjectMapper;
-import org.springframework.stereotype.Component;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
@Slf4j
-@Component
+@Service
@RequiredArgsConstructor
public class SyncUtils {
+ private final CmHandleQueries cmHandleQueries;
- private static final SecureRandom secureRandom = new SecureRandom();
- private final CpsDataService cpsDataService;
-
- private final CpsDataPersistenceService cpsDataPersistenceService;
+ private final DmiDataOperations dmiDataOperations;
private final JsonObjectMapper jsonObjectMapper;
- private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
+ private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:");
+
+ /**
+ * Query data nodes for cm handles with an "ADVISED" cm handle state.
+ *
+ * @return cm handles (data nodes) in ADVISED state (empty list if none found)
+ */
+ public List<DataNode> getAdvisedCmHandles() {
+ final List<DataNode> advisedCmHandlesAsDataNodes = cmHandleQueries.queryCmHandlesByState(CmHandleState.ADVISED);
+ log.debug("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodes.size());
+ return advisedCmHandlesAsDataNodes;
+ }
+
+ /**
+ * First query data nodes for cm handles with CM Handle Operational Sync State in "UNSYNCHRONIZED" and
+ * randomly select a CM Handle and query the data nodes for CM Handle State in "READY".
+ *
+ * @return a randomized yang model cm handle list with State in READY and Operation Sync State in "UNSYNCHRONIZED",
+ * return empty list if not found
+ */
+ public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
+ final List<DataNode> unsynchronizedCmHandles = cmHandleQueries
+ .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
+
+ final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>();
+ for (final DataNode unsynchronizedCmHandle : unsynchronizedCmHandles) {
+ final String cmHandleId = unsynchronizedCmHandle.getLeaves().get("id").toString();
+ if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
+ yangModelCmHandles.addAll(
+ convertCmHandlesDataNodesToYangModelCmHandles(
+ Collections.singletonList(unsynchronizedCmHandle)));
+ }
+ }
+
+ Collections.shuffle(yangModelCmHandles);
+
+ return yangModelCmHandles;
+ }
+
+ /**
+ * Query data nodes for cm handles with an "LOCKED" cm handle state with reason LOCKED_MODULE_SYNC_FAILED".
+ *
+ * @return a random LOCKED yang model cm handle, return null if not found
+ */
+ public List<YangModelCmHandle> getModuleSyncFailedCmHandles() {
+ final List<DataNode> lockedCmHandlesAsDataNodeList = cmHandleQueries.queryCmHandleDataNodesByCpsPath(
+ "//lock-reason[@reason=\"LOCKED_MODULE_SYNC_FAILED\"]",
+ FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
+ return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList);
+ }
+
+ /**
+ * Update Composite State attempts counter and set new lock reason and details.
+ *
+ * @param lockReasonCategory lock reason category
+ * @param errorMessage error message
+ */
+ public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
+ final LockReasonCategory lockReasonCategory,
+ final String errorMessage) {
+ int attempt = 1;
+ if (compositeState.getLockReason() != null) {
+ final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
+ if (matcher.find()) {
+ attempt = 1 + Integer.parseInt(matcher.group(1));
+ }
+ }
+ compositeState.setLockReason(CompositeState.LockReason.builder()
+ .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
+ .lockReasonCategory(lockReasonCategory).build());
+ }
+
/**
- * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
+ * Check if the retry mechanism should attempt to unlock the cm handle based on the last update time.
*
- * @return a random yang model cm handle with an ADVISED state, return null if not found
+ * @param compositeState the composite state currently in the locked state
+ * @return if the retry mechanism should be attempted
*/
- public YangModelCmHandle getAnAdvisedCmHandle() {
- final List<DataNode> advisedCmHandles = cpsDataPersistenceService.queryDataNodes("NCMP-Admin",
- "ncmp-dmi-registry", "//cm-handles[@state=\"ADVISED\"]",
- FetchDescendantsOption.OMIT_DESCENDANTS);
- if (advisedCmHandles.isEmpty()) {
- return null;
+ public boolean isReadyForRetry(final CompositeState compositeState) {
+ int timeInMinutesUntilNextAttempt = 1;
+ final OffsetDateTime time =
+ OffsetDateTime.parse(compositeState.getLastUpdateTime(),
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
+ final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
+ if (matcher.find()) {
+ timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(matcher.group(1)));
+ } else {
+ log.debug("First Attempt: no current attempts found.");
+ }
+ final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
+ if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
+ log.info("Time until next attempt is {} minutes: ",
+ timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
}
- final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size());
- final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves()
- .get("id").toString();
- return yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId);
+ return timeSinceLastAttempt > timeInMinutesUntilNextAttempt;
}
/**
- * Update the Cm Handle state to "READY".
+ * Get the Resourece Data from Node through DMI Passthrough service.
*
- * @param yangModelCmHandle yang model cm handle
- * @param cmHandleState cm handle state
+ * @param cmHandleId cm handle id
+ * @return optional string containing the resource data
*/
- public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState cmHandleState) {
- yangModelCmHandle.setCmHandleState(cmHandleState);
- final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
- jsonObjectMapper.asJsonString(yangModelCmHandle));
- cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
- cmHandleJsonData, OffsetDateTime.now());
+ public String getResourceData(final String cmHandleId) {
+ final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
+ PASSTHROUGH_OPERATIONAL.getDatastoreName(),
+ cmHandleId,
+ UUID.randomUUID().toString());
+ if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
+ return getFirstResource(resourceDataResponseEntity.getBody());
+ }
+ return null;
+ }
+
+ private String getFirstResource(final Object responseBody) {
+ final String jsonObjectAsString = jsonObjectMapper.asJsonString(responseBody);
+ final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString);
+ final Iterator<Map.Entry<String, JsonNode>> overallJsonTreeMap = overallJsonNode.fields();
+ final Map.Entry<String, JsonNode> firstElement = overallJsonTreeMap.next();
+ return jsonObjectMapper.asJsonString(Map.of(firstElement.getKey(), firstElement.getValue()));
}
+ private static List<YangModelCmHandle> convertCmHandlesDataNodesToYangModelCmHandles(
+ final List<DataNode> cmHandlesAsDataNodeList) {
+ return cmHandlesAsDataNodeList.stream()
+ .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
+ cmHandle.getLeaves().get("id").toString())).collect(Collectors.toList());
+ }
}