diff options
Diffstat (limited to 'cps-ri/src/main/java/org')
-rwxr-xr-x | cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java | 3 | ||||
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 49 |
2 files changed, 42 insertions, 10 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java index 677652899..53ceaaae1 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * Modifications Copyright (C) 2021 Pantheon.tech * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +84,7 @@ public class FragmentEntity implements Serializable { @JoinColumn(name = "anchor_id") private AnchorEntity anchor; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") private Set<FragmentEntity> childFragments; } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index d8f3df112..fd4e768ca 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -29,6 +29,7 @@ import com.google.gson.GsonBuilder; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; @@ -60,12 +61,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService @Override public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath, final DataNode dataNode) { - final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); - final FragmentEntity parentFragment = - fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath); - final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode); - parentFragment.getChildFragments().add(childFragment); + final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath); + final FragmentEntity fragmentEntity = + toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode); + parentFragment.getChildFragments().add(fragmentEntity); fragmentRepository.save(parentFragment); } @@ -114,11 +113,15 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService @Override public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath, final FetchDescendantsOption fetchDescendantsOption) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath); + return toDataNode(fragmentEntity, fetchDescendantsOption); + } + + private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName, + final String xpath) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); - final FragmentEntity fragmentEntity = - fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath); - return toDataNode(fragmentEntity, fetchDescendantsOption); + return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath); } private static DataNode toDataNode(final FragmentEntity fragmentEntity, @@ -140,4 +143,32 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } return Collections.emptyList(); } + + @Override + public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath, + final Map<String, Object> leaves) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath); + fragmentEntity.setAttributes(GSON.toJson(leaves)); + fragmentRepository.save(fragmentEntity); + } + + @Override + public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath()); + removeExistingDescendants(fragmentEntity); + + fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves())); + final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map( + childDataNode -> convertToFragmentWithAllDescendants( + fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode) + ).collect(Collectors.toUnmodifiableSet()); + fragmentEntity.setChildFragments(childFragmentEntities); + + fragmentRepository.save(fragmentEntity); + } + + private void removeExistingDescendants(final FragmentEntity fragmentEntity) { + fragmentEntity.setChildFragments(Collections.emptySet()); + fragmentRepository.save(fragmentEntity); + } } |