Fix compile issue after facade introduction
[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 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;
33 import java.util.Map;
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;
52
53 @Slf4j
54 @Service
55 @RequiredArgsConstructor
56 public class ModuleOperationsUtils {
57
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"]""";
72
73     /**
74      * Query data nodes for cm handles with an "ADVISED" cm handle state.
75      *
76      * @return cm handles (data nodes) in ADVISED state (empty list if none found)
77      */
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;
83     }
84
85     /**
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".
88      *
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
91      */
92     public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
93         final List<DataNode> unsynchronizedCmHandles = cmHandleQueryService
94                 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
95
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)));
102             }
103         }
104         Collections.shuffle(yangModelCmHandles);
105         return yangModelCmHandles;
106     }
107
108     /**
109      * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
110      *
111      * @return a random LOCKED yang model cm handle, return null if not found
112      */
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);
118     }
119
120     /**
121      * Update Composite State attempts counter and set new lock reason and details.
122      *
123      * @param lockReasonCategory lock reason category
124      * @param errorMessage       error message
125      */
126     public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
127                                                    final LockReasonCategory lockReasonCategory,
128                                                    final String errorMessage) {
129         int attempt = 1;
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));
134         }
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)
139                 .build());
140     }
141
142     /**
143      * Extract lock reason details as key-value pair.
144      *
145      * @param compositeStateLockReason lock reason having all the details
146      * @return a map of lock reason details
147      */
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));
157             }
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));
162             }
163             return compositeStateDetails;
164         }
165         return Collections.emptyMap();
166     }
167
168
169     /**
170      * Check if a module sync retry is needed.
171      *
172      * @param compositeState the composite state currently in the locked state
173      * @return if the retry mechanism should be attempted
174      */
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();
179
180         final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE == lockReason.getLockReasonCategory();
181         if (moduleUpgrade) {
182             log.info("Locked for module upgrade");
183             return true;
184         }
185
186         final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
187                 == lockReason.getLockReasonCategory();
188         final boolean failedDuringModuleUpgrade = LockReasonCategory.MODULE_UPGRADE_FAILED
189                 == lockReason.getLockReasonCategory();
190
191         if (failedDuringModuleSync || failedDuringModuleUpgrade) {
192             log.info("Locked for module {} (last attempt failed).", failedDuringModuleSync ? "sync" : "upgrade");
193             return isRetryDue(lockReason, time);
194         }
195         log.info("Locked for other reason");
196         return false;
197     }
198
199     /**
200      * Get the Resource Data from Node through DMI Passthrough service.
201      *
202      * @param cmHandleId cm handle id
203      * @return optional string containing the resource data
204      */
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());
210         }
211         return null;
212     }
213
214     /**
215      * Checks if cm handle state module is in upgrade or upgrade failed.
216      *
217      * @param compositeState current lock reason of  cm handle
218      * @return true or false based on lock reason category
219      */
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()));
225     }
226
227     public static String getUpgradedModuleSetTagFromLockReason(final CompositeState.LockReason lockReason) {
228         return getLockedCompositeStateDetails(lockReason).getOrDefault(MODULE_SET_TAG_KEY, "");
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().map(YangDataConverter::convertCmHandleToYangModel).toList();
242     }
243
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.");
250         } else {
251             timeInMinutesUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(compositeStateDetails
252                     .get(RETRY_ATTEMPT_KEY)));
253         }
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);
257             return false;
258         }
259         log.info("Retry due now");
260         return true;
261     }
262 }