Merge "Data fragment update by xpath #2 - persistence layer"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
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  *  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.impl;
22
23 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
24
25 import com.google.common.collect.ImmutableSet;
26 import com.google.common.collect.ImmutableSet.Builder;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34 import org.onap.cps.spi.CpsDataPersistenceService;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.entities.AnchorEntity;
37 import org.onap.cps.spi.entities.DataspaceEntity;
38 import org.onap.cps.spi.entities.FragmentEntity;
39 import org.onap.cps.spi.model.DataNode;
40 import org.onap.cps.spi.model.DataNodeBuilder;
41 import org.onap.cps.spi.repository.AnchorRepository;
42 import org.onap.cps.spi.repository.DataspaceRepository;
43 import org.onap.cps.spi.repository.FragmentRepository;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Service;
46
47 @Service
48 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
49
50     @Autowired
51     private DataspaceRepository dataspaceRepository;
52
53     @Autowired
54     private AnchorRepository anchorRepository;
55
56     @Autowired
57     private FragmentRepository fragmentRepository;
58
59     private static final Gson GSON = new GsonBuilder().create();
60
61     @Override
62     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
63         final DataNode dataNode) {
64         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
65         final FragmentEntity fragmentEntity =
66             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
67         parentFragment.getChildFragments().add(fragmentEntity);
68         fragmentRepository.save(parentFragment);
69     }
70
71     @Override
72     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
73         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
74         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
75         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
76             dataNode);
77         fragmentRepository.save(fragmentEntity);
78     }
79
80     /**
81      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
82      * for all DataNode children recursively.
83      *
84      * @param dataspaceEntity       dataspace
85      * @param anchorEntity          anchorEntity
86      * @param dataNodeToBeConverted dataNode
87      * @return a Fragment built from current DataNode
88      */
89     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
90         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
91         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
92         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
93         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
94             final FragmentEntity childFragment =
95                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
96                     childDataNode);
97             childFragmentsImmutableSetBuilder.add(childFragment);
98         }
99         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
100         return parentFragment;
101     }
102
103     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
104         final AnchorEntity anchorEntity, final DataNode dataNode) {
105         return FragmentEntity.builder()
106             .dataspace(dataspaceEntity)
107             .anchor(anchorEntity)
108             .xpath(dataNode.getXpath())
109             .attributes(GSON.toJson(dataNode.getLeaves()))
110             .build();
111     }
112
113     @Override
114     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
115         final FetchDescendantsOption fetchDescendantsOption) {
116         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
117         return toDataNode(fragmentEntity, fetchDescendantsOption);
118     }
119
120     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
121         final String xpath) {
122         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
123         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
124         return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
125     }
126
127     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
128         final FetchDescendantsOption fetchDescendantsOption) {
129         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
130         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
131         return new DataNodeBuilder()
132             .withXpath(fragmentEntity.getXpath())
133             .withLeaves(leaves)
134             .withChildDataNodes(childDataNodes).build();
135     }
136
137     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
138         final FetchDescendantsOption fetchDescendantsOption) {
139         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
140             return fragmentEntity.getChildFragments().stream()
141                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
142                 .collect(Collectors.toUnmodifiableList());
143         }
144         return Collections.emptyList();
145     }
146
147     @Override
148     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
149         final Map<String, Object> leaves) {
150         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
151         fragmentEntity.setAttributes(GSON.toJson(leaves));
152         fragmentRepository.save(fragmentEntity);
153     }
154
155     @Override
156     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
157         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
158         removeExistingDescendants(fragmentEntity);
159
160         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
161         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
162             childDataNode -> convertToFragmentWithAllDescendants(
163                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
164         ).collect(Collectors.toUnmodifiableSet());
165         fragmentEntity.setChildFragments(childFragmentEntities);
166
167         fragmentRepository.save(fragmentEntity);
168     }
169
170     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
171         fragmentEntity.setChildFragments(Collections.emptySet());
172         fragmentRepository.save(fragmentEntity);
173     }
174 }