Replace list-node content (part 1): CPS Service and persistence layers
[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  *  Modifications Copyright (C) 2020-2021 Bell Canada.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
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.impl;
23
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
25
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.ImmutableSet.Builder;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import javax.transaction.Transactional;
39 import org.onap.cps.spi.CpsDataPersistenceService;
40 import org.onap.cps.spi.FetchDescendantsOption;
41 import org.onap.cps.spi.entities.AnchorEntity;
42 import org.onap.cps.spi.entities.DataspaceEntity;
43 import org.onap.cps.spi.entities.FragmentEntity;
44 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
45 import org.onap.cps.spi.model.DataNode;
46 import org.onap.cps.spi.model.DataNodeBuilder;
47 import org.onap.cps.spi.query.CpsPathQuery;
48 import org.onap.cps.spi.query.CpsPathQueryType;
49 import org.onap.cps.spi.repository.AnchorRepository;
50 import org.onap.cps.spi.repository.DataspaceRepository;
51 import org.onap.cps.spi.repository.FragmentRepository;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.dao.DataIntegrityViolationException;
54 import org.springframework.stereotype.Service;
55
56 @Service
57 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
58
59     @Autowired
60     private DataspaceRepository dataspaceRepository;
61
62     @Autowired
63     private AnchorRepository anchorRepository;
64
65     @Autowired
66     private FragmentRepository fragmentRepository;
67
68     private static final Gson GSON = new GsonBuilder().create();
69     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
70
71     @Override
72     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
73         final DataNode dataNode) {
74         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
75         final var fragmentEntity =
76             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
77         parentFragment.getChildFragments().add(fragmentEntity);
78         try {
79             fragmentRepository.save(parentFragment);
80         } catch (final DataIntegrityViolationException exception) {
81             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
82         }
83     }
84
85     @Override
86     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
87         final Collection<DataNode> dataNodes) {
88         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
89         final List<FragmentEntity> newFragmentEntities =
90             dataNodes.stream().map(
91                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
92             ).collect(Collectors.toUnmodifiableList());
93         parentFragment.getChildFragments().addAll(newFragmentEntities);
94         try {
95             fragmentRepository.save(parentFragment);
96         } catch (final DataIntegrityViolationException exception) {
97             final List<String> conflictXpaths = dataNodes.stream()
98                 .map(DataNode::getXpath)
99                 .collect(Collectors.toList());
100             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
101         }
102     }
103
104     @Override
105     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
106         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
107         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
108         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
109             dataNode);
110         try {
111             fragmentRepository.save(fragmentEntity);
112         } catch (final DataIntegrityViolationException exception) {
113             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
114         }
115     }
116
117     /**
118      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
119      * for all DataNode children recursively.
120      *
121      * @param dataspaceEntity       dataspace
122      * @param anchorEntity          anchorEntity
123      * @param dataNodeToBeConverted dataNode
124      * @return a Fragment built from current DataNode
125      */
126     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
127         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
128         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
129         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
130         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
131             final FragmentEntity childFragment =
132                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
133                     childDataNode);
134             childFragmentsImmutableSetBuilder.add(childFragment);
135         }
136         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
137         return parentFragment;
138     }
139
140     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
141         final AnchorEntity anchorEntity, final DataNode dataNode) {
142         return FragmentEntity.builder()
143             .dataspace(dataspaceEntity)
144             .anchor(anchorEntity)
145             .xpath(dataNode.getXpath())
146             .attributes(GSON.toJson(dataNode.getLeaves()))
147             .build();
148     }
149
150     @Override
151     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
152         final FetchDescendantsOption fetchDescendantsOption) {
153         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
154         return toDataNode(fragmentEntity, fetchDescendantsOption);
155     }
156
157     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
158         final String xpath) {
159         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
160         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
161         if (isRootXpath(xpath)) {
162             return fragmentRepository.getFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
163         } else {
164             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
165                 xpath);
166         }
167     }
168
169     @Override
170     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
171         final FetchDescendantsOption fetchDescendantsOption) {
172         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
173         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
174         final var cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
175         List<FragmentEntity> fragmentEntities;
176         if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
177             fragmentEntities = fragmentRepository
178                 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
179                     cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
180         } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
181             final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
182             fragmentEntities = fragmentRepository
183                 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
184                     leafDataAsJson);
185         } else {
186             fragmentEntities = fragmentRepository
187                 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
188         }
189         if (cpsPathQuery.hasAncestorAxis()) {
190             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
191             fragmentEntities = ancestorXpaths.isEmpty()
192                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
193         }
194         return fragmentEntities.stream()
195             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
196             .collect(Collectors.toUnmodifiableList());
197     }
198
199     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
200         final CpsPathQuery cpsPathQuery) {
201         final Set<String> ancestorXpath = new HashSet<>();
202         final var pattern =
203             Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
204                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
205         for (final FragmentEntity fragmentEntity : fragmentEntities) {
206             final var matcher = pattern.matcher(fragmentEntity.getXpath());
207             if (matcher.matches()) {
208                 ancestorXpath.add(matcher.group(1));
209             }
210         }
211         return ancestorXpath;
212     }
213
214     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
215         final FetchDescendantsOption fetchDescendantsOption) {
216         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
217         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
218         return new DataNodeBuilder()
219             .withXpath(fragmentEntity.getXpath())
220             .withLeaves(leaves)
221             .withChildDataNodes(childDataNodes).build();
222     }
223
224     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
225         final FetchDescendantsOption fetchDescendantsOption) {
226         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
227             return fragmentEntity.getChildFragments().stream()
228                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
229                 .collect(Collectors.toUnmodifiableList());
230         }
231         return Collections.emptyList();
232     }
233
234     @Override
235     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
236         final Map<String, Object> leaves) {
237         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
238         fragmentEntity.setAttributes(GSON.toJson(leaves));
239         fragmentRepository.save(fragmentEntity);
240     }
241
242     @Override
243     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
244         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
245         removeExistingDescendants(fragmentEntity);
246
247         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
248         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
249             childDataNode -> convertToFragmentWithAllDescendants(
250                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
251         ).collect(Collectors.toUnmodifiableSet());
252         fragmentEntity.setChildFragments(childFragmentEntities);
253
254         fragmentRepository.save(fragmentEntity);
255     }
256
257     @Override
258     @Transactional
259     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
260         final Collection<DataNode> dataNodes) {
261         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
262         final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
263         final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
264         removeListNodeDescendants(parentEntity, listNodeXpath);
265         final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
266             dataNode -> convertToFragmentWithAllDescendants(
267                 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
268         ).collect(Collectors.toUnmodifiableSet());
269         parentEntity.getChildFragments().addAll(childFragmentEntities);
270         fragmentRepository.save(parentEntity);
271     }
272
273     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
274         final String listNodeXpathPrefix = listNodeXpath + "[";
275         if (parentFragmentEntity.getChildFragments()
276             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
277             fragmentRepository.save(parentFragmentEntity);
278         }
279     }
280
281     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
282         fragmentEntity.setChildFragments(Collections.emptySet());
283         fragmentRepository.save(fragmentEntity);
284     }
285
286     private boolean isRootXpath(final String xpath) {
287         return "/".equals(xpath) || "".equals(xpath);
288     }
289 }