2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.spi.impl;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.ImmutableSet.Builder;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import org.onap.cps.spi.CpsDataPersistenceService;
27 import org.onap.cps.spi.entities.AnchorEntity;
28 import org.onap.cps.spi.entities.DataspaceEntity;
29 import org.onap.cps.spi.entities.FragmentEntity;
30 import org.onap.cps.spi.model.DataNode;
31 import org.onap.cps.spi.repository.AnchorRepository;
32 import org.onap.cps.spi.repository.DataspaceRepository;
33 import org.onap.cps.spi.repository.FragmentRepository;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
38 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
41 private DataspaceRepository dataspaceRepository;
44 private AnchorRepository anchorRepository;
47 private FragmentRepository fragmentRepository;
49 private static final Gson GSON = new GsonBuilder().create();
52 public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
53 final DataNode dataNode) {
54 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
55 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
56 final FragmentEntity parentFragment =
57 fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath);
58 final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode);
59 parentFragment.getChildFragments().add(childFragment);
60 fragmentRepository.save(parentFragment);
64 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
65 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
66 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
67 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
69 fragmentRepository.save(fragmentEntity);
73 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
74 * for all DataNode children recursively.
76 * @param dataspaceEntity dataspace
77 * @param anchorEntity anchorEntity
78 * @param dataNodeToBeConverted dataNode
79 * @return a Fragment built from current DataNode
81 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
82 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
83 final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
84 final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
85 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
86 final FragmentEntity childFragment =
87 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
89 childFragmentsImmutableSetBuilder.add(childFragment);
91 parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
92 return parentFragment;
95 private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
96 final AnchorEntity anchorEntity,
97 final DataNode dataNode) {
98 return FragmentEntity.builder()
99 .dataspace(dataspaceEntity)
100 .anchor(anchorEntity)
101 .xpath(dataNode.getXpath())
102 .attributes(GSON.toJson(dataNode.getLeaves()))