summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/main/java/org/onap
diff options
context:
space:
mode:
authorniamhcore <niamh.core@est.tech>2021-03-03 12:05:09 +0000
committerniamhcore <niamh.core@est.tech>2021-03-04 16:27:53 +0000
commit50130c04626e0c5b09b344b2e11bb99c62dbf926 (patch)
tree72e703c7c74e40165dacd993ecf35ddb4670da51 /cps-ri/src/main/java/org/onap
parent333f2b5a7a54f325a8d7137e6b8c0502fc860441 (diff)
CPS-265 - Update cps path query to support 'ends with'
Issue-ID: CPS-265 Signed-off-by: niamhcore <niamh.core@est.tech> Change-Id: I604191feaad820983d86e6fd844f543f51096a4e
Diffstat (limited to 'cps-ri/src/main/java/org/onap')
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java13
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java23
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java34
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java10
4 files changed, 68 insertions, 12 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
index 0c61c99094..4cfa78b3e3 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
@@ -39,6 +39,7 @@ import org.onap.cps.spi.entities.FragmentEntity;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.model.DataNodeBuilder;
import org.onap.cps.spi.query.CpsPathQuery;
+import org.onap.cps.spi.query.CpsPathQueryType;
import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.FragmentRepository;
@@ -131,9 +132,15 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
- final List<FragmentEntity> fragmentEntities = fragmentRepository
- .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery
- .getXpathPrefix(), cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
+ final List<FragmentEntity> fragmentEntities;
+ if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
+ fragmentEntities = fragmentRepository
+ .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(), cpsPathQuery
+ .getLeafName(), cpsPathQuery.getLeafValue());
+ } else {
+ fragmentEntities = fragmentRepository
+ .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
+ }
return fragmentEntities.stream()
.map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
.collect(Collectors.toUnmodifiableList());
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java
index 4fcf6e444b..e85414cdc9 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java
@@ -31,32 +31,43 @@ import org.onap.cps.spi.exceptions.CpsPathException;
@Setter(AccessLevel.PRIVATE)
public class CpsPathQuery {
+ private CpsPathQueryType cpsPathQueryType;
private String xpathPrefix;
private String leafName;
private Object leafValue;
+ private String endsWith;
public static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN =
- Pattern.compile("(.*)\\[\\s*@(.*?)\\s*=\\s*(.*?)\\s*]");
+ Pattern.compile("((?:\\/[^\\/]+)+?)\\[\\s*@(\\S+?)\\s*=\\s*(.*?)\\s*\\]");
- public static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
+ public static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)");
public static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
+ public static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
+
/**
- * Returns a xpath prefix, leaf name and leaf value for the given cps path.
+ * Returns a cps path query.
*
* @param cpsPath cps path
- * @return a CpsPath object containing the xpath prefix, leaf name and leaf value.
+ * @return a CpsPath object.
*/
public static CpsPathQuery createFrom(final String cpsPath) {
- final Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
+ Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
+ final CpsPathQuery cpsPathQuery = new CpsPathQuery();
if (matcher.matches()) {
- final CpsPathQuery cpsPathQuery = new CpsPathQuery();
+ cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
cpsPathQuery.setXpathPrefix(matcher.group(1));
cpsPathQuery.setLeafName(matcher.group(2));
cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3)));
return cpsPathQuery;
}
+ matcher = QUERY_CPS_PATH_ENDS_WITH_PATTERN.matcher(cpsPath);
+ if (matcher.matches()) {
+ cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_ENDS_WITH);
+ cpsPathQuery.setEndsWith(matcher.group(1));
+ return cpsPathQuery;
+ }
throw new CpsPathException("Invalid cps path.",
String.format("Cannot interpret or parse cps path %s.", cpsPath));
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java
new file mode 100644
index 0000000000..1a0f8cabc1
--- /dev/null
+++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java
@@ -0,0 +1,34 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.query;
+
+/**
+ * The enum Cps path query type.
+ */
+public enum CpsPathQueryType {
+ /**
+ * Xpath ends with cps path query type e.g. //cps-path .
+ */
+ XPATH_ENDS_WITH,
+ /**
+ * Xpath leaf value cps path query type e.g. /cps-path[@leafName=leafValue] .
+ */
+ XPATH_LEAF_VALUE
+}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
index a40168a9d6..5ff7cfc976 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
@@ -55,9 +55,13 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>
@Query(value =
"SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "
+ "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",
- nativeQuery = true)
- // Above query will match an xpath with or without the index for a list [@key=value]
- // and match anchor id, leaf name and leaf value
+ nativeQuery = true)
+ // Above query will match an xpath with or without the index for a list [@key=value] and match anchor id,
+ // leaf name and leaf value
List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")
String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);
+
+ @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE %:xpath", nativeQuery = true)
+ // Above query will match the end of an xpath and anchor id
+ List<FragmentEntity> getByAnchorAndEndsWithXpath(@Param("anchor") int anchorId, @Param("xpath") String xpath);
} \ No newline at end of file