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.scheduling.annotation.Scheduled;
36 import org.springframework.stereotype.Component;
39 @RequiredArgsConstructor
41 public class ModuleSyncWatchdog {
43 private final InventoryPersistence inventoryPersistence;
45 private final SyncUtils syncUtils;
47 private final ModuleSyncService moduleSyncService;
49 private final ConcurrentMap<String, Boolean> moduleSyncSemaphoreMap;
52 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
54 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
55 public void executeAdvisedCmHandlePoll() {
56 syncUtils.getAdvisedCmHandles().forEach(advisedCmHandle -> {
57 final String cmHandleId = advisedCmHandle.getId();
58 if (hasPushedIntoSemaphoreMap(cmHandleId)) {
59 log.debug("executing module sync on {}", cmHandleId);
60 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
62 moduleSyncService.deleteSchemaSetIfExists(advisedCmHandle);
63 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
64 setCompositeStateToReadyWithInitialDataStoreSyncState().accept(compositeState);
65 updateModuleSyncSemaphoreMap(cmHandleId);
66 } catch (final Exception e) {
67 setCompositeStateToLocked().accept(compositeState);
68 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
69 LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
71 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
72 log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
74 log.debug("{} already processed by another instance", cmHandleId);
77 log.debug("No Cm-Handles currently found in an ADVISED state");
81 * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
83 @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
84 public void executeLockedCmHandlePoll() {
85 final List<YangModelCmHandle> lockedCmHandles = syncUtils.getModuleSyncFailedCmHandles();
86 for (final YangModelCmHandle lockedCmHandle : lockedCmHandles) {
87 final CompositeState compositeState = lockedCmHandle.getCompositeState();
88 final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
89 if (isReadyForRetry) {
90 setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
91 log.debug("Locked cm handle {} is being re-synced", lockedCmHandle.getId());
92 inventoryPersistence.saveCmHandleState(lockedCmHandle.getId(), compositeState);
97 private Consumer<CompositeState> setCompositeStateToLocked() {
98 return compositeState -> {
99 compositeState.setCmHandleState(CmHandleState.LOCKED);
100 compositeState.setLastUpdateTimeNow();
104 private Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
105 return compositeState -> {
106 compositeState.setDataSyncEnabled(false);
107 compositeState.setCmHandleState(CmHandleState.READY);
108 final CompositeState.Operational operational = getDataStoreSyncState();
109 final CompositeState.DataStores dataStores = CompositeState.DataStores.builder()
110 .operationalDataStore(operational)
112 compositeState.setDataStores(dataStores);
116 private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
117 compositeState.setCmHandleState(CmHandleState.ADVISED);
118 compositeState.setLastUpdateTimeNow();
119 final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
120 final CompositeState.LockReason lockReason = CompositeState.LockReason.builder()
121 .details(oldLockReasonDetails).build();
122 compositeState.setLockReason(lockReason);
125 private CompositeState.Operational getDataStoreSyncState() {
126 final DataStoreSyncState dataStoreSyncState = DataStoreSyncState.NONE_REQUESTED;
127 return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
130 private void updateModuleSyncSemaphoreMap(final String cmHandleId) {
131 moduleSyncSemaphoreMap.replace(cmHandleId, true);
134 private boolean hasPushedIntoSemaphoreMap(final String cmHandleId) {
135 return moduleSyncSemaphoreMap.putIfAbsent(cmHandleId, false) == null;