Fix Absolute Path to list with Integer/String key
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2020-2022 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  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.spi.impl;
24
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.ImmutableSet.Builder;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
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.Matcher;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import javax.transaction.Transactional;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42 import org.hibernate.StaleStateException;
43 import org.onap.cps.cpspath.parser.CpsPathQuery;
44 import org.onap.cps.cpspath.parser.CpsPathUtil;
45 import org.onap.cps.cpspath.parser.PathParsingException;
46 import org.onap.cps.spi.CpsDataPersistenceService;
47 import org.onap.cps.spi.FetchDescendantsOption;
48 import org.onap.cps.spi.entities.AnchorEntity;
49 import org.onap.cps.spi.entities.DataspaceEntity;
50 import org.onap.cps.spi.entities.FragmentEntity;
51 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
52 import org.onap.cps.spi.exceptions.ConcurrencyException;
53 import org.onap.cps.spi.exceptions.CpsAdminException;
54 import org.onap.cps.spi.exceptions.CpsPathException;
55 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
56 import org.onap.cps.spi.model.DataNode;
57 import org.onap.cps.spi.model.DataNodeBuilder;
58 import org.onap.cps.spi.repository.AnchorRepository;
59 import org.onap.cps.spi.repository.DataspaceRepository;
60 import org.onap.cps.spi.repository.FragmentRepository;
61 import org.onap.cps.spi.utils.SessionManager;
62 import org.onap.cps.utils.JsonObjectMapper;
63 import org.springframework.dao.DataIntegrityViolationException;
64 import org.springframework.stereotype.Service;
65
66 @Service
67 @Slf4j
68 @RequiredArgsConstructor
69 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
70
71     private final DataspaceRepository dataspaceRepository;
72
73     private final AnchorRepository anchorRepository;
74
75     private final FragmentRepository fragmentRepository;
76
77     private final JsonObjectMapper jsonObjectMapper;
78
79     private final SessionManager sessionManager;
80
81     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
82     private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
83             Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
84
85     @Override
86     @Transactional
87     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentNodeXpath,
88         final DataNode newChildDataNode) {
89         addChildDataNodes(dataspaceName, anchorName, parentNodeXpath,  Collections.singleton(newChildDataNode));
90     }
91
92     @Override
93     @Transactional
94     public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
95         final Collection<DataNode> newListElements) {
96         addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, newListElements);
97     }
98
99     private void addChildDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
100                                 final Collection<DataNode> newChildren) {
101         final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
102         try {
103             for (final DataNode newChildAsDataNode : newChildren) {
104                 final FragmentEntity newChildAsFragmentEntity = convertToFragmentWithAllDescendants(
105                     parentFragmentEntity.getDataspace(),
106                     parentFragmentEntity.getAnchor(),
107                     newChildAsDataNode);
108                 newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
109                 fragmentRepository.save(newChildAsFragmentEntity);
110             }
111         } catch (final DataIntegrityViolationException exception) {
112             final List<String> conflictXpaths = newChildren.stream()
113                 .map(DataNode::getXpath)
114                 .collect(Collectors.toList());
115             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
116         }
117     }
118
119     @Override
120     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
121         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
122         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
123         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
124             dataNode);
125         try {
126             fragmentRepository.save(fragmentEntity);
127         } catch (final DataIntegrityViolationException exception) {
128             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
129         }
130     }
131
132     /**
133      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
134      * for all DataNode children recursively.
135      *
136      * @param dataspaceEntity       dataspace
137      * @param anchorEntity          anchorEntity
138      * @param dataNodeToBeConverted dataNode
139      * @return a Fragment built from current DataNode
140      */
141     private FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
142         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
143         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
144         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
145         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
146             final FragmentEntity childFragment =
147                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
148                     childDataNode);
149             childFragmentsImmutableSetBuilder.add(childFragment);
150         }
151         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
152         return parentFragment;
153     }
154
155     private FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
156         final AnchorEntity anchorEntity, final DataNode dataNode) {
157         return FragmentEntity.builder()
158             .dataspace(dataspaceEntity)
159             .anchor(anchorEntity)
160             .xpath(dataNode.getXpath())
161             .attributes(jsonObjectMapper.asJsonString(dataNode.getLeaves()))
162             .build();
163     }
164
165     @Override
166     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
167         final FetchDescendantsOption fetchDescendantsOption) {
168         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
169         return toDataNode(fragmentEntity, fetchDescendantsOption);
170     }
171
172     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
173         final String xpath) {
174         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
175         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
176         if (isRootXpath(xpath)) {
177             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
178         } else {
179             final String normalizedXpath;
180             try {
181                 normalizedXpath = CpsPathUtil.getNormalizedXpath(xpath);
182             } catch (final PathParsingException e) {
183                 throw new CpsPathException(e.getMessage());
184             }
185             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
186                     normalizedXpath);
187         }
188     }
189
190     @Override
191     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
192         final FetchDescendantsOption fetchDescendantsOption) {
193         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
194         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
195         final CpsPathQuery cpsPathQuery;
196         try {
197             cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath);
198         } catch (final PathParsingException e) {
199             throw new CpsPathException(e.getMessage());
200         }
201         List<FragmentEntity> fragmentEntities =
202             fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
203         if (cpsPathQuery.hasAncestorAxis()) {
204             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
205             fragmentEntities = ancestorXpaths.isEmpty()
206                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
207         }
208         return fragmentEntities.stream()
209             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
210             .collect(Collectors.toUnmodifiableList());
211     }
212
213     @Override
214     public String startSession() {
215         return sessionManager.startSession();
216     }
217
218     @Override
219     public void closeSession(final String sessionId) {
220         sessionManager.closeSession(sessionId);
221     }
222
223     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
224         final CpsPathQuery cpsPathQuery) {
225         final Set<String> ancestorXpath = new HashSet<>();
226         final Pattern pattern =
227             Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
228                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
229         for (final FragmentEntity fragmentEntity : fragmentEntities) {
230             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
231             if (matcher.matches()) {
232                 ancestorXpath.add(matcher.group(1));
233             }
234         }
235         return ancestorXpath;
236     }
237
238     private DataNode toDataNode(final FragmentEntity fragmentEntity,
239         final FetchDescendantsOption fetchDescendantsOption) {
240         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
241         Map<String, Object> leaves = new HashMap<>();
242         if (fragmentEntity.getAttributes() != null) {
243             leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
244         }
245         return new DataNodeBuilder()
246             .withXpath(fragmentEntity.getXpath())
247             .withLeaves(leaves)
248             .withChildDataNodes(childDataNodes).build();
249     }
250
251     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
252         final FetchDescendantsOption fetchDescendantsOption) {
253         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
254             return fragmentEntity.getChildFragments().stream()
255                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
256                 .collect(Collectors.toUnmodifiableList());
257         }
258         return Collections.emptyList();
259     }
260
261     @Override
262     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
263         final Map<String, Object> leaves) {
264         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
265         fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
266         fragmentRepository.save(fragmentEntity);
267     }
268
269     @Override
270     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
271         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
272         replaceDataNodeTree(fragmentEntity, dataNode);
273         try {
274             fragmentRepository.save(fragmentEntity);
275         } catch (final StaleStateException staleStateException) {
276             throw new ConcurrencyException("Concurrent Transactions",
277                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
278                     dataspaceName, anchorName, dataNode.getXpath()),
279                 staleStateException);
280         }
281     }
282
283     private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
284                                             final DataNode newDataNode) {
285
286         existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
287
288         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
289             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
290
291         final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
292
293         for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
294             final FragmentEntity childFragment;
295             if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
296                 childFragment = convertToFragmentWithAllDescendants(
297                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
298             } else {
299                 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
300                 replaceDataNodeTree(childFragment, newDataNodeChild);
301             }
302             updatedChildFragments.add(childFragment);
303         }
304         existingFragmentEntity.getChildFragments().clear();
305         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
306     }
307
308     @Override
309     @Transactional
310     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
311                                    final Collection<DataNode> newListElements) {
312         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
313         final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
314         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
315             extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
316         deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
317         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
318         for (final DataNode newListElement : newListElements) {
319             final FragmentEntity existingListElementEntity =
320                 existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
321             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
322                 existingListElementEntity);
323
324             updatedChildFragmentEntities.add(entityToBeAdded);
325         }
326         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
327         fragmentRepository.save(parentEntity);
328     }
329
330     @Override
331     @Transactional
332     public void deleteDataNodes(final String dataspaceName, final String anchorName) {
333         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
334         anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
335             .ifPresent(
336                 anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
337     }
338
339     @Override
340     @Transactional
341     public void deleteListDataNode(final String dataspaceName, final String anchorName,
342                                    final String targetXpath) {
343         deleteDataNode(dataspaceName, anchorName, targetXpath, true);
344     }
345
346     @Override
347     @Transactional
348     public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
349         deleteDataNode(dataspaceName, anchorName, targetXpath, false);
350     }
351
352     private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
353         final boolean onlySupportListNodeDeletion) {
354         final String parentNodeXpath;
355         FragmentEntity parentFragmentEntity = null;
356         boolean targetDeleted = false;
357         if (isRootXpath(targetXpath)) {
358             deleteDataNodes(dataspaceName, anchorName);
359             targetDeleted = true;
360         } else {
361             if (isRootContainerNodeXpath(targetXpath)) {
362                 parentNodeXpath = targetXpath;
363             } else {
364                 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
365             }
366             parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
367             final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
368             final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
369                 .matcher(lastXpathElement).find();
370             if (isListElement) {
371                 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
372             } else {
373                 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
374                 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
375                 if (tryToDeleteDataNode) {
376                     targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
377                 }
378             }
379         }
380         if (!targetDeleted) {
381             final String additionalInformation = onlySupportListNodeDeletion
382                 ? "The target is probably not a List." : "";
383             throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
384                 parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
385         }
386     }
387
388     private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
389         final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath);
390         if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) {
391             fragmentRepository.delete(parentFragmentEntity);
392             return true;
393         }
394         if (parentFragmentEntity.getChildFragments()
395             .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
396             fragmentRepository.save(parentFragmentEntity);
397             return true;
398         }
399         return false;
400     }
401
402     private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
403         final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath);
404         final String deleteTargetXpathPrefix = normalizedListXpath + "[";
405         if (parentFragmentEntity.getChildFragments()
406             .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
407             fragmentRepository.save(parentFragmentEntity);
408             return true;
409         }
410         return false;
411     }
412
413     private static void deleteListElements(
414         final Collection<FragmentEntity> fragmentEntities,
415         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
416         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
417     }
418
419     private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
420         if (newListElements.isEmpty()) {
421             throw new CpsAdminException("Invalid list replacement",
422                 "Cannot replace list elements with empty collection");
423         }
424         final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
425         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
426     }
427
428     private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
429                                                             final DataNode newListElement,
430                                                             final FragmentEntity existingListElementEntity) {
431         if (existingListElementEntity == null) {
432             return convertToFragmentWithAllDescendants(
433                 parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
434         }
435         if (newListElement.getChildDataNodes().isEmpty()) {
436             copyAttributesFromNewListElement(existingListElementEntity, newListElement);
437             existingListElementEntity.getChildFragments().clear();
438         } else {
439             replaceDataNodeTree(existingListElementEntity, newListElement);
440         }
441         return existingListElementEntity;
442     }
443
444     private static boolean isNewDataNode(final DataNode replacementDataNode,
445                                          final Map<String, FragmentEntity> existingListElementsByXpath) {
446         return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
447     }
448
449     private static boolean isRootContainerNodeXpath(final String xpath) {
450         return 0 == xpath.lastIndexOf('/');
451     }
452
453     private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
454                                                          final DataNode newListElement) {
455         final FragmentEntity replacementFragmentEntity =
456                 FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
457                         newListElement.getLeaves())).build();
458         existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
459     }
460
461     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
462         final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
463         return childEntities.stream()
464             .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
465             .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
466     }
467
468     private static boolean isRootXpath(final String xpath) {
469         return "/".equals(xpath) || "".equals(xpath);
470     }
471 }