Merge "Fix: Make bookstore data consistent"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / DataNodeHelper.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
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  *
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.ncmp.api.impl.utils;
22
23 import java.io.Serializable;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32 import org.onap.cps.spi.model.DataNode;
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class DataNodeHelper {
36
37     /**
38      * The nested DataNode object is being flattened.
39      *
40      * @param dataNode object.
41      * @return DataNode as stream.
42      */
43     public static Stream<DataNode> flatten(final DataNode dataNode) {
44         return Stream.concat(Stream.of(dataNode),
45                 dataNode.getChildDataNodes().stream().flatMap(DataNodeHelper::flatten));
46     }
47
48     /**
49      * The leaves for each DataNode is listed as map.
50      *
51      * @param dataNodes as collection
52      * @return list of map for the all leaves
53      */
54     public static List<Map<String, Serializable>> getDataNodeLeaves(final Collection<DataNode> dataNodes) {
55         return dataNodes.stream()
56                 .flatMap(DataNodeHelper::flatten)
57                 .map(DataNode::getLeaves)
58                 .collect(Collectors.toList());
59     }
60
61     /**
62      * Extracts the mapping of cm handle id to status with details from nodes leaves.
63      *
64      * @param dataNodeLeaves as a list of map
65      * @return cm handle id to status and details mapping
66      */
67     public static Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMap(
68             final List<Map<String, Serializable>> dataNodeLeaves) {
69         return dataNodeLeaves.stream()
70                 .filter(entryset -> entryset.values().contains("PENDING")
71                         || entryset.values().contains("ACCEPTED")
72                         || entryset.values().contains("REJECTED"))
73                 .collect(
74                         HashMap<String, Map<String, String>>::new,
75                         (result, entry) -> {
76                             final String cmHandleId = (String) entry.get("cmHandleId");
77                             final String status = (String) entry.get("status");
78                             final String details = (String) entry.get("details");
79
80                             if (cmHandleId != null && status != null) {
81                                 result.put(cmHandleId, new HashMap<>());
82                                 result.get(cmHandleId).put("status", status);
83                                 result.get(cmHandleId).put("details", details == null ? "" : details);
84                             }
85                         },
86                         HashMap::putAll
87                 );
88     }
89
90     /**
91      * Extracts the mapping of cm handle id to status with details from data node collection.
92      *
93      * @param dataNodes as a collection
94      * @return cm handle id to status and details mapping
95      */
96     public static Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapFromDataNode(
97             final Collection<DataNode> dataNodes) {
98         return cmHandleIdToStatusAndDetailsAsMap(getDataNodeLeaves(dataNodes));
99     }
100 }