2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.inventory.sync;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.atomic.AtomicInteger;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
32 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
33 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
34 import org.onap.cps.ncmp.api.inventory.CmHandleState;
35 import org.onap.cps.ncmp.api.inventory.CompositeState;
36 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
37 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
38 import org.onap.cps.spi.model.DataNode;
39 import org.springframework.stereotype.Component;
41 @RequiredArgsConstructor
44 public class ModuleSyncTasks {
45 private final InventoryPersistence inventoryPersistence;
46 private final SyncUtils syncUtils;
47 private final ModuleSyncService moduleSyncService;
48 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
50 private static final CompletableFuture<Void> COMPLETED_FUTURE = CompletableFuture.completedFuture(null);
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 task is finished
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 = new HashMap<>();
64 for (final DataNode cmHandleAsDataNode : cmHandlesAsDataNodes) {
65 final String cmHandleId = String.valueOf(cmHandleAsDataNode.getLeaves().get("id"));
66 final YangModelCmHandle yangModelCmHandle =
67 YangDataConverter.convertCmHandleToYangModel(cmHandleAsDataNode, cmHandleId);
68 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
70 moduleSyncService.deleteSchemaSetIfExists(cmHandleId);
71 moduleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle);
72 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY);
73 } catch (final Exception e) {
74 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
75 LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
76 setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason());
77 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
79 log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
81 updateCmHandlesStateBatch(cmHandelStatePerCmHandle);
83 batchCounter.getAndDecrement();
85 return COMPLETED_FUTURE;
89 * Reset state to "ADVISED" for any previously failed cm handles.
91 * @param failedCmHandles previously failed (locked) cm handles
92 * @return completed future to handle post-processing
94 public CompletableFuture<Void> resetFailedCmHandles(final List<YangModelCmHandle> failedCmHandles) {
95 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(failedCmHandles.size());
96 for (final YangModelCmHandle failedCmHandle : failedCmHandles) {
97 final CompositeState compositeState = failedCmHandle.getCompositeState();
98 final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
99 if (isReadyForRetry) {
100 log.debug("Reset cm handle {} state to ADVISED to be re-attempted by module-sync watchdog",
101 failedCmHandle.getId());
102 cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED);
105 updateCmHandlesStateBatch(cmHandleStatePerCmHandle);
106 return COMPLETED_FUTURE;
109 private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
110 final CompositeState.LockReason lockReason) {
111 advisedCmHandle.getCompositeState().setLockReason(lockReason);
114 private void updateCmHandlesStateBatch(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
115 // To be refactored as part of CPS-1231; Use state-save-batch capability (depends sub-task12, 13)
116 for (final Map.Entry<YangModelCmHandle, CmHandleState> entry : cmHandleStatePerCmHandle.entrySet()) {
117 lcmEventsCmHandleStateHandler.updateCmHandleState(entry.getKey(), entry.getValue());