NCMP: Delete DatastoreType enum from cps-ncmp-rest
[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  *  ================================================================================
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.spi.entities;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29
30 @NoArgsConstructor(access = AccessLevel.PRIVATE)
31 public class FragmentEntityArranger {
32
33     /**
34      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
35      *
36      * @param anchorEntity the anchor(entity) all the fragments belong to
37      * @param fragmentExtracts FragmentExtracts to convert
38      * @return a collection of FragmentEntities (trees) with descendants.
39      */
40     public static Collection<FragmentEntity> toFragmentEntityTrees(final AnchorEntity anchorEntity,
41                                                       final Collection<FragmentExtract> fragmentExtracts) {
42         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
43         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
44             final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
45             fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
46         }
47         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
48     }
49
50     /**
51      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
52      *
53      * @param anchorEntityPerId the anchor(entities) the fragments belong to
54      * @param fragmentExtracts FragmentExtracts to convert
55      * @return a collection of FragmentEntities (trees) with descendants.
56      */
57     public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
58             final Map<Integer, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
59         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
60         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
61             final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
62             final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
63             fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
64         }
65         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
66     }
67
68     private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
69                                                    final FragmentExtract fragmentExtract) {
70         final FragmentEntity fragmentEntity = new FragmentEntity();
71         if (anchorEntity != null) {
72             fragmentEntity.setAnchor(anchorEntity);
73             fragmentEntity.setDataspace(anchorEntity.getDataspace());
74         }
75         fragmentEntity.setId(fragmentExtract.getId());
76         fragmentEntity.setXpath(fragmentExtract.getXpath());
77         fragmentEntity.setAttributes(fragmentExtract.getAttributes());
78         fragmentEntity.setParentId(fragmentExtract.getParentId());
79         fragmentEntity.setChildFragments(new HashSet<>());
80         return fragmentEntity;
81     }
82
83     private static Collection<FragmentEntity> reuniteChildrenWithTheirParents(
84         final Map<Long, FragmentEntity> fragmentEntityPerId) {
85         final Collection<FragmentEntity> fragmentEntitiesWithoutParentInResultSet = new HashSet<>();
86         for (final FragmentEntity fragmentEntity : fragmentEntityPerId.values()) {
87             final FragmentEntity parentFragmentEntity = fragmentEntityPerId.get(fragmentEntity.getParentId());
88             if (parentFragmentEntity == null) {
89                 fragmentEntitiesWithoutParentInResultSet.add(fragmentEntity);
90             } else {
91                 parentFragmentEntity.getChildFragments().add(fragmentEntity);
92             }
93         }
94         return fragmentEntitiesWithoutParentInResultSet;
95     }
96
97 }