Module Sync Lock State implementation
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdog.java
index 353db9d..2187ec6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * ============LICENSE_START=======================================================
+ *  ============LICENSE_START=======================================================
  *  Copyright (C) 2022 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,6 +24,9 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 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.InventoryPersistence;
+import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
@@ -32,6 +35,8 @@ import org.springframework.stereotype.Component;
 @Component
 public class ModuleSyncWatchdog {
 
+    private final InventoryPersistence inventoryPersistence;
+
     private final SyncUtils syncUtils;
 
     private final ModuleSyncService moduleSyncService;
@@ -43,11 +48,21 @@ public class ModuleSyncWatchdog {
     public void executeAdvisedCmHandlePoll() {
         YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
         while (advisedCmHandle != null) {
-            moduleSyncService.syncAndCreateSchemaSet(advisedCmHandle);
-            // ToDo Lock Cm Handle if module sync fails
-            syncUtils.updateCmHandleState(advisedCmHandle, CmHandleState.READY);
-            log.info("{} is now in {} state", advisedCmHandle.getId(),
-                    advisedCmHandle.getCompositeState().getCmhandleState());
+            final String cmHandleId = advisedCmHandle.getId();
+            final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
+            try {
+                moduleSyncService.syncAndCreateSchemaSet(advisedCmHandle);
+                compositeState.setCmHandleState(CmHandleState.READY);
+            } catch (final Exception e) {
+                compositeState.setCmHandleState(CmHandleState.LOCKED);
+                syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
+                    LockReasonCategory.LOCKED_MISBEHAVING,
+                    e.getMessage());
+            }
+            compositeState.setLastUpdateTimeNow();
+            inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
+            log.info("{} is now in {} state", cmHandleId,
+                advisedCmHandle.getCompositeState().getCmHandleState());
             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
         }
         log.debug("No Cm-Handles currently found in an ADVISED state");