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.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import java.util.stream.Collectors;
40 import javax.transaction.Transactional;
41 import lombok.RequiredArgsConstructor;
42 import lombok.extern.slf4j.Slf4j;
43 import org.hibernate.StaleStateException;
44 import org.onap.cps.cpspath.parser.CpsPathQuery;
45 import org.onap.cps.cpspath.parser.CpsPathUtil;
46 import org.onap.cps.cpspath.parser.PathParsingException;
47 import org.onap.cps.spi.CpsDataPersistenceService;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.entities.AnchorEntity;
50 import org.onap.cps.spi.entities.DataspaceEntity;
51 import org.onap.cps.spi.entities.FragmentEntity;
52 import org.onap.cps.spi.entities.SchemaSetEntity;
53 import org.onap.cps.spi.entities.YangResourceEntity;
54 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
55 import org.onap.cps.spi.exceptions.ConcurrencyException;
56 import org.onap.cps.spi.exceptions.CpsAdminException;
57 import org.onap.cps.spi.exceptions.CpsPathException;
58 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
59 import org.onap.cps.spi.model.DataNode;
60 import org.onap.cps.spi.model.DataNodeBuilder;
61 import org.onap.cps.spi.repository.AnchorRepository;
62 import org.onap.cps.spi.repository.DataspaceRepository;
63 import org.onap.cps.spi.repository.FragmentRepository;
64 import org.onap.cps.spi.utils.SessionManager;
65 import org.onap.cps.utils.JsonObjectMapper;
66 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.springframework.dao.DataIntegrityViolationException;
69 import org.springframework.stereotype.Service;
73 @RequiredArgsConstructor
74 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
76 private final DataspaceRepository dataspaceRepository;
78 private final AnchorRepository anchorRepository;
80 private final FragmentRepository fragmentRepository;
82 private final JsonObjectMapper jsonObjectMapper;
84 private final SessionManager sessionManager;
86 private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
87 private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
88 Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
92 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentNodeXpath,
93 final DataNode newChildDataNode) {
94 addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, Collections.singleton(newChildDataNode));
99 public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
100 final Collection<DataNode> newListElements) {
101 addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, newListElements);
104 private void addChildDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
105 final Collection<DataNode> newChildren) {
106 final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
108 final List<FragmentEntity> fragmentEntities = new ArrayList<>();
109 newChildren.forEach(newChildAsDataNode -> {
110 final FragmentEntity newChildAsFragmentEntity = convertToFragmentWithAllDescendants(
111 parentFragmentEntity.getDataspace(),
112 parentFragmentEntity.getAnchor(),
114 newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
115 fragmentEntities.add(newChildAsFragmentEntity);
117 fragmentRepository.saveAll(fragmentEntities);
118 } catch (final DataIntegrityViolationException exception) {
119 final List<String> conflictXpaths = newChildren.stream()
120 .map(DataNode::getXpath)
121 .collect(Collectors.toList());
122 throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
127 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
128 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
129 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
130 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
133 fragmentRepository.save(fragmentEntity);
134 } catch (final DataIntegrityViolationException exception) {
135 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
140 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
141 * for all DataNode children recursively.
143 * @param dataspaceEntity dataspace
144 * @param anchorEntity anchorEntity
145 * @param dataNodeToBeConverted dataNode
146 * @return a Fragment built from current DataNode
148 private FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
149 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
150 final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
151 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
152 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
153 final FragmentEntity childFragment =
154 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
156 childFragmentsImmutableSetBuilder.add(childFragment);
158 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
159 return parentFragment;
162 private FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
163 final AnchorEntity anchorEntity, final DataNode dataNode) {
164 return FragmentEntity.builder()
165 .dataspace(dataspaceEntity)
166 .anchor(anchorEntity)
167 .xpath(dataNode.getXpath())
168 .attributes(jsonObjectMapper.asJsonString(dataNode.getLeaves()))
173 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
174 final FetchDescendantsOption fetchDescendantsOption) {
175 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
176 return toDataNode(fragmentEntity, fetchDescendantsOption);
179 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
180 final String xpath) {
181 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
182 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
183 if (isRootXpath(xpath)) {
184 return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
186 final String normalizedXpath;
188 normalizedXpath = CpsPathUtil.getNormalizedXpath(xpath);
189 } catch (final PathParsingException e) {
190 throw new CpsPathException(e.getMessage());
192 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
198 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
199 final FetchDescendantsOption fetchDescendantsOption) {
200 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
201 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
202 final CpsPathQuery cpsPathQuery;
204 cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath);
205 } catch (final PathParsingException e) {
206 throw new CpsPathException(e.getMessage());
208 List<FragmentEntity> fragmentEntities =
209 fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
210 if (cpsPathQuery.hasAncestorAxis()) {
211 final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
212 fragmentEntities = ancestorXpaths.isEmpty() ? Collections.emptyList()
213 : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
215 return fragmentEntities.stream()
216 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
217 .collect(Collectors.toUnmodifiableList());
221 public String startSession() {
222 return sessionManager.startSession();
226 public void closeSession(final String sessionId) {
227 sessionManager.closeSession(sessionId, SessionManager.WITH_COMMIT);
231 public void lockAnchor(final String sessionId, final String dataspaceName,
232 final String anchorName, final Long timeoutInMilliseconds) {
233 sessionManager.lockAnchor(sessionId, dataspaceName, anchorName, timeoutInMilliseconds);
236 private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
237 final CpsPathQuery cpsPathQuery) {
238 final Set<String> ancestorXpath = new HashSet<>();
239 final Pattern pattern =
240 Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
241 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
242 for (final FragmentEntity fragmentEntity : fragmentEntities) {
243 final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
244 if (matcher.matches()) {
245 ancestorXpath.add(matcher.group(1));
248 return ancestorXpath;
251 private DataNode toDataNode(final FragmentEntity fragmentEntity,
252 final FetchDescendantsOption fetchDescendantsOption) {
253 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
254 Map<String, Object> leaves = new HashMap<>();
255 if (fragmentEntity.getAttributes() != null) {
256 leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
258 return new DataNodeBuilder()
259 .withModuleNamePrefix(getFirstModuleName(fragmentEntity))
260 .withXpath(fragmentEntity.getXpath())
262 .withChildDataNodes(childDataNodes).build();
265 private String getFirstModuleName(final FragmentEntity fragmentEntity) {
266 final SchemaSetEntity schemaSetEntity = fragmentEntity.getAnchor().getSchemaSet();
267 final Map<String, String> yangResourceNameToContent =
268 schemaSetEntity.getYangResources().stream().collect(
269 Collectors.toMap(YangResourceEntity::getFileName, YangResourceEntity::getContent));
270 final SchemaContext schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent)
272 return schemaContext.getModules().iterator().next().getName();
275 private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
276 final FetchDescendantsOption fetchDescendantsOption) {
277 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
278 return fragmentEntity.getChildFragments().stream()
279 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
280 .collect(Collectors.toUnmodifiableList());
282 return Collections.emptyList();
286 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
287 final Map<String, Object> leaves) {
288 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
289 fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
290 fragmentRepository.save(fragmentEntity);
294 public void updateDataNodeAndDescendants(final String dataspaceName, final String anchorName,
295 final DataNode dataNode) {
296 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
297 updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode);
299 fragmentRepository.save(fragmentEntity);
300 } catch (final StaleStateException staleStateException) {
301 throw new ConcurrencyException("Concurrent Transactions",
302 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
303 dataspaceName, anchorName, dataNode.getXpath()),
304 staleStateException);
309 public void updateDataNodesAndDescendants(final String dataspaceName,
310 final String anchorName,
311 final List<DataNode> dataNodes) {
312 final Map<DataNode, FragmentEntity> dataNodeFragmentEntityMap = dataNodes.stream()
313 .collect(Collectors.toMap(
314 dataNode -> dataNode, dataNode -> getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath())));
315 dataNodeFragmentEntityMap.forEach(
316 (dataNode, fragmentEntity) -> updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode));
318 fragmentRepository.saveAll(dataNodeFragmentEntityMap.values());
319 } catch (final StaleStateException staleStateException) {
320 throw new ConcurrencyException("Concurrent Transactions",
321 String.format("A data node in dataspace :'%s' with Anchor : '%s' is updated by another transaction.",
322 dataspaceName, anchorName),
323 staleStateException);
327 private void updateFragmentEntityAndDescendantsWithDataNode(final FragmentEntity existingFragmentEntity,
328 final DataNode newDataNode) {
330 existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
332 final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments().stream()
333 .collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
335 final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
337 for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
338 final FragmentEntity childFragment;
339 if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
340 childFragment = convertToFragmentWithAllDescendants(
341 existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
343 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
344 updateFragmentEntityAndDescendantsWithDataNode(childFragment, newDataNodeChild);
346 updatedChildFragments.add(childFragment);
349 existingFragmentEntity.getChildFragments().clear();
350 existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
355 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
356 final Collection<DataNode> newListElements) {
357 final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
358 final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
359 final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
360 extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
361 deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
362 final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
363 for (final DataNode newListElement : newListElements) {
364 final FragmentEntity existingListElementEntity =
365 existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
366 final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
367 existingListElementEntity);
369 updatedChildFragmentEntities.add(entityToBeAdded);
371 parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
372 fragmentRepository.save(parentEntity);
377 public void deleteDataNodes(final String dataspaceName, final String anchorName) {
378 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
379 anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
381 anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
386 public void deleteListDataNode(final String dataspaceName, final String anchorName,
387 final String targetXpath) {
388 deleteDataNode(dataspaceName, anchorName, targetXpath, true);
393 public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
394 deleteDataNode(dataspaceName, anchorName, targetXpath, false);
397 private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
398 final boolean onlySupportListNodeDeletion) {
399 final String parentNodeXpath;
400 FragmentEntity parentFragmentEntity = null;
401 boolean targetDeleted = false;
402 if (isRootXpath(targetXpath)) {
403 deleteDataNodes(dataspaceName, anchorName);
404 targetDeleted = true;
406 if (isRootContainerNodeXpath(targetXpath)) {
407 parentNodeXpath = targetXpath;
409 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
411 parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
412 final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
413 final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
414 .matcher(lastXpathElement).find();
416 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
418 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
419 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
420 if (tryToDeleteDataNode) {
421 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
425 if (!targetDeleted) {
426 final String additionalInformation = onlySupportListNodeDeletion
427 ? "The target is probably not a List." : "";
428 throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
429 parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
433 private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
434 final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath);
435 if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) {
436 fragmentRepository.delete(parentFragmentEntity);
439 if (parentFragmentEntity.getChildFragments()
440 .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
441 fragmentRepository.save(parentFragmentEntity);
447 private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
448 final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath);
449 final String deleteTargetXpathPrefix = normalizedListXpath + "[";
450 if (parentFragmentEntity.getChildFragments()
451 .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
452 fragmentRepository.save(parentFragmentEntity);
458 private static void deleteListElements(
459 final Collection<FragmentEntity> fragmentEntities,
460 final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
461 fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
464 private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
465 if (newListElements.isEmpty()) {
466 throw new CpsAdminException("Invalid list replacement",
467 "Cannot replace list elements with empty collection");
469 final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
470 return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
473 private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
474 final DataNode newListElement,
475 final FragmentEntity existingListElementEntity) {
476 if (existingListElementEntity == null) {
477 return convertToFragmentWithAllDescendants(
478 parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
480 if (newListElement.getChildDataNodes().isEmpty()) {
481 copyAttributesFromNewListElement(existingListElementEntity, newListElement);
482 existingListElementEntity.getChildFragments().clear();
484 updateFragmentEntityAndDescendantsWithDataNode(existingListElementEntity, newListElement);
486 return existingListElementEntity;
489 private static boolean isNewDataNode(final DataNode replacementDataNode,
490 final Map<String, FragmentEntity> existingListElementsByXpath) {
491 return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
494 private static boolean isRootContainerNodeXpath(final String xpath) {
495 return 0 == xpath.lastIndexOf('/');
498 private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
499 final DataNode newListElement) {
500 final FragmentEntity replacementFragmentEntity =
501 FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
502 newListElement.getLeaves())).build();
503 existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
506 private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
507 final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
508 return childEntities.stream()
509 .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
510 .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
513 private static boolean isRootXpath(final String xpath) {
514 return "/".equals(xpath) || "".equals(xpath);