Merge "Update Model to allow Persisting of alternateId"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / sync / ModuleSyncTasks.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.inventory.sync;
22
23 import com.hazelcast.map.IMap;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
33 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
34 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
35 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
36 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory;
37 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
38 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
39 import org.onap.cps.spi.model.DataNode;
40 import org.springframework.stereotype.Component;
41
42 @RequiredArgsConstructor
43 @Component
44 @Slf4j
45 public class ModuleSyncTasks {
46     private final InventoryPersistence inventoryPersistence;
47     private final ModuleOperationsUtils moduleOperationsUtils;
48     private final ModuleSyncService moduleSyncService;
49     private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
50     private final IMap<String, Object> moduleSyncStartedOnCmHandles;
51
52     /**
53      * Perform module sync on a batch of cm handles.
54      *
55      * @param cmHandlesAsDataNodes         a batch of Data nodes representing cm handles to perform module sync on
56      * @param batchCounter                 the number of batches currently being processed, will be decreased when
57      *                                     task is finished or fails
58      * @return completed future to handle post-processing
59      */
60     public CompletableFuture<Void> performModuleSync(final Collection<DataNode> cmHandlesAsDataNodes,
61                                                      final AtomicInteger batchCounter) {
62         try {
63             final Map<YangModelCmHandle, CmHandleState> cmHandelStatePerCmHandle
64                     = new HashMap<>(cmHandlesAsDataNodes.size());
65             for (final DataNode cmHandleAsDataNode : cmHandlesAsDataNodes) {
66                 final String cmHandleId = String.valueOf(cmHandleAsDataNode.getLeaves().get("id"));
67                 final YangModelCmHandle yangModelCmHandle =
68                         YangDataConverter.convertCmHandleToYangModel(cmHandleAsDataNode, cmHandleId);
69                 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
70                 final boolean inUpgrade = ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState);
71                 try {
72                     if (!inUpgrade) {
73                         moduleSyncService.deleteSchemaSetIfExists(cmHandleId);
74                     }
75                     moduleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle);
76                     yangModelCmHandle.getCompositeState().setLockReason(null);
77                     cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY);
78                 } catch (final Exception e) {
79                     log.warn("Processing of {} module failed due to reason {}.", cmHandleId, e.getMessage());
80                     final LockReasonCategory lockReasonCategory = inUpgrade ? LockReasonCategory.MODULE_UPGRADE_FAILED
81                             : LockReasonCategory.MODULE_SYNC_FAILED;
82                     moduleOperationsUtils.updateLockReasonDetailsAndAttempts(compositeState,
83                             lockReasonCategory, e.getMessage());
84                     setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason());
85                     cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
86                 }
87                 log.info("{} is now in {} state", cmHandleId, cmHandelStatePerCmHandle.get(yangModelCmHandle).name());
88             }
89             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandelStatePerCmHandle);
90         } finally {
91             batchCounter.getAndDecrement();
92             log.info("Processing module sync batch finished. {} batch(es) active.", batchCounter.get());
93         }
94         return CompletableFuture.completedFuture(null);
95     }
96
97     /**
98      * Reset state to "ADVISED" for any previously failed cm handles.
99      *
100      * @param failedCmHandles previously failed (locked) cm handles
101      */
102     public void resetFailedCmHandles(final List<YangModelCmHandle> failedCmHandles) {
103         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(failedCmHandles.size());
104         for (final YangModelCmHandle failedCmHandle : failedCmHandles) {
105             final CompositeState compositeState = failedCmHandle.getCompositeState();
106             final boolean isReadyForRetry = moduleOperationsUtils.needsModuleSyncRetryOrUpgrade(compositeState);
107             log.info("Retry for cmHandleId : {} is {}", failedCmHandle.getId(), isReadyForRetry);
108             if (isReadyForRetry) {
109                 final String resetCmHandleId = failedCmHandle.getId();
110                 log.debug("Reset cm handle {} state to ADVISED to be re-attempted by module-sync watchdog",
111                         resetCmHandleId);
112                 cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED);
113                 removeResetCmHandleFromModuleSyncMap(resetCmHandleId);
114             }
115         }
116         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
117     }
118
119     private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
120                                         final CompositeState.LockReason lockReason) {
121         advisedCmHandle.getCompositeState().setLockReason(lockReason);
122     }
123
124     private void removeResetCmHandleFromModuleSyncMap(final String resetCmHandleId) {
125         if (moduleSyncStartedOnCmHandles.remove(resetCmHandleId) != null) {
126             log.info("{} removed from in progress map", resetCmHandleId);
127         }
128     }
129 }