2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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.impl.inventory.sync;
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
25 import com.hazelcast.map.IMap;
26 import java.time.OffsetDateTime;
27 import java.util.concurrent.TimeUnit;
28 import java.util.function.Consumer;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.api.CpsDataService;
32 import org.onap.cps.ncmp.api.impl.config.embeddedcache.SynchronizationCacheConfig;
33 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
34 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
35 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
36 import org.springframework.scheduling.annotation.Scheduled;
37 import org.springframework.stereotype.Service;
40 @RequiredArgsConstructor
42 public class DataSyncWatchdog {
44 private static final boolean DATA_SYNC_IN_PROGRESS = false;
45 private static final boolean DATA_SYNC_DONE = true;
47 private final InventoryPersistence inventoryPersistence;
49 private final CpsDataService cpsDataService;
51 private final SyncUtils syncUtils;
53 private final IMap<String, Boolean> dataSyncSemaphores;
56 * Execute Cm Handle poll which queries the cm handle state in 'READY' and Operational Datastore Sync State in
59 @Scheduled(fixedDelayString = "${ncmp.timers.cm-handle-data-sync.sleep-time-ms:30000}")
60 public void executeUnSynchronizedReadyCmHandlePoll() {
61 syncUtils.getUnsynchronizedReadyCmHandles().forEach(unSynchronizedReadyCmHandle -> {
62 final String cmHandleId = unSynchronizedReadyCmHandle.getId();
63 if (hasPushedIntoSemaphoreMap(cmHandleId)) {
64 log.debug("Executing data sync on {}", cmHandleId);
65 final CompositeState compositeState = inventoryPersistence
66 .getCmHandleState(cmHandleId);
67 final String resourceData = syncUtils.getResourceData(cmHandleId);
68 if (resourceData == null) {
69 log.debug("Error retrieving resource data for Cm-Handle: {}", cmHandleId);
71 cpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
72 resourceData, OffsetDateTime.now());
73 setSyncStateToSynchronized().accept(compositeState);
74 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
75 updateDataSyncSemaphoreMap(cmHandleId);
78 log.debug("{} already processed by another instance", cmHandleId);
81 log.debug("No Cm-Handles currently found in READY State and Operational Sync State is UNSYNCHRONIZED");
84 private Consumer<CompositeState> setSyncStateToSynchronized() {
85 return compositeState -> {
86 compositeState.setLastUpdateTimeNow();
87 compositeState.getDataStores()
88 .setOperationalDataStore(CompositeState.Operational.builder()
89 .dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
90 .lastSyncTime(CompositeState.nowInSyncTimeFormat()).build());
94 private void updateDataSyncSemaphoreMap(final String cmHandleId) {
95 dataSyncSemaphores.replace(cmHandleId, DATA_SYNC_DONE);
98 private boolean hasPushedIntoSemaphoreMap(final String cmHandleId) {
99 return dataSyncSemaphores.putIfAbsent(cmHandleId, DATA_SYNC_IN_PROGRESS,
100 SynchronizationCacheConfig.DATA_SYNC_SEMAPHORE_TTL_SECS, TimeUnit.SECONDS) == null;