aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/AlternateIdChecker.java
blob: f14439f6904526a4a86efb87526af87d0497ae1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * ============LICENSE_START========================================================
 * Copyright (c) 2024 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an 'AS IS' BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.ncmp.api.impl.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class AlternateIdChecker {

    public enum Operation {
        CREATE, UPDATE
    }

    private final InventoryPersistence inventoryPersistence;

    private static final String NO_CURRENT_ALTERNATE_ID = "";

    /**
     * Check if the alternate can be applied to the given cm handle (id).
     * Conditions:
     * - proposed alternate is blank (it wil be ignored)
     * - proposed alternate is same as current (no change)
     * - proposed alternate is not in use for a different cm handle (in the DB)
     *
     * @param cmHandleId cm handle id
     * @param proposedAlternateId proposed alternate id
     * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
     */
    public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) {
        String currentAlternateId = "";
        try {
            final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
            currentAlternateId = yangModelCmHandle.getAlternateId();
        } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
            // work with blank current alternate id
        }
        return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId);
    }

    /**
     * Check if the alternate can be applied to the given cm handle.
     * Conditions:
     * - proposed alternate is blank (it wil be ignored)
     * - proposed alternate is same as current (no change)
     * - proposed alternate is not in use for a different cm handle (in the DB)
     *
     * @param cmHandleId   cm handle id
     * @param currentAlternateId current alternate id
     * @param proposedAlternateId new alternate id
     * @return true if the new alternate id not in use or equal to current alternate id, false otherwise
     */
    public boolean canApplyAlternateId(final String cmHandleId,
                                       final String currentAlternateId,
                                       final String proposedAlternateId) {
        if (StringUtils.isBlank(currentAlternateId)) {
            if (alternateIdAlreadyInDb(proposedAlternateId)) {
                log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
                    + "assigned to a different cm handle", cmHandleId);
                return false;
            }
            return true;
        }
        if (currentAlternateId.equals(proposedAlternateId)) {
            return true;
        }
        log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}",
            cmHandleId, currentAlternateId);
        return false;
    }

    /**
     * Check all alternate ids of a batch of cm handles.
     * Includes cross-checks in the batch itself for duplicates. Only the first entry encountered wil be accepted.
     *
     * @param newNcmpServiceCmHandles the proposed new cm handles
     * @param operation type of operation being executed
     * @return collection of cm handles ids which are acceptable
     */
    public Collection<String> getIdsOfCmHandlesWithRejectedAlternateId(
                                    final Collection<NcmpServiceCmHandle> newNcmpServiceCmHandles,
                                    final Operation operation) {
        final Set<String> acceptedAlternateIds = new HashSet<>(newNcmpServiceCmHandles.size());
        final Collection<String> rejectedCmHandleIds = new ArrayList<>();
        for (final NcmpServiceCmHandle ncmpServiceCmHandle : newNcmpServiceCmHandles) {
            final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
            final String proposedAlternateId = ncmpServiceCmHandle.getAlternateId();
            final boolean isAcceptable;
            if (StringUtils.isEmpty(proposedAlternateId)) {
                isAcceptable = true;
            } else {
                if (acceptedAlternateIds.contains(proposedAlternateId)) {
                    isAcceptable = false;
                    log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
                        + "assigned to a different cm handle (in this batch)", cmHandleId);
                } else {
                    if (Operation.CREATE.equals(operation)) {
                        isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
                    } else {
                        isAcceptable = canApplyAlternateId(cmHandleId, proposedAlternateId);
                    }
                }
            }
            if (isAcceptable) {
                acceptedAlternateIds.add(proposedAlternateId);
            } else {
                rejectedCmHandleIds.add(cmHandleId);
            }
        }
        return rejectedCmHandleIds;
    }

    private boolean alternateIdAlreadyInDb(final String alternateId) {
        try {
            inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId);
        } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
            return false;
        }
        return true;
    }

}