2 * ============LICENSE_START========================================================
3 * Copyright (c) 2024 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.impl.utils;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
32 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
34 import org.springframework.stereotype.Service;
38 @RequiredArgsConstructor
39 public class AlternateIdChecker {
41 public enum Operation {
45 private final InventoryPersistence inventoryPersistence;
47 private static final String NO_CURRENT_ALTERNATE_ID = "";
50 * Check if the alternate can be applied to the given cm handle (id).
52 * - proposed alternate is blank (it wil be ignored)
53 * - proposed alternate is same as current (no change)
54 * - proposed alternate is not in use for a different cm handle (in the DB)
56 * @param cmHandleId cm handle id
57 * @param proposedAlternateId proposed alternate id
58 * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
60 public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) {
61 String currentAlternateId = "";
63 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
64 currentAlternateId = yangModelCmHandle.getAlternateId();
65 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
66 // work with blank current alternate id
68 return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId);
72 * Check if the alternate can be applied to the given cm handle.
74 * - proposed alternate is blank (it wil be ignored)
75 * - proposed alternate is same as current (no change)
76 * - proposed alternate is not in use for a different cm handle (in the DB)
78 * @param cmHandleId cm handle id
79 * @param currentAlternateId current alternate id
80 * @param proposedAlternateId new alternate id
81 * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
83 public boolean canApplyAlternateId(final String cmHandleId,
84 final String currentAlternateId,
85 final String proposedAlternateId) {
86 if (StringUtils.isBlank(currentAlternateId)) {
87 if (alternateIdAlreadyInDb(proposedAlternateId)) {
88 log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
89 + "assigned to a different cm handle", cmHandleId);
94 if (currentAlternateId.equals(proposedAlternateId)) {
97 log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}",
98 cmHandleId, currentAlternateId);
103 * Check all alternate ids of a batch of cm handles.
104 * Includes cross-checks in the batch itself for duplicates. Only the first entry encountered wil be accepted.
106 * @param newNcmpServiceCmHandles the proposed new cm handles
107 * @param operation type of operation being executed
108 * @return collection of cm handles ids which are acceptable
110 public Collection<String> getIdsOfCmHandlesWithRejectedAlternateId(
111 final Collection<NcmpServiceCmHandle> newNcmpServiceCmHandles,
112 final Operation operation) {
113 final Set<String> acceptedAlternateIds = new HashSet<>(newNcmpServiceCmHandles.size());
114 final Collection<String> rejectedCmHandleIds = new ArrayList<>();
115 for (final NcmpServiceCmHandle ncmpServiceCmHandle : newNcmpServiceCmHandles) {
116 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
117 final String proposedAlternateId = ncmpServiceCmHandle.getAlternateId();
118 if (isProposedAlternateIdAcceptable(proposedAlternateId, operation, acceptedAlternateIds, cmHandleId)) {
119 acceptedAlternateIds.add(proposedAlternateId);
121 rejectedCmHandleIds.add(cmHandleId);
124 return rejectedCmHandleIds;
127 private boolean isProposedAlternateIdAcceptable(final String proposedAlternateId, final Operation operation,
128 final Set<String> acceptedAlternateIds, final String cmHandleId) {
129 if (StringUtils.isEmpty(proposedAlternateId)) {
132 if (acceptedAlternateIds.contains(proposedAlternateId)) {
133 log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
134 + "assigned to a different cm handle (in this batch)", cmHandleId);
137 if (Operation.CREATE.equals(operation)) {
138 return canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
140 return canApplyAlternateId(cmHandleId, proposedAlternateId);
143 private boolean alternateIdAlreadyInDb(final String alternateId) {
145 inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId);
146 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {