Upgrade to Java 17
[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-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
11  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.utils;
24
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;
29
30 import com.google.common.collect.ImmutableMap;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Map;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.onap.cps.spi.model.DataNode;
37
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public class DataMapUtils {
40
41     /**
42      * Converts DataNode structure into a map including the root node identifier for a JSON response.
43      *
44      * @param dataNode data node object
45      * @return a map representing same data with the root node identifier
46      */
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();
50     }
51
52     /**
53      * Converts DataNode structure into a map including the root node identifier for a JSON response.
54      *
55      * @param dataNode data node object
56      * @return a map representing same data with the root node identifier
57      */
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();
64     }
65
66     /**
67      * Converts DataNode structure into a map for a JSON response.
68      *
69      * @param dataNode data node object
70      * @return a map representing same data
71      */
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()))
77             .build();
78     }
79
80     private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
81         if (dataNodes.isEmpty()) {
82             return Collections.emptyMap();
83         }
84         return ImmutableMap.<String, Object>builder()
85             .putAll(
86                 dataNodes.stream()
87                     .filter(dataNode -> isListElement(dataNode.getXpath()))
88                     .collect(groupingBy(
89                         dataNode -> getNodeIdentifier(dataNode.getXpath()),
90                         mapping(DataMapUtils::toDataMap, toUnmodifiableList())
91                     ))
92             ).build();
93     }
94
95     private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
96         if (dataNodes.isEmpty()) {
97             return Collections.emptyMap();
98         }
99         return dataNodes.stream()
100             .filter(dataNode -> isContainerNode(dataNode.getXpath()))
101             .collect(
102                 toUnmodifiableMap(
103                     dataNode -> getNodeIdentifier(dataNode.getXpath()),
104                     DataMapUtils::toDataMap
105                 ));
106     }
107
108     private static String getNodeIdentifier(String xpath) {
109         if (xpath.endsWith("]")) {
110             xpath = xpath.substring(0, xpath.lastIndexOf('['));
111         }
112         final int fromIndex = xpath.lastIndexOf('/') + 1;
113         return xpath.substring(fromIndex);
114     }
115
116     private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
117         if (moduleNamePrefix != null) {
118             return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
119         }
120         return getNodeIdentifier(xpath);
121     }
122
123     private static boolean isContainerNode(final String xpath) {
124         return !isListElement(xpath);
125     }
126
127     private static boolean isListElement(final String xpath) {
128         return xpath.endsWith("]");
129     }
130 }