Get Node API fix
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / DataMapUtils.java
index 3ec4764..4413c6b 100644 (file)
@@ -1,6 +1,8 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
+ *  Modifications (C) 2021-2022 Nordix Foundation
+ *  Modifications Copyright (C) 2022 Bell Canada
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -32,20 +34,27 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.onap.cps.spi.model.DataNode;
 
-/*
- TODO: this utility class belongs to REST, however it expected to be used by both CPS Core and xNF Proxy;
-  placed in cps-service until shared module is done for REST services, then to be moved there
-  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public class DataMapUtils {
 
+    /**
+     * 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> toDataMapWithIdentifier(final DataNode dataNode) {
+        return ImmutableMap.<String, Object>builder()
+            .put(getNodeIdentifierWithPrefix(dataNode.getXpath(), dataNode.getModuleNamePrefix()), toDataMap(dataNode))
+            .build();
+    }
+
     /**
      * Converts DataNode structure into a map for a JSON response.
      *
      * @param dataNode data node object
      * @return a map representing same data
      */
-
     public static Map<String, Object> toDataMap(final DataNode dataNode) {
         return ImmutableMap.<String, Object>builder()
             .putAll(dataNode.getLeaves())
@@ -61,7 +70,7 @@ public class DataMapUtils {
         return ImmutableMap.<String, Object>builder()
             .putAll(
                 dataNodes.stream()
-                    .filter(dataNode -> isListNode(dataNode.getXpath()))
+                    .filter(dataNode -> isListElement(dataNode.getXpath()))
                     .collect(groupingBy(
                         dataNode -> getNodeIdentifier(dataNode.getXpath()),
                         mapping(DataMapUtils::toDataMap, toUnmodifiableList())
@@ -82,17 +91,26 @@ 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) {
+        if (moduleNamePrefix != null) {
+            return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
+        }
+        return getNodeIdentifier(xpath);
     }
 
     private static boolean isContainerNode(final String xpath) {
-        return !isListNode(xpath);
+        return !isListElement(xpath);
     }
 
-    private static boolean isListNode(final String xpath) {
+    private static boolean isListElement(final String xpath) {
         return xpath.endsWith("]");
     }
 }