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 lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
28 import org.onap.cps.ncmp.api.inventory.CmHandleState;
29 import org.onap.cps.ncmp.api.inventory.CompositeState;
30 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
32 import org.springframework.scheduling.annotation.Scheduled;
33 import org.springframework.stereotype.Component;
36 @RequiredArgsConstructor
38 public class ModuleSyncWatchdog {
40 private final InventoryPersistence inventoryPersistence;
42 private final SyncUtils syncUtils;
44 private final ModuleSyncService moduleSyncService;
47 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
49 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
50 public void executeAdvisedCmHandlePoll() {
51 YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
52 while (advisedCmHandle != null) {
53 final String cmHandleId = advisedCmHandle.getId();
54 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
56 moduleSyncService.deleteSchemaSetIfExists(advisedCmHandle);
57 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
58 compositeState.setCmHandleState(CmHandleState.READY);
59 } catch (final Exception e) {
60 compositeState.setCmHandleState(CmHandleState.LOCKED);
61 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
62 LockReasonCategory.LOCKED_MISBEHAVING,
65 compositeState.setLastUpdateTimeNow();
66 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
67 log.info("{} is now in {} state", cmHandleId,
68 advisedCmHandle.getCompositeState().getCmHandleState());
69 advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
71 log.debug("No Cm-Handles currently found in an ADVISED state");
75 * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
77 @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
78 public void executeLockedCmHandlePoll() {
79 final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
80 for (final YangModelCmHandle moduleSyncFailedCmHandle : lockedMisbehavingCmHandles) {
81 final CompositeState compositeState = moduleSyncFailedCmHandle.getCompositeState();
82 final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
83 if (isReadyForRetry) {
84 setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
85 log.debug("Locked misbehaving cm handle {} is being recycled", moduleSyncFailedCmHandle.getId());
86 inventoryPersistence.saveCmHandleState(moduleSyncFailedCmHandle.getId(), compositeState);
91 private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
92 compositeState.setCmHandleState(CmHandleState.ADVISED);
93 compositeState.setLastUpdateTimeNow();
94 final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
95 compositeState.setLockReason(CompositeState.LockReason.builder()
96 .details(oldLockReasonDetails).build());