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
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.deleteSchemaSetIfExists(cmHandleId);
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);
87 log.info("{} is now in {} state", cmHandleId, cmHandelStatePerCmHandle.get(yangModelCmHandle).name());
89 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandelStatePerCmHandle);
91 batchCounter.getAndDecrement();
92 log.info("Processing module sync batch finished. {} batch(es) active.", batchCounter.get());
94 return CompletableFuture.completedFuture(null);
98 * Reset state to "ADVISED" for any previously failed cm handles.
100 * @param failedCmHandles previously failed (locked) cm handles
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",
112 cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED);
113 removeResetCmHandleFromModuleSyncMap(resetCmHandleId);
116 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
119 private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
120 final CompositeState.LockReason lockReason) {
121 advisedCmHandle.getCompositeState().setLockReason(lockReason);
124 private void removeResetCmHandleFromModuleSyncMap(final String resetCmHandleId) {
125 if (moduleSyncStartedOnCmHandles.remove(resetCmHandleId) != null) {
126 log.info("{} removed from in progress map", resetCmHandleId);