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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.spi.impl;
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
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;
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;
68 @RequiredArgsConstructor
69 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
71 private final DataspaceRepository dataspaceRepository;
73 private final AnchorRepository anchorRepository;
75 private final FragmentRepository fragmentRepository;
77 private final JsonObjectMapper jsonObjectMapper;
79 private final SessionManager sessionManager;
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}))\\]$");
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));
94 public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
95 final Collection<DataNode> newListElements) {
96 addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, newListElements);
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);
103 for (final DataNode newChildAsDataNode : newChildren) {
104 final FragmentEntity newChildAsFragmentEntity = convertToFragmentWithAllDescendants(
105 parentFragmentEntity.getDataspace(),
106 parentFragmentEntity.getAnchor(),
108 newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
109 fragmentRepository.save(newChildAsFragmentEntity);
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);
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,
126 fragmentRepository.save(fragmentEntity);
127 } catch (final DataIntegrityViolationException exception) {
128 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
133 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
134 * for all DataNode children recursively.
136 * @param dataspaceEntity dataspace
137 * @param anchorEntity anchorEntity
138 * @param dataNodeToBeConverted dataNode
139 * @return a Fragment built from current DataNode
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(),
149 childFragmentsImmutableSetBuilder.add(childFragment);
151 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
152 return parentFragment;
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()))
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);
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);
179 final String normalizedXpath;
181 normalizedXpath = CpsPathUtil.getNormalizedXpath(xpath);
182 } catch (final PathParsingException e) {
183 throw new CpsPathException(e.getMessage());
185 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
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;
197 cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath);
198 } catch (final PathParsingException e) {
199 throw new CpsPathException(e.getMessage());
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);
208 return fragmentEntities.stream()
209 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
210 .collect(Collectors.toUnmodifiableList());
214 public String startSession() {
215 return sessionManager.startSession();
219 public void closeSession(final String sessionId) {
220 sessionManager.closeSession(sessionId);
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));
235 return ancestorXpath;
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);
245 return new DataNodeBuilder()
246 .withXpath(fragmentEntity.getXpath())
248 .withChildDataNodes(childDataNodes).build();
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());
258 return Collections.emptyList();
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);
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);
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);
283 private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
284 final DataNode newDataNode) {
286 existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
288 final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
289 .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
291 final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
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);
299 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
300 replaceDataNodeTree(childFragment, newDataNodeChild);
302 updatedChildFragments.add(childFragment);
304 existingFragmentEntity.getChildFragments().clear();
305 existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
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);
324 updatedChildFragmentEntities.add(entityToBeAdded);
326 parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
327 fragmentRepository.save(parentEntity);
332 public void deleteDataNodes(final String dataspaceName, final String anchorName) {
333 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
334 anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
336 anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
341 public void deleteListDataNode(final String dataspaceName, final String anchorName,
342 final String targetXpath) {
343 deleteDataNode(dataspaceName, anchorName, targetXpath, true);
348 public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
349 deleteDataNode(dataspaceName, anchorName, targetXpath, false);
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;
361 if (isRootContainerNodeXpath(targetXpath)) {
362 parentNodeXpath = targetXpath;
364 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
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();
371 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
373 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
374 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
375 if (tryToDeleteDataNode) {
376 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
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);
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);
394 if (parentFragmentEntity.getChildFragments()
395 .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
396 fragmentRepository.save(parentFragmentEntity);
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);
413 private static void deleteListElements(
414 final Collection<FragmentEntity> fragmentEntities,
415 final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
416 fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
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");
424 final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
425 return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
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);
435 if (newListElement.getChildDataNodes().isEmpty()) {
436 copyAttributesFromNewListElement(existingListElementEntity, newListElement);
437 existingListElementEntity.getChildFragments().clear();
439 replaceDataNodeTree(existingListElementEntity, newListElement);
441 return existingListElementEntity;
444 private static boolean isNewDataNode(final DataNode replacementDataNode,
445 final Map<String, FragmentEntity> existingListElementsByXpath) {
446 return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
449 private static boolean isRootContainerNodeXpath(final String xpath) {
450 return 0 == xpath.lastIndexOf('/');
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());
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));
468 private static boolean isRootXpath(final String xpath) {
469 return "/".equals(xpath) || "".equals(xpath);