Composite state transition code change
[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 java.util.function.Consumer;
26 import lombok.RequiredArgsConstructor;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
29 import org.onap.cps.ncmp.api.inventory.CmHandleState;
30 import org.onap.cps.ncmp.api.inventory.CompositeState;
31 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
32 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
33 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
36
37 @Slf4j
38 @RequiredArgsConstructor
39 @Component
40 public class ModuleSyncWatchdog {
41
42     private final InventoryPersistence inventoryPersistence;
43
44     private final SyncUtils syncUtils;
45
46     private final ModuleSyncService moduleSyncService;
47
48     /**
49      * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
50      */
51     @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
52     public void executeAdvisedCmHandlePoll() {
53         YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
54         while (advisedCmHandle != null) {
55             final String cmHandleId = advisedCmHandle.getId();
56             final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
57             try {
58                 moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
59                 setCompositeStateToReadyWithInitialDataStoreSyncState().accept(compositeState);
60             } catch (final Exception e) {
61                 setCompositeStateToLocked().accept(compositeState);
62                 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
63                         LockReasonCategory.LOCKED_MISBEHAVING, e.getMessage());
64             }
65             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
66             log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
67             advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
68         }
69         log.debug("No Cm-Handles currently found in an ADVISED state");
70     }
71
72     /**
73      * Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
74      */
75     @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
76     public void executeLockedCmHandlePoll() {
77         final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingYangModelCmHandles();
78         for (final YangModelCmHandle moduleSyncFailedCmHandle : lockedMisbehavingCmHandles) {
79             final CompositeState compositeState = moduleSyncFailedCmHandle.getCompositeState();
80             final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
81             if (isReadyForRetry) {
82                 setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
83                 log.debug("Locked misbehaving cm handle {} is being recycled", moduleSyncFailedCmHandle.getId());
84                 inventoryPersistence.saveCmHandleState(moduleSyncFailedCmHandle.getId(), compositeState);
85             }
86         }
87     }
88
89     private Consumer<CompositeState> setCompositeStateToLocked() {
90         return compositeState -> {
91             compositeState.setCmHandleState(CmHandleState.LOCKED);
92             compositeState.setLastUpdateTimeNow();
93         };
94     }
95
96     private Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
97         return compositeState -> {
98             compositeState.setCmHandleState(CmHandleState.READY);
99             final CompositeState.Operational operational = CompositeState.Operational.builder()
100                     .dataStoreSyncState(DataStoreSyncState.UNSYNCHRONIZED)
101                     .lastSyncTime(CompositeState.nowInSyncTimeFormat())
102                     .build();
103             final CompositeState.DataStores dataStores = CompositeState.DataStores.builder()
104                     .operationalDataStore(operational)
105                     .build();
106             compositeState.setDataStores(dataStores);
107         };
108     }
109
110     private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
111         compositeState.setCmHandleState(CmHandleState.ADVISED);
112         compositeState.setLastUpdateTimeNow();
113         final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
114         final CompositeState.LockReason lockReason = CompositeState.LockReason.builder()
115                 .details(oldLockReasonDetails).build();
116         compositeState.setLockReason(lockReason);
117     }
118 }