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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory.sync;
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;
39 @RequiredArgsConstructor
40 public class SyncUtils {
42 private static final SecureRandom secureRandom = new SecureRandom();
45 private final InventoryPersistence inventoryPersistence;
47 private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:");
50 * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
52 * @return a random yang model cm handle with an ADVISED state, return null if not found
54 public YangModelCmHandle getAnAdvisedCmHandle() {
55 final List<DataNode> advisedCmHandles = inventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED);
56 if (advisedCmHandles.isEmpty()) {
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);
67 * Update Composite State attempts counter and set new lock reason and details.
69 * @param lockReasonCategory lock reason category
70 * @param errorMessage error message
72 public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState,
73 final LockReasonCategory lockReasonCategory,
74 final String errorMessage) {
76 if (compositeState.getLockReason() != null) {
77 final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails());
79 attempt = 1 + Integer.parseInt(matcher.group(1));
82 compositeState.setLockReason(CompositeState.LockReason.builder()
83 .details(String.format("Attempt #%d failed: %s", attempt, errorMessage))
84 .lockReasonCategory(lockReasonCategory).build());