Merge "CPS 1824: Delta Between 2 Anchors release notes"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / CompositeStateUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2024 Nordix Foundation
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.inventory;
22
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26
27 /**
28  * It will have all the utility method responsible for handling the composite state.
29  */
30 @Slf4j
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public class CompositeStateUtils {
33
34     /**
35      * Sets the cmHandleState to the provided state and updates the timestamp.
36      */
37     public static void setCompositeState(final CmHandleState cmHandleState,
38                                                    final CompositeState compositeState) {
39         compositeState.setCmHandleState(cmHandleState);
40         compositeState.setLastUpdateTimeNow();
41     }
42
43     /**
44      * Set the Operational datastore sync state based on the global flag.
45      */
46     public static void setInitialDataStoreSyncState(final CompositeState compositeState) {
47         compositeState.setDataSyncEnabled(false);
48         final CompositeState.Operational operational =
49             getInitialDataStoreSyncState(compositeState.getDataSyncEnabled());
50         final CompositeState.DataStores dataStores =
51             CompositeState.DataStores.builder().operationalDataStore(operational).build();
52         compositeState.setDataStores(dataStores);
53     }
54
55     /**
56      * Set the data sync enabled flag, along with the data store sync state based on this flag.
57      *
58      * @param dataSyncEnabled data sync enabled flag
59      * @param compositeState  cm handle composite state
60      */
61     public static void setDataSyncEnabledFlagWithDataSyncState(final boolean dataSyncEnabled,
62             final CompositeState compositeState) {
63         compositeState.setDataSyncEnabled(dataSyncEnabled);
64         compositeState.setLastUpdateTimeNow();
65         final CompositeState.Operational operational = getInitialDataStoreSyncState(dataSyncEnabled);
66         final CompositeState.DataStores dataStores =
67                 CompositeState.DataStores.builder().operationalDataStore(operational).build();
68         compositeState.setDataStores(dataStores);
69     }
70
71     /**
72      * Get initial data sync state based on data sync enabled boolean flag.
73      *
74      * @param dataSyncEnabled data sync enabled boolean flag
75      * @return the data store sync state
76      */
77     private static CompositeState.Operational getInitialDataStoreSyncState(final boolean dataSyncEnabled) {
78         final DataStoreSyncState dataStoreSyncState =
79                 dataSyncEnabled ? DataStoreSyncState.UNSYNCHRONIZED : DataStoreSyncState.NONE_REQUESTED;
80         return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
81     }
82
83     /**
84      * Sets the cmHandleState to ADVISED and retain the lock details. Used in retry scenarios.
85      */
86     public static void setCompositeStateForRetry(final CompositeState compositeState) {
87         compositeState.setCmHandleState(CmHandleState.ADVISED);
88         compositeState.setLastUpdateTimeNow();
89         final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
90         final CompositeState.LockReason lockReason =
91             CompositeState.LockReason.builder()
92                 .lockReasonCategory(compositeState.getLockReason().getLockReasonCategory())
93                 .details(oldLockReasonDetails).build();
94         compositeState.setLockReason(lockReason);
95     }
96 }