Merge "CmHandleState transition using state handler"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / CompositeStateUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 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.inventory;
22
23 import java.util.function.Consumer;
24 import lombok.AccessLevel;
25 import lombok.NoArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27
28 /**
29  * It will have all the utility method responsible for handling the composite state.
30  */
31 @Slf4j
32 @NoArgsConstructor(access = AccessLevel.PRIVATE)
33 public class CompositeStateUtils {
34
35     /**
36      * Sets the cmHandleState to the provided state and updates the timestamp.
37      *
38      * @return Updated CompositeState
39      */
40     public static Consumer<CompositeState> setCompositeState(final CmHandleState cmHandleState) {
41         return compositeState -> {
42             compositeState.setCmHandleState(cmHandleState);
43             compositeState.setLastUpdateTimeNow();
44         };
45     }
46
47     /**
48      * Sets the cmHandleState to READY and operational datastore sync state based on the global flag.
49      *
50      * @return Updated CompositeState
51      */
52     public static Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
53         return compositeState -> {
54             compositeState.setDataSyncEnabled(false);
55             compositeState.setLastUpdateTimeNow();
56             compositeState.setCmHandleState(CmHandleState.READY);
57             final CompositeState.Operational operational =
58                     getInitialDataStoreSyncState(compositeState.getDataSyncEnabled());
59             final CompositeState.DataStores dataStores =
60                     CompositeState.DataStores.builder().operationalDataStore(operational).build();
61             compositeState.setDataStores(dataStores);
62         };
63     }
64
65     /**
66      * Set the data sync enabled flag, along with the data store sync state based on this flag.
67      *
68      * @param dataSyncEnabled data sync enabled flag
69      * @param compositeState cm handle composite state
70      */
71     public static void setDataSyncEnabledFlagWithDataSyncState(final boolean dataSyncEnabled,
72                                                                final CompositeState compositeState) {
73         compositeState.setDataSyncEnabled(dataSyncEnabled);
74         compositeState.setLastUpdateTimeNow();
75         final CompositeState.Operational operational = getInitialDataStoreSyncState(dataSyncEnabled);
76         final CompositeState.DataStores dataStores =
77             CompositeState.DataStores.builder().operationalDataStore(operational).build();
78         compositeState.setDataStores(dataStores);
79     }
80
81     /**
82      * Get initial data sync state based on data sync enabled boolean flag.
83      *
84      * @param dataSyncEnabled data sync enabled boolean flag
85      * @return the data store sync state
86      */
87     private static CompositeState.Operational getInitialDataStoreSyncState(final boolean dataSyncEnabled) {
88         final DataStoreSyncState dataStoreSyncState =
89                 dataSyncEnabled ? DataStoreSyncState.UNSYNCHRONIZED : DataStoreSyncState.NONE_REQUESTED;
90         return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
91     }
92
93     /**
94      * Sets the cmHandleState to ADVISED and retain the lock details. Used in retry scenarios.
95      *
96      * @return Updated CompositeState
97      */
98     public static Consumer<CompositeState> setCompositeStateForRetry() {
99         return compositeState -> {
100             compositeState.setCmHandleState(CmHandleState.ADVISED);
101             compositeState.setLastUpdateTimeNow();
102             final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
103             final CompositeState.LockReason lockReason =
104                     CompositeState.LockReason.builder().details(oldLockReasonDetails).build();
105             compositeState.setLockReason(lockReason);
106         };
107     }
108 }