Merge "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     public enum Operation {
42         CREATE, UPDATE
43     }
44
45     private final InventoryPersistence inventoryPersistence;
46
47     private static final String NO_CURRENT_ALTERNATE_ID = "";
48
49     /**
50      * Check if the alternate can be applied to the given cm handle (id).
51      * Conditions:
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)
55      *
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
59      */
60     public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) {
61         String currentAlternateId = "";
62         try {
63             final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
64             currentAlternateId = yangModelCmHandle.getAlternateId();
65         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
66             // work with blank current alternate id
67         }
68         return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId);
69     }
70
71     /**
72      * Check if the alternate can be applied to the given cm handle.
73      * Conditions:
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)
77      *
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
82      */
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);
90                 return false;
91             }
92             return true;
93         }
94         if (currentAlternateId.equals(proposedAlternateId)) {
95             return true;
96         }
97         log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}",
98             cmHandleId, currentAlternateId);
99         return false;
100     }
101
102     /**
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.
105      *
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
109      */
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             final boolean isAcceptable;
119             if (StringUtils.isEmpty(proposedAlternateId)) {
120                 isAcceptable = true;
121             } else {
122                 if (acceptedAlternateIds.contains(proposedAlternateId)) {
123                     isAcceptable = false;
124                     log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
125                         + "assigned to a different cm handle (in this batch)", cmHandleId);
126                 } else {
127                     if (Operation.CREATE.equals(operation)) {
128                         isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
129                     } else {
130                         isAcceptable = canApplyAlternateId(cmHandleId, proposedAlternateId);
131                     }
132                 }
133             }
134             if (isAcceptable) {
135                 acceptedAlternateIds.add(proposedAlternateId);
136             } else {
137                 rejectedCmHandleIds.add(cmHandleId);
138             }
139         }
140         return rejectedCmHandleIds;
141     }
142
143     private boolean alternateIdAlreadyInDb(final String alternateId) {
144         try {
145             inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId);
146         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
147             return false;
148         }
149         return true;
150     }
151
152 }