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.Iterator;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import lombok.AccessLevel;
32 import lombok.NoArgsConstructor;
33 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
34 import org.onap.cps.spi.model.DataNode;
36 @NoArgsConstructor(access = AccessLevel.PRIVATE)
37 public class DataNodeHelper {
40 * The nested DataNode object is being flattened.
42 * @param dataNode object.
43 * @return DataNode as stream.
45 public static Stream<DataNode> flatten(final DataNode dataNode) {
46 return Stream.concat(Stream.of(dataNode),
47 dataNode.getChildDataNodes().stream().flatMap(DataNodeHelper::flatten));
51 * The leaves for each DataNode is listed as map.
53 * @param dataNodes as collection.
54 * @return list of map for the all leaves.
56 public static List<Map<String, Serializable>> getDataNodeLeaves(final Collection<DataNode> dataNodes) {
57 return dataNodes.stream()
58 .flatMap(DataNodeHelper::flatten)
59 .map(DataNode::getLeaves)
60 .collect(Collectors.toList());
64 * The cm handle and status is listed as a collection.
66 * @param dataNodeLeaves as a list of map.
67 * @return list of collection containing cm handle id and statuses.
69 public static List<Collection<Serializable>> getCmHandleIdToStatus(
70 final List<Map<String, Serializable>> dataNodeLeaves) {
71 return dataNodeLeaves.stream()
73 .filter(col -> col.contains("PENDING")
74 || col.contains("ACCEPTED")
75 || col.contains("REJECTED"))
76 .collect(Collectors.toList());
80 * The cm handle and status is returned as a map.
82 * @param cmHandleIdToStatus as a list of collection
83 * @return a map of cm handle id to status
85 public static Map<String, SubscriptionStatus> getCmHandleIdToStatusMap(
86 final List<Collection<Serializable>> cmHandleIdToStatus) {
87 final Map<String, SubscriptionStatus> resultMap = new HashMap<>();
88 for (final Collection<Serializable> cmHandleToStatusBucket: cmHandleIdToStatus) {
89 final Iterator<Serializable> bucketIterator = cmHandleToStatusBucket.iterator();
90 while (bucketIterator.hasNext()) {
91 SubscriptionStatus.populateCmHandleToSubscriptionStatusMap(resultMap, bucketIterator);