Persistence layer - Query Datanodes using cpsPath that contains contains a leaf name...
[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 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
25
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.ImmutableSet.Builder;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35 import org.onap.cps.spi.CpsDataPersistenceService;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.entities.AnchorEntity;
38 import org.onap.cps.spi.entities.DataspaceEntity;
39 import org.onap.cps.spi.entities.FragmentEntity;
40 import org.onap.cps.spi.model.DataNode;
41 import org.onap.cps.spi.model.DataNodeBuilder;
42 import org.onap.cps.spi.query.CpsPathQuery;
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;
48
49 @Service
50 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
51
52     @Autowired
53     private DataspaceRepository dataspaceRepository;
54
55     @Autowired
56     private AnchorRepository anchorRepository;
57
58     @Autowired
59     private FragmentRepository fragmentRepository;
60
61     private static final Gson GSON = new GsonBuilder().create();
62
63     @Override
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);
71     }
72
73     @Override
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,
78             dataNode);
79         fragmentRepository.save(fragmentEntity);
80     }
81
82     /**
83      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
84      * for all DataNode children recursively.
85      *
86      * @param dataspaceEntity       dataspace
87      * @param anchorEntity          anchorEntity
88      * @param dataNodeToBeConverted dataNode
89      * @return a Fragment built from current DataNode
90      */
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(),
98                     childDataNode);
99             childFragmentsImmutableSetBuilder.add(childFragment);
100         }
101         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
102         return parentFragment;
103     }
104
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()))
112             .build();
113     }
114
115     @Override
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);
120     }
121
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);
127     }
128
129     @Override
130     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath) {
131         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
132         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
133         final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
134         final List<FragmentEntity> fragmentEntities = fragmentRepository
135             .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery
136                 .getXpathPrefix(), cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
137         return fragmentEntities.stream()
138             .map(fragmentEntity -> toDataNode(fragmentEntity, OMIT_DESCENDANTS))
139             .collect(Collectors.toUnmodifiableList());
140     }
141
142     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
143         final FetchDescendantsOption fetchDescendantsOption) {
144         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
145         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
146         return new DataNodeBuilder()
147             .withXpath(fragmentEntity.getXpath())
148             .withLeaves(leaves)
149             .withChildDataNodes(childDataNodes).build();
150     }
151
152     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
153         final FetchDescendantsOption fetchDescendantsOption) {
154         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
155             return fragmentEntity.getChildFragments().stream()
156                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
157                 .collect(Collectors.toUnmodifiableList());
158         }
159         return Collections.emptyList();
160     }
161
162     @Override
163     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
164         final Map<String, Object> leaves) {
165         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
166         fragmentEntity.setAttributes(GSON.toJson(leaves));
167         fragmentRepository.save(fragmentEntity);
168     }
169
170     @Override
171     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
172         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
173         removeExistingDescendants(fragmentEntity);
174
175         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
176         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
177             childDataNode -> convertToFragmentWithAllDescendants(
178                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
179         ).collect(Collectors.toUnmodifiableSet());
180         fragmentEntity.setChildFragments(childFragmentEntities);
181
182         fragmentRepository.save(fragmentEntity);
183     }
184
185     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
186         fragmentEntity.setChildFragments(Collections.emptySet());
187         fragmentRepository.save(fragmentEntity);
188     }
189 }