2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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 static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import java.time.Duration;
28 import java.time.OffsetDateTime;
29 import java.time.format.DateTimeFormatter;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.UUID;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import lombok.RequiredArgsConstructor;
40 import lombok.extern.slf4j.Slf4j;
41 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
42 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
43 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
44 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
45 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory;
46 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
47 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
48 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
49 import org.onap.cps.spi.FetchDescendantsOption;
50 import org.onap.cps.spi.model.DataNode;
51 import org.onap.cps.utils.JsonObjectMapper;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.stereotype.Service;
57 @RequiredArgsConstructor
58 public class ModuleOperationsUtils {
60 private final CmHandleQueries cmHandleQueries;
61 private final DmiDataOperations dmiDataOperations;
62 private final JsonObjectMapper jsonObjectMapper;
63 private static final String RETRY_ATTEMPT_KEY = "attempt";
64 public static final String MODULE_SET_TAG_KEY = "moduleSetTag";
65 public static final String MODULE_SET_TAG_MESSAGE_FORMAT = "Upgrade to ModuleSetTag: {0}";
66 private static final String UPGRADE_FORMAT = "Upgrade to ModuleSetTag: %s";
67 private static final String UPGRADE_FAILED_FORMAT = UPGRADE_FORMAT + " Attempt #%d failed: %s";
68 private static final Pattern retryAttemptPattern = Pattern.compile("Attempt #(\\d+) failed:.+");
69 private static final Pattern moduleSetTagPattern = Pattern.compile("Upgrade to ModuleSetTag: (\\S+)");
72 * Query data nodes for cm handles with an "ADVISED" cm handle state.
74 * @return cm handles (data nodes) in ADVISED state (empty list if none found)
76 public List<DataNode> getAdvisedCmHandles() {
77 final List<DataNode> advisedCmHandlesAsDataNodes = cmHandleQueries.queryCmHandlesByState(CmHandleState.ADVISED);
78 log.debug("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodes.size());
79 return advisedCmHandlesAsDataNodes;
83 * First query data nodes for cm handles with CM Handle Operational Sync State in "UNSYNCHRONIZED" and
84 * randomly select a CM Handle and query the data nodes for CM Handle State in "READY".
86 * @return a randomized yang model cm handle list with State in READY and Operation Sync State in "UNSYNCHRONIZED",
87 * return empty list if not found
89 public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
90 final List<DataNode> unsynchronizedCmHandles = cmHandleQueries
91 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
93 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>();
94 for (final DataNode unsynchronizedCmHandle : unsynchronizedCmHandles) {
95 final String cmHandleId = unsynchronizedCmHandle.getLeaves().get("id").toString();
96 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
97 yangModelCmHandles.addAll(convertCmHandlesDataNodesToYangModelCmHandles(
98 Collections.singletonList(unsynchronizedCmHandle)));
101 Collections.shuffle(yangModelCmHandles);
102 return yangModelCmHandles;
106 * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
108 * @return a random LOCKED yang model cm handle, return null if not found
110 public List<YangModelCmHandle> getCmHandlesThatFailedModelSyncOrUpgrade() {
111 final List<DataNode> lockedCmHandlesAsDataNodeList
112 = cmHandleQueries.queryCmHandleAncestorsByCpsPath(
113 "//lock-reason[@reason=\"MODULE_SYNC_FAILED\" or @reason=\"MODULE_UPGRADE\"]",
114 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
115 return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList);
119 * Update Composite State attempts counter and set new lock reason and details.
121 * @param lockReasonCategory lock reason category
122 * @param errorMessage error message
124 public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
125 final LockReasonCategory lockReasonCategory,
126 final String errorMessage) {
128 final Map<String, String> compositeStateDetails
129 = getLockedCompositeStateDetails(compositeState.getLockReason());
130 if (!compositeStateDetails.isEmpty()) {
131 attempt = 1 + Integer.parseInt(compositeStateDetails.get(RETRY_ATTEMPT_KEY));
133 compositeState.setLockReason(CompositeState.LockReason.builder()
134 .details(String.format(UPGRADE_FAILED_FORMAT,
135 compositeStateDetails.get(MODULE_SET_TAG_KEY), attempt, errorMessage))
136 .lockReasonCategory(lockReasonCategory).build());
140 * Extract lock reason details as key-value pair.
142 * @param compositeStateLockReason lock reason having all the details
143 * @return a map of lock reason details
145 public static Map<String, String> getLockedCompositeStateDetails(final CompositeState.LockReason
146 compositeStateLockReason) {
147 if (compositeStateLockReason != null) {
148 final Map<String, String> compositeStateDetails = new HashMap<>(2);
149 final String lockedCompositeStateReasonDetails = compositeStateLockReason.getDetails();
150 final Matcher retryAttemptMatcher = retryAttemptPattern.matcher(lockedCompositeStateReasonDetails);
151 if (retryAttemptMatcher.find()) {
152 final int attemptsRegexGroupId = 1;
153 compositeStateDetails.put(RETRY_ATTEMPT_KEY, retryAttemptMatcher.group(attemptsRegexGroupId));
155 final Matcher moduleSetTagMatcher = moduleSetTagPattern.matcher(lockedCompositeStateReasonDetails);
156 if (moduleSetTagMatcher.find()) {
157 final int moduleSetTagRegexGroupId = 1;
158 compositeStateDetails.put(MODULE_SET_TAG_KEY, moduleSetTagMatcher.group(moduleSetTagRegexGroupId));
160 return compositeStateDetails;
162 return Collections.emptyMap();
167 * Check if a module sync retry is needed.
169 * @param compositeState the composite state currently in the locked state
170 * @return if the retry mechanism should be attempted
172 public boolean needsModuleSyncRetryOrUpgrade(final CompositeState compositeState) {
173 final OffsetDateTime time = OffsetDateTime.parse(compositeState.getLastUpdateTime(),
174 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
175 final CompositeState.LockReason lockReason = compositeState.getLockReason();
177 final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
178 == lockReason.getLockReasonCategory();
179 final boolean failedDuringModuleUpgrade = LockReasonCategory.MODULE_UPGRADE_FAILED
180 == lockReason.getLockReasonCategory();
182 if (failedDuringModuleSync || failedDuringModuleUpgrade) {
183 log.info("Locked for module {}.", failedDuringModuleSync ? "sync" : "upgrade");
184 return isRetryDue(lockReason, time);
186 log.info("Locked for other reason");
191 * Get the Resourece Data from Node through DMI Passthrough service.
193 * @param cmHandleId cm handle id
194 * @return optional string containing the resource data
196 public String getResourceData(final String cmHandleId) {
197 final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
198 PASSTHROUGH_OPERATIONAL.getDatastoreName(),
200 UUID.randomUUID().toString());
201 if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
202 return getFirstResource(resourceDataResponseEntity.getBody());
208 * Checks if cm handle state module is in upgrade or upgrade failed.
210 * @param compositeState current lock reason of cm handle
211 * @return true or false based on lock reason category
213 public static boolean isInUpgradeOrUpgradeFailed(final CompositeState compositeState) {
214 return compositeState.getLockReason() != null
215 && (LockReasonCategory.MODULE_UPGRADE.equals(compositeState.getLockReason().getLockReasonCategory())
216 || LockReasonCategory.MODULE_UPGRADE_FAILED.equals(compositeState.getLockReason()
217 .getLockReasonCategory()));
220 private String getFirstResource(final Object responseBody) {
221 final String jsonObjectAsString = jsonObjectMapper.asJsonString(responseBody);
222 final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString);
223 final Iterator<Map.Entry<String, JsonNode>> overallJsonTreeMap = overallJsonNode.fields();
224 final Map.Entry<String, JsonNode> firstElement = overallJsonTreeMap.next();
225 return jsonObjectMapper.asJsonString(Map.of(firstElement.getKey(), firstElement.getValue()));
228 private List<YangModelCmHandle> convertCmHandlesDataNodesToYangModelCmHandles(
229 final List<DataNode> cmHandlesAsDataNodeList) {
230 return cmHandlesAsDataNodeList.stream()
231 .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
232 cmHandle.getLeaves().get("id").toString())).toList();
235 private boolean isRetryDue(final CompositeState.LockReason compositeStateLockReason, final OffsetDateTime time) {
236 final int timeInMinutesUntilNextAttempt;
237 final Map<String, String> compositeStateDetails = getLockedCompositeStateDetails(compositeStateLockReason);
238 if (compositeStateDetails.isEmpty()) {
239 timeInMinutesUntilNextAttempt = 1;
240 log.info("First Attempt: no current attempts found.");
242 timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(compositeStateDetails
243 .get(RETRY_ATTEMPT_KEY)));
245 final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
246 if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
247 log.info("Time until next attempt is {} minutes: ", timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
250 log.info("Retry due now");