2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications (C) 2021-2023 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.cpspath.parser.CpsPathQuery;
37 import org.onap.cps.cpspath.parser.CpsPathUtil;
38 import org.onap.cps.spi.model.DataNode;
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public class DataMapUtils {
44 * Converts DataNode structure into a map including the root node identifier for a JSON response.
46 * @param dataNode data node object
47 * @return a map representing same data with the root node identifier
49 public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode, final String prefix) {
50 final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
51 return ImmutableMap.<String, Object>builder().put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
55 * Converts DataNode structure into a map including the root node identifier for a JSON response.
57 * @param dataNode data node object
58 * @return a map representing same data with the root node identifier
60 public static Map<String, Object> toDataMapWithIdentifierAndAnchor(final DataNode dataNode, final String prefix) {
61 final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
62 final Map<String, Object> dataMap = ImmutableMap.<String, Object>builder()
63 .put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
64 return ImmutableMap.<String, Object>builder().put("anchorName", dataNode.getAnchorName())
65 .put("dataNode", dataMap).build();
69 * Converts DataNode structure into a map for a JSON response.
71 * @param dataNode data node object
72 * @return a map representing same data
74 public static Map<String, Object> toDataMap(final DataNode dataNode) {
75 return ImmutableMap.<String, Object>builder()
76 .putAll(dataNode.getLeaves())
77 .putAll(listElementsAsMap(dataNode.getChildDataNodes()))
78 .putAll(containerElementsAsMap(dataNode.getChildDataNodes()))
82 private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
83 if (dataNodes.isEmpty()) {
84 return Collections.emptyMap();
86 return ImmutableMap.<String, Object>builder()
89 .filter(dataNode -> isListElement(dataNode.getXpath()))
91 dataNode -> getNodeIdentifier(dataNode.getXpath()),
92 mapping(DataMapUtils::toDataMap, toUnmodifiableList())
97 private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
98 if (dataNodes.isEmpty()) {
99 return Collections.emptyMap();
101 return dataNodes.stream()
102 .filter(dataNode -> isContainerNode(dataNode.getXpath()))
105 dataNode -> getNodeIdentifier(dataNode.getXpath()),
106 DataMapUtils::toDataMap
110 private static String getNodeIdentifier(String xpath) {
111 final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath);
112 if (cpsPathQuery.isPathToListElement()) {
113 xpath = cpsPathQuery.getXpathPrefix();
115 final int fromIndex = xpath.lastIndexOf('/') + 1;
116 return xpath.substring(fromIndex);
119 private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
120 if (moduleNamePrefix != null) {
121 return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
123 return getNodeIdentifier(xpath);
126 private static boolean isContainerNode(final String xpath) {
127 return !isListElement(xpath);
130 private static boolean isListElement(final String xpath) {
131 return xpath.endsWith("]");