Added parent is null condition when searching for root fragments
[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  *  Modifications Copyright (C) 2020-2021 Bell Canada.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl;
23
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_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.Collection;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import javax.transaction.Transactional;
39 import org.onap.cps.cpspath.parser.CpsPathQuery;
40 import org.onap.cps.cpspath.parser.CpsPathQueryType;
41 import org.onap.cps.spi.CpsDataPersistenceService;
42 import org.onap.cps.spi.FetchDescendantsOption;
43 import org.onap.cps.spi.entities.AnchorEntity;
44 import org.onap.cps.spi.entities.DataspaceEntity;
45 import org.onap.cps.spi.entities.FragmentEntity;
46 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
47 import org.onap.cps.spi.exceptions.CpsPathException;
48 import org.onap.cps.spi.model.DataNode;
49 import org.onap.cps.spi.model.DataNodeBuilder;
50 import org.onap.cps.spi.repository.AnchorRepository;
51 import org.onap.cps.spi.repository.DataspaceRepository;
52 import org.onap.cps.spi.repository.FragmentRepository;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.dao.DataIntegrityViolationException;
55 import org.springframework.stereotype.Service;
56
57 @Service
58 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
59
60     @Autowired
61     private DataspaceRepository dataspaceRepository;
62
63     @Autowired
64     private AnchorRepository anchorRepository;
65
66     @Autowired
67     private FragmentRepository fragmentRepository;
68
69     private static final Gson GSON = new GsonBuilder().create();
70     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
71
72     @Override
73     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
74         final DataNode dataNode) {
75         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
76         final var fragmentEntity =
77             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
78         parentFragment.getChildFragments().add(fragmentEntity);
79         try {
80             fragmentRepository.save(parentFragment);
81         } catch (final DataIntegrityViolationException exception) {
82             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
83         }
84     }
85
86     @Override
87     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
88         final Collection<DataNode> dataNodes) {
89         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
90         final List<FragmentEntity> newFragmentEntities =
91             dataNodes.stream().map(
92                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
93             ).collect(Collectors.toUnmodifiableList());
94         parentFragment.getChildFragments().addAll(newFragmentEntities);
95         try {
96             fragmentRepository.save(parentFragment);
97         } catch (final DataIntegrityViolationException exception) {
98             final List<String> conflictXpaths = dataNodes.stream()
99                 .map(DataNode::getXpath)
100                 .collect(Collectors.toList());
101             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
102         }
103     }
104
105     @Override
106     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
107         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
108         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
109         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
110             dataNode);
111         try {
112             fragmentRepository.save(fragmentEntity);
113         } catch (final DataIntegrityViolationException exception) {
114             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
115         }
116     }
117
118     /**
119      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
120      * for all DataNode children recursively.
121      *
122      * @param dataspaceEntity       dataspace
123      * @param anchorEntity          anchorEntity
124      * @param dataNodeToBeConverted dataNode
125      * @return a Fragment built from current DataNode
126      */
127     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
128         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
129         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
130         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
131         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
132             final FragmentEntity childFragment =
133                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
134                     childDataNode);
135             childFragmentsImmutableSetBuilder.add(childFragment);
136         }
137         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
138         return parentFragment;
139     }
140
141     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
142         final AnchorEntity anchorEntity, final DataNode dataNode) {
143         return FragmentEntity.builder()
144             .dataspace(dataspaceEntity)
145             .anchor(anchorEntity)
146             .xpath(dataNode.getXpath())
147             .attributes(GSON.toJson(dataNode.getLeaves()))
148             .build();
149     }
150
151     @Override
152     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
153         final FetchDescendantsOption fetchDescendantsOption) {
154         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
155         return toDataNode(fragmentEntity, fetchDescendantsOption);
156     }
157
158     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
159         final String xpath) {
160         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
161         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
162         if (isRootXpath(xpath)) {
163             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
164         } else {
165             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
166                 xpath);
167         }
168     }
169
170     @Override
171     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
172         final FetchDescendantsOption fetchDescendantsOption) {
173         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
174         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
175         final CpsPathQuery cpsPathQuery;
176         try {
177             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
178         } catch (final IllegalStateException e) {
179             throw new CpsPathException(e.getMessage());
180         }
181         List<FragmentEntity> fragmentEntities;
182         if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
183             fragmentEntities = fragmentRepository
184                 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
185                     cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
186         } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
187             final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
188             fragmentEntities = fragmentRepository
189                 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
190                     leafDataAsJson);
191         } else {
192             fragmentEntities = fragmentRepository
193                 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
194         }
195         if (cpsPathQuery.hasAncestorAxis()) {
196             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
197             fragmentEntities = ancestorXpaths.isEmpty()
198                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
199         }
200         return fragmentEntities.stream()
201             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
202             .collect(Collectors.toUnmodifiableList());
203     }
204
205     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
206         final CpsPathQuery cpsPathQuery) {
207         final Set<String> ancestorXpath = new HashSet<>();
208         final var pattern =
209             Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
210                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
211         for (final FragmentEntity fragmentEntity : fragmentEntities) {
212             final var matcher = pattern.matcher(fragmentEntity.getXpath());
213             if (matcher.matches()) {
214                 ancestorXpath.add(matcher.group(1));
215             }
216         }
217         return ancestorXpath;
218     }
219
220     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
221         final FetchDescendantsOption fetchDescendantsOption) {
222         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
223         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
224         return new DataNodeBuilder()
225             .withXpath(fragmentEntity.getXpath())
226             .withLeaves(leaves)
227             .withChildDataNodes(childDataNodes).build();
228     }
229
230     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
231         final FetchDescendantsOption fetchDescendantsOption) {
232         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
233             return fragmentEntity.getChildFragments().stream()
234                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
235                 .collect(Collectors.toUnmodifiableList());
236         }
237         return Collections.emptyList();
238     }
239
240     @Override
241     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
242         final Map<String, Object> leaves) {
243         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
244         fragmentEntity.setAttributes(GSON.toJson(leaves));
245         fragmentRepository.save(fragmentEntity);
246     }
247
248     @Override
249     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
250         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
251         removeExistingDescendants(fragmentEntity);
252
253         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
254         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
255             childDataNode -> convertToFragmentWithAllDescendants(
256                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
257         ).collect(Collectors.toUnmodifiableSet());
258         fragmentEntity.setChildFragments(childFragmentEntities);
259
260         fragmentRepository.save(fragmentEntity);
261     }
262
263     @Override
264     @Transactional
265     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
266         final Collection<DataNode> dataNodes) {
267         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
268         final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
269         final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
270         removeListNodeDescendants(parentEntity, listNodeXpath);
271         final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
272             dataNode -> convertToFragmentWithAllDescendants(
273                 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
274         ).collect(Collectors.toUnmodifiableSet());
275         parentEntity.getChildFragments().addAll(childFragmentEntities);
276         fragmentRepository.save(parentEntity);
277     }
278
279     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
280         final String listNodeXpathPrefix = listNodeXpath + "[";
281         if (parentFragmentEntity.getChildFragments()
282             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
283             fragmentRepository.save(parentFragmentEntity);
284         }
285     }
286
287     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
288         fragmentEntity.setChildFragments(Collections.emptySet());
289         fragmentRepository.save(fragmentEntity);
290     }
291
292     private static boolean isRootXpath(final String xpath) {
293         return "/".equals(xpath) || "".equals(xpath);
294     }
295 }