Merge "Fix sonar code smells"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdog.java
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory.sync;
23
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;
35
36 @Slf4j
37 @RequiredArgsConstructor
38 @Component
39 public class ModuleSyncWatchdog {
40
41     private final InventoryPersistence inventoryPersistence;
42
43     private final SyncUtils syncUtils;
44
45     private final ModuleSyncService moduleSyncService;
46
47     /**
48      * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
49      */
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);
56             try {
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,
63                     e.getMessage());
64             }
65             compositeState.setLastUpdateTimeNow();
66             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
67             log.info("{} is now in {} state", cmHandleId,
68                 advisedCmHandle.getCompositeState().getCmHandleState());
69             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
70         }
71         log.debug("No Cm-Handles currently found in an ADVISED state");
72     }
73
74     /**
75      * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
76      */
77     @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
78     public void executeLockedMisbehavingCmHandlePoll() {
79         final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingCmHandles();
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);
88         }
89     }
90 }