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 lombok.RequiredArgsConstructor;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
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.InventoryPersistence;
33 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
38 @RequiredArgsConstructor
40 public class ModuleSyncWatchdog {
42 private final InventoryPersistence inventoryPersistence;
44 private final SyncUtils syncUtils;
46 private final ModuleSyncService moduleSyncService;
48 private final ConcurrentMap<String, Boolean> moduleSyncSemaphoreMap;
50 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
53 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
55 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
56 public void executeAdvisedCmHandlePoll() {
57 syncUtils.getAdvisedCmHandles().forEach(advisedCmHandle -> {
58 final String cmHandleId = advisedCmHandle.getId();
59 if (hasPushedIntoSemaphoreMap(cmHandleId)) {
60 log.debug("executing module sync on {}", cmHandleId);
61 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
63 moduleSyncService.deleteSchemaSetIfExists(advisedCmHandle);
64 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
65 lcmEventsCmHandleStateHandler.updateCmHandleState(advisedCmHandle, CmHandleState.READY);
66 updateModuleSyncSemaphoreMap(cmHandleId);
67 } catch (final Exception e) {
68 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
69 LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
70 setCmHandleStateLocked(advisedCmHandle, compositeState.getLockReason());
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 log.debug("Reset cm handle {} state to ADVISED to re-attempt module-sync", lockedCmHandle.getId());
91 lcmEventsCmHandleStateHandler.updateCmHandleState(lockedCmHandle, CmHandleState.ADVISED);
96 private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
97 final CompositeState.LockReason lockReason) {
98 advisedCmHandle.getCompositeState().setLockReason(lockReason);
99 lcmEventsCmHandleStateHandler.updateCmHandleState(advisedCmHandle, CmHandleState.LOCKED);
102 private void updateModuleSyncSemaphoreMap(final String cmHandleId) {
103 moduleSyncSemaphoreMap.replace(cmHandleId, true);
106 private boolean hasPushedIntoSemaphoreMap(final String cmHandleId) {
107 return moduleSyncSemaphoreMap.putIfAbsent(cmHandleId, false) == null;