Persistence layer - Query Datanodes using cpsPath that contains contains a leaf name...
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
index fd4e768..2d9588e 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.cps.spi.impl;
 
 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
+import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet.Builder;
@@ -38,6 +39,7 @@ import org.onap.cps.spi.entities.DataspaceEntity;
 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.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
 import org.onap.cps.spi.repository.FragmentRepository;
@@ -124,6 +126,19 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
     }
 
+    @Override
+    public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath) {
+        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());
+        return fragmentEntities.stream()
+            .map(fragmentEntity -> toDataNode(fragmentEntity, OMIT_DESCENDANTS))
+            .collect(Collectors.toUnmodifiableList());
+    }
+
     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
         final FetchDescendantsOption fetchDescendantsOption) {
         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);