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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory;
23 import java.util.function.Consumer;
24 import lombok.AccessLevel;
25 import lombok.NoArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
29 * It will have all the utility method responsible for handling the composite state.
32 @NoArgsConstructor(access = AccessLevel.PRIVATE)
33 public class CompositeStateUtils {
36 * Sets the cmHandleState to the provided state and updates the timestamp.
38 * @return Updated CompositeState
40 public static Consumer<CompositeState> setCompositeState(final CmHandleState cmHandleState) {
41 return compositeState -> {
42 compositeState.setCmHandleState(cmHandleState);
43 compositeState.setLastUpdateTimeNow();
48 * Sets the cmHandleState to READY and operational datastore sync state based on the global flag.
50 * @return Updated CompositeState
52 public static Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState(
53 final boolean isGlobalDataSyncCacheEnabled) {
54 return compositeState -> {
55 compositeState.setDataSyncEnabled(isGlobalDataSyncCacheEnabled);
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);
65 private static CompositeState.Operational getInitialDataStoreSyncState(final boolean dataSyncEnabled) {
66 final DataStoreSyncState dataStoreSyncState =
67 dataSyncEnabled ? DataStoreSyncState.UNSYNCHRONIZED : DataStoreSyncState.NONE_REQUESTED;
68 return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
72 * Sets the cmHandleState to ADVISED and retain the lock details. Used in retry scenarios.
74 * @return Updated CompositeState
76 public static Consumer<CompositeState> setCompositeStateForRetry() {
77 return compositeState -> {
78 compositeState.setCmHandleState(CmHandleState.ADVISED);
79 compositeState.setLastUpdateTimeNow();
80 final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
81 final CompositeState.LockReason lockReason =
82 CompositeState.LockReason.builder().details(oldLockReasonDetails).build();
83 compositeState.setLockReason(lockReason);