2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.impl;
23 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
25 import com.google.common.collect.ImmutableSet;
26 import com.google.common.collect.ImmutableSet.Builder;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import java.util.Collections;
30 import java.util.List;
33 import java.util.stream.Collectors;
34 import org.onap.cps.spi.CpsDataPersistenceService;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.entities.AnchorEntity;
37 import org.onap.cps.spi.entities.DataspaceEntity;
38 import org.onap.cps.spi.entities.FragmentEntity;
39 import org.onap.cps.spi.model.DataNode;
40 import org.onap.cps.spi.model.DataNodeBuilder;
41 import org.onap.cps.spi.query.CpsPathQuery;
42 import org.onap.cps.spi.repository.AnchorRepository;
43 import org.onap.cps.spi.repository.DataspaceRepository;
44 import org.onap.cps.spi.repository.FragmentRepository;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Service;
49 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
52 private DataspaceRepository dataspaceRepository;
55 private AnchorRepository anchorRepository;
58 private FragmentRepository fragmentRepository;
60 private static final Gson GSON = new GsonBuilder().create();
63 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
64 final DataNode dataNode) {
65 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
66 final FragmentEntity fragmentEntity =
67 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
68 parentFragment.getChildFragments().add(fragmentEntity);
69 fragmentRepository.save(parentFragment);
73 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
74 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
75 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
76 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
78 fragmentRepository.save(fragmentEntity);
82 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
83 * for all DataNode children recursively.
85 * @param dataspaceEntity dataspace
86 * @param anchorEntity anchorEntity
87 * @param dataNodeToBeConverted dataNode
88 * @return a Fragment built from current DataNode
90 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
91 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
92 final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
93 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
94 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
95 final FragmentEntity childFragment =
96 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
98 childFragmentsImmutableSetBuilder.add(childFragment);
100 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
101 return parentFragment;
104 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
105 final AnchorEntity anchorEntity, final DataNode dataNode) {
106 return FragmentEntity.builder()
107 .dataspace(dataspaceEntity)
108 .anchor(anchorEntity)
109 .xpath(dataNode.getXpath())
110 .attributes(GSON.toJson(dataNode.getLeaves()))
115 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
116 final FetchDescendantsOption fetchDescendantsOption) {
117 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
118 return toDataNode(fragmentEntity, fetchDescendantsOption);
121 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
122 final String xpath) {
123 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
124 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
125 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
129 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
130 final FetchDescendantsOption fetchDescendantsOption) {
131 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
132 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
133 final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
134 final List<FragmentEntity> fragmentEntities = fragmentRepository
135 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery
136 .getXpathPrefix(), cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
137 return fragmentEntities.stream()
138 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
139 .collect(Collectors.toUnmodifiableList());
142 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
143 final FetchDescendantsOption fetchDescendantsOption) {
144 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
145 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
146 return new DataNodeBuilder()
147 .withXpath(fragmentEntity.getXpath())
149 .withChildDataNodes(childDataNodes).build();
152 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
153 final FetchDescendantsOption fetchDescendantsOption) {
154 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
155 return fragmentEntity.getChildFragments().stream()
156 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
157 .collect(Collectors.toUnmodifiableList());
159 return Collections.emptyList();
163 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
164 final Map<String, Object> leaves) {
165 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
166 fragmentEntity.setAttributes(GSON.toJson(leaves));
167 fragmentRepository.save(fragmentEntity);
171 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
172 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
173 removeExistingDescendants(fragmentEntity);
175 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
176 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
177 childDataNode -> convertToFragmentWithAllDescendants(
178 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
179 ).collect(Collectors.toUnmodifiableSet());
180 fragmentEntity.setChildFragments(childFragmentEntities);
182 fragmentRepository.save(fragmentEntity);
185 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
186 fragmentEntity.setChildFragments(Collections.emptySet());
187 fragmentRepository.save(fragmentEntity);