CPS-265 - Update cps path query to support 'ends with'
[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.Set;
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.query.CpsPathQuery;
42 import org.onap.cps.spi.query.CpsPathQueryType;
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 FetchDescendantsOption fetchDescendantsOption) {
132         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
133         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
134         final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
135         final List<FragmentEntity> fragmentEntities;
136         if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
137             fragmentEntities = fragmentRepository
138                 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(), cpsPathQuery
139                     .getLeafName(), cpsPathQuery.getLeafValue());
140         } else {
141             fragmentEntities = fragmentRepository
142                     .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
143         }
144         return fragmentEntities.stream()
145             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
146             .collect(Collectors.toUnmodifiableList());
147     }
148
149     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
150         final FetchDescendantsOption fetchDescendantsOption) {
151         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
152         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
153         return new DataNodeBuilder()
154             .withXpath(fragmentEntity.getXpath())
155             .withLeaves(leaves)
156             .withChildDataNodes(childDataNodes).build();
157     }
158
159     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
160         final FetchDescendantsOption fetchDescendantsOption) {
161         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
162             return fragmentEntity.getChildFragments().stream()
163                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
164                 .collect(Collectors.toUnmodifiableList());
165         }
166         return Collections.emptyList();
167     }
168
169     @Override
170     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
171         final Map<String, Object> leaves) {
172         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
173         fragmentEntity.setAttributes(GSON.toJson(leaves));
174         fragmentRepository.save(fragmentEntity);
175     }
176
177     @Override
178     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
179         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
180         removeExistingDescendants(fragmentEntity);
181
182         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
183         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
184             childDataNode -> convertToFragmentWithAllDescendants(
185                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
186         ).collect(Collectors.toUnmodifiableSet());
187         fragmentEntity.setChildFragments(childFragmentEntities);
188
189         fragmentRepository.save(fragmentEntity);
190     }
191
192     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
193         fragmentEntity.setChildFragments(Collections.emptySet());
194         fragmentRepository.save(fragmentEntity);
195     }
196 }