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 javax.transaction.Transactional;
39 import org.onap.cps.cpspath.parser.CpsPathQuery;
40 import org.onap.cps.cpspath.parser.CpsPathQueryType;
41 import org.onap.cps.spi.CpsDataPersistenceService;
42 import org.onap.cps.spi.FetchDescendantsOption;
43 import org.onap.cps.spi.entities.AnchorEntity;
44 import org.onap.cps.spi.entities.DataspaceEntity;
45 import org.onap.cps.spi.entities.FragmentEntity;
46 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
47 import org.onap.cps.spi.exceptions.CpsPathException;
48 import org.onap.cps.spi.model.DataNode;
49 import org.onap.cps.spi.model.DataNodeBuilder;
50 import org.onap.cps.spi.repository.AnchorRepository;
51 import org.onap.cps.spi.repository.DataspaceRepository;
52 import org.onap.cps.spi.repository.FragmentRepository;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.dao.DataIntegrityViolationException;
55 import org.springframework.stereotype.Service;
58 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
61 private DataspaceRepository dataspaceRepository;
64 private AnchorRepository anchorRepository;
67 private FragmentRepository fragmentRepository;
69 private static final Gson GSON = new GsonBuilder().create();
70 private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
73 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
74 final DataNode dataNode) {
75 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
76 final var fragmentEntity =
77 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
78 parentFragment.getChildFragments().add(fragmentEntity);
80 fragmentRepository.save(parentFragment);
81 } catch (final DataIntegrityViolationException exception) {
82 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
87 public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
88 final Collection<DataNode> dataNodes) {
89 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
90 final List<FragmentEntity> newFragmentEntities =
91 dataNodes.stream().map(
92 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
93 ).collect(Collectors.toUnmodifiableList());
94 parentFragment.getChildFragments().addAll(newFragmentEntities);
96 fragmentRepository.save(parentFragment);
97 } catch (final DataIntegrityViolationException exception) {
98 final List<String> conflictXpaths = dataNodes.stream()
99 .map(DataNode::getXpath)
100 .collect(Collectors.toList());
101 throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
106 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
107 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
108 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
109 final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
112 fragmentRepository.save(fragmentEntity);
113 } catch (final DataIntegrityViolationException exception) {
114 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
119 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
120 * for all DataNode children recursively.
122 * @param dataspaceEntity dataspace
123 * @param anchorEntity anchorEntity
124 * @param dataNodeToBeConverted dataNode
125 * @return a Fragment built from current DataNode
127 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
128 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
129 final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
130 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
131 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
132 final FragmentEntity childFragment =
133 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
135 childFragmentsImmutableSetBuilder.add(childFragment);
137 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
138 return parentFragment;
141 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
142 final AnchorEntity anchorEntity, final DataNode dataNode) {
143 return FragmentEntity.builder()
144 .dataspace(dataspaceEntity)
145 .anchor(anchorEntity)
146 .xpath(dataNode.getXpath())
147 .attributes(GSON.toJson(dataNode.getLeaves()))
152 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
153 final FetchDescendantsOption fetchDescendantsOption) {
154 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
155 return toDataNode(fragmentEntity, fetchDescendantsOption);
158 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
159 final String xpath) {
160 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
161 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
162 if (isRootXpath(xpath)) {
163 return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
165 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
171 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
172 final FetchDescendantsOption fetchDescendantsOption) {
173 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
174 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
175 final CpsPathQuery cpsPathQuery;
177 cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
178 } catch (final IllegalStateException e) {
179 throw new CpsPathException(e.getMessage());
181 List<FragmentEntity> fragmentEntities;
182 if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
183 fragmentEntities = fragmentRepository
184 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
185 cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
186 } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
187 final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
188 fragmentEntities = fragmentRepository
189 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
192 fragmentEntities = fragmentRepository
193 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
195 if (cpsPathQuery.hasAncestorAxis()) {
196 final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
197 fragmentEntities = ancestorXpaths.isEmpty()
198 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
200 return fragmentEntities.stream()
201 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
202 .collect(Collectors.toUnmodifiableList());
205 private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
206 final CpsPathQuery cpsPathQuery) {
207 final Set<String> ancestorXpath = new HashSet<>();
209 Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
210 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
211 for (final FragmentEntity fragmentEntity : fragmentEntities) {
212 final var matcher = pattern.matcher(fragmentEntity.getXpath());
213 if (matcher.matches()) {
214 ancestorXpath.add(matcher.group(1));
217 return ancestorXpath;
220 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
221 final FetchDescendantsOption fetchDescendantsOption) {
222 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
223 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
224 return new DataNodeBuilder()
225 .withXpath(fragmentEntity.getXpath())
227 .withChildDataNodes(childDataNodes).build();
230 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
231 final FetchDescendantsOption fetchDescendantsOption) {
232 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
233 return fragmentEntity.getChildFragments().stream()
234 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
235 .collect(Collectors.toUnmodifiableList());
237 return Collections.emptyList();
241 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
242 final Map<String, Object> leaves) {
243 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
244 fragmentEntity.setAttributes(GSON.toJson(leaves));
245 fragmentRepository.save(fragmentEntity);
249 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
250 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
251 removeExistingDescendants(fragmentEntity);
253 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
254 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
255 childDataNode -> convertToFragmentWithAllDescendants(
256 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
257 ).collect(Collectors.toUnmodifiableSet());
258 fragmentEntity.setChildFragments(childFragmentEntities);
260 fragmentRepository.save(fragmentEntity);
265 public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
266 final Collection<DataNode> dataNodes) {
267 final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
268 final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
269 final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
270 removeListNodeDescendants(parentEntity, listNodeXpath);
271 final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
272 dataNode -> convertToFragmentWithAllDescendants(
273 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
274 ).collect(Collectors.toUnmodifiableSet());
275 parentEntity.getChildFragments().addAll(childFragmentEntities);
276 fragmentRepository.save(parentEntity);
279 private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
280 final String listNodeXpathPrefix = listNodeXpath + "[";
281 if (parentFragmentEntity.getChildFragments()
282 .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
283 fragmentRepository.save(parentFragmentEntity);
287 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
288 fragmentEntity.setChildFragments(Collections.emptySet());
289 fragmentRepository.save(fragmentEntity);
292 private static boolean isRootXpath(final String xpath) {
293 return "/".equals(xpath) || "".equals(xpath);