Merge "Delete list-node p1 service and persistence layers"
[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  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.spi.impl;
24
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.ImmutableSet.Builder;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import java.util.stream.Collectors;
40 import javax.transaction.Transactional;
41 import lombok.extern.slf4j.Slf4j;
42 import org.hibernate.StaleStateException;
43 import org.onap.cps.cpspath.parser.CpsPathQuery;
44 import org.onap.cps.spi.CpsDataPersistenceService;
45 import org.onap.cps.spi.FetchDescendantsOption;
46 import org.onap.cps.spi.entities.AnchorEntity;
47 import org.onap.cps.spi.entities.DataspaceEntity;
48 import org.onap.cps.spi.entities.FragmentEntity;
49 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
50 import org.onap.cps.spi.exceptions.ConcurrencyException;
51 import org.onap.cps.spi.exceptions.CpsPathException;
52 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
53 import org.onap.cps.spi.model.DataNode;
54 import org.onap.cps.spi.model.DataNodeBuilder;
55 import org.onap.cps.spi.repository.AnchorRepository;
56 import org.onap.cps.spi.repository.DataspaceRepository;
57 import org.onap.cps.spi.repository.FragmentRepository;
58 import org.springframework.dao.DataIntegrityViolationException;
59 import org.springframework.stereotype.Service;
60
61 @Service
62 @Slf4j
63 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
64
65     private DataspaceRepository dataspaceRepository;
66
67     private AnchorRepository anchorRepository;
68
69     private FragmentRepository fragmentRepository;
70
71     /**
72      * Constructor.
73      *
74      * @param dataspaceRepository dataspaceRepository
75      * @param anchorRepository    anchorRepository
76      * @param fragmentRepository  fragmentRepository
77      */
78     public CpsDataPersistenceServiceImpl(final DataspaceRepository dataspaceRepository,
79         final AnchorRepository anchorRepository, final FragmentRepository fragmentRepository) {
80         this.dataspaceRepository = dataspaceRepository;
81         this.anchorRepository = anchorRepository;
82         this.fragmentRepository = fragmentRepository;
83     }
84
85     private static final Gson GSON = new GsonBuilder().create();
86     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
87     private static final String REG_EX_FOR_LIST_NODE_KEY = "\\[(\\@([^/]*?))+( and)*\\]$";
88
89     @Override
90     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
91         final DataNode dataNode) {
92         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
93         final var fragmentEntity =
94             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
95         parentFragment.getChildFragments().add(fragmentEntity);
96         try {
97             fragmentRepository.save(parentFragment);
98         } catch (final DataIntegrityViolationException exception) {
99             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
100         }
101     }
102
103     @Override
104     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
105         final Collection<DataNode> dataNodes) {
106         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
107         final List<FragmentEntity> newFragmentEntities =
108             dataNodes.stream().map(
109                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
110             ).collect(Collectors.toUnmodifiableList());
111         parentFragment.getChildFragments().addAll(newFragmentEntities);
112         try {
113             fragmentRepository.save(parentFragment);
114             dataNodes.forEach(
115                 dataNode -> getChildFragments(dataspaceName, anchorName, dataNode)
116             );
117         } catch (final DataIntegrityViolationException exception) {
118             final List<String> conflictXpaths = dataNodes.stream()
119                 .map(DataNode::getXpath)
120                 .collect(Collectors.toList());
121             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
122         }
123     }
124
125     @Override
126     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
127         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
128         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
129         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
130             dataNode);
131         try {
132             fragmentRepository.save(fragmentEntity);
133         } catch (final DataIntegrityViolationException exception) {
134             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
135         }
136     }
137
138     /**
139      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
140      * for all DataNode children recursively.
141      *
142      * @param dataspaceEntity       dataspace
143      * @param anchorEntity          anchorEntity
144      * @param dataNodeToBeConverted dataNode
145      * @return a Fragment built from current DataNode
146      */
147     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
148         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
149         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
150         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
151         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
152             final FragmentEntity childFragment =
153                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
154                     childDataNode);
155             childFragmentsImmutableSetBuilder.add(childFragment);
156         }
157         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
158         return parentFragment;
159     }
160
161     private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) {
162         for (final DataNode childDataNode: dataNode.getChildDataNodes()) {
163             final FragmentEntity getChildsParentFragmentByXPath =
164                 getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
165             final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(),
166                 getChildsParentFragmentByXPath.getAnchor(), childDataNode);
167             getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity);
168             fragmentRepository.save(getChildsParentFragmentByXPath);
169         }
170     }
171
172     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
173         final AnchorEntity anchorEntity, final DataNode dataNode) {
174         return FragmentEntity.builder()
175             .dataspace(dataspaceEntity)
176             .anchor(anchorEntity)
177             .xpath(dataNode.getXpath())
178             .attributes(GSON.toJson(dataNode.getLeaves()))
179             .build();
180     }
181
182     @Override
183     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
184         final FetchDescendantsOption fetchDescendantsOption) {
185         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
186         return toDataNode(fragmentEntity, fetchDescendantsOption);
187     }
188
189     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
190         final String xpath) {
191         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
192         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
193         if (isRootXpath(xpath)) {
194             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
195         } else {
196             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
197                 xpath);
198         }
199     }
200
201     @Override
202     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
203         final FetchDescendantsOption fetchDescendantsOption) {
204         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
205         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
206         final CpsPathQuery cpsPathQuery;
207         try {
208             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
209         } catch (final IllegalStateException e) {
210             throw new CpsPathException(e.getMessage());
211         }
212         List<FragmentEntity> fragmentEntities =
213             fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
214         if (cpsPathQuery.hasAncestorAxis()) {
215             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
216             fragmentEntities = ancestorXpaths.isEmpty()
217                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
218         }
219         return fragmentEntities.stream()
220             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
221             .collect(Collectors.toUnmodifiableList());
222     }
223
224     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
225         final CpsPathQuery cpsPathQuery) {
226         final Set<String> ancestorXpath = new HashSet<>();
227         final var pattern =
228             Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
229                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
230         for (final FragmentEntity fragmentEntity : fragmentEntities) {
231             final var matcher = pattern.matcher(fragmentEntity.getXpath());
232             if (matcher.matches()) {
233                 ancestorXpath.add(matcher.group(1));
234             }
235         }
236         return ancestorXpath;
237     }
238
239     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
240         final FetchDescendantsOption fetchDescendantsOption) {
241         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
242         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
243         return new DataNodeBuilder()
244             .withXpath(fragmentEntity.getXpath())
245             .withLeaves(leaves)
246             .withChildDataNodes(childDataNodes).build();
247     }
248
249     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
250         final FetchDescendantsOption fetchDescendantsOption) {
251         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
252             return fragmentEntity.getChildFragments().stream()
253                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
254                 .collect(Collectors.toUnmodifiableList());
255         }
256         return Collections.emptyList();
257     }
258
259     @Override
260     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
261         final Map<String, Object> leaves) {
262         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
263         fragmentEntity.setAttributes(GSON.toJson(leaves));
264         fragmentRepository.save(fragmentEntity);
265     }
266
267     @Override
268     public void replaceDataNodeTree(final String dataspaceName, final String anchorName,
269         final DataNode dataNode) {
270         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
271         replaceDataNodeTree(fragmentEntity, dataNode);
272         try {
273             fragmentRepository.save(fragmentEntity);
274         } catch (final StaleStateException staleStateException) {
275             throw new ConcurrencyException("Concurrent Transactions",
276                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
277                     dataspaceName, anchorName, dataNode.getXpath()),
278                 staleStateException);
279         }
280     }
281
282     private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity, final DataNode submittedDataNode) {
283
284         existingFragmentEntity.setAttributes(GSON.toJson(submittedDataNode.getLeaves()));
285
286         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
287             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
288
289         final var updatedChildFragments = new HashSet<FragmentEntity>();
290
291         for (final DataNode submittedChildDataNode : submittedDataNode.getChildDataNodes()) {
292             final FragmentEntity childFragment;
293             if (existingChildrenByXpath.containsKey(submittedChildDataNode.getXpath())) {
294                 childFragment = existingChildrenByXpath.get(submittedChildDataNode.getXpath());
295                 replaceDataNodeTree(childFragment, submittedChildDataNode);
296             } else {
297                 childFragment = convertToFragmentWithAllDescendants(
298                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), submittedChildDataNode);
299             }
300             updatedChildFragments.add(childFragment);
301         }
302         existingFragmentEntity.setChildFragments(updatedChildFragments);
303     }
304
305     @Override
306     @Transactional
307     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
308         final Collection<DataNode> dataNodes) {
309         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
310         final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
311         final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
312         removeListNodeDescendants(parentEntity, listNodeXpath);
313         final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
314             dataNode -> convertToFragmentWithAllDescendants(
315                 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
316         ).collect(Collectors.toUnmodifiableSet());
317         parentEntity.getChildFragments().addAll(childFragmentEntities);
318         fragmentRepository.save(parentEntity);
319     }
320
321     @Override
322     @Transactional
323     public void deleteListDataNodes(final String dataspaceName, final String anchorName, final String listNodeXpath) {
324         final var parentNodeXpath = listNodeXpath.substring(0, listNodeXpath.lastIndexOf('/'));
325         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
326         final var descendantNode = listNodeXpath.substring(listNodeXpath.lastIndexOf('/'));
327         final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(descendantNode);
328
329         final boolean xpathPointsToAValidChildNodeWithKey = parentEntity.getChildFragments().stream().anyMatch(
330             (fragment) -> fragment.getXpath().equals(listNodeXpath));
331
332         final boolean xpathPointsToAValidChildNodeWithoutKey = parentEntity.getChildFragments().stream().anyMatch(
333             (fragment) -> fragment.getXpath().replaceAll(REG_EX_FOR_LIST_NODE_KEY, "").equals(listNodeXpath));
334
335         if ((descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithKey)
336             ||
337             (!descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithoutKey)) {
338             removeListNodeDescendants(parentEntity, listNodeXpath);
339         } else {
340             throw new DataNodeNotFoundException(parentEntity.getDataspace().getName(),
341                 parentEntity.getAnchor().getName(), listNodeXpath);
342         }
343     }
344
345     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
346         final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(listNodeXpath);
347         final String listNodeXpathPrefix = listNodeXpath + (descendantNodeHasListNodeKey.find() ? "" : "[");
348         if (parentFragmentEntity.getChildFragments()
349             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
350             fragmentRepository.save(parentFragmentEntity);
351         }
352     }
353
354     private static boolean isRootXpath(final String xpath) {
355         return "/".equals(xpath) || "".equals(xpath);
356     }
357 }