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