Merge "Add integration test for extending API: Get Module Definitions"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / sync / ModuleOperationsUtils.java
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.inventory.sync;
23
24 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL;
25
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;
35 import java.util.Map;
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.apache.commons.lang3.StringUtils;
42 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
43 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
44 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
45 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
46 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory;
47 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
48 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
49 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
50 import org.onap.cps.spi.FetchDescendantsOption;
51 import org.onap.cps.spi.model.DataNode;
52 import org.onap.cps.utils.JsonObjectMapper;
53 import org.springframework.http.ResponseEntity;
54 import org.springframework.stereotype.Service;
55
56 @Slf4j
57 @Service
58 @RequiredArgsConstructor
59 public class ModuleOperationsUtils {
60
61     private final CmHandleQueries cmHandleQueries;
62     private final DmiDataOperations dmiDataOperations;
63     private final JsonObjectMapper jsonObjectMapper;
64     private static final String RETRY_ATTEMPT_KEY = "attempt";
65     public static final String MODULE_SET_TAG_KEY = "moduleSetTag";
66     public static final String MODULE_SET_TAG_MESSAGE_FORMAT = "Upgrade to ModuleSetTag: {0}";
67     private static final String UPGRADE_FORMAT = "Upgrade to ModuleSetTag: %s";
68     private static final String LOCK_REASON_DETAILS_MSG_FORMAT = UPGRADE_FORMAT + " Attempt #%d failed: %s";
69     private static final Pattern retryAttemptPattern = Pattern.compile("Attempt #(\\d+) failed:.+");
70     private static final Pattern moduleSetTagPattern = Pattern.compile("Upgrade to ModuleSetTag: (\\S+)");
71     private static final String CPS_PATH_CM_HANDLES_MODEL_SYNC_FAILED_OR_UPGRADE = """
72             //lock-reason[@reason="MODULE_SYNC_FAILED"
73             or @reason="MODULE_UPGRADE"
74             or @reason="MODULE_UPGRADE_FAILED"]""";
75
76     /**
77      * Query data nodes for cm handles with an "ADVISED" cm handle state.
78      *
79      * @return cm handles (data nodes) in ADVISED state (empty list if none found)
80      */
81     public List<DataNode> getAdvisedCmHandles() {
82         final List<DataNode> advisedCmHandlesAsDataNodes = cmHandleQueries.queryCmHandlesByState(CmHandleState.ADVISED);
83         log.debug("Total number of fetched advised cm handle(s) is (are) {}", advisedCmHandlesAsDataNodes.size());
84         return advisedCmHandlesAsDataNodes;
85     }
86
87     /**
88      * First query data nodes for cm handles with CM Handle Operational Sync State in "UNSYNCHRONIZED" and
89      * randomly select a CM Handle and query the data nodes for CM Handle State in "READY".
90      *
91      * @return a randomized yang model cm handle list with State in READY and Operation Sync State in "UNSYNCHRONIZED",
92      *         return empty list if not found
93      */
94     public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
95         final List<DataNode> unsynchronizedCmHandles = cmHandleQueries
96                 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
97
98         final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>();
99         for (final DataNode unsynchronizedCmHandle : unsynchronizedCmHandles) {
100             final String cmHandleId = unsynchronizedCmHandle.getLeaves().get("id").toString();
101             if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
102                 yangModelCmHandles.addAll(convertCmHandlesDataNodesToYangModelCmHandles(
103                                 Collections.singletonList(unsynchronizedCmHandle)));
104             }
105         }
106         Collections.shuffle(yangModelCmHandles);
107         return yangModelCmHandles;
108     }
109
110     /**
111      * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
112      *
113      * @return a random LOCKED yang model cm handle, return null if not found
114      */
115     public List<YangModelCmHandle> getCmHandlesThatFailedModelSyncOrUpgrade() {
116         final List<DataNode> lockedCmHandlesAsDataNodeList
117                 = cmHandleQueries.queryCmHandleAncestorsByCpsPath(CPS_PATH_CM_HANDLES_MODEL_SYNC_FAILED_OR_UPGRADE,
118                 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
119         return convertCmHandlesDataNodesToYangModelCmHandles(lockedCmHandlesAsDataNodeList);
120     }
121
122     /**
123      * Update Composite State attempts counter and set new lock reason and details.
124      *
125      * @param lockReasonCategory lock reason category
126      * @param errorMessage       error message
127      */
128     public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
129                                                    final LockReasonCategory lockReasonCategory,
130                                                    final String errorMessage) {
131         int attempt = 1;
132         final Map<String, String> compositeStateDetails
133                 = getLockedCompositeStateDetails(compositeState.getLockReason());
134         if (!compositeStateDetails.isEmpty() && compositeStateDetails.containsKey(RETRY_ATTEMPT_KEY)) {
135             attempt = 1 + Integer.parseInt(compositeStateDetails.get(RETRY_ATTEMPT_KEY));
136         }
137         final String moduleSetTag = compositeStateDetails.get(MODULE_SET_TAG_KEY);
138         compositeState.setLockReason(CompositeState.LockReason.builder()
139                 .details(String.format(LOCK_REASON_DETAILS_MSG_FORMAT, StringUtils.isNotBlank(moduleSetTag)
140                         ? moduleSetTag : "not-specified", attempt, errorMessage)).lockReasonCategory(lockReasonCategory)
141                 .build());
142     }
143
144     /**
145      * Extract lock reason details as key-value pair.
146      *
147      * @param compositeStateLockReason lock reason having all the details
148      * @return a map of lock reason details
149      */
150     public static Map<String, String> getLockedCompositeStateDetails(final CompositeState.LockReason
151                                                                              compositeStateLockReason) {
152         if (compositeStateLockReason != null) {
153             final Map<String, String> compositeStateDetails = new HashMap<>(2);
154             final String lockedCompositeStateReasonDetails = compositeStateLockReason.getDetails();
155             final Matcher retryAttemptMatcher = retryAttemptPattern.matcher(lockedCompositeStateReasonDetails);
156             if (retryAttemptMatcher.find()) {
157                 final int attemptsRegexGroupId = 1;
158                 compositeStateDetails.put(RETRY_ATTEMPT_KEY, retryAttemptMatcher.group(attemptsRegexGroupId));
159             }
160             final Matcher moduleSetTagMatcher = moduleSetTagPattern.matcher(lockedCompositeStateReasonDetails);
161             if (moduleSetTagMatcher.find()) {
162                 final int moduleSetTagRegexGroupId = 1;
163                 compositeStateDetails.put(MODULE_SET_TAG_KEY, moduleSetTagMatcher.group(moduleSetTagRegexGroupId));
164             }
165             return compositeStateDetails;
166         }
167         return Collections.emptyMap();
168     }
169
170
171     /**
172      * Check if a module sync retry is needed.
173      *
174      * @param compositeState the composite state currently in the locked state
175      * @return if the retry mechanism should be attempted
176      */
177     public boolean needsModuleSyncRetryOrUpgrade(final CompositeState compositeState) {
178         final OffsetDateTime time = OffsetDateTime.parse(compositeState.getLastUpdateTime(),
179                 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
180         final CompositeState.LockReason lockReason = compositeState.getLockReason();
181
182         final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE == lockReason.getLockReasonCategory();
183         if (moduleUpgrade) {
184             log.info("Locked for module upgrade");
185             return true;
186         }
187
188         final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
189                 == lockReason.getLockReasonCategory();
190         final boolean failedDuringModuleUpgrade = LockReasonCategory.MODULE_UPGRADE_FAILED
191                 == lockReason.getLockReasonCategory();
192
193         if (failedDuringModuleSync || failedDuringModuleUpgrade) {
194             log.info("Locked for module {} (last attempt failed).", failedDuringModuleSync ? "sync" : "upgrade");
195             return isRetryDue(lockReason, time);
196         }
197         log.info("Locked for other reason");
198         return false;
199     }
200
201     /**
202      * Get the Resourece Data from Node through DMI Passthrough service.
203      *
204      * @param cmHandleId cm handle id
205      * @return optional string containing the resource data
206      */
207     public String getResourceData(final String cmHandleId) {
208         final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
209                 PASSTHROUGH_OPERATIONAL.getDatastoreName(),
210                 cmHandleId,
211                 UUID.randomUUID().toString());
212         if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
213             return getFirstResource(resourceDataResponseEntity.getBody());
214         }
215         return null;
216     }
217
218     /**
219      * Checks if cm handle state module is in upgrade or upgrade failed.
220      *
221      * @param compositeState current lock reason of  cm handle
222      * @return true or false based on lock reason category
223      */
224     public static boolean isInUpgradeOrUpgradeFailed(final CompositeState compositeState) {
225         return compositeState.getLockReason() != null
226                 && (LockReasonCategory.MODULE_UPGRADE.equals(compositeState.getLockReason().getLockReasonCategory())
227                 || LockReasonCategory.MODULE_UPGRADE_FAILED.equals(compositeState.getLockReason()
228                 .getLockReasonCategory()));
229     }
230
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()));
237     }
238
239     private List<YangModelCmHandle> convertCmHandlesDataNodesToYangModelCmHandles(
240             final List<DataNode> cmHandlesAsDataNodeList) {
241         return cmHandlesAsDataNodeList.stream()
242                 .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
243                         cmHandle.getLeaves().get("id").toString())).toList();
244     }
245
246     private boolean isRetryDue(final CompositeState.LockReason compositeStateLockReason, final OffsetDateTime time) {
247         final int timeInMinutesUntilNextAttempt;
248         final Map<String, String> compositeStateDetails = getLockedCompositeStateDetails(compositeStateLockReason);
249         if (compositeStateDetails.isEmpty()) {
250             timeInMinutesUntilNextAttempt = 1;
251             log.info("First Attempt: no current attempts found.");
252         } else {
253             timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(compositeStateDetails
254                     .get(RETRY_ATTEMPT_KEY)));
255         }
256         final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
257         if (timeInMinutesUntilNextAttempt >= timeSinceLastAttempt) {
258             log.info("Time until next attempt is {} minutes: ", timeInMinutesUntilNextAttempt - timeSinceLastAttempt);
259             return false;
260         }
261         log.info("Retry due now");
262         return true;
263     }
264 }