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.query.CpsPathQueryType;
43 import org.onap.cps.spi.repository.AnchorRepository;
44 import org.onap.cps.spi.repository.DataspaceRepository;
45 import org.onap.cps.spi.repository.FragmentRepository;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Service;
50 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
53 private DataspaceRepository dataspaceRepository;
56 private AnchorRepository anchorRepository;
59 private FragmentRepository fragmentRepository;
61 private static final Gson GSON = new GsonBuilder().create();
64 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
65 final DataNode dataNode) {
66 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
67 final FragmentEntity fragmentEntity =
68 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
69 parentFragment.getChildFragments().add(fragmentEntity);
70 fragmentRepository.save(parentFragment);
74 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
75 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
76 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
77 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
79 fragmentRepository.save(fragmentEntity);
83 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
84 * for all DataNode children recursively.
86 * @param dataspaceEntity dataspace
87 * @param anchorEntity anchorEntity
88 * @param dataNodeToBeConverted dataNode
89 * @return a Fragment built from current DataNode
91 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
92 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
93 final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
94 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
95 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
96 final FragmentEntity childFragment =
97 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
99 childFragmentsImmutableSetBuilder.add(childFragment);
101 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
102 return parentFragment;
105 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
106 final AnchorEntity anchorEntity, final DataNode dataNode) {
107 return FragmentEntity.builder()
108 .dataspace(dataspaceEntity)
109 .anchor(anchorEntity)
110 .xpath(dataNode.getXpath())
111 .attributes(GSON.toJson(dataNode.getLeaves()))
116 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
117 final FetchDescendantsOption fetchDescendantsOption) {
118 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
119 return toDataNode(fragmentEntity, fetchDescendantsOption);
122 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
123 final String xpath) {
124 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
125 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
126 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
130 public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
131 final FetchDescendantsOption fetchDescendantsOption) {
132 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
133 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
134 final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
135 final List<FragmentEntity> fragmentEntities;
136 if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
137 fragmentEntities = fragmentRepository
138 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(), cpsPathQuery
139 .getLeafName(), cpsPathQuery.getLeafValue());
141 fragmentEntities = fragmentRepository
142 .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
144 return fragmentEntities.stream()
145 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
146 .collect(Collectors.toUnmodifiableList());
149 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
150 final FetchDescendantsOption fetchDescendantsOption) {
151 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
152 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
153 return new DataNodeBuilder()
154 .withXpath(fragmentEntity.getXpath())
156 .withChildDataNodes(childDataNodes).build();
159 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
160 final FetchDescendantsOption fetchDescendantsOption) {
161 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
162 return fragmentEntity.getChildFragments().stream()
163 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
164 .collect(Collectors.toUnmodifiableList());
166 return Collections.emptyList();
170 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
171 final Map<String, Object> leaves) {
172 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
173 fragmentEntity.setAttributes(GSON.toJson(leaves));
174 fragmentRepository.save(fragmentEntity);
178 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
179 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
180 removeExistingDescendants(fragmentEntity);
182 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
183 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
184 childDataNode -> convertToFragmentWithAllDescendants(
185 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
186 ).collect(Collectors.toUnmodifiableSet());
187 fragmentEntity.setChildFragments(childFragmentEntities);
189 fragmentRepository.save(fragmentEntity);
192 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
193 fragmentEntity.setChildFragments(Collections.emptySet());
194 fragmentRepository.save(fragmentEntity);