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_CHILD_ONLY
34 = new FetchDescendantsOption(1, "DirectChildOnly");
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$|^direct$|^[0-9]+$|^-1$|^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;
80 * @return depth: -1 for all descendants, 0 for no descendants, or positive value for fixed level of descendants
82 public int getDepth() {
87 * get fetch descendants option for given descendant.
89 * @param fetchDescendantsOptionAsString fetch descendants option string
90 * @return fetch descendants option for given descendant
92 public static FetchDescendantsOption getFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
93 validateFetchDescendantsOption(fetchDescendantsOptionAsString);
94 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)
95 || "0".equals(fetchDescendantsOptionAsString) || "none".equals(fetchDescendantsOptionAsString)) {
96 return FetchDescendantsOption.OMIT_DESCENDANTS;
97 } else if ("-1".equals(fetchDescendantsOptionAsString) || "all".equals(fetchDescendantsOptionAsString)) {
98 return FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
99 } else if ("1".equals(fetchDescendantsOptionAsString) || "direct".equals(fetchDescendantsOptionAsString)) {
100 return FetchDescendantsOption.DIRECT_CHILD_ONLY;
102 final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString);
103 return new FetchDescendantsOption(depth);
108 public String toString() {
112 private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
113 if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) {
116 final Matcher matcher = FETCH_DESCENDANTS_OPTION_PATTERN.matcher(fetchDescendantsOptionAsString);
117 if (!matcher.matches()) {
118 throw new DataValidationException("FetchDescendantsOption validation error.",
119 fetchDescendantsOptionAsString + " is not valid fetch descendants option");
123 private static void validateDepth(final int depth) {
125 throw new IllegalArgumentException("A depth of less than minus one is not allowed");