2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications (C) 2021 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils;
23 import static java.util.stream.Collectors.groupingBy;
24 import static java.util.stream.Collectors.mapping;
25 import static java.util.stream.Collectors.toUnmodifiableList;
26 import static java.util.stream.Collectors.toUnmodifiableMap;
28 import com.google.common.collect.ImmutableMap;
29 import java.util.Collection;
30 import java.util.Collections;
32 import lombok.AccessLevel;
33 import lombok.NoArgsConstructor;
34 import org.onap.cps.spi.model.DataNode;
36 @NoArgsConstructor(access = AccessLevel.PRIVATE)
37 public class DataMapUtils {
40 * Converts DataNode structure into a map for a JSON response.
42 * @param dataNode data node object
43 * @return a map representing same data
46 public static Map<String, Object> toDataMap(final DataNode dataNode) {
47 return ImmutableMap.<String, Object>builder()
48 .putAll(dataNode.getLeaves())
49 .putAll(listElementsAsMap(dataNode.getChildDataNodes()))
50 .putAll(containerElementsAsMap(dataNode.getChildDataNodes()))
54 private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
55 if (dataNodes.isEmpty()) {
56 return Collections.emptyMap();
58 return ImmutableMap.<String, Object>builder()
61 .filter(dataNode -> isListNode(dataNode.getXpath()))
63 dataNode -> getNodeIdentifier(dataNode.getXpath()),
64 mapping(DataMapUtils::toDataMap, toUnmodifiableList())
69 private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
70 if (dataNodes.isEmpty()) {
71 return Collections.emptyMap();
73 return dataNodes.stream()
74 .filter(dataNode -> isContainerNode(dataNode.getXpath()))
77 dataNode -> getNodeIdentifier(dataNode.getXpath()),
78 DataMapUtils::toDataMap
82 private static String getNodeIdentifier(final String xpath) {
83 final int fromIndex = xpath.lastIndexOf("/") + 1;
84 final int toIndex = xpath.indexOf("[", fromIndex);
85 return toIndex > 0 ? xpath.substring(fromIndex, toIndex) : xpath.substring(fromIndex);
88 private static boolean isContainerNode(final String xpath) {
89 return !isListNode(xpath);
92 private static boolean isListNode(final String xpath) {
93 return xpath.endsWith("]");