Retry Module-Sync based on from last failure
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / SyncUtils.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.inventory.sync;
23
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.google.common.collect.ImmutableMap;
26 import java.security.SecureRandom;
27 import java.time.Duration;
28 import java.time.OffsetDateTime;
29 import java.time.format.DateTimeFormatter;
30 import java.util.Collections;
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 java.util.stream.Collectors;
38 import lombok.RequiredArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
41 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
42 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
43 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
44 import org.onap.cps.ncmp.api.inventory.CmHandleState;
45 import org.onap.cps.ncmp.api.inventory.CompositeState;
46 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
47 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
48 import org.onap.cps.ncmp.api.inventory.SyncState;
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 SyncUtils {
59
60     private static final SecureRandom secureRandom = new SecureRandom();
61
62     private final InventoryPersistence inventoryPersistence;
63
64     private final DmiDataOperations dmiDataOperations;
65
66     private final JsonObjectMapper jsonObjectMapper;
67
68     private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:");
69
70     /**
71      * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
72      *
73      * @return a random yang model cm handle with an ADVISED state, return null if not found
74      */
75     public YangModelCmHandle getAnAdvisedCmHandle() {
76         final List<DataNode> advisedCmHandles = inventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED);
77         if (advisedCmHandles.isEmpty()) {
78             return null;
79         }
80         final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size());
81         final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves()
82             .get("id").toString();
83         return inventoryPersistence.getYangModelCmHandle(cmHandleId);
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 random yang model cm handle with State in READY and Operation Sync State in "UNSYNCHRONIZED",
91      *         return null if not found
92      */
93     public YangModelCmHandle getAnUnSynchronizedReadyCmHandle() {
94         final List<DataNode> unSynchronizedCmHandles = inventoryPersistence
95                 .getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED);
96         if (unSynchronizedCmHandles.isEmpty()) {
97             return null;
98         }
99         Collections.shuffle(unSynchronizedCmHandles);
100         for (final DataNode cmHandle : unSynchronizedCmHandles) {
101             final String cmHandleId = cmHandle.getLeaves().get("id").toString();
102             final List<DataNode> readyCmHandles = inventoryPersistence
103                     .getCmHandlesByIdAndState(cmHandleId, CmHandleState.READY);
104             if (!readyCmHandles.isEmpty()) {
105                 return inventoryPersistence.getYangModelCmHandle(cmHandleId);
106             }
107         }
108         return null;
109     }
110
111     /**
112      * Query data nodes for cm handles with an "LOCKED" cm handle state with reason LOCKED_MISBEHAVING".
113      *
114      * @return a random yang model cm handle with an ADVISED state, return null if not found
115      */
116     public List<YangModelCmHandle> getLockedMisbehavingYangModelCmHandles() {
117         final List<DataNode> lockedCmHandleAsDataNodeList = inventoryPersistence.getCmHandleDataNodesByCpsPath(
118             "//lock-reason[@reason=\"LOCKED_MISBEHAVING\"]/ancestor::cm-handles",
119             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
120         return lockedCmHandleAsDataNodeList.stream()
121             .map(cmHandle -> YangDataConverter.convertCmHandleToYangModel(cmHandle,
122                 cmHandle.getLeaves().get("id").toString())).collect(Collectors.toList());
123     }
124
125     /**
126      * Update Composite State attempts counter and set new lock reason and details.
127      *
128      * @param lockReasonCategory lock reason category
129      * @param errorMessage       error message
130      */
131     public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
132                                                    final LockReasonCategory lockReasonCategory,
133                                                    final String errorMessage) {
134         int attempt = 1;
135         if (compositeState.getLockReason() != null) {
136             final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
137             if (matcher.find()) {
138                 attempt = 1 + Integer.parseInt(matcher.group(1));
139             }
140         }
141         compositeState.setLockReason(CompositeState.LockReason.builder()
142             .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
143             .lockReasonCategory(lockReasonCategory).build());
144     }
145
146
147     /**
148      * Check if the retry mechanism should attempt to unlock the cm handle based on the last update time.
149      *
150      * @param compositeState the composite state currently in the locked state
151      * @return if the retry mechanism should be attempted
152      */
153     public boolean isReadyForRetry(final CompositeState compositeState) {
154         int timeUntilNextAttempt = 1;
155         final OffsetDateTime time =
156             OffsetDateTime.parse(compositeState.getLastUpdateTime(),
157                 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
158         final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
159         if (matcher.find()) {
160             timeUntilNextAttempt = (int) Math.pow(2, Integer.parseInt(matcher.group(1)));
161         } else {
162             log.debug("First Attempt: no current attempts found.");
163         }
164         final int timeSinceLastAttempt = (int) Duration.between(time, OffsetDateTime.now()).toMinutes();
165         return timeSinceLastAttempt > timeUntilNextAttempt;
166     }
167
168     /**
169      * Get the Resourece Data from Node through DMI Passthrough service.
170      *
171      * @param cmHandleId cm handle id
172      * @return optional string containing the resource data
173      */
174     public String getResourceData(final String cmHandleId) {
175         final ResponseEntity<Object> resourceDataResponseEntity = dmiDataOperations.getResourceDataFromDmi(
176                 cmHandleId, DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
177                 UUID.randomUUID().toString());
178         if (resourceDataResponseEntity.getStatusCode().is2xxSuccessful()) {
179             return getFirstResource(resourceDataResponseEntity.getBody());
180         }
181         return null;
182     }
183
184     private String getFirstResource(final Object responseBody) {
185         final String jsonObjectAsString = jsonObjectMapper.asJsonString(responseBody);
186         final JsonNode overallJsonNode = jsonObjectMapper.convertToJsonNode(jsonObjectAsString);
187         final Iterator<Map.Entry<String, JsonNode>> overallJsonTreeMap = overallJsonNode.fields();
188         final Map.Entry<String, JsonNode> firstElement = overallJsonTreeMap.next();
189         return jsonObjectMapper.asJsonString(ImmutableMap.of(firstElement.getKey(), firstElement.getValue()));
190     }
191 }