2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.inventory.sync;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import java.time.Duration;
26 import java.time.OffsetDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.UUID;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import lombok.RequiredArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueryService;
40 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
41 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
42 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
43 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory;
44 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
45 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
46 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
47 import org.onap.cps.spi.FetchDescendantsOption;
48 import org.onap.cps.spi.model.DataNode;
49 import org.onap.cps.utils.JsonObjectMapper;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.stereotype.Service;
55 @RequiredArgsConstructor
56 public class ModuleOperationsUtils {
58 private final CmHandleQueryService cmHandleQueryService;
59 private final DmiDataOperations dmiDataOperations;
60 private final JsonObjectMapper jsonObjectMapper;
61 private static final String RETRY_ATTEMPT_KEY = "attempt";
62 private static final String MODULE_SET_TAG_KEY = "moduleSetTag";
63 public static final String MODULE_SET_TAG_MESSAGE_FORMAT = "Upgrade to ModuleSetTag: %s";
64 private static final String LOCK_REASON_DETAILS_MSG_FORMAT =
65 MODULE_SET_TAG_MESSAGE_FORMAT + " Attempt #%d failed: %s";
66 private static final Pattern retryAttemptPattern = Pattern.compile("Attempt #(\\d+) failed:.+");
67 private static final Pattern moduleSetTagPattern = Pattern.compile("Upgrade to ModuleSetTag: (\\S+)");
68 private static final String CPS_PATH_CM_HANDLES_MODEL_SYNC_FAILED_OR_UPGRADE = """
69 //lock-reason[@reason="MODULE_SYNC_FAILED"
70 or @reason="MODULE_UPGRADE"
71 or @reason="MODULE_UPGRADE_FAILED"]""";
74 * Query data nodes for cm handles with an "ADVISED" cm handle state.
76 * @return cm handles (data nodes) in ADVISED state (empty list if none found)
78 public List<DataNode> getAdvisedCmHandles() {
79 final List<DataNode> advisedCmHandlesAsDataNodes =
80 cmHandleQueryService.queryCmHandlesByState(CmHandleState.ADVISED);
81 log.debug("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodes.size());
82 return advisedCmHandlesAsDataNodes;
86 * First query data nodes for cm handles with CM Handle Operational Sync State in "UNSYNCHRONIZED" and
87 * randomly select a CM Handle and query the data nodes for CM Handle State in "READY".
89 * @return a randomized yang model cm handle list with State in READY and Operation Sync State in "UNSYNCHRONIZED",
90 * return empty list if not found
92 public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
93 final List<DataNode> unsynchronizedCmHandles = cmHandleQueryService
94 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
96 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>();
97 for (final DataNode unsynchronizedCmHandle : unsynchronizedCmHandles) {
98 final String cmHandleId = unsynchronizedCmHandle.getLeaves().get("id").toString();
99 if (cmHandleQueryService.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
100 yangModelCmHandles.addAll(convertCmHandlesDataNodesToYangModelCmHandles(
101 Collections.singletonList(unsynchronizedCmHandle)));
104 Collections.shuffle(yangModelCmHandles);
105 return yangModelCmHandles;
109 * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
111 * @return a random LOCKED yang model cm handle, return null if not found
113 public List<YangModelCmHandle> getCmHandlesThatFailedModelSyncOrUpgrade() {
114 final List<DataNode> lockedCmHandlesAsDataNodeList
115 = cmHandleQueryService.queryCmHandleAncestorsByCpsPath(CPS_PATH_CM_HANDLES_MODEL_SYNC_FAILED_OR_UPGRADE,
116 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
117 return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList);
121 * Update Composite State attempts counter and set new lock reason and details.
123 * @param lockReasonCategory lock reason category
124 * @param errorMessage error message
126 public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
127 final LockReasonCategory lockReasonCategory,
128 final String errorMessage) {
130 final Map<String, String> compositeStateDetails
131 = getLockedCompositeStateDetails(compositeState.getLockReason());
132 if (!compositeStateDetails.isEmpty() && compositeStateDetails.containsKey(RETRY_ATTEMPT_KEY)) {
133 attempt = 1 + Integer.parseInt(compositeStateDetails.get(RETRY_ATTEMPT_KEY));
135 final String moduleSetTag = compositeStateDetails.getOrDefault(MODULE_SET_TAG_KEY, "");
136 compositeState.setLockReason(CompositeState.LockReason.builder()
137 .details(String.format(LOCK_REASON_DETAILS_MSG_FORMAT, moduleSetTag, attempt, errorMessage))
138 .lockReasonCategory(lockReasonCategory)
143 * Extract lock reason details as key-value pair.
145 * @param compositeStateLockReason lock reason having all the details
146 * @return a map of lock reason details
148 public static Map<String, String> getLockedCompositeStateDetails(final CompositeState.LockReason
149 compositeStateLockReason) {
150 if (compositeStateLockReason != null) {
151 final Map<String, String> compositeStateDetails = new HashMap<>(2);
152 final String lockedCompositeStateReasonDetails = compositeStateLockReason.getDetails();
153 final Matcher retryAttemptMatcher = retryAttemptPattern.matcher(lockedCompositeStateReasonDetails);
154 if (retryAttemptMatcher.find()) {
155 final int attemptsRegexGroupId = 1;
156 compositeStateDetails.put(RETRY_ATTEMPT_KEY, retryAttemptMatcher.group(attemptsRegexGroupId));
158 final Matcher moduleSetTagMatcher = moduleSetTagPattern.matcher(lockedCompositeStateReasonDetails);
159 if (moduleSetTagMatcher.find()) {
160 final int moduleSetTagRegexGroupId = 1;
161 compositeStateDetails.put(MODULE_SET_TAG_KEY, moduleSetTagMatcher.group(moduleSetTagRegexGroupId));
163 return compositeStateDetails;
165 return Collections.emptyMap();
170 * Check if a module sync retry is needed.
172 * @param compositeState the composite state currently in the locked state
173 * @return if the retry mechanism should be attempted
175 public boolean needsModuleSyncRetryOrUpgrade(final CompositeState compositeState) {
176 final OffsetDateTime time = OffsetDateTime.parse(compositeState.getLastUpdateTime(),
177 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
178 final CompositeState.LockReason lockReason = compositeState.getLockReason();
180 final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE == lockReason.getLockReasonCategory();
182 log.info("Locked for module upgrade");
186 final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
187 == lockReason.getLockReasonCategory();
188 final boolean failedDuringModuleUpgrade = LockReasonCategory.MODULE_UPGRADE_FAILED
189 == lockReason.getLockReasonCategory();
191 if (failedDuringModuleSync || failedDuringModuleUpgrade) {
192 log.info("Locked for module {} (last attempt failed).", failedDuringModuleSync ? "sync" : "upgrade");
193 return isRetryDue(lockReason, time);
195 log.info("Locked for other reason");
200 * Get the Resource Data from Node through DMI Passthrough service.
202 * @param cmHandleId cm handle id
203 * @return optional string containing the resource data
205 public String getResourceData(final String cmHandleId) {
206 final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getAllResourceDataFromDmi(
207 cmHandleId, UUID.randomUUID().toString());
208 if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
209 return getFirstResource(resourceDataResponseEntity.getBody());
215 * Checks if cm handle state module is in upgrade or upgrade failed.
217 * @param compositeState current lock reason of cm handle
218 * @return true or false based on lock reason category
220 public static boolean inUpgradeOrUpgradeFailed(final CompositeState compositeState) {
221 return compositeState.getLockReason() != null
222 && (LockReasonCategory.MODULE_UPGRADE.equals(compositeState.getLockReason().getLockReasonCategory())
223 || LockReasonCategory.MODULE_UPGRADE_FAILED.equals(compositeState.getLockReason()
224 .getLockReasonCategory()));
227 public static String getUpgradedModuleSetTagFromLockReason(final CompositeState.LockReason lockReason) {
228 return getLockedCompositeStateDetails(lockReason).getOrDefault(MODULE_SET_TAG_KEY, "");
231 private String getFirstResource(final Object responseBody) {
232 final String jsonObjectAsString = jsonObjectMapper.asJsonString(responseBody);
233 final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString);
234 final Iterator<Map.Entry<String, JsonNode>> overallJsonTreeMap = overallJsonNode.fields();
235 final Map.Entry<String, JsonNode> firstElement = overallJsonTreeMap.next();
236 return jsonObjectMapper.asJsonString(Map.of(firstElement.getKey(), firstElement.getValue()));
239 private List<YangModelCmHandle> convertCmHandlesDataNodesToYangModelCmHandles(
240 final List<DataNode> cmHandlesAsDataNodeList) {
241 return cmHandlesAsDataNodeList.stream().map(YangDataConverter::convertCmHandleToYangModel).toList();
244 private boolean isRetryDue(final CompositeState.LockReason compositeStateLockReason, final OffsetDateTime time) {
245 final int timeInMinutesUntilNextAttempt;
246 final Map<String, String> compositeStateDetails = getLockedCompositeStateDetails(compositeStateLockReason);
247 if (compositeStateDetails.isEmpty()) {
248 timeInMinutesUntilNextAttempt = 1;
249 log.info("First Attempt: no current attempts found.");
251 timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(compositeStateDetails
252 .get(RETRY_ATTEMPT_KEY)));
254 final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
255 if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
256 log.info("Time until next attempt is {} minutes: ", timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
259 log.info("Retry due now");