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 private final InventoryPersistence inventoryPersistence;
43 private static final String NO_CURRENT_ALTERNATE_ID = "";
46 * Check if the alternate can be applied to the given cm handle (id).
48 * - proposed alternate is blank (it wil be ignored)
49 * - proposed alternate is same as current (no change)
50 * - proposed alternate is not in use for a different cm handle (in the DB)
52 * @param cmHandleId cm handle id
53 * @param proposedAlternateId proposed alternate id
54 * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
56 public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) {
57 String currentAlternateId = "";
59 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
60 currentAlternateId = yangModelCmHandle.getAlternateId();
61 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
62 // work with blank current alternate id
64 return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId);
68 * Check if the alternate can be applied to the given cm handle.
70 * - proposed alternate is blank (it wil be ignored)
71 * - proposed alternate is same as current (no change)
72 * - proposed alternate is not in use for a different cm handle (in the DB)
74 * @param cmHandleId cm handle id
75 * @param currentAlternateId current alternate id
76 * @param proposedAlternateId new alternate id
77 * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
79 public boolean canApplyAlternateId(final String cmHandleId,
80 final String currentAlternateId,
81 final String proposedAlternateId) {
82 if (StringUtils.isBlank(currentAlternateId)) {
83 if (alternateIdAlreadyInDb(proposedAlternateId)) {
84 log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
85 + "assigned to a different cm handle", cmHandleId);
90 if (currentAlternateId.equals(proposedAlternateId)) {
93 log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}",
94 cmHandleId, currentAlternateId);
99 * Check all alternate ids of a batch of NEW cm handles.
100 * Includes cross-checks in the batch itself for duplicates. Only the first entry encountered wil be accepted.
101 * This method can only be used for NEW cm handle registrations NOT for updating existing ones
103 * @param newNcmpServiceCmHandles the proposed new cm handles
104 * @return collection of cm handles ids which are acceptable
106 public Collection<String> getIdsOfCmHandlesWithRejectedAlternateId(
107 final Collection<NcmpServiceCmHandle> newNcmpServiceCmHandles) {
108 final Set<String> acceptedAlternateIds = new HashSet<>(newNcmpServiceCmHandles.size());
109 final Collection<String> rejectedCmHandleIds = new ArrayList<>();
110 for (final NcmpServiceCmHandle ncmpServiceCmHandle : newNcmpServiceCmHandles) {
111 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
112 final String proposedAlternateId = ncmpServiceCmHandle.getAlternateId();
113 final boolean isAcceptable;
114 if (StringUtils.isEmpty(proposedAlternateId)) {
117 if (acceptedAlternateIds.contains(proposedAlternateId)) {
118 isAcceptable = false;
119 log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
120 + "assigned to a different cm handle (in this batch)", cmHandleId);
122 isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
126 acceptedAlternateIds.add(proposedAlternateId);
128 rejectedCmHandleIds.add(cmHandleId);
131 return rejectedCmHandleIds;
134 private boolean alternateIdAlreadyInDb(final String alternateId) {
136 inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId);
137 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {