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.Iterator;
33 import java.util.List;
35 import java.util.UUID;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import lombok.RequiredArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
41 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
42 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
43 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
44 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory;
45 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
46 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
47 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.model.DataNode;
50 import org.onap.cps.utils.JsonObjectMapper;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.stereotype.Service;
56 @RequiredArgsConstructor
57 public class SyncUtils {
59 private final CmHandleQueries cmHandleQueries;
60 private final DmiDataOperations dmiDataOperations;
61 private final JsonObjectMapper jsonObjectMapper;
62 private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:");
65 * Query data nodes for cm handles with an "ADVISED" cm handle state.
67 * @return cm handles (data nodes) in ADVISED state (empty list if none found)
69 public List<DataNode> getAdvisedCmHandles() {
70 final List<DataNode> advisedCmHandlesAsDataNodes = cmHandleQueries.queryCmHandlesByState(CmHandleState.ADVISED);
71 log.debug("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodes.size());
72 return advisedCmHandlesAsDataNodes;
76 * First query data nodes for cm handles with CM Handle Operational Sync State in "UNSYNCHRONIZED" and
77 * randomly select a CM Handle and query the data nodes for CM Handle State in "READY".
79 * @return a randomized yang model cm handle list with State in READY and Operation Sync State in "UNSYNCHRONIZED",
80 * return empty list if not found
82 public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
83 final List<DataNode> unsynchronizedCmHandles = cmHandleQueries
84 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
86 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>();
87 for (final DataNode unsynchronizedCmHandle : unsynchronizedCmHandles) {
88 final String cmHandleId = unsynchronizedCmHandle.getLeaves().get("id").toString();
89 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
90 yangModelCmHandles.addAll(
91 convertCmHandlesDataNodesToYangModelCmHandles(
92 Collections.singletonList(unsynchronizedCmHandle)));
96 Collections.shuffle(yangModelCmHandles);
98 return yangModelCmHandles;
102 * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
104 * @return a random LOCKED yang model cm handle, return null if not found
106 public List<YangModelCmHandle> getCmHandlesThatFailedModelSyncOrUpgrade() {
107 final List<DataNode> lockedCmHandlesAsDataNodeList
108 = cmHandleQueries.queryCmHandleAncestorsByCpsPath(
109 "//lock-reason[@reason=\"MODULE_SYNC_FAILED\" or @reason=\"MODULE_UPGRADE\"]",
110 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
111 return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList);
115 * Update Composite State attempts counter and set new lock reason and details.
117 * @param lockReasonCategory lock reason category
118 * @param errorMessage error message
120 public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
121 final LockReasonCategory lockReasonCategory,
122 final String errorMessage) {
124 if (compositeState.getLockReason() != null) {
125 final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
126 if (matcher.find()) {
127 attempt = 1 + Integer.parseInt(matcher.group(1));
130 compositeState.setLockReason(CompositeState.LockReason.builder()
131 .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
132 .lockReasonCategory(lockReasonCategory).build());
137 * Check if a module sync retry is needed.
139 * @param compositeState the composite state currently in the locked state
140 * @return if the retry mechanism should be attempted
142 public boolean needsModuleSyncRetryOrUpgrade(final CompositeState compositeState) {
143 final OffsetDateTime time = OffsetDateTime.parse(compositeState.getLastUpdateTime(),
144 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
145 final CompositeState.LockReason lockReason = compositeState.getLockReason();
147 final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
148 == lockReason.getLockReasonCategory();
149 final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE
150 == lockReason.getLockReasonCategory();
152 if (failedDuringModuleSync) {
153 final int timeInMinutesUntilNextAttempt;
154 final Matcher matcher = retryAttemptPattern.matcher(lockReason.getDetails());
155 if (matcher.find()) {
156 timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(matcher.group(1)));
158 timeInMinutesUntilNextAttempt = 1;
159 log.info("First Attempt: no current attempts found.");
161 final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
162 if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
163 log.info("Time until next attempt is {} minutes: ",
164 timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
167 log.info("Retry due now");
169 } else if (moduleUpgrade) {
170 log.info("Locked for module upgrade.");
173 log.info("Locked for other reason");
178 * Get the Resourece Data from Node through DMI Passthrough service.
180 * @param cmHandleId cm handle id
181 * @return optional string containing the resource data
183 public String getResourceData(final String cmHandleId) {
184 final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
185 PASSTHROUGH_OPERATIONAL.getDatastoreName(),
187 UUID.randomUUID().toString());
188 if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
189 return getFirstResource(resourceDataResponseEntity.getBody());
194 private String getFirstResource(final Object responseBody) {
195 final String jsonObjectAsString = jsonObjectMapper.asJsonString(responseBody);
196 final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString);
197 final Iterator<Map.Entry<String, JsonNode>> overallJsonTreeMap = overallJsonNode.fields();
198 final Map.Entry<String, JsonNode> firstElement = overallJsonTreeMap.next();
199 return jsonObjectMapper.asJsonString(Map.of(firstElement.getKey(), firstElement.getValue()));
202 private static List<YangModelCmHandle> convertCmHandlesDataNodesToYangModelCmHandles(
203 final List<DataNode> cmHandlesAsDataNodeList) {
204 return cmHandlesAsDataNodeList.stream()
205 .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
206 cmHandle.getLeaves().get("id").toString())).toList();