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 Gson GSON = new GsonBuilder().create();
52 public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
53 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
54 final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
55 final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
57 fragmentRepository.save(fragmentEntity);
61 * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
62 * for all DataNode children recursively.
64 * @param dataspaceEntity dataspace
65 * @param anchorEntity anchorEntity
66 * @param dataNodeToBeConverted dataNode
67 * @return a Fragment built from current DataNode
69 private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
70 final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
71 final FragmentEntity parentFragment = FragmentEntity.builder()
72 .dataspace(dataspaceEntity)
74 .xpath(dataNodeToBeConverted.getXpath())
75 .attributes(GSON.toJson(dataNodeToBeConverted.getLeaves()))
78 final Builder<FragmentEntity> fragmentEntityBuilder = ImmutableSet.builder();
79 for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
80 final FragmentEntity childFragment =
81 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
83 fragmentEntityBuilder.add(childFragment);
85 parentFragment.setChildFragments(fragmentEntityBuilder.build());
86 return parentFragment;