2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Copyright (C) 2022-2023 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 DIRECT_CHILDREN_ONLY
34 = new FetchDescendantsOption(1, "DirectChildrenOnly");
35 public static final FetchDescendantsOption OMIT_DESCENDANTS
36 = new FetchDescendantsOption(0, "OmitDescendants");
37 public static final FetchDescendantsOption INCLUDE_ALL_DESCENDANTS
38 = new FetchDescendantsOption(-1, "IncludeAllDescendants");
40 FetchDescendantsOption(final int depth) {
41 this(depth, "Depth=" + depth);
44 private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN =
45 Pattern.compile("^$|^all$|^none$|^[0-9]+$|^-1$");
47 private final int depth;
49 private final String optionName;
54 * @return true if next level of depth is available
55 * @throws IllegalArgumentException when depth less than -1
57 public boolean hasNext() {
59 return depth > 0 || this.depth == INCLUDE_ALL_DESCENDANTS.depth;
63 * Next fetch descendants option.
65 * @return the next fetch descendants option
66 * @throws IllegalArgumentException when depth less than -1 or 0
68 public FetchDescendantsOption next() {
70 throw new IllegalArgumentException("Do not use next() method with zero depth");
72 final FetchDescendantsOption nextDescendantsOption = this.depth == INCLUDE_ALL_DESCENDANTS.depth
73 ? INCLUDE_ALL_DESCENDANTS : new FetchDescendantsOption(depth - 1);
74 validateDepth(nextDescendantsOption.depth);
75 return nextDescendantsOption;
79 * get fetch descendants option for given descendant.
81 * @param fetchDescendantsOptionAsString fetch descendants option string
82 * @return fetch descendants option for given descendant
84 public static FetchDescendantsOption getFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
85 validateFetchDescendantsOption(fetchDescendantsOptionAsString);
86 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)
87 || "0".equals(fetchDescendantsOptionAsString) || "none".equals(fetchDescendantsOptionAsString)) {
88 return FetchDescendantsOption.OMIT_DESCENDANTS;
89 } else if ("-1".equals(fetchDescendantsOptionAsString) || "all".equals(fetchDescendantsOptionAsString)) {
90 return FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
92 final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString);
93 return new FetchDescendantsOption(depth);
98 public String toString() {
102 private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
103 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) {
106 final Matcher matcher = FETCH_DESCENDANTS_OPTION_PATTERN.matcher(fetchDescendantsOptionAsString);
107 if (!matcher.matches()) {
108 throw new DataValidationException("FetchDescendantsOption validation error.",
109 fetchDescendantsOptionAsString + " is not valid fetch descendants option");
113 private static void validateDepth(final int depth) {
115 throw new IllegalArgumentException("A depth of less than minus one is not allowed");