Query data nodes across all anchors under one dataspace
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / DataMapUtils.java
index ff5204f..b0e109b 100644 (file)
@@ -2,6 +2,8 @@
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
  *  Modifications (C) 2021-2022 Nordix Foundation
+ *  Modifications Copyright (C) 2022 Bell Canada
+ *  Modifications Copyright (C) 2022-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.
@@ -42,10 +44,23 @@ public class DataMapUtils {
      * @param dataNode data node object
      * @return a map representing same data with the root node identifier
      */
-    public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode) {
-        return ImmutableMap.<String, Object>builder()
-            .put(getNodeIdentifierWithPrefix(dataNode.getXpath(), dataNode.getModuleNamePrefix()), toDataMap(dataNode))
-            .build();
+    public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode, final String prefix) {
+        final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
+        return ImmutableMap.<String, Object>builder().put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
+    }
+
+    /**
+     * Converts DataNode structure into a map including the root node identifier for a JSON response.
+     *
+     * @param dataNode data node object
+     * @return a map representing same data with the root node identifier
+     */
+    public static Map<String, Object> toDataMapWithIdentifierAndAnchor(final DataNode dataNode, final String prefix) {
+        final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
+        final Map<String, Object> dataMap = ImmutableMap.<String, Object>builder()
+                .put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
+        return ImmutableMap.<String, Object>builder().put("anchorName", dataNode.getAnchorName())
+                .put("dataNode", dataMap).build();
     }
 
     /**
@@ -90,10 +105,12 @@ public class DataMapUtils {
                 ));
     }
 
-    private static String getNodeIdentifier(final String xpath) {
-        final int fromIndex = xpath.lastIndexOf("/") + 1;
-        final int toIndex = xpath.indexOf("[", fromIndex);
-        return toIndex > 0 ? xpath.substring(fromIndex, toIndex) : xpath.substring(fromIndex);
+    private static String getNodeIdentifier(String xpath) {
+        if (xpath.endsWith("]")) {
+            xpath = xpath.substring(0, xpath.lastIndexOf('['));
+        }
+        final int fromIndex = xpath.lastIndexOf('/') + 1;
+        return xpath.substring(fromIndex);
     }
 
     private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {