2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications (C) 2021-2022 Nordix Foundation
5 * Modifications Copyright (C) 2022 Bell Canada
6 * Modifications Copyright (C) 2022-2023 TechMahindra Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.utils;
25 import static java.util.stream.Collectors.groupingBy;
26 import static java.util.stream.Collectors.mapping;
27 import static java.util.stream.Collectors.toUnmodifiableList;
28 import static java.util.stream.Collectors.toUnmodifiableMap;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.Collection;
32 import java.util.Collections;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.onap.cps.spi.model.DataNode;
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public class DataMapUtils {
42 * Converts DataNode structure into a map including the root node identifier for a JSON response.
44 * @param dataNode data node object
45 * @return a map representing same data with the root node identifier
47 public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode, final String prefix) {
48 final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
49 return ImmutableMap.<String, Object>builder().put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
53 * Converts DataNode structure into a map including the root node identifier for a JSON response.
55 * @param dataNode data node object
56 * @return a map representing same data with the root node identifier
58 public static Map<String, Object> toDataMapWithIdentifierAndAnchor(final DataNode dataNode, final String prefix) {
59 final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
60 final Map<String, Object> dataMap = ImmutableMap.<String, Object>builder()
61 .put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
62 return ImmutableMap.<String, Object>builder().put("anchorName", dataNode.getAnchorName())
63 .put("dataNode", dataMap).build();
67 * Converts DataNode structure into a map for a JSON response.
69 * @param dataNode data node object
70 * @return a map representing same data
72 public static Map<String, Object> toDataMap(final DataNode dataNode) {
73 return ImmutableMap.<String, Object>builder()
74 .putAll(dataNode.getLeaves())
75 .putAll(listElementsAsMap(dataNode.getChildDataNodes()))
76 .putAll(containerElementsAsMap(dataNode.getChildDataNodes()))
80 private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
81 if (dataNodes.isEmpty()) {
82 return Collections.emptyMap();
84 return ImmutableMap.<String, Object>builder()
87 .filter(dataNode -> isListElement(dataNode.getXpath()))
89 dataNode -> getNodeIdentifier(dataNode.getXpath()),
90 mapping(DataMapUtils::toDataMap, toUnmodifiableList())
95 private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
96 if (dataNodes.isEmpty()) {
97 return Collections.emptyMap();
99 return dataNodes.stream()
100 .filter(dataNode -> isContainerNode(dataNode.getXpath()))
103 dataNode -> getNodeIdentifier(dataNode.getXpath()),
104 DataMapUtils::toDataMap
108 private static String getNodeIdentifier(String xpath) {
109 if (xpath.endsWith("]")) {
110 xpath = xpath.substring(0, xpath.lastIndexOf('['));
112 final int fromIndex = xpath.lastIndexOf('/') + 1;
113 return xpath.substring(fromIndex);
116 private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
117 if (moduleNamePrefix != null) {
118 return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
120 return getNodeIdentifier(xpath);
123 private static boolean isContainerNode(final String xpath) {
124 return !isListElement(xpath);
127 private static boolean isListElement(final String xpath) {
128 return xpath.endsWith("]");