/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation
+ * Copyright (C) 2021-2022 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
* Modifications Copyright (C) 2020-2021 Bell Canada.
* ================================================================================
import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
+import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.StaleStateException;
import org.onap.cps.cpspath.parser.CpsPathQuery;
import org.onap.cps.spi.exceptions.ConcurrencyException;
import org.onap.cps.spi.exceptions.CpsPathException;
import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
-import org.onap.cps.spi.exceptions.DataValidationException;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.model.DataNodeBuilder;
import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.FragmentRepository;
+import org.onap.cps.utils.JsonObjectMapper;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
@Service
@Slf4j
+@RequiredArgsConstructor
public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
- private DataspaceRepository dataspaceRepository;
+ private final DataspaceRepository dataspaceRepository;
- private AnchorRepository anchorRepository;
+ private final AnchorRepository anchorRepository;
- private FragmentRepository fragmentRepository;
+ private final FragmentRepository fragmentRepository;
- private final ObjectMapper objectMapper;
+ private final JsonObjectMapper jsonObjectMapper;
- /**
- * Constructor.
- *
- * @param dataspaceRepository dataspaceRepository
- * @param anchorRepository anchorRepository
- * @param fragmentRepository fragmentRepository
- */
- public CpsDataPersistenceServiceImpl(final DataspaceRepository dataspaceRepository,
- final AnchorRepository anchorRepository, final FragmentRepository fragmentRepository) {
- this.dataspaceRepository = dataspaceRepository;
- this.anchorRepository = anchorRepository;
- this.fragmentRepository = fragmentRepository;
- objectMapper = new ObjectMapper();
- }
- private static final Gson GSON = new GsonBuilder().create();
private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
- private static final String REG_EX_FOR_LIST_ELEMENT_KEY_PREDICATE = "\\[(\\@([^/]*?)){0,99}( and)*\\]$";
+ private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
+ Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
@Override
public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
* @param dataNodeToBeConverted dataNode
* @return a Fragment built from current DataNode
*/
- private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
+ private FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
}
}
- private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
+ private FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
final AnchorEntity anchorEntity, final DataNode dataNode) {
return FragmentEntity.builder()
.dataspace(dataspaceEntity)
.anchor(anchorEntity)
.xpath(dataNode.getXpath())
- .attributes(GSON.toJson(dataNode.getLeaves()))
+ .attributes(jsonObjectMapper.asJsonString(dataNode.getLeaves()))
.build();
}
final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
Map<String, Object> leaves = new HashMap<>();
if (fragmentEntity.getAttributes() != null) {
- try {
- leaves = objectMapper.readValue(fragmentEntity.getAttributes(), Map.class);
- } catch (final JsonProcessingException jsonProcessingException) {
- final String message = "Parsing error occurred while processing fragmentEntity attributes.";
- log.error(message);
- throw new DataValidationException(message,
- jsonProcessingException.getMessage(), jsonProcessingException);
- }
+ leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
}
return new DataNodeBuilder()
.withXpath(fragmentEntity.getXpath())
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));
+ fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
fragmentRepository.save(fragmentEntity);
}
}
}
- private static void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
+ private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
final DataNode newDataNode) {
- existingFragmentEntity.setAttributes(GSON.toJson(newDataNode.getLeaves()));
+ existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
.stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
final String parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
- final boolean isListElement = Pattern.compile(REG_EX_FOR_LIST_ELEMENT_KEY_PREDICATE)
- .matcher(lastXpathElement).find();
+ final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE.matcher(lastXpathElement).find();
boolean targetExist;
if (isListElement) {
targetExist = deleteDataNode(parentFragmentEntity, targetXpath);
return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[") + 1);
}
- private static FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
+ private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
final DataNode newListElement,
final FragmentEntity existingListElementEntity) {
if (existingListElementEntity == null) {
return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
}
- private static void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
+ private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
final DataNode newListElement) {
final FragmentEntity replacementFragmentEntity =
- FragmentEntity.builder().attributes(GSON.toJson(newListElement.getLeaves())).build();
+ FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
+ newListElement.getLeaves())).build();
existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
}