2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.utils;
22 import static java.util.stream.Collectors.groupingBy;
23 import static java.util.stream.Collectors.mapping;
24 import static java.util.stream.Collectors.toUnmodifiableList;
25 import static java.util.stream.Collectors.toUnmodifiableMap;
27 import com.google.common.collect.ImmutableMap;
28 import java.util.Collection;
29 import java.util.Collections;
31 import lombok.AccessLevel;
32 import lombok.NoArgsConstructor;
33 import org.onap.cps.spi.model.DataNode;
36 TODO: this utility class belongs to REST, however it expected to be used by both CPS Core and xNF Proxy;
37 placed in cps-service until shared module is done for REST services, then to be moved there
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 public class DataMapUtils {
43 * Converts DataNode structure into a map for a JSON response.
45 * @param dataNode data node object
46 * @return a map representing same data
49 public static Map<String, Object> toDataMap(final DataNode dataNode) {
50 return ImmutableMap.<String, Object>builder()
51 .putAll(dataNode.getLeaves())
52 .putAll(listElementsAsMap(dataNode.getChildDataNodes()))
53 .putAll(containerElementsAsMap(dataNode.getChildDataNodes()))
57 private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
58 if (dataNodes.isEmpty()) {
59 return Collections.emptyMap();
61 return ImmutableMap.<String, Object>builder()
64 .filter(dataNode -> isListNode(dataNode.getXpath()))
66 dataNode -> getNodeIdentifier(dataNode.getXpath()),
67 mapping(DataMapUtils::toDataMap, toUnmodifiableList())
72 private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
73 if (dataNodes.isEmpty()) {
74 return Collections.emptyMap();
76 return dataNodes.stream()
77 .filter(dataNode -> isContainerNode(dataNode.getXpath()))
80 dataNode -> getNodeIdentifier(dataNode.getXpath()),
81 DataMapUtils::toDataMap
85 private static String getNodeIdentifier(final String xpath) {
86 final int fromIndex = xpath.lastIndexOf("/") + 1;
87 final int toIndex = xpath.indexOf("[", fromIndex);
88 return toIndex > 0 ? xpath.substring(fromIndex, toIndex) : xpath.substring(fromIndex);
91 private static boolean isContainerNode(final String xpath) {
92 return !isListNode(xpath);
95 private static boolean isListNode(final String xpath) {
96 return xpath.endsWith("]");