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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.utils;
23 import java.io.Serializable;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
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;
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class DataNodeHelper {
38 * The nested DataNode object is being flattened.
40 * @param dataNode object.
41 * @return DataNode as stream.
43 public static Stream<DataNode> flatten(final DataNode dataNode) {
44 return Stream.concat(Stream.of(dataNode),
45 dataNode.getChildDataNodes().stream().flatMap(DataNodeHelper::flatten));
49 * The leaves for each DataNode is listed as map.
51 * @param dataNodes as collection
52 * @return list of map for the all leaves
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());
62 * Extracts the mapping of cm handle id to status with details from nodes leaves.
64 * @param dataNodeLeaves as a list of map
65 * @return cm handle id to status and details mapping
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"))
74 HashMap<String, Map<String, String>>::new,
76 final String cmHandleId = (String) entry.get("cmHandleId");
77 final String status = (String) entry.get("status");
78 final String details = (String) entry.get("details");
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);
91 * Extracts the mapping of cm handle id to status with details from data node collection.
93 * @param dataNodes as a collection
94 * @return cm handle id to status and details mapping
96 public static Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapFromDataNode(
97 final Collection<DataNode> dataNodes) {
98 return cmHandleIdToStatusAndDetailsAsMap(getDataNodeLeaves(dataNodes));