Merge "Fetching data node by xpath - persistence layer"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.impl;
22
23 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
24
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;
31 import java.util.Map;
32 import java.util.stream.Collectors;
33 import org.onap.cps.spi.CpsDataPersistenceService;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.entities.AnchorEntity;
36 import org.onap.cps.spi.entities.DataspaceEntity;
37 import org.onap.cps.spi.entities.FragmentEntity;
38 import org.onap.cps.spi.model.DataNode;
39 import org.onap.cps.spi.model.DataNodeBuilder;
40 import org.onap.cps.spi.repository.AnchorRepository;
41 import org.onap.cps.spi.repository.DataspaceRepository;
42 import org.onap.cps.spi.repository.FragmentRepository;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Service;
45
46 @Service
47 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
48
49     @Autowired
50     private DataspaceRepository dataspaceRepository;
51
52     @Autowired
53     private AnchorRepository anchorRepository;
54
55     @Autowired
56     private FragmentRepository fragmentRepository;
57
58     private static final Gson GSON = new GsonBuilder().create();
59
60     @Override
61     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
62         final DataNode dataNode) {
63         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
64         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
65         final FragmentEntity parentFragment =
66             fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath);
67         final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode);
68         parentFragment.getChildFragments().add(childFragment);
69         fragmentRepository.save(parentFragment);
70     }
71
72     @Override
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,
77             dataNode);
78         fragmentRepository.save(fragmentEntity);
79     }
80
81     /**
82      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
83      * for all DataNode children recursively.
84      *
85      * @param dataspaceEntity       dataspace
86      * @param anchorEntity          anchorEntity
87      * @param dataNodeToBeConverted dataNode
88      * @return a Fragment built from current DataNode
89      */
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(),
97                     childDataNode);
98             childFragmentsImmutableSetBuilder.add(childFragment);
99         }
100         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
101         return parentFragment;
102     }
103
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()))
111             .build();
112     }
113
114     @Override
115     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
116         final FetchDescendantsOption fetchDescendantsOption) {
117         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
118         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
119         final FragmentEntity fragmentEntity =
120             fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
121         return toDataNode(fragmentEntity, fetchDescendantsOption);
122     }
123
124     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
125         final FetchDescendantsOption fetchDescendantsOption) {
126         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
127         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
128         return new DataNodeBuilder()
129             .withXpath(fragmentEntity.getXpath())
130             .withLeaves(leaves)
131             .withChildDataNodes(childDataNodes).build();
132     }
133
134     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
135         final FetchDescendantsOption fetchDescendantsOption) {
136         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
137             return fragmentEntity.getChildFragments().stream()
138                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
139                 .collect(Collectors.toUnmodifiableList());
140         }
141         return Collections.emptyList();
142     }
143 }