Merge "Enable/Disable Data Sync for Cm Handle"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / CompositeStateBuilder.java
index f4d9638..d6a3330 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  * Copyright (C) 2022 Bell Canada
- * Copyright (C) 2022 Nordix Foundation.
+ * Modifications Copyright (C) 2022 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ public class CompositeStateBuilder {
     private LockReason lockReason;
     private DataStores datastores;
     private String lastUpdatedTime;
+    private Boolean dataSyncEnabled;
 
     /**
      * To create the {@link CompositeState}.
@@ -44,6 +45,7 @@ public class CompositeStateBuilder {
         compositeState.setLockReason(lockReason);
         compositeState.setDataStores(datastores);
         compositeState.setLastUpdateTime(lastUpdatedTime);
+        compositeState.setDataSyncEnabled(dataSyncEnabled);
         return compositeState;
     }
 
@@ -94,13 +96,14 @@ public class CompositeStateBuilder {
     /**
      * To use attributes for creating {@link CompositeState}.
      *
-     * @param syncState for the locked state
+     * @param dataStoreSyncState for the locked state
      * @param lastSyncTime for the locked state
      * @return CompositeStateBuilder
      */
-    public CompositeStateBuilder withOperationalDataStores(final SyncState syncState, final String lastSyncTime) {
+    public CompositeStateBuilder withOperationalDataStores(final DataStoreSyncState dataStoreSyncState,
+                                                           final String lastSyncTime) {
         this.datastores = DataStores.builder().operationalDataStore(
-            Operational.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build();
+            Operational.builder().dataStoreSyncState(dataStoreSyncState).lastSyncTime(lastSyncTime).build()).build();
         return this;
     }
 
@@ -111,22 +114,21 @@ public class CompositeStateBuilder {
      * @return CompositeState
      */
     public CompositeStateBuilder fromDataNode(final DataNode dataNode) {
-        this.cmHandleState =  CmHandleState.valueOf((String) dataNode.getLeaves()
-                .get("cm-handle-state"));
+        this.cmHandleState = CmHandleState.valueOf((String) dataNode.getLeaves()
+            .get("cm-handle-state"));
+        this.lastUpdatedTime = (String) dataNode.getLeaves().get("last-update-time");
+        if (this.cmHandleState == CmHandleState.READY) {
+            this.dataSyncEnabled = (Boolean) dataNode.getLeaves().get("data-sync-enabled");
+        }
         for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
             if (stateChildNode.getXpath().endsWith("/lock-reason")) {
-                this.lockReason = new LockReason(LockReasonCategory.valueOf(
-                        (String) stateChildNode.getLeaves().get("reason")),
-                        (String) stateChildNode.getLeaves().get("details"));
+                this.lockReason = getLockReason(stateChildNode);
             }
             if (stateChildNode.getXpath().endsWith("/datastores")) {
                 for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
                     Operational operationalDataStore = null;
                     if (dataStoreNodes.getXpath().contains("/operational")) {
-                        operationalDataStore = Operational.builder()
-                            .syncState(SyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
-                            .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
-                            .build();
+                        operationalDataStore = getOperationalDataStore(dataStoreNodes);
                     }
                     this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();
                 }
@@ -135,4 +137,18 @@ public class CompositeStateBuilder {
         return this;
     }
 
+    private Operational getOperationalDataStore(final DataNode dataStoreNodes) {
+        return Operational.builder()
+                .dataStoreSyncState(DataStoreSyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
+                .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
+                .build();
+    }
+
+    private LockReason getLockReason(final DataNode stateChildNode) {
+        final boolean isLockReasonExists = stateChildNode.getLeaves().containsKey("reason");
+        return new LockReason(isLockReasonExists
+                ? LockReasonCategory.valueOf((String) stateChildNode.getLeaves().get("reason"))
+                : null, (String) stateChildNode.getLeaves().get("details"));
+    }
+
 }