Get Node API fix
[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  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.utils;
23
24 import static java.util.stream.Collectors.groupingBy;
25 import static java.util.stream.Collectors.mapping;
26 import static java.util.stream.Collectors.toUnmodifiableList;
27 import static java.util.stream.Collectors.toUnmodifiableMap;
28
29 import com.google.common.collect.ImmutableMap;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Map;
33 import lombok.AccessLevel;
34 import lombok.NoArgsConstructor;
35 import org.onap.cps.spi.model.DataNode;
36
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 public class DataMapUtils {
39
40     /**
41      * Converts DataNode structure into a map including the root node identifier for a JSON response.
42      *
43      * @param dataNode data node object
44      * @return a map representing same data with the root node identifier
45      */
46     public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode) {
47         return ImmutableMap.<String, Object>builder()
48             .put(getNodeIdentifierWithPrefix(dataNode.getXpath(), dataNode.getModuleNamePrefix()), toDataMap(dataNode))
49             .build();
50     }
51
52     /**
53      * Converts DataNode structure into a map for a JSON response.
54      *
55      * @param dataNode data node object
56      * @return a map representing same data
57      */
58     public static Map<String, Object> toDataMap(final DataNode dataNode) {
59         return ImmutableMap.<String, Object>builder()
60             .putAll(dataNode.getLeaves())
61             .putAll(listElementsAsMap(dataNode.getChildDataNodes()))
62             .putAll(containerElementsAsMap(dataNode.getChildDataNodes()))
63             .build();
64     }
65
66     private static Map<String, Object> listElementsAsMap(final Collection<DataNode> dataNodes) {
67         if (dataNodes.isEmpty()) {
68             return Collections.emptyMap();
69         }
70         return ImmutableMap.<String, Object>builder()
71             .putAll(
72                 dataNodes.stream()
73                     .filter(dataNode -> isListElement(dataNode.getXpath()))
74                     .collect(groupingBy(
75                         dataNode -> getNodeIdentifier(dataNode.getXpath()),
76                         mapping(DataMapUtils::toDataMap, toUnmodifiableList())
77                     ))
78             ).build();
79     }
80
81     private static Map<String, Object> containerElementsAsMap(final Collection<DataNode> dataNodes) {
82         if (dataNodes.isEmpty()) {
83             return Collections.emptyMap();
84         }
85         return dataNodes.stream()
86             .filter(dataNode -> isContainerNode(dataNode.getXpath()))
87             .collect(
88                 toUnmodifiableMap(
89                     dataNode -> getNodeIdentifier(dataNode.getXpath()),
90                     DataMapUtils::toDataMap
91                 ));
92     }
93
94     private static String getNodeIdentifier(String xpath) {
95         if (xpath.endsWith("]")) {
96             xpath = xpath.substring(0, xpath.lastIndexOf('['));
97         }
98         final int fromIndex = xpath.lastIndexOf('/') + 1;
99         return xpath.substring(fromIndex);
100     }
101
102     private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
103         if (moduleNamePrefix != null) {
104             return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
105         }
106         return getNodeIdentifier(xpath);
107     }
108
109     private static boolean isContainerNode(final String xpath) {
110         return !isListElement(xpath);
111     }
112
113     private static boolean isListElement(final String xpath) {
114         return xpath.endsWith("]");
115     }
116 }