diff options
author | rajesh.kumar <rk00747546@techmahindra.com> | 2022-12-06 11:47:24 +0000 |
---|---|---|
committer | rajesh.kumar <rk00747546@techmahindra.com> | 2023-01-23 05:22:04 +0000 |
commit | cc21a025308153e8f187cd3e82cf828191d7b387 (patch) | |
tree | e33b4fb9725481bdf2526e2a3246f12984bcac73 /cps-service/src/main | |
parent | 5b00ef4d6ab15006b880a22dc69a936e4f784623 (diff) |
Added depth parameter in query nodes API.
Issue-ID: CPS-1381
Change-ID: I73f97f986a817d423f93a8d922dcd9647b1206bb
Signed-off-by: rajesh.kumar <rk00747546@techmahindra.com>
Diffstat (limited to 'cps-service/src/main')
-rw-r--r-- | cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java b/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java index b80054ac3b..0c8cddcd73 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java +++ b/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech * Copyright (C) 2022 Nordix Foundation + * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +21,11 @@ package org.onap.cps.spi; +import com.google.common.base.Strings; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; +import org.onap.cps.spi.exceptions.DataValidationException; @RequiredArgsConstructor public class FetchDescendantsOption { @@ -29,6 +34,9 @@ public class FetchDescendantsOption { public static final FetchDescendantsOption OMIT_DESCENDANTS = new FetchDescendantsOption(0); public static final FetchDescendantsOption INCLUDE_ALL_DESCENDANTS = new FetchDescendantsOption(-1); + private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN = + Pattern.compile("^$|^all$|^none$|^[0-9]+$|^-1$"); + private final int depth; /** @@ -58,6 +66,36 @@ public class FetchDescendantsOption { return nextDescendantsOption; } + /** + * get fetch descendants option for given descendant. + * + * @param fetchDescendantsOptionAsString fetch descendants option string + * @return fetch descendants option for given descendant + */ + public static FetchDescendantsOption getFetchDescendantsOption(final String fetchDescendantsOptionAsString) { + validateFetchDescendantsOption(fetchDescendantsOptionAsString); + if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString) + || "0".equals(fetchDescendantsOptionAsString) || "none".equals(fetchDescendantsOptionAsString)) { + return FetchDescendantsOption.OMIT_DESCENDANTS; + } else if ("-1".equals(fetchDescendantsOptionAsString) || "all".equals(fetchDescendantsOptionAsString)) { + return FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; + } else { + final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString); + return new FetchDescendantsOption(depth); + } + } + + private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) { + if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) { + return; + } + final Matcher matcher = FETCH_DESCENDANTS_OPTION_PATTERN.matcher(fetchDescendantsOptionAsString); + if (!matcher.matches()) { + throw new DataValidationException("FetchDescendantsOption validation error.", + fetchDescendantsOptionAsString + " is not valid fetch descendants option"); + } + } + private static void validateDepth(final int depth) { if (depth < -1) { throw new IllegalArgumentException("A depth of less than minus one is not allowed"); |