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