Retry Module-Sync based on from last failure
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdog.java
index 9cfa0a0..402f9f6 100644 (file)
 
 package org.onap.cps.ncmp.api.inventory.sync;
 
-import static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.CREATE;
-
 import java.util.List;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.api.impl.event.NcmpEventsService;
 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.CompositeState.LockReason;
 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -47,11 +43,8 @@ public class ModuleSyncWatchdog {
 
     private final ModuleSyncService moduleSyncService;
 
-    private final NcmpEventsService ncmpEventsService;
-
     /**
      * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
-     * Also publish the LCM Create Event when cm handle state is moved to 'READY'.
      */
     @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
     public void executeAdvisedCmHandlePoll() {
@@ -72,10 +65,6 @@ public class ModuleSyncWatchdog {
             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
             log.info("{} is now in {} state", cmHandleId,
                 advisedCmHandle.getCompositeState().getCmHandleState());
-            if (compositeState.getCmHandleState() == CmHandleState.READY) {
-                log.debug("Publishing LCM Create Event for cmHandleId : {}", cmHandleId);
-                ncmpEventsService.publishNcmpEvent(cmHandleId, CREATE);
-            }
             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
         }
         log.debug("No Cm-Handles currently found in an ADVISED state");
@@ -85,16 +74,24 @@ public class ModuleSyncWatchdog {
      * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
      */
     @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
-    public void executeLockedMisbehavingCmHandlePoll() {
+    public void executeLockedCmHandlePoll() {
         final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
-        for (final YangModelCmHandle lockedMisbehavingModelCmHandle: lockedMisbehavingCmHandles) {
-            final CompositeState updatedCompositeState = lockedMisbehavingModelCmHandle.getCompositeState();
-            updatedCompositeState.setCmHandleState(CmHandleState.ADVISED);
-            updatedCompositeState.setLastUpdateTimeNow();
-            updatedCompositeState.setLockReason(LockReason.builder()
-                .details(updatedCompositeState.getLockReason().getDetails()).build());
-            log.debug("Locked misbehaving cm handle {} is being recycled", lockedMisbehavingModelCmHandle.getId());
-            inventoryPersistence.saveCmHandleState(lockedMisbehavingModelCmHandle.getId(), updatedCompositeState);
+        for (final YangModelCmHandle moduleSyncFailedCmHandle : lockedMisbehavingCmHandles) {
+            final CompositeState compositeState = moduleSyncFailedCmHandle.getCompositeState();
+            final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
+            if (isReadyForRetry) {
+                setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
+                log.debug("Locked misbehaving cm handle {} is being recycled", moduleSyncFailedCmHandle.getId());
+                inventoryPersistence.saveCmHandleState(moduleSyncFailedCmHandle.getId(), compositeState);
+            }
         }
     }
+
+    private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
+        compositeState.setCmHandleState(CmHandleState.ADVISED);
+        compositeState.setLastUpdateTimeNow();
+        final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
+        compositeState.setLockReason(CompositeState.LockReason.builder()
+                .details(oldLockReasonDetails).build());
+    }
 }