use separated get methods for every cmHandle instead of one "get all"
[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.InventoryPersistence;
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
32 import org.springframework.scheduling.annotation.Scheduled;
33 import org.springframework.stereotype.Component;
34
35 @Slf4j
36 @RequiredArgsConstructor
37 @Component
38 public class ModuleSyncWatchdog {
39
40     private final InventoryPersistence inventoryPersistence;
41
42     private final SyncUtils syncUtils;
43
44     private final ModuleSyncService moduleSyncService;
45
46     /**
47      * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
48      */
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);
55             try {
56                 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
57                 compositeState.setCmHandleState(CmHandleState.READY);
58             } catch (final Exception e) {
59                 compositeState.setCmHandleState(CmHandleState.LOCKED);
60                 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
61                     LockReasonCategory.LOCKED_MISBEHAVING,
62                     e.getMessage());
63             }
64             compositeState.setLastUpdateTimeNow();
65             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
66             log.info("{} is now in {} state", cmHandleId,
67                 advisedCmHandle.getCompositeState().getCmHandleState());
68             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
69         }
70         log.debug("No Cm-Handles currently found in an ADVISED state");
71     }
72
73     /**
74      * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
75      */
76     @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
77     public void executeLockedCmHandlePoll() {
78         final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
79         for (final YangModelCmHandle moduleSyncFailedCmHandle : lockedMisbehavingCmHandles) {
80             final CompositeState compositeState = moduleSyncFailedCmHandle.getCompositeState();
81             final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
82             if (isReadyForRetry) {
83                 setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
84                 log.debug("Locked misbehaving cm handle {} is being recycled", moduleSyncFailedCmHandle.getId());
85                 inventoryPersistence.saveCmHandleState(moduleSyncFailedCmHandle.getId(), compositeState);
86             }
87         }
88     }
89
90     private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
91         compositeState.setCmHandleState(CmHandleState.ADVISED);
92         compositeState.setLastUpdateTimeNow();
93         final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
94         compositeState.setLockReason(CompositeState.LockReason.builder()
95                 .details(oldLockReasonDetails).build());
96     }
97 }