2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.inventory.sync;
23 import com.hazelcast.map.IMap;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
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;
42 @RequiredArgsConstructor
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;
53 * Perform module sync on a batch of cm handles.
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
60 public CompletableFuture<Void> performModuleSync(final Collection<DataNode> cmHandlesAsDataNodes,
61 final AtomicInteger batchCounter) {
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);
69 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
70 final boolean inUpgrade = ModuleOperationsUtils.inUpgradeOrUpgradeFailed(compositeState);
73 moduleSyncService.syncAndUpgradeSchemaSet(yangModelCmHandle);
75 moduleSyncService.deleteSchemaSetIfExists(cmHandleId);
76 moduleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle);
78 yangModelCmHandle.getCompositeState().setLockReason(null);
79 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY);
80 } catch (final Exception e) {
81 log.warn("Processing of {} module failed due to reason {}.", cmHandleId, e.getMessage());
82 final LockReasonCategory lockReasonCategory = inUpgrade ? LockReasonCategory.MODULE_UPGRADE_FAILED
83 : LockReasonCategory.MODULE_SYNC_FAILED;
84 moduleOperationsUtils.updateLockReasonDetailsAndAttempts(compositeState,
85 lockReasonCategory, e.getMessage());
86 setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason());
87 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
89 log.info("{} is now in {} state", cmHandleId, cmHandelStatePerCmHandle.get(yangModelCmHandle).name());
91 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandelStatePerCmHandle);
93 batchCounter.getAndDecrement();
94 log.info("Processing module sync batch finished. {} batch(es) active.", batchCounter.get());
96 return CompletableFuture.completedFuture(null);
100 * Reset state to "ADVISED" for any previously failed cm handles.
102 * @param failedCmHandles previously failed (locked) cm handles
104 public void resetFailedCmHandles(final List<YangModelCmHandle> failedCmHandles) {
105 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(failedCmHandles.size());
106 for (final YangModelCmHandle failedCmHandle : failedCmHandles) {
107 final CompositeState compositeState = failedCmHandle.getCompositeState();
108 final boolean isReadyForRetry = moduleOperationsUtils.needsModuleSyncRetryOrUpgrade(compositeState);
109 log.info("Retry for cmHandleId : {} is {}", failedCmHandle.getId(), isReadyForRetry);
110 if (isReadyForRetry) {
111 final String resetCmHandleId = failedCmHandle.getId();
112 log.debug("Reset cm handle {} state to ADVISED to be re-attempted by module-sync watchdog",
114 cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED);
115 removeResetCmHandleFromModuleSyncMap(resetCmHandleId);
118 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
121 private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
122 final CompositeState.LockReason lockReason) {
123 advisedCmHandle.getCompositeState().setLockReason(lockReason);
126 private void removeResetCmHandleFromModuleSyncMap(final String resetCmHandleId) {
127 if (moduleSyncStartedOnCmHandles.remove(resetCmHandleId) != null) {
128 log.info("{} removed from in progress map", resetCmHandleId);