b4d5a094479255cf6772d5f50c385f8cffb4ac28
[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-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
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.cpspath.parser.CpsPathQuery;
37 import org.onap.cps.cpspath.parser.CpsPathUtil;
38 import org.onap.cps.spi.model.DataNode;
39
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public class DataMapUtils {
42
43     /**
44      * Converts DataNode structure into a map including the root node identifier for a JSON response.
45      *
46      * @param dataNode data node object
47      * @return a map representing same data with the root node identifier
48      */
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();
52     }
53
54     /**
55      * Converts DataNode structure into a map including the root node identifier for a JSON response.
56      *
57      * @param dataNode data node object
58      * @return a map representing same data with the root node identifier
59      */
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();
66     }
67
68     /**
69      * Converts DataNode structure into a map for a JSON response.
70      *
71      * @param dataNode data node object
72      * @return a map representing same data
73      */
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()))
79             .build();
80     }
81
82     private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
83         if (dataNodes.isEmpty()) {
84             return Collections.emptyMap();
85         }
86         return ImmutableMap.<String, Object>builder()
87             .putAll(
88                 dataNodes.stream()
89                     .filter(dataNode -> isListElement(dataNode.getXpath()))
90                     .collect(groupingBy(
91                         dataNode -> getNodeIdentifier(dataNode.getXpath()),
92                         mapping(DataMapUtils::toDataMap, toUnmodifiableList())
93                     ))
94             ).build();
95     }
96
97     private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
98         if (dataNodes.isEmpty()) {
99             return Collections.emptyMap();
100         }
101         return dataNodes.stream()
102             .filter(dataNode -> isContainerNode(dataNode.getXpath()))
103             .collect(
104                 toUnmodifiableMap(
105                     dataNode -> getNodeIdentifier(dataNode.getXpath()),
106                     DataMapUtils::toDataMap
107                 ));
108     }
109
110     private static String getNodeIdentifier(String xpath) {
111         final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath);
112         if (cpsPathQuery.isPathToListElement()) {
113             xpath = cpsPathQuery.getXpathPrefix();
114         }
115         final int fromIndex = xpath.lastIndexOf('/') + 1;
116         return xpath.substring(fromIndex);
117     }
118
119     private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
120         if (moduleNamePrefix != null) {
121             return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
122         }
123         return getNodeIdentifier(xpath);
124     }
125
126     private static boolean isContainerNode(final String xpath) {
127         return !isListElement(xpath);
128     }
129
130     private static boolean isListElement(final String xpath) {
131         return xpath.endsWith("]");
132     }
133 }