2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync;
24 import java.util.List;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.function.Consumer;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
30 import org.onap.cps.ncmp.api.inventory.CmHandleState;
31 import org.onap.cps.ncmp.api.inventory.CompositeState;
32 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
33 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
34 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.scheduling.annotation.Scheduled;
37 import org.springframework.stereotype.Component;
40 @RequiredArgsConstructor
42 public class ModuleSyncWatchdog {
44 private final InventoryPersistence inventoryPersistence;
46 private final SyncUtils syncUtils;
48 private final ModuleSyncService moduleSyncService;
50 @Value("${data-sync.cache.enabled:false}")
51 private boolean isGlobalDataSyncCacheEnabled;
53 private final ConcurrentMap<String, Boolean> moduleSyncSemaphoreMap;
56 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
58 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
59 public void executeAdvisedCmHandlePoll() {
60 syncUtils.getAdvisedCmHandles().forEach(advisedCmHandle -> {
61 final String cmHandleId = advisedCmHandle.getId();
62 if (hasPushedIntoSemaphoreMap(cmHandleId)) {
63 log.debug("executing module sync on {}", cmHandleId);
64 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
66 moduleSyncService.deleteSchemaSetIfExists(advisedCmHandle);
67 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
68 setCompositeStateToReadyWithInitialDataStoreSyncState().accept(compositeState);
69 updateModuleSyncSemaphoreMap(cmHandleId);
70 } catch (final Exception e) {
71 setCompositeStateToLocked().accept(compositeState);
72 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
73 LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
75 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
76 log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
78 log.debug("{} already processed by another instance", cmHandleId);
81 log.debug("No Cm-Handles currently found in an ADVISED state");
85 * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
87 @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
88 public void executeLockedCmHandlePoll() {
89 final List<YangModelCmHandle> lockedCmHandles = syncUtils.getModuleSyncFailedCmHandles();
90 for (final YangModelCmHandle lockedCmHandle : lockedCmHandles) {
91 final CompositeState compositeState = lockedCmHandle.getCompositeState();
92 final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
93 if (isReadyForRetry) {
94 setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
95 log.debug("Locked cm handle {} is being re-synced", lockedCmHandle.getId());
96 inventoryPersistence.saveCmHandleState(lockedCmHandle.getId(), compositeState);
101 private Consumer<CompositeState> setCompositeStateToLocked() {
102 return compositeState -> {
103 compositeState.setCmHandleState(CmHandleState.LOCKED);
104 compositeState.setLastUpdateTimeNow();
108 private Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
109 return compositeState -> {
110 compositeState.setDataSyncEnabled(isGlobalDataSyncCacheEnabled);
111 compositeState.setCmHandleState(CmHandleState.READY);
112 final CompositeState.Operational operational = getDataStoreSyncState(compositeState.getDataSyncEnabled());
113 final CompositeState.DataStores dataStores = CompositeState.DataStores.builder()
114 .operationalDataStore(operational)
116 compositeState.setDataStores(dataStores);
120 private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
121 compositeState.setCmHandleState(CmHandleState.ADVISED);
122 compositeState.setLastUpdateTimeNow();
123 final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
124 final CompositeState.LockReason lockReason = CompositeState.LockReason.builder()
125 .details(oldLockReasonDetails).build();
126 compositeState.setLockReason(lockReason);
129 private CompositeState.Operational getDataStoreSyncState(final boolean dataSyncEnabled) {
130 final DataStoreSyncState dataStoreSyncState = dataSyncEnabled
131 ? DataStoreSyncState.UNSYNCHRONIZED : DataStoreSyncState.NONE_REQUESTED;
132 return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
135 private void updateModuleSyncSemaphoreMap(final String cmHandleId) {
136 moduleSyncSemaphoreMap.replace(cmHandleId, true);
139 private boolean hasPushedIntoSemaphoreMap(final String cmHandleId) {
140 return moduleSyncSemaphoreMap.putIfAbsent(cmHandleId, false) == null;