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 org.onap.cps.cpspath.parser.CpsPathQuery;
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 fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
183 if (cpsPathQuery.hasAncestorAxis()) {
184 final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
185 fragmentEntities = ancestorXpaths.isEmpty()
186 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
188 return fragmentEntities.stream()
189 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
190 .collect(Collectors.toUnmodifiableList());
193 private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
194 final CpsPathQuery cpsPathQuery) {
195 final Set<String> ancestorXpath = new HashSet<>();
197 Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
198 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
199 for (final FragmentEntity fragmentEntity : fragmentEntities) {
200 final var matcher = pattern.matcher(fragmentEntity.getXpath());
201 if (matcher.matches()) {
202 ancestorXpath.add(matcher.group(1));
205 return ancestorXpath;
208 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
209 final FetchDescendantsOption fetchDescendantsOption) {
210 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
211 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
212 return new DataNodeBuilder()
213 .withXpath(fragmentEntity.getXpath())
215 .withChildDataNodes(childDataNodes).build();
218 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
219 final FetchDescendantsOption fetchDescendantsOption) {
220 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
221 return fragmentEntity.getChildFragments().stream()
222 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
223 .collect(Collectors.toUnmodifiableList());
225 return Collections.emptyList();
229 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
230 final Map<String, Object> leaves) {
231 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
232 fragmentEntity.setAttributes(GSON.toJson(leaves));
233 fragmentRepository.save(fragmentEntity);
237 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
238 final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
239 removeExistingDescendants(fragmentEntity);
241 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
242 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
243 childDataNode -> convertToFragmentWithAllDescendants(
244 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
245 ).collect(Collectors.toUnmodifiableSet());
246 fragmentEntity.setChildFragments(childFragmentEntities);
248 fragmentRepository.save(fragmentEntity);
253 public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
254 final Collection<DataNode> dataNodes) {
255 final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
256 final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
257 final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
258 removeListNodeDescendants(parentEntity, listNodeXpath);
259 final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
260 dataNode -> convertToFragmentWithAllDescendants(
261 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
262 ).collect(Collectors.toUnmodifiableSet());
263 parentEntity.getChildFragments().addAll(childFragmentEntities);
264 fragmentRepository.save(parentEntity);
267 private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
268 final String listNodeXpathPrefix = listNodeXpath + "[";
269 if (parentFragmentEntity.getChildFragments()
270 .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
271 fragmentRepository.save(parentFragmentEntity);
275 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
276 fragmentEntity.setChildFragments(Collections.emptySet());
277 fragmentRepository.save(fragmentEntity);
280 private static boolean isRootXpath(final String xpath) {
281 return "/".equals(xpath) || "".equals(xpath);