Align JSON DataNode for Get and Post/Put API in CPS
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / DataMapUtils.java
index 3ec4764..42719d9 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
+ *  Modifications (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.
@@ -32,20 +33,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(getNodeIdentifier(dataNode.getXpath()), 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 +69,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())
@@ -89,10 +97,10 @@ public class DataMapUtils {
     }
 
     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("]");
     }
 }