Improve 32K limit tests
[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.Iterator;
27 import java.util.List;
28 import java.util.Map;
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;
35
36 @NoArgsConstructor(access = AccessLevel.PRIVATE)
37 public class DataNodeHelper {
38
39     /**
40      * The nested DataNode object is being flattened.
41      *
42      * @param dataNode object.
43      * @return DataNode as stream.
44      */
45     public static Stream<DataNode> flatten(final DataNode dataNode) {
46         return Stream.concat(Stream.of(dataNode),
47                 dataNode.getChildDataNodes().stream().flatMap(DataNodeHelper::flatten));
48     }
49
50     /**
51      * The leaves for each DataNode is listed as map.
52      *
53      * @param dataNodes as collection.
54      * @return list of map for the all leaves.
55      */
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());
61     }
62
63     /**
64      * The cm handle and status is listed as a collection.
65      *
66      * @param dataNodeLeaves as a list of map.
67      * @return list of collection containing cm handle id and statuses.
68      */
69     public static List<Collection<Serializable>> getCmHandleIdToStatus(
70             final List<Map<String, Serializable>> dataNodeLeaves) {
71         return dataNodeLeaves.stream()
72                 .map(Map::values)
73                 .filter(col -> col.contains("PENDING")
74                         || col.contains("ACCEPTED")
75                         || col.contains("REJECTED"))
76                 .collect(Collectors.toList());
77     }
78
79     /**
80      * The cm handle and status is returned as a map.
81      *
82      * @param cmHandleIdToStatus as a list of collection
83      * @return a map of cm handle id to status
84      */
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);
92             }
93         }
94         return resultMap;
95     }
96
97     /**
98      * Extracts the mapping of cm handle id to status from data node collection.
99      *
100      * @param dataNodes as a collection
101      * @return cm handle id to status mapping
102      */
103     public static Map<String, SubscriptionStatus> getCmHandleIdToStatusMapFromDataNodes(
104             final Collection<DataNode> dataNodes) {
105         return getCmHandleIdToStatusMap(getCmHandleIdToStatus(getDataNodeLeaves(dataNodes)));
106     }
107 }