Add 'direct' keyword to descendants option to query direct children (ep1)
[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-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
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 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");
39
40     FetchDescendantsOption(final int depth) {
41         this(depth, "Depth=" + depth);
42     }
43
44     private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN =
45         Pattern.compile("^$|^all$|^none$|^direct$|^[0-9]+$|^-1$|^1$");
46
47     private final int depth;
48
49     private final String optionName;
50
51     /**
52      * Has next depth.
53      *
54      * @return true if next level of depth is available
55      * @throws IllegalArgumentException when depth less than -1
56      */
57     public boolean hasNext() {
58         validateDepth(depth);
59         return depth > 0 || this.depth == INCLUDE_ALL_DESCENDANTS.depth;
60     }
61
62     /**
63      * Next fetch descendants option.
64      *
65      * @return the next fetch descendants option
66      * @throws IllegalArgumentException when depth less than -1 or 0
67      */
68     public FetchDescendantsOption next() {
69         if (depth == 0) {
70             throw new IllegalArgumentException("Do not use next() method with zero depth");
71         }
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;
76     }
77
78     /**
79      * Get depth.
80      * @return depth: -1 for all descendants, 0 for no descendants, or positive value for fixed level of descendants
81      */
82     public int getDepth() {
83         return depth;
84     }
85
86     /**
87      * get fetch descendants option for given descendant.
88      *
89      * @param fetchDescendantsOptionAsString fetch descendants option string
90      * @return fetch descendants option for given descendant
91      */
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_CHILDREN_ONLY;
101         } else {
102             final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString);
103             return new FetchDescendantsOption(depth);
104         }
105     }
106
107     @Override
108     public String toString() {
109         return optionName;
110     }
111
112     private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) {
113         if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) {
114             return;
115         }
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");
120         }
121     }
122
123     private static void validateDepth(final int depth) {
124         if (depth < -1) {
125             throw new IllegalArgumentException("A depth of less than minus one is not allowed");
126         }
127     }
128
129 }