Data Sync Watchdog Process
[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 static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.CREATE;
25
26 import java.util.List;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.impl.event.NcmpEventsService;
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
31 import org.onap.cps.ncmp.api.inventory.CmHandleState;
32 import org.onap.cps.ncmp.api.inventory.CompositeState;
33 import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason;
34 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
35 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
36 import org.springframework.scheduling.annotation.Scheduled;
37 import org.springframework.stereotype.Component;
38
39 @Slf4j
40 @RequiredArgsConstructor
41 @Component
42 public class ModuleSyncWatchdog {
43
44     private final InventoryPersistence inventoryPersistence;
45
46     private final SyncUtils syncUtils;
47
48     private final ModuleSyncService moduleSyncService;
49
50     private final NcmpEventsService ncmpEventsService;
51
52     /**
53      * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
54      * Also publish the LCM Create Event when cm handle state is moved to 'READY'.
55      */
56     @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
57     public void executeAdvisedCmHandlePoll() {
58         YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
59         while (advisedCmHandle != null) {
60             final String cmHandleId = advisedCmHandle.getId();
61             final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
62             try {
63                 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
64                 compositeState.setCmHandleState(CmHandleState.READY);
65             } catch (final Exception e) {
66                 compositeState.setCmHandleState(CmHandleState.LOCKED);
67                 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
68                     LockReasonCategory.LOCKED_MISBEHAVING,
69                     e.getMessage());
70             }
71             compositeState.setLastUpdateTimeNow();
72             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
73             log.info("{} is now in {} state", cmHandleId,
74                 advisedCmHandle.getCompositeState().getCmHandleState());
75             if (compositeState.getCmHandleState() == CmHandleState.READY) {
76                 log.debug("Publishing LCM Create Event for cmHandleId : {}", cmHandleId);
77                 ncmpEventsService.publishNcmpEvent(cmHandleId, CREATE);
78             }
79             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
80         }
81         log.debug("No Cm-Handles currently found in an ADVISED state");
82     }
83
84     /**
85      * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
86      */
87     @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
88     public void executeLockedMisbehavingCmHandlePoll() {
89         final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
90         for (final YangModelCmHandle lockedMisbehavingModelCmHandle: lockedMisbehavingCmHandles) {
91             final CompositeState updatedCompositeState = lockedMisbehavingModelCmHandle.getCompositeState();
92             updatedCompositeState.setCmHandleState(CmHandleState.ADVISED);
93             updatedCompositeState.setLastUpdateTimeNow();
94             updatedCompositeState.setLockReason(LockReason.builder()
95                 .details(updatedCompositeState.getLockReason().getDetails()).build());
96             log.debug("Locked misbehaving cm handle {} is being recycled", lockedMisbehavingModelCmHandle.getId());
97             inventoryPersistence.saveCmHandleState(lockedMisbehavingModelCmHandle.getId(), updatedCompositeState);
98         }
99     }
100 }