Merge "Added depth parameter in query nodes API."
[cps.git] / cps-service / src / main / java / org / onap / cps / spi / FetchDescendantsOption.java
1 /*
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
10  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi;
23
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;
29
30 @RequiredArgsConstructor
31 public class FetchDescendantsOption {
32
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);
36
37     private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN =
38         Pattern.compile("^$|^all$|^none$|^[0-9]+$|^-1$");
39
40     private final int depth;
41
42     /**
43      * Has next depth.
44      *
45      * @return true if next level of depth is available
46      * @throws IllegalArgumentException when depth less than -1
47      */
48     public boolean hasNext() {
49         validateDepth(depth);
50         return depth > 0 || this.depth == INCLUDE_ALL_DESCENDANTS.depth;
51     }
52
53     /**
54      * Next fetch descendants option.
55      *
56      * @return the next fetch descendants option
57      * @throws IllegalArgumentException when depth less than -1 or 0
58      */
59     public FetchDescendantsOption next() {
60         if (depth == 0) {
61             throw new IllegalArgumentException("Do not use next() method with zero depth");
62         }
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;
67     }
68
69     /**
70      * get fetch descendants option for given descendant.
71      *
72      * @param fetchDescendantsOptionAsString fetch descendants option string
73      * @return fetch descendants option for given descendant
74      */
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;
82         } else {
83             final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString);
84             return new FetchDescendantsOption(depth);
85         }
86     }
87
88     private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
89         if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) {
90             return;
91         }
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");
96         }
97     }
98
99     private static void validateDepth(final int depth) {
100         if (depth < -1) {
101             throw new IllegalArgumentException("A depth of less than minus one is not allowed");
102         }
103     }
104
105 }