Merge "Add withTrustLevel condition to CmHandle Query API"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / sync / SyncUtils.java
1 /*
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
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.Iterator;
33 import java.util.List;
34 import java.util.Map;
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;
53
54 @Slf4j
55 @Service
56 @RequiredArgsConstructor
57 public class SyncUtils {
58
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:");
63
64     /**
65      * Query data nodes for cm handles with an "ADVISED" cm handle state.
66      *
67      * @return cm handles (data nodes) in ADVISED state (empty list if none found)
68      */
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;
73     }
74
75     /**
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".
78      *
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
81      */
82     public List<YangModelCmHandle> getUnsynchronizedReadyCmHandles() {
83         final List<DataNode> unsynchronizedCmHandles = cmHandleQueries
84                 .queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED);
85
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)));
93             }
94         }
95
96         Collections.shuffle(yangModelCmHandles);
97
98         return yangModelCmHandles;
99     }
100
101     /**
102      * Query data nodes for cm handles with an "LOCKED" cm handle state with reason.
103      *
104      * @return a random LOCKED yang model cm handle, return null if not found
105      */
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);
112     }
113
114     /**
115      * Update Composite State attempts counter and set new lock reason and details.
116      *
117      * @param lockReasonCategory lock reason category
118      * @param errorMessage       error message
119      */
120     public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
121                                                    final LockReasonCategory lockReasonCategory,
122                                                    final String errorMessage) {
123         int attempt = 1;
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));
128             }
129         }
130         compositeState.setLockReason(CompositeState.LockReason.builder()
131                 .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
132                 .lockReasonCategory(lockReasonCategory).build());
133     }
134
135
136     /**
137      * Check if a module sync retry is needed.
138      *
139      * @param compositeState the composite state currently in the locked state
140      * @return if the retry mechanism should be attempted
141      */
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();
146
147         final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED
148                 == lockReason.getLockReasonCategory();
149         final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE
150                 == lockReason.getLockReasonCategory();
151
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)));
157             } else {
158                 timeInMinutesUntilNextAttempt = 1;
159                 log.info("First Attempt: no current attempts found.");
160             }
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);
165                 return false;
166             }
167             log.info("Retry due now");
168             return true;
169         } else if (moduleUpgrade) {
170             log.info("Locked for module upgrade.");
171             return true;
172         }
173         log.info("Locked for other reason");
174         return false;
175     }
176
177     /**
178      * Get the Resourece Data from Node through DMI Passthrough service.
179      *
180      * @param cmHandleId cm handle id
181      * @return optional string containing the resource data
182      */
183     public String getResourceData(final String cmHandleId) {
184         final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
185                 PASSTHROUGH_OPERATIONAL.getDatastoreName(),
186                 cmHandleId,
187                 UUID.randomUUID().toString());
188         if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
189             return getFirstResource(resourceDataResponseEntity.getBody());
190         }
191         return null;
192     }
193
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()));
200     }
201
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();
207     }
208 }