Update postman collection to utilize newest yang files
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / AlternateIdChecker.java
1 /*
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
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.impl.utils;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Set;
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;
35
36 @Service
37 @Slf4j
38 @RequiredArgsConstructor
39 public class AlternateIdChecker {
40
41     private final InventoryPersistence inventoryPersistence;
42
43     private static final String NO_CURRENT_ALTERNATE_ID = "";
44
45     /**
46      * Check if the alternate can be applied to the given cm handle (id).
47      * Conditions:
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)
51      *
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
55      */
56     public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) {
57         String currentAlternateId = "";
58         try {
59             final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
60             currentAlternateId = yangModelCmHandle.getAlternateId();
61         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
62             // work with blank current alternate id
63         }
64         return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId);
65     }
66
67     /**
68      * Check if the alternate can be applied to the given cm handle.
69      * Conditions:
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)
73      *
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
78      */
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);
86                 return false;
87             }
88             return true;
89         }
90         if (currentAlternateId.equals(proposedAlternateId)) {
91             return true;
92         }
93         log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}",
94             cmHandleId, currentAlternateId);
95         return false;
96     }
97
98     /**
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
102      *
103      * @param newNcmpServiceCmHandles the proposed new cm handles
104      * @return collection of cm handles ids which are acceptable
105      */
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)) {
115                 isAcceptable = true;
116             } else {
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);
121                 } else {
122                     isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
123                 }
124             }
125             if (isAcceptable) {
126                 acceptedAlternateIds.add(proposedAlternateId);
127             } else {
128                 rejectedCmHandleIds.add(cmHandleId);
129             }
130         }
131         return rejectedCmHandleIds;
132     }
133
134     private boolean alternateIdAlreadyInDb(final String alternateId) {
135         try {
136             inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId);
137         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
138             return false;
139         }
140         return true;
141     }
142
143 }