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 lombok.RequiredArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
31 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
32 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
33 import org.onap.cps.ncmp.api.inventory.CmHandleState;
34 import org.onap.cps.ncmp.api.inventory.CompositeState;
35 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
36 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
37 import org.onap.cps.spi.model.DataNode;
38 import org.springframework.stereotype.Component;
40 @RequiredArgsConstructor
43 public class ModuleSyncTasks {
44 private final InventoryPersistence inventoryPersistence;
45 private final SyncUtils syncUtils;
46 private final ModuleSyncService moduleSyncService;
47 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
49 private static final CompletableFuture<Void> COMPLETED_FUTURE = CompletableFuture.completedFuture(null);
52 * Perform module sync on a batch of cm handles.
54 * @param cmHandlesAsDataNodes a batch of Data nodes representing cm handles to perform module sync on
55 * @return completed future to handle post-processing
57 public CompletableFuture<Void> performModuleSync(final Collection<DataNode> cmHandlesAsDataNodes) {
58 final Map<YangModelCmHandle, CmHandleState> cmHandelStatePerCmHandle = new HashMap<>();
59 for (final DataNode cmHandleAsDataNode : cmHandlesAsDataNodes) {
60 final String cmHandleId = String.valueOf(cmHandleAsDataNode.getLeaves().get("id"));
61 final YangModelCmHandle yangModelCmHandle =
62 YangDataConverter.convertCmHandleToYangModel(cmHandleAsDataNode, cmHandleId);
63 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
65 moduleSyncService.deleteSchemaSetIfExists(cmHandleId);
66 moduleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle);
67 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY);
68 } catch (final Exception e) {
69 syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
70 LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
71 setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason());
72 cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
74 log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
76 updateCmHandlesStateBatch(cmHandelStatePerCmHandle);
77 return COMPLETED_FUTURE;
81 * Reset state to "ADVISED" for any previously failed cm handles.
83 * @param failedCmHandles previously failed (locked) cm handles
84 * @return completed future to handle post-processing
86 public CompletableFuture<Void> resetFailedCmHandles(final List<YangModelCmHandle> failedCmHandles) {
87 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(failedCmHandles.size());
88 for (final YangModelCmHandle failedCmHandle : failedCmHandles) {
89 final CompositeState compositeState = failedCmHandle.getCompositeState();
90 final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
91 if (isReadyForRetry) {
92 log.debug("Reset cm handle {} state to ADVISED to be re-attempted by module-sync watchdog",
93 failedCmHandle.getId());
94 cmHandleStatePerCmHandle.put(failedCmHandle, CmHandleState.ADVISED);
97 updateCmHandlesStateBatch(cmHandleStatePerCmHandle);
98 return COMPLETED_FUTURE;
101 private void setCmHandleStateLocked(final YangModelCmHandle advisedCmHandle,
102 final CompositeState.LockReason lockReason) {
103 advisedCmHandle.getCompositeState().setLockReason(lockReason);
106 private void updateCmHandlesStateBatch(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
107 // To be refactored as part of CPS-1231; Use state-save-batch capability (depends sub-task12, 13)
108 for (final Map.Entry<YangModelCmHandle, CmHandleState> entry : cmHandleStatePerCmHandle.entrySet()) {
109 lcmEventsCmHandleStateHandler.updateCmHandleState(entry.getKey(), entry.getValue());