71a95f1ca084b4b7daa4eb2b5186dd14cd5a1328
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / DataMapUtils.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.utils;
22
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;
27
28 import com.google.common.collect.ImmutableMap;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Map;
32 import lombok.AccessLevel;
33 import lombok.NoArgsConstructor;
34 import org.onap.cps.spi.model.DataNode;
35
36 @NoArgsConstructor(access = AccessLevel.PRIVATE)
37 public class DataMapUtils {
38
39     /**
40      * Converts DataNode structure into a map for a JSON response.
41      *
42      * @param dataNode data node object
43      * @return a map representing same data
44      */
45
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()))
51             .build();
52     }
53
54     private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
55         if (dataNodes.isEmpty()) {
56             return Collections.emptyMap();
57         }
58         return ImmutableMap.<String, Object>builder()
59             .putAll(
60                 dataNodes.stream()
61                     .filter(dataNode -> isListElement(dataNode.getXpath()))
62                     .collect(groupingBy(
63                         dataNode -> getNodeIdentifier(dataNode.getXpath()),
64                         mapping(DataMapUtils::toDataMap, toUnmodifiableList())
65                     ))
66             ).build();
67     }
68
69     private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
70         if (dataNodes.isEmpty()) {
71             return Collections.emptyMap();
72         }
73         return dataNodes.stream()
74             .filter(dataNode -> isContainerNode(dataNode.getXpath()))
75             .collect(
76                 toUnmodifiableMap(
77                     dataNode -> getNodeIdentifier(dataNode.getXpath()),
78                     DataMapUtils::toDataMap
79                 ));
80     }
81
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);
86     }
87
88     private static boolean isContainerNode(final String xpath) {
89         return !isListElement(xpath);
90     }
91
92     private static boolean isListElement(final String xpath) {
93         return xpath.endsWith("]");
94     }
95 }