Merge "DMI Data AVC to use kafka headers"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / entities / FragmentEntityArranger.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.entities;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public class FragmentEntityArranger {
33
34     /**
35      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
36      *
37      * @param anchorEntity the anchor(entity) all the fragments belong to
38      * @param fragmentExtracts FragmentExtracts to convert
39      * @return a collection of FragmentEntities (trees) with descendants.
40      */
41     public static Collection<FragmentEntity> toFragmentEntityTrees(final AnchorEntity anchorEntity,
42                                                       final Collection<FragmentExtract> fragmentExtracts) {
43         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
44         if (fragmentExtracts !=  null) {
45             for (final FragmentExtract fragmentExtract : fragmentExtracts) {
46                 final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
47                 fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
48             }
49         }
50         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
51     }
52
53     /**
54      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
55      *
56      * @param anchorEntityPerId the anchor(entities) the fragments belong to
57      * @param fragmentExtracts FragmentExtracts to convert
58      * @return a collection of FragmentEntities (trees) with descendants.
59      */
60     public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
61             final Map<Integer, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
62         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
63         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
64             final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
65             final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
66             fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
67         }
68         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
69     }
70
71     private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
72                                                    final FragmentExtract fragmentExtract) {
73         final FragmentEntity fragmentEntity = new FragmentEntity();
74         if (anchorEntity != null) {
75             fragmentEntity.setAnchor(anchorEntity);
76             fragmentEntity.setDataspace(anchorEntity.getDataspace());
77         }
78         fragmentEntity.setId(fragmentExtract.getId());
79         fragmentEntity.setXpath(fragmentExtract.getXpath());
80         fragmentEntity.setAttributes(fragmentExtract.getAttributes());
81         fragmentEntity.setParentId(fragmentExtract.getParentId());
82         fragmentEntity.setChildFragments(new HashSet<>());
83         return fragmentEntity;
84     }
85
86     private static Collection<FragmentEntity> reuniteChildrenWithTheirParents(
87         final Map<Long, FragmentEntity> fragmentEntityPerId) {
88         final Collection<FragmentEntity> fragmentEntitiesWithoutParentInResultSet = new HashSet<>();
89         for (final FragmentEntity fragmentEntity : fragmentEntityPerId.values()) {
90             final FragmentEntity parentFragmentEntity = fragmentEntityPerId.get(fragmentEntity.getParentId());
91             if (parentFragmentEntity == null) {
92                 fragmentEntitiesWithoutParentInResultSet.add(fragmentEntity);
93             } else {
94                 parentFragmentEntity.getChildFragments().add(fragmentEntity);
95             }
96         }
97         return fragmentEntitiesWithoutParentInResultSet;
98     }
99
100 }