Merge "Simplified 'External' lock reason Mapping"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / SyncUtils.java
index 8b7dfe6..046a116 100644 (file)
@@ -43,9 +43,9 @@ 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.CmHandleState;
 import org.onap.cps.ncmp.api.inventory.CompositeState;
+import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
-import org.onap.cps.ncmp.api.inventory.SyncState;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.utils.JsonObjectMapper;
@@ -92,7 +92,7 @@ public class SyncUtils {
      */
     public YangModelCmHandle getAnUnSynchronizedReadyCmHandle() {
         final List<DataNode> unSynchronizedCmHandles = inventoryPersistence
-                .getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED);
+                .getCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
         if (unSynchronizedCmHandles.isEmpty()) {
             return null;
         }
@@ -109,13 +109,13 @@ public class SyncUtils {
     }
 
     /**
-     * Query data nodes for cm handles with an "LOCKED" cm handle state with reason LOCKED_MISBEHAVING".
+     * Query data nodes for cm handles with an "LOCKED" cm handle state with reason LOCKED_MODULE_SYNC_FAILED".
      *
-     * @return a random yang model cm handle with an ADVISED state, return null if not found
+     * @return a random LOCKED yang model cm handle, return null if not found
      */
-    public List<YangModelCmHandle> getLockedMisbehavingYangModelCmHandles() {
+    public List<YangModelCmHandle> getModuleSyncFailedCmHandles() {
         final List<DataNode> lockedCmHandleAsDataNodeList = inventoryPersistence.getCmHandleDataNodesByCpsPath(
-            "//lock-reason[@reason=\"LOCKED_MISBEHAVING\"]/ancestor::cm-handles",
+            "//lock-reason[@reason=\"LOCKED_MODULE_SYNC_FAILED\"]/ancestor::cm-handles",
             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
         return lockedCmHandleAsDataNodeList.stream()
             .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
@@ -151,18 +151,22 @@ public class SyncUtils {
      * @return if the retry mechanism should be attempted
      */
     public boolean isReadyForRetry(final CompositeState compositeState) {
-        int timeUntilNextAttempt = 1;
+        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()) {
-            timeUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(matcher.group(1)));
+            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();
-        return timeSinceLastAttempt > timeUntilNextAttempt;
+        if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
+            log.info("Time until next attempt is {} minutes: ",
+                timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
+        }
+        return timeSinceLastAttempt > timeInMinutesUntilNextAttempt;
     }
 
     /**