Add graceful shutdown for Session Manager
[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, SessionManager.WITH_COMMIT);
221     }
222
223     @Override
224     public void lockAnchor(final String sessionId, final String dataspaceName,
225                             final String anchorName, final Long timeoutInMilliseconds) {
226         sessionManager.lockAnchor(sessionId, dataspaceName, anchorName, timeoutInMilliseconds);
227     }
228
229     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
230         final CpsPathQuery cpsPathQuery) {
231         final Set<String> ancestorXpath = new HashSet<>();
232         final Pattern pattern =
233             Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
234                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
235         for (final FragmentEntity fragmentEntity : fragmentEntities) {
236             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
237             if (matcher.matches()) {
238                 ancestorXpath.add(matcher.group(1));
239             }
240         }
241         return ancestorXpath;
242     }
243
244     private DataNode toDataNode(final FragmentEntity fragmentEntity,
245         final FetchDescendantsOption fetchDescendantsOption) {
246         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
247         Map<String, Object> leaves = new HashMap<>();
248         if (fragmentEntity.getAttributes() != null) {
249             leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
250         }
251         return new DataNodeBuilder()
252             .withXpath(fragmentEntity.getXpath())
253             .withLeaves(leaves)
254             .withChildDataNodes(childDataNodes).build();
255     }
256
257     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
258         final FetchDescendantsOption fetchDescendantsOption) {
259         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
260             return fragmentEntity.getChildFragments().stream()
261                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
262                 .collect(Collectors.toUnmodifiableList());
263         }
264         return Collections.emptyList();
265     }
266
267     @Override
268     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
269         final Map<String, Object> leaves) {
270         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
271         fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
272         fragmentRepository.save(fragmentEntity);
273     }
274
275     @Override
276     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
277         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
278         replaceDataNodeTree(fragmentEntity, dataNode);
279         try {
280             fragmentRepository.save(fragmentEntity);
281         } catch (final StaleStateException staleStateException) {
282             throw new ConcurrencyException("Concurrent Transactions",
283                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
284                     dataspaceName, anchorName, dataNode.getXpath()),
285                 staleStateException);
286         }
287     }
288
289     private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
290                                             final DataNode newDataNode) {
291
292         existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
293
294         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
295             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
296
297         final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
298
299         for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
300             final FragmentEntity childFragment;
301             if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
302                 childFragment = convertToFragmentWithAllDescendants(
303                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
304             } else {
305                 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
306                 replaceDataNodeTree(childFragment, newDataNodeChild);
307             }
308             updatedChildFragments.add(childFragment);
309         }
310         existingFragmentEntity.getChildFragments().clear();
311         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
312     }
313
314     @Override
315     @Transactional
316     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
317                                    final Collection<DataNode> newListElements) {
318         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
319         final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
320         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
321             extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
322         deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
323         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
324         for (final DataNode newListElement : newListElements) {
325             final FragmentEntity existingListElementEntity =
326                 existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
327             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
328                 existingListElementEntity);
329
330             updatedChildFragmentEntities.add(entityToBeAdded);
331         }
332         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
333         fragmentRepository.save(parentEntity);
334     }
335
336     @Override
337     @Transactional
338     public void deleteDataNodes(final String dataspaceName, final String anchorName) {
339         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
340         anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
341             .ifPresent(
342                 anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
343     }
344
345     @Override
346     @Transactional
347     public void deleteListDataNode(final String dataspaceName, final String anchorName,
348                                    final String targetXpath) {
349         deleteDataNode(dataspaceName, anchorName, targetXpath, true);
350     }
351
352     @Override
353     @Transactional
354     public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
355         deleteDataNode(dataspaceName, anchorName, targetXpath, false);
356     }
357
358     private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
359         final boolean onlySupportListNodeDeletion) {
360         final String parentNodeXpath;
361         FragmentEntity parentFragmentEntity = null;
362         boolean targetDeleted = false;
363         if (isRootXpath(targetXpath)) {
364             deleteDataNodes(dataspaceName, anchorName);
365             targetDeleted = true;
366         } else {
367             if (isRootContainerNodeXpath(targetXpath)) {
368                 parentNodeXpath = targetXpath;
369             } else {
370                 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
371             }
372             parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
373             final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
374             final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
375                 .matcher(lastXpathElement).find();
376             if (isListElement) {
377                 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
378             } else {
379                 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
380                 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
381                 if (tryToDeleteDataNode) {
382                     targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
383                 }
384             }
385         }
386         if (!targetDeleted) {
387             final String additionalInformation = onlySupportListNodeDeletion
388                 ? "The target is probably not a List." : "";
389             throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
390                 parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
391         }
392     }
393
394     private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
395         final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath);
396         if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) {
397             fragmentRepository.delete(parentFragmentEntity);
398             return true;
399         }
400         if (parentFragmentEntity.getChildFragments()
401             .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
402             fragmentRepository.save(parentFragmentEntity);
403             return true;
404         }
405         return false;
406     }
407
408     private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
409         final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath);
410         final String deleteTargetXpathPrefix = normalizedListXpath + "[";
411         if (parentFragmentEntity.getChildFragments()
412             .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
413             fragmentRepository.save(parentFragmentEntity);
414             return true;
415         }
416         return false;
417     }
418
419     private static void deleteListElements(
420         final Collection<FragmentEntity> fragmentEntities,
421         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
422         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
423     }
424
425     private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
426         if (newListElements.isEmpty()) {
427             throw new CpsAdminException("Invalid list replacement",
428                 "Cannot replace list elements with empty collection");
429         }
430         final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
431         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
432     }
433
434     private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
435                                                             final DataNode newListElement,
436                                                             final FragmentEntity existingListElementEntity) {
437         if (existingListElementEntity == null) {
438             return convertToFragmentWithAllDescendants(
439                 parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
440         }
441         if (newListElement.getChildDataNodes().isEmpty()) {
442             copyAttributesFromNewListElement(existingListElementEntity, newListElement);
443             existingListElementEntity.getChildFragments().clear();
444         } else {
445             replaceDataNodeTree(existingListElementEntity, newListElement);
446         }
447         return existingListElementEntity;
448     }
449
450     private static boolean isNewDataNode(final DataNode replacementDataNode,
451                                          final Map<String, FragmentEntity> existingListElementsByXpath) {
452         return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
453     }
454
455     private static boolean isRootContainerNodeXpath(final String xpath) {
456         return 0 == xpath.lastIndexOf('/');
457     }
458
459     private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
460                                                          final DataNode newListElement) {
461         final FragmentEntity replacementFragmentEntity =
462                 FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
463                         newListElement.getLeaves())).build();
464         existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
465     }
466
467     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
468         final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
469         return childEntities.stream()
470             .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
471             .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
472     }
473
474     private static boolean isRootXpath(final String xpath) {
475         return "/".equals(xpath) || "".equals(xpath);
476     }
477 }