Composite state transition code change 20/129720/2
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 29 Jun 2022 11:36:40 +0000 (12:36 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Thu, 30 Jun 2022 15:08:38 +0000 (16:08 +0100)
- Code cannge to set datastore to Unsynchronized when state moves from advice to ready
- if resource data is not available or there is any issue while reading
  data from simulator then state will be set to synchronized else remain
  unsynchronized.
- Handle serialization failure on empty bean.

Issue-ID: CPS-1000
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
Change-Id: Ie51a787770da649e00aaefc00341756f92f9fa83

cps-application/src/main/resources/application.yml
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/DataSyncWatchdog.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java
cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy

index 14abebb..d16e977 100644 (file)
@@ -82,6 +82,8 @@ spring:
 \r
     jackson:\r
       default-property-inclusion: NON_NULL\r
+      serialization:\r
+        FAIL_ON_EMPTY_BEANS: false\r
 app:\r
     ncmp:\r
         async-m2m:\r
index f0d1c9b..d47da6c 100644 (file)
@@ -47,7 +47,9 @@ public class InventoryPersistence {
 
     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
 
-    private static final String XPATH_TO_CM_HANDLE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
+    private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
+
+    private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
 
     private final JsonObjectMapper jsonObjectMapper;
 
@@ -57,8 +59,6 @@ public class InventoryPersistence {
 
     private final CpsDataPersistenceService cpsDataPersistenceService;
 
-    private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
-
     /**
      * Get the Cm Handle Composite State from the data node.
      *
@@ -67,9 +67,9 @@ public class InventoryPersistence {
      */
     public CompositeState getCmHandleState(final String cmHandleId) {
         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(XPATH_TO_CM_HANDLE, cmHandleId) + "/state",
+            String.format(xpathCmHandle, cmHandleId) + "/state",
             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-        return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
+        return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
     }
 
     /**
@@ -82,7 +82,7 @@ public class InventoryPersistence {
         final String cmHandleJsonData = String.format("{\"state\":%s}",
             jsonObjectMapper.asJsonString(compositeState));
         cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(XPATH_TO_CM_HANDLE, cmHandleId),
+            String.format(xpathCmHandle, cmHandleId),
             cmHandleJsonData, OffsetDateTime.now());
     }
 
@@ -95,7 +95,7 @@ public class InventoryPersistence {
     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
             NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
-                + cmHandleState + "\"]/ancestor::cm-handles",
+                + cmHandleState + ANCESTOR_CM_HANDLES,
             FetchDescendantsOption.OMIT_DESCENDANTS);
     }
 
@@ -121,7 +121,7 @@ public class InventoryPersistence {
     public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
             NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
-                + cmHandleState + "\"]/ancestor::cm-handles",
+                + cmHandleState + ANCESTOR_CM_HANDLES,
             FetchDescendantsOption.OMIT_DESCENDANTS);
     }
 
@@ -133,7 +133,7 @@ public class InventoryPersistence {
     public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
             NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
-                + "/operational[@sync-state=\"" + dataStoreSyncState + "\"]/ancestor::cm-handles",
+                + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES,
             FetchDescendantsOption.OMIT_DESCENDANTS);
     }
 
@@ -160,7 +160,7 @@ public class InventoryPersistence {
     private DataNode getCmHandleDataNode(final String cmHandle) {
         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
             NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(XPATH_TO_CM_HANDLE, cmHandle),
+            String.format(xpathCmHandle, cmHandle),
             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
     }
 
index 153921a..adfa33a 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.cps.ncmp.api.inventory.sync;
 
 import java.time.OffsetDateTime;
+import java.util.function.Consumer;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.api.CpsDataService;
@@ -60,15 +61,21 @@ public class DataSyncWatchdog {
             } else {
                 cpsDataService.saveData("NFP-Operational", cmHandleId,
                         resourceData, OffsetDateTime.now());
+                setSyncStateToSynchronized().accept(compositeState);
+                inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
             }
+            unSynchronizedReadyCmHandle = syncUtils.getAnUnSynchronizedReadyCmHandle();
+        }
+        log.debug("No Cm-Handles currently found in an READY State and Operational Sync State is UNSYNCHRONIZED");
+    }
+
+    private Consumer<CompositeState> setSyncStateToSynchronized() {
+        return compositeState -> {
             compositeState.setLastUpdateTimeNow();
             compositeState.getDataStores()
                     .setOperationalDataStore(CompositeState.Operational.builder()
                             .dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
                             .lastSyncTime(CompositeState.nowInSyncTimeFormat()).build());
-            inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
-            unSynchronizedReadyCmHandle = syncUtils.getAnUnSynchronizedReadyCmHandle();
-        }
-        log.debug("No Cm-Handles currently found in an READY State and Operational Sync State is UNSYNCHRONIZED");
+        };
     }
 }
index 402f9f6..ac53d88 100644 (file)
 package org.onap.cps.ncmp.api.inventory.sync;
 
 import java.util.List;
+import java.util.function.Consumer;
 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.DataStoreSyncState;
 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -54,17 +56,14 @@ public class ModuleSyncWatchdog {
             final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
             try {
                 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
-                compositeState.setCmHandleState(CmHandleState.READY);
+                setCompositeStateToReadyWithInitialDataStoreSyncState().accept(compositeState);
             } catch (final Exception e) {
-                compositeState.setCmHandleState(CmHandleState.LOCKED);
+                setCompositeStateToLocked().accept(compositeState);
                 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
-                    LockReasonCategory.LOCKED_MISBEHAVING,
-                    e.getMessage());
+                        LockReasonCategory.LOCKED_MISBEHAVING, e.getMessage());
             }
-            compositeState.setLastUpdateTimeNow();
             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
-            log.info("{} is now in {} state", cmHandleId,
-                advisedCmHandle.getCompositeState().getCmHandleState());
+            log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
         }
         log.debug("No Cm-Handles currently found in an ADVISED state");
@@ -87,11 +86,33 @@ public class ModuleSyncWatchdog {
         }
     }
 
+    private Consumer<CompositeState> setCompositeStateToLocked() {
+        return compositeState -> {
+            compositeState.setCmHandleState(CmHandleState.LOCKED);
+            compositeState.setLastUpdateTimeNow();
+        };
+    }
+
+    private Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
+        return compositeState -> {
+            compositeState.setCmHandleState(CmHandleState.READY);
+            final CompositeState.Operational operational = CompositeState.Operational.builder()
+                    .dataStoreSyncState(DataStoreSyncState.UNSYNCHRONIZED)
+                    .lastSyncTime(CompositeState.nowInSyncTimeFormat())
+                    .build();
+            final CompositeState.DataStores dataStores = CompositeState.DataStores.builder()
+                    .operationalDataStore(operational)
+                    .build();
+            compositeState.setDataStores(dataStores);
+        };
+    }
+
     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());
+        final CompositeState.LockReason lockReason = CompositeState.LockReason.builder()
+                .details(oldLockReasonDetails).build();
+        compositeState.setLockReason(lockReason);
     }
 }
index acb5241..4610144 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.cps.utils
 
 
 import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.SerializationFeature
 import groovy.json.JsonSlurper
 import org.onap.cps.TestUtils
 import org.onap.cps.spi.exceptions.DataValidationException
@@ -79,10 +80,12 @@ class JsonObjectMapperSpec extends Specification {
     def 'Map a unstructured object to json String.'() {
         given: 'Unstructured object'
             def object = new Object()
+        and: 'disable serialization failure on empty bean'
+            spiedObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
         when: 'the object is mapped to string'
             jsonObjectMapper.asJsonString(object);
-        then: 'an exception is thrown'
-            thrown(DataValidationException)
+        then: 'no exception is thrown'
+            noExceptionThrown()
     }
 
     def 'Map a structurally compatible json String to JsonNode.'() {