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);
224 public void lockAnchor(final String sessionId, final String dataspaceName,
225 final String anchorName, final Long timeoutInMilliseconds) {
226 sessionManager.lockAnchor(sessionId, dataspaceName, anchorName, timeoutInMilliseconds);
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));
241 return ancestorXpath;
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);
251 return new DataNodeBuilder()
252 .withXpath(fragmentEntity.getXpath())
254 .withChildDataNodes(childDataNodes).build();
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());
264 return Collections.emptyList();
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);
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);
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);
289 private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
290 final DataNode newDataNode) {
292 existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
294 final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
295 .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
297 final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
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);
305 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
306 replaceDataNodeTree(childFragment, newDataNodeChild);
308 updatedChildFragments.add(childFragment);
310 existingFragmentEntity.getChildFragments().clear();
311 existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
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);
330 updatedChildFragmentEntities.add(entityToBeAdded);
332 parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
333 fragmentRepository.save(parentEntity);
338 public void deleteDataNodes(final String dataspaceName, final String anchorName) {
339 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
340 anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
342 anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
347 public void deleteListDataNode(final String dataspaceName, final String anchorName,
348 final String targetXpath) {
349 deleteDataNode(dataspaceName, anchorName, targetXpath, true);
354 public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
355 deleteDataNode(dataspaceName, anchorName, targetXpath, false);
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;
367 if (isRootContainerNodeXpath(targetXpath)) {
368 parentNodeXpath = targetXpath;
370 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
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();
377 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
379 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
380 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
381 if (tryToDeleteDataNode) {
382 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
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);
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);
400 if (parentFragmentEntity.getChildFragments()
401 .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
402 fragmentRepository.save(parentFragmentEntity);
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);
419 private static void deleteListElements(
420 final Collection<FragmentEntity> fragmentEntities,
421 final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
422 fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
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");
430 final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
431 return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
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);
441 if (newListElement.getChildDataNodes().isEmpty()) {
442 copyAttributesFromNewListElement(existingListElementEntity, newListElement);
443 existingListElementEntity.getChildFragments().clear();
445 replaceDataNodeTree(existingListElementEntity, newListElement);
447 return existingListElementEntity;
450 private static boolean isNewDataNode(final DataNode replacementDataNode,
451 final Map<String, FragmentEntity> existingListElementsByXpath) {
452 return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
455 private static boolean isRootContainerNodeXpath(final String xpath) {
456 return 0 == xpath.lastIndexOf('/');
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());
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));
474 private static boolean isRootXpath(final String xpath) {
475 return "/".equals(xpath) || "".equals(xpath);