2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Copyright (C) 2022 Nordix Foundation
5 * Modifications Copyright (C) 2023 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi;
24 import com.google.common.base.Strings;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.spi.exceptions.DataValidationException;
30 @RequiredArgsConstructor
31 public class FetchDescendantsOption {
33 public static final FetchDescendantsOption FETCH_DIRECT_CHILDREN_ONLY = new FetchDescendantsOption(1);
34 public static final FetchDescendantsOption OMIT_DESCENDANTS = new FetchDescendantsOption(0);
35 public static final FetchDescendantsOption INCLUDE_ALL_DESCENDANTS = new FetchDescendantsOption(-1);
37 private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN =
38 Pattern.compile("^$|^all$|^none$|^[0-9]+$|^-1$");
40 private final int depth;
45 * @return true if next level of depth is available
46 * @throws IllegalArgumentException when depth less than -1
48 public boolean hasNext() {
50 return depth > 0 || this.depth == INCLUDE_ALL_DESCENDANTS.depth;
54 * Next fetch descendants option.
56 * @return the next fetch descendants option
57 * @throws IllegalArgumentException when depth less than -1 or 0
59 public FetchDescendantsOption next() {
61 throw new IllegalArgumentException("Do not use next() method with zero depth");
63 final FetchDescendantsOption nextDescendantsOption = this.depth == INCLUDE_ALL_DESCENDANTS.depth
64 ? INCLUDE_ALL_DESCENDANTS : new FetchDescendantsOption(depth - 1);
65 validateDepth(nextDescendantsOption.depth);
66 return nextDescendantsOption;
70 * get fetch descendants option for given descendant.
72 * @param fetchDescendantsOptionAsString fetch descendants option string
73 * @return fetch descendants option for given descendant
75 public static FetchDescendantsOption getFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
76 validateFetchDescendantsOption(fetchDescendantsOptionAsString);
77 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)
78 || "0".equals(fetchDescendantsOptionAsString) || "none".equals(fetchDescendantsOptionAsString)) {
79 return FetchDescendantsOption.OMIT_DESCENDANTS;
80 } else if ("-1".equals(fetchDescendantsOptionAsString) || "all".equals(fetchDescendantsOptionAsString)) {
81 return FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
83 final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString);
84 return new FetchDescendantsOption(depth);
88 private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
89 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) {
92 final Matcher matcher = FETCH_DESCENDANTS_OPTION_PATTERN.matcher(fetchDescendantsOptionAsString);
93 if (!matcher.matches()) {
94 throw new DataValidationException("FetchDescendantsOption validation error.",
95 fetchDescendantsOptionAsString + " is not valid fetch descendants option");
99 private static void validateDepth(final int depth) {
101 throw new IllegalArgumentException("A depth of less than minus one is not allowed");