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
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 com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import javax.transaction.Transactional;
40 import lombok.extern.slf4j.Slf4j;
41 import org.hibernate.StaleStateException;
42 import org.onap.cps.cpspath.parser.CpsPathQuery;
43 import org.onap.cps.spi.CpsDataPersistenceService;
44 import org.onap.cps.spi.FetchDescendantsOption;
45 import org.onap.cps.spi.entities.AnchorEntity;
46 import org.onap.cps.spi.entities.DataspaceEntity;
47 import org.onap.cps.spi.entities.FragmentEntity;
48 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
49 import org.onap.cps.spi.exceptions.ConcurrencyException;
50 import org.onap.cps.spi.exceptions.CpsPathException;
51 import org.onap.cps.spi.model.DataNode;
52 import org.onap.cps.spi.model.DataNodeBuilder;
53 import org.onap.cps.spi.repository.AnchorRepository;
54 import org.onap.cps.spi.repository.DataspaceRepository;
55 import org.onap.cps.spi.repository.FragmentRepository;
56 import org.springframework.dao.DataIntegrityViolationException;
57 import org.springframework.stereotype.Service;
61 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
63 private DataspaceRepository dataspaceRepository;
65 private AnchorRepository anchorRepository;
67 private FragmentRepository fragmentRepository;
72 * @param dataspaceRepository dataspaceRepository
73 * @param anchorRepository anchorRepository
74 * @param fragmentRepository fragmentRepository
76 public CpsDataPersistenceServiceImpl(final DataspaceRepository dataspaceRepository,
77 final AnchorRepository anchorRepository, final FragmentRepository fragmentRepository) {
78 this.dataspaceRepository = dataspaceRepository;
79 this.anchorRepository = anchorRepository;
80 this.fragmentRepository = fragmentRepository;
83 private static final Gson GSON = new GsonBuilder().create();
84 private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
87 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
88 final DataNode dataNode) {
89 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
90 final var fragmentEntity =
91 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
92 parentFragment.getChildFragments().add(fragmentEntity);
94 fragmentRepository.save(parentFragment);
95 } catch (final DataIntegrityViolationException exception) {
96 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
101 public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
102 final Collection<DataNode> dataNodes) {
103 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
104 final List<FragmentEntity> newFragmentEntities =
105 dataNodes.stream().map(
106 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
107 ).collect(Collectors.toUnmodifiableList());
108 parentFragment.getChildFragments().addAll(newFragmentEntities);
110 fragmentRepository.save(parentFragment);
112 dataNode -> getChildFragments(dataspaceName, anchorName, dataNode)
114 } catch (final DataIntegrityViolationException exception) {
115 final List<String> conflictXpaths = dataNodes.stream()
116 .map(DataNode::getXpath)
117 .collect(Collectors.toList());
118 throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
123 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
124 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
125 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
126 final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
129 fragmentRepository.save(fragmentEntity);
130 } catch (final DataIntegrityViolationException exception) {
131 throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
136 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
137 * for all DataNode children recursively.
139 * @param dataspaceEntity dataspace
140 * @param anchorEntity anchorEntity
141 * @param dataNodeToBeConverted dataNode
142 * @return a Fragment built from current DataNode
144 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
145 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
146 final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
147 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
148 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
149 final FragmentEntity childFragment =
150 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
152 childFragmentsImmutableSetBuilder.add(childFragment);
154 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
155 return parentFragment;
158 private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) {
159 for (final DataNode childDataNode: dataNode.getChildDataNodes()) {
160 final FragmentEntity getChildsParentFragmentByXPath =
161 getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
162 final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(),
163 getChildsParentFragmentByXPath.getAnchor(), childDataNode);
164 getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity);
165 fragmentRepository.save(getChildsParentFragmentByXPath);
169 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
170 final AnchorEntity anchorEntity, final DataNode dataNode) {
171 return FragmentEntity.builder()
172 .dataspace(dataspaceEntity)
173 .anchor(anchorEntity)
174 .xpath(dataNode.getXpath())
175 .attributes(GSON.toJson(dataNode.getLeaves()))
180 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
181 final FetchDescendantsOption fetchDescendantsOption) {
182 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
183 return toDataNode(fragmentEntity, fetchDescendantsOption);
186 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
187 final String xpath) {
188 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
189 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
190 if (isRootXpath(xpath)) {
191 return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
193 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
199 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
200 final FetchDescendantsOption fetchDescendantsOption) {
201 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
202 final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
203 final CpsPathQuery cpsPathQuery;
205 cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
206 } catch (final IllegalStateException e) {
207 throw new CpsPathException(e.getMessage());
209 List<FragmentEntity> fragmentEntities =
210 fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
211 if (cpsPathQuery.hasAncestorAxis()) {
212 final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
213 fragmentEntities = ancestorXpaths.isEmpty()
214 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
216 return fragmentEntities.stream()
217 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
218 .collect(Collectors.toUnmodifiableList());
221 private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
222 final CpsPathQuery cpsPathQuery) {
223 final Set<String> ancestorXpath = new HashSet<>();
225 Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
226 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
227 for (final FragmentEntity fragmentEntity : fragmentEntities) {
228 final var matcher = pattern.matcher(fragmentEntity.getXpath());
229 if (matcher.matches()) {
230 ancestorXpath.add(matcher.group(1));
233 return ancestorXpath;
236 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
237 final FetchDescendantsOption fetchDescendantsOption) {
238 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
239 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
240 return new DataNodeBuilder()
241 .withXpath(fragmentEntity.getXpath())
243 .withChildDataNodes(childDataNodes).build();
246 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
247 final FetchDescendantsOption fetchDescendantsOption) {
248 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
249 return fragmentEntity.getChildFragments().stream()
250 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
251 .collect(Collectors.toUnmodifiableList());
253 return Collections.emptyList();
257 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
258 final Map<String, Object> leaves) {
259 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
260 fragmentEntity.setAttributes(GSON.toJson(leaves));
261 fragmentRepository.save(fragmentEntity);
265 public void replaceDataNodeTree(final String dataspaceName, final String anchorName,
266 final DataNode dataNode) {
267 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
268 replaceDataNodeTree(fragmentEntity, dataNode);
270 fragmentRepository.save(fragmentEntity);
271 } catch (final StaleStateException staleStateException) {
272 throw new ConcurrencyException("Concurrent Transactions",
273 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
274 dataspaceName, anchorName, dataNode.getXpath()),
275 staleStateException);
279 private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity, final DataNode submittedDataNode) {
281 existingFragmentEntity.setAttributes(GSON.toJson(submittedDataNode.getLeaves()));
283 final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
284 .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
286 final var updatedChildFragments = new HashSet<FragmentEntity>();
288 for (final DataNode submittedChildDataNode : submittedDataNode.getChildDataNodes()) {
289 final FragmentEntity childFragment;
290 if (existingChildrenByXpath.containsKey(submittedChildDataNode.getXpath())) {
291 childFragment = existingChildrenByXpath.get(submittedChildDataNode.getXpath());
292 replaceDataNodeTree(childFragment, submittedChildDataNode);
294 childFragment = convertToFragmentWithAllDescendants(
295 existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), submittedChildDataNode);
297 updatedChildFragments.add(childFragment);
299 existingFragmentEntity.setChildFragments(updatedChildFragments);
304 public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
305 final Collection<DataNode> dataNodes) {
306 final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
307 final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
308 final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
309 removeListNodeDescendants(parentEntity, listNodeXpath);
310 final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
311 dataNode -> convertToFragmentWithAllDescendants(
312 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
313 ).collect(Collectors.toUnmodifiableSet());
314 parentEntity.getChildFragments().addAll(childFragmentEntities);
315 fragmentRepository.save(parentEntity);
318 private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
319 final String listNodeXpathPrefix = listNodeXpath + "[";
320 if (parentFragmentEntity.getChildFragments()
321 .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
322 fragmentRepository.save(parentFragmentEntity);
326 private static boolean isRootXpath(final String xpath) {
327 return "/".equals(xpath) || "".equals(xpath);