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.repository.AnchorRepository;
42 import org.onap.cps.spi.repository.DataspaceRepository;
43 import org.onap.cps.spi.repository.FragmentRepository;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Service;
48 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
51 private DataspaceRepository dataspaceRepository;
54 private AnchorRepository anchorRepository;
57 private FragmentRepository fragmentRepository;
59 private static final Gson GSON = new GsonBuilder().create();
62 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
63 final DataNode dataNode) {
64 final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
65 final FragmentEntity fragmentEntity =
66 toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
67 parentFragment.getChildFragments().add(fragmentEntity);
68 fragmentRepository.save(parentFragment);
72 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
73 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
74 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
75 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
77 fragmentRepository.save(fragmentEntity);
81 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
82 * for all DataNode children recursively.
84 * @param dataspaceEntity dataspace
85 * @param anchorEntity anchorEntity
86 * @param dataNodeToBeConverted dataNode
87 * @return a Fragment built from current DataNode
89 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
90 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
91 final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
92 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
93 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
94 final FragmentEntity childFragment =
95 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
97 childFragmentsImmutableSetBuilder.add(childFragment);
99 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
100 return parentFragment;
103 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
104 final AnchorEntity anchorEntity, final DataNode dataNode) {
105 return FragmentEntity.builder()
106 .dataspace(dataspaceEntity)
107 .anchor(anchorEntity)
108 .xpath(dataNode.getXpath())
109 .attributes(GSON.toJson(dataNode.getLeaves()))
114 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
115 final FetchDescendantsOption fetchDescendantsOption) {
116 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
117 return toDataNode(fragmentEntity, fetchDescendantsOption);
120 private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
121 final String xpath) {
122 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
123 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
124 return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
127 private static DataNode toDataNode(final FragmentEntity fragmentEntity,
128 final FetchDescendantsOption fetchDescendantsOption) {
129 final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
130 final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
131 return new DataNodeBuilder()
132 .withXpath(fragmentEntity.getXpath())
134 .withChildDataNodes(childDataNodes).build();
137 private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
138 final FetchDescendantsOption fetchDescendantsOption) {
139 if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
140 return fragmentEntity.getChildFragments().stream()
141 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
142 .collect(Collectors.toUnmodifiableList());
144 return Collections.emptyList();
148 public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
149 final Map<String, Object> leaves) {
150 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
151 fragmentEntity.setAttributes(GSON.toJson(leaves));
152 fragmentRepository.save(fragmentEntity);
156 public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
157 final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
158 removeExistingDescendants(fragmentEntity);
160 fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
161 final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
162 childDataNode -> convertToFragmentWithAllDescendants(
163 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
164 ).collect(Collectors.toUnmodifiableSet());
165 fragmentEntity.setChildFragments(childFragmentEntities);
167 fragmentRepository.save(fragmentEntity);
170 private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
171 fragmentEntity.setChildFragments(Collections.emptySet());
172 fragmentRepository.save(fragmentEntity);