summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy47
-rw-r--r--cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy41
6 files changed, 135 insertions, 33 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
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
index 8001c659ff..bb0b471256 100644
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
@@ -312,14 +312,14 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
when: 'a query is executed to get a data node by the given cps path'
def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
then: 'the correct data is returned'
- def leaves ='[common-leaf-name:common-leaf-value, common-leaf-name-int:5.0]'
+ def leaves = '[common-leaf-name:common-leaf-value, common-leaf-name-int:5.0]'
DataNode dataNode = result.stream().findFirst().get()
dataNode.getLeaves().toString() == leaves
dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
where: 'the following data is used'
- type | cpsPath | includeDescendantsOption | expectedNumberOfChidlNodes
- 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' | OMIT_DESCENDANTS | 0
- 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS | 1
+ type | cpsPath | includeDescendantsOption | expectedNumberOfChidlNodes
+ 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' | OMIT_DESCENDANTS | 0
+ 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS | 1
}
@Unroll
@@ -330,10 +330,39 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
then: 'no data is returned'
result.isEmpty()
where: 'following cps queries are performed'
- scenario | cpsPath
- 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]'
- 'missing / at beginning of path' | 'parent-200/child-202[@common-leaf-name-int=5]'
- 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
- 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
+ scenario | cpsPath
+ 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]'
+ 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
+ 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
+ 'empty cps path of type ends with' | '///'
+ }
+
+ @Unroll
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Cps Path query with and without descendants using #type.'() {
+ when: 'a query is executed to get a data node by the given cps path'
+ def cpsPath = '///child-202'
+ def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
+ then: 'the data node has the correct number of children'
+ DataNode dataNode = result.stream().findFirst().get()
+ dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
+ where: 'the following data is used'
+ type | includeDescendantsOption | expectedNumberOfChildNodes
+ 'ends with and omit descendants' | OMIT_DESCENDANTS | 0
+ 'ends with and include all descendants' | INCLUDE_ALL_DESCENDANTS | 1
+ }
+
+ @Unroll
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Cps Path query using ends with variations of #type.'() {
+ when: 'a query is executed to get a data node by the given cps path'
+ def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
+ then: 'the correct number of data nodes is returned'
+ result.size() == expectedNumberOfDataNodes
+ where: 'the following data is used'
+ type | cpsPath | expectedNumberOfDataNodes
+ 'single match with / prefix' | '///child-202' | 1
+ 'single match without / prefix' | '//grand-child-202' | 1
+ 'multiple matches' | '//202' | 2
}
}
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy
index 1e457fb062..708761326b 100644
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy
@@ -28,28 +28,45 @@ class CpsPathQuerySpec extends Specification {
def objectUnderTest = new CpsPathQuery()
@Unroll
- def 'Parse cps path with valid cps path and a filter for a leaf of type : #type.'()
- { when: 'the given cps path is parsed'
- def result = objectUnderTest.createFrom(cpsPath)
- then: 'object has the expected attribute'
+ def 'Parse cps path with valid cps path and a filter for a leaf of type : #type.'() {
+ when: 'the given cps path is parsed'
+ def result = objectUnderTest.createFrom(cpsPath)
+ then: 'the query has the right type'
+ result.cpsPathQueryType == CpsPathQueryType.XPATH_LEAF_VALUE
+ and: 'the right query parameters are set'
result.xpathPrefix == '/parent-200/child-202'
result.leafName == expectedLeafName
result.leafValue == expectedLeafValue
where: 'the following data is used'
- type | cpsPath || expectedLeafName | expectedLeafValue
- 'String' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' || 'common-leaf-name' | 'common-leaf-value'
- 'Integer' | '/parent-200/child-202[@common-leaf-name-int=5]' || 'common-leaf-name-int' | 5
+ type | cpsPath || expectedLeafName | expectedLeafValue
+ 'String' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' || 'common-leaf-name' | 'common-leaf-value'
+ 'Integer' | '/parent-200/child-202[@common-leaf-name-int=5]' || 'common-leaf-name-int' | 5
+ 'Integer value with spaces' | '/parent-200/child-202[@common-leaf-name-int = 5]' || 'common-leaf-name-int' | 5
}
@Unroll
- def 'Parse cps path with : #scenario.'()
- { when: 'the given cps path is parsed'
+ def 'Parse cps path of type ends with a #scenario.'() {
+ when: 'the given cps path is parsed'
+ def result = objectUnderTest.createFrom(cpsPath)
+ then: 'the query has the right type'
+ result.cpsPathQueryType == CpsPathQueryType.XPATH_ENDS_WITH
+ and: 'the right ends with parameters are set'
+ result.endsWith == expectedEndsWithValue
+ where: 'the following data is used'
+ scenario | cpsPath || expectedEndsWithValue
+ 'yang container' | '///cps-path' || '/cps-path'
+ 'yang list' | '///cps-path[@key=value]' || '/cps-path[@key=value]'
+ }
+
+ @Unroll
+ def 'Parse cps path with #scenario.'() {
+ when: 'the given cps path is parsed'
objectUnderTest.createFrom(cpsPath)
then: 'a CpsPathException is thrown'
thrown(CpsPathException)
where: 'the following data is used'
- scenario | cpsPath
- 'invalid cps path' | 'invalid-cps-path'
- 'cps path with float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]'
+ scenario | cpsPath
+ 'no / at the start' | 'invalid-cps-path/child'
+ 'float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]'
}
}