Fix sonar code smells
[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  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory.sync;
22
23 import java.security.SecureRandom;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
30 import org.onap.cps.ncmp.api.inventory.CmHandleState;
31 import org.onap.cps.ncmp.api.inventory.CompositeState;
32 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
33 import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
34 import org.onap.cps.spi.model.DataNode;
35 import org.springframework.stereotype.Component;
36
37 @Slf4j
38 @Component
39 @RequiredArgsConstructor
40 public class SyncUtils {
41
42     private static final SecureRandom secureRandom = new SecureRandom();
43
44
45     private final InventoryPersistence inventoryPersistence;
46
47     private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:");
48
49     /**
50      * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
51      *
52      * @return a random yang model cm handle with an ADVISED state, return null if not found
53      */
54     public YangModelCmHandle getAnAdvisedCmHandle() {
55         final List<DataNode> advisedCmHandles = inventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED);
56         if (advisedCmHandles.isEmpty()) {
57             return null;
58         }
59         final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size());
60         final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves()
61             .get("id").toString();
62         return inventoryPersistence.getYangModelCmHandle(cmHandleId);
63     }
64
65
66     /**
67      * Update Composite State attempts counter and set new lock reason and details.
68      *
69      * @param lockReasonCategory lock reason category
70      * @param errorMessage       error message
71      */
72     public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
73                                                    final LockReasonCategory lockReasonCategory,
74                                                    final String errorMessage) {
75         int attempt = 1;
76         if (compositeState.getLockReason() != null) {
77             final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
78             if (matcher.find()) {
79                 attempt = 1 + Integer.parseInt(matcher.group(1));
80             }
81         }
82         compositeState.setLockReason(CompositeState.LockReason.builder()
83             .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
84             .lockReasonCategory(lockReasonCategory).build());
85     }
86
87
88 }