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.CompositeState.LockReason;
31 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
32 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Component;
37 @RequiredArgsConstructor
39 public class ModuleSyncWatchdog {
41 private final InventoryPersistence inventoryPersistence;
43 private final SyncUtils syncUtils;
45 private final ModuleSyncService moduleSyncService;
48 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
50 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
51 public void executeAdvisedCmHandlePoll() {
52 YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
53 while (advisedCmHandle != null) {
54 final String cmHandleId = advisedCmHandle.getId();
55 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
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 executeLockedMisbehavingCmHandlePoll() {
79 final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
80 for (final YangModelCmHandle lockedMisbehavingModelCmHandle: lockedMisbehavingCmHandles) {
81 final CompositeState updatedCompositeState = lockedMisbehavingModelCmHandle.getCompositeState();
82 updatedCompositeState.setCmHandleState(CmHandleState.ADVISED);
83 updatedCompositeState.setLastUpdateTimeNow();
84 updatedCompositeState.setLockReason(LockReason.builder()
85 .details(updatedCompositeState.getLockReason().getDetails()).build());
86 log.debug("Locked misbehaving cm handle {} is being recycled", lockedMisbehavingModelCmHandle.getId());
87 inventoryPersistence.saveCmHandleState(lockedMisbehavingModelCmHandle.getId(), updatedCompositeState);