2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2020-2021 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl;
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.ImmutableSet.Builder;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import org.onap.cps.spi.CpsDataPersistenceService;
39 import org.onap.cps.spi.FetchDescendantsOption;
40 import org.onap.cps.spi.entities.AnchorEntity;
41 import org.onap.cps.spi.entities.DataspaceEntity;
42 import org.onap.cps.spi.entities.FragmentEntity;
43 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
44 import org.onap.cps.spi.model.DataNode;
45 import org.onap.cps.spi.model.DataNodeBuilder;
46 import org.onap.cps.spi.query.CpsPathQuery;
47 import org.onap.cps.spi.query.CpsPathQueryType;
48 import org.onap.cps.spi.repository.AnchorRepository;
49 import org.onap.cps.spi.repository.DataspaceRepository;
50 import org.onap.cps.spi.repository.FragmentRepository;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.dao.DataIntegrityViolationException;
53 import org.springframework.stereotype.Service;
56 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
59 private DataspaceRepository dataspaceRepository;
62 private AnchorRepository anchorRepository;
65 private FragmentRepository fragmentRepository;
67 private static final Gson GSON = new GsonBuilder().create();
68 private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
71 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
72 final DataNode dataNode) {
73 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
74 final var fragmentEntity =
75 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
76 parentFragment.getChildFragments().add(fragmentEntity);
78 fragmentRepository.save(parentFragment);
79 } catch (final DataIntegrityViolationException exception) {
80 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
85 public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
86 final Collection<DataNode> dataNodes) {
87 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
88 final List<FragmentEntity> newFragmentEntities =
89 dataNodes.stream().map(
90 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
91 ).collect(Collectors.toUnmodifiableList());
92 parentFragment.getChildFragments().addAll(newFragmentEntities);
94 fragmentRepository.save(parentFragment);
95 } catch (final DataIntegrityViolationException exception) {
96 final List<String> conflictXpaths = dataNodes.stream()
97 .map(DataNode::getXpath)
98 .collect(Collectors.toList());
99 throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
104 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
105 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
106 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
107 final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
110 fragmentRepository.save(fragmentEntity);
111 } catch (final DataIntegrityViolationException exception) {
112 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
117 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
118 * for all DataNode children recursively.
120 * @param dataspaceEntity dataspace
121 * @param anchorEntity anchorEntity
122 * @param dataNodeToBeConverted dataNode
123 * @return a Fragment built from current DataNode
125 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
126 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
127 final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
128 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
129 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
130 final FragmentEntity childFragment =
131 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
133 childFragmentsImmutableSetBuilder.add(childFragment);
135 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
136 return parentFragment;
139 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
140 final AnchorEntity anchorEntity, final DataNode dataNode) {
141 return FragmentEntity.builder()
142 .dataspace(dataspaceEntity)
143 .anchor(anchorEntity)
144 .xpath(dataNode.getXpath())
145 .attributes(GSON.toJson(dataNode.getLeaves()))
150 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
151 final FetchDescendantsOption fetchDescendantsOption) {
152 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
153 return toDataNode(fragmentEntity, fetchDescendantsOption);
156 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
157 final String xpath) {
158 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
159 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
160 if (isRootXpath(xpath)) {
161 return fragmentRepository.getFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
163 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
169 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
170 final FetchDescendantsOption fetchDescendantsOption) {
171 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
172 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
173 final var cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
174 List<FragmentEntity> fragmentEntities;
175 if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
176 fragmentEntities = fragmentRepository
177 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
178 cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
179 } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
180 final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
181 fragmentEntities = fragmentRepository
182 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
185 fragmentEntities = fragmentRepository
186 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
188 if (cpsPathQuery.hasAncestorAxis()) {
189 final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
190 fragmentEntities = ancestorXpaths.isEmpty()
191 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
193 return fragmentEntities.stream()
194 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
195 .collect(Collectors.toUnmodifiableList());
198 private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
199 final CpsPathQuery cpsPathQuery) {
200 final Set<String> ancestorXpath = new HashSet<>();
202 Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
203 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
204 for (final FragmentEntity fragmentEntity : fragmentEntities) {
205 final var matcher = pattern.matcher(fragmentEntity.getXpath());
206 if (matcher.matches()) {
207 ancestorXpath.add(matcher.group(1));
210 return ancestorXpath;
213 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
214 final FetchDescendantsOption fetchDescendantsOption) {
215 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
216 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
217 return new DataNodeBuilder()
218 .withXpath(fragmentEntity.getXpath())
220 .withChildDataNodes(childDataNodes).build();
223 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
224 final FetchDescendantsOption fetchDescendantsOption) {
225 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
226 return fragmentEntity.getChildFragments().stream()
227 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
228 .collect(Collectors.toUnmodifiableList());
230 return Collections.emptyList();
234 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
235 final Map<String, Object> leaves) {
236 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
237 fragmentEntity.setAttributes(GSON.toJson(leaves));
238 fragmentRepository.save(fragmentEntity);
242 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
243 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
244 removeExistingDescendants(fragmentEntity);
246 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
247 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
248 childDataNode -> convertToFragmentWithAllDescendants(
249 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
250 ).collect(Collectors.toUnmodifiableSet());
251 fragmentEntity.setChildFragments(childFragmentEntities);
253 fragmentRepository.save(fragmentEntity);
256 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
257 fragmentEntity.setChildFragments(Collections.emptySet());
258 fragmentRepository.save(fragmentEntity);
261 private boolean isRootXpath(final String xpath) {
262 return "/".equals(xpath) || "".equals(xpath);