f924c70453c48037fd91b6437d1da5709ce696c2
[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.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.google.common.collect.ImmutableSet;
30 import com.google.common.collect.ImmutableSet.Builder;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42 import java.util.stream.Collectors;
43 import javax.transaction.Transactional;
44 import lombok.extern.slf4j.Slf4j;
45 import org.hibernate.StaleStateException;
46 import org.onap.cps.cpspath.parser.CpsPathQuery;
47 import org.onap.cps.spi.CpsDataPersistenceService;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.entities.AnchorEntity;
50 import org.onap.cps.spi.entities.DataspaceEntity;
51 import org.onap.cps.spi.entities.FragmentEntity;
52 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
53 import org.onap.cps.spi.exceptions.ConcurrencyException;
54 import org.onap.cps.spi.exceptions.CpsPathException;
55 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
56 import org.onap.cps.spi.exceptions.DataValidationException;
57 import org.onap.cps.spi.model.DataNode;
58 import org.onap.cps.spi.model.DataNodeBuilder;
59 import org.onap.cps.spi.repository.AnchorRepository;
60 import org.onap.cps.spi.repository.DataspaceRepository;
61 import org.onap.cps.spi.repository.FragmentRepository;
62 import org.springframework.dao.DataIntegrityViolationException;
63 import org.springframework.stereotype.Service;
64
65 @Service
66 @Slf4j
67 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
68
69     private DataspaceRepository dataspaceRepository;
70
71     private AnchorRepository anchorRepository;
72
73     private FragmentRepository fragmentRepository;
74
75     private final ObjectMapper objectMapper;
76
77     /**
78      * Constructor.
79      *
80      * @param dataspaceRepository dataspaceRepository
81      * @param anchorRepository    anchorRepository
82      * @param fragmentRepository  fragmentRepository
83      */
84     public CpsDataPersistenceServiceImpl(final DataspaceRepository dataspaceRepository,
85         final AnchorRepository anchorRepository, final FragmentRepository fragmentRepository) {
86         this.dataspaceRepository = dataspaceRepository;
87         this.anchorRepository = anchorRepository;
88         this.fragmentRepository = fragmentRepository;
89         objectMapper = new ObjectMapper();
90     }
91
92     private static final Gson GSON = new GsonBuilder().create();
93     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
94     private static final String REG_EX_FOR_LIST_NODE_KEY = "\\[(\\@([^/]*?)){0,99}( and)*\\]$";
95
96     @Override
97     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
98         final DataNode dataNode) {
99         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
100         final FragmentEntity fragmentEntity =
101             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
102         parentFragment.getChildFragments().add(fragmentEntity);
103         try {
104             fragmentRepository.save(parentFragment);
105         } catch (final DataIntegrityViolationException exception) {
106             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
107         }
108     }
109
110     @Override
111     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
112         final Collection<DataNode> dataNodes) {
113         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
114         final List<FragmentEntity> newFragmentEntities =
115             dataNodes.stream().map(
116                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
117             ).collect(Collectors.toUnmodifiableList());
118         parentFragment.getChildFragments().addAll(newFragmentEntities);
119         try {
120             fragmentRepository.save(parentFragment);
121             dataNodes.forEach(
122                 dataNode -> getChildFragments(dataspaceName, anchorName, dataNode)
123             );
124         } catch (final DataIntegrityViolationException exception) {
125             final List<String> conflictXpaths = dataNodes.stream()
126                 .map(DataNode::getXpath)
127                 .collect(Collectors.toList());
128             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
129         }
130     }
131
132     @Override
133     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
134         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
135         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
136         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
137             dataNode);
138         try {
139             fragmentRepository.save(fragmentEntity);
140         } catch (final DataIntegrityViolationException exception) {
141             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
142         }
143     }
144
145     /**
146      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
147      * for all DataNode children recursively.
148      *
149      * @param dataspaceEntity       dataspace
150      * @param anchorEntity          anchorEntity
151      * @param dataNodeToBeConverted dataNode
152      * @return a Fragment built from current DataNode
153      */
154     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
155         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
156         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
157         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
158         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
159             final FragmentEntity childFragment =
160                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
161                     childDataNode);
162             childFragmentsImmutableSetBuilder.add(childFragment);
163         }
164         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
165         return parentFragment;
166     }
167
168     private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) {
169         for (final DataNode childDataNode: dataNode.getChildDataNodes()) {
170             final FragmentEntity getChildsParentFragmentByXPath =
171                 getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
172             final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(),
173                 getChildsParentFragmentByXPath.getAnchor(), childDataNode);
174             getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity);
175             fragmentRepository.save(getChildsParentFragmentByXPath);
176         }
177     }
178
179     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
180         final AnchorEntity anchorEntity, final DataNode dataNode) {
181         return FragmentEntity.builder()
182             .dataspace(dataspaceEntity)
183             .anchor(anchorEntity)
184             .xpath(dataNode.getXpath())
185             .attributes(GSON.toJson(dataNode.getLeaves()))
186             .build();
187     }
188
189     @Override
190     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
191         final FetchDescendantsOption fetchDescendantsOption) {
192         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
193         return toDataNode(fragmentEntity, fetchDescendantsOption);
194     }
195
196     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
197         final String xpath) {
198         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
199         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
200         if (isRootXpath(xpath)) {
201             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
202         } else {
203             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
204                 xpath);
205         }
206     }
207
208     @Override
209     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
210         final FetchDescendantsOption fetchDescendantsOption) {
211         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
212         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
213         final CpsPathQuery cpsPathQuery;
214         try {
215             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
216         } catch (final IllegalStateException e) {
217             throw new CpsPathException(e.getMessage());
218         }
219         List<FragmentEntity> fragmentEntities =
220             fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
221         if (cpsPathQuery.hasAncestorAxis()) {
222             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
223             fragmentEntities = ancestorXpaths.isEmpty()
224                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
225         }
226         return fragmentEntities.stream()
227             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
228             .collect(Collectors.toUnmodifiableList());
229     }
230
231     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
232         final CpsPathQuery cpsPathQuery) {
233         final Set<String> ancestorXpath = new HashSet<>();
234         final Pattern pattern =
235             Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
236                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
237         for (final FragmentEntity fragmentEntity : fragmentEntities) {
238             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
239             if (matcher.matches()) {
240                 ancestorXpath.add(matcher.group(1));
241             }
242         }
243         return ancestorXpath;
244     }
245
246     private DataNode toDataNode(final FragmentEntity fragmentEntity,
247         final FetchDescendantsOption fetchDescendantsOption) {
248         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
249         Map<String, Object> leaves = new HashMap<>();
250         if (fragmentEntity.getAttributes() != null) {
251             try {
252                 leaves = objectMapper.readValue(fragmentEntity.getAttributes(), Map.class);
253             } catch (final JsonProcessingException jsonProcessingException) {
254                 final String message = "Parsing error occurred while processing fragmentEntity attributes.";
255                 log.error(message);
256                 throw new DataValidationException(message,
257                     jsonProcessingException.getMessage(), jsonProcessingException);
258             }
259         }
260         return new DataNodeBuilder()
261             .withXpath(fragmentEntity.getXpath())
262             .withLeaves(leaves)
263             .withChildDataNodes(childDataNodes).build();
264     }
265
266     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
267         final FetchDescendantsOption fetchDescendantsOption) {
268         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
269             return fragmentEntity.getChildFragments().stream()
270                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
271                 .collect(Collectors.toUnmodifiableList());
272         }
273         return Collections.emptyList();
274     }
275
276     @Override
277     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
278         final Map<String, Object> leaves) {
279         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
280         fragmentEntity.setAttributes(GSON.toJson(leaves));
281         fragmentRepository.save(fragmentEntity);
282     }
283
284     @Override
285     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
286         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
287         replaceDataNodeTree(fragmentEntity, dataNode);
288         try {
289             fragmentRepository.save(fragmentEntity);
290         } catch (final StaleStateException staleStateException) {
291             throw new ConcurrencyException("Concurrent Transactions",
292                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
293                     dataspaceName, anchorName, dataNode.getXpath()),
294                 staleStateException);
295         }
296     }
297
298     private static void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
299                                             final DataNode submittedDataNode) {
300
301         existingFragmentEntity.setAttributes(GSON.toJson(submittedDataNode.getLeaves()));
302
303         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
304             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
305
306         final Collection updatedChildFragments = new HashSet<FragmentEntity>();
307
308         for (final DataNode submittedChildDataNode : submittedDataNode.getChildDataNodes()) {
309             final FragmentEntity childFragment;
310             if (isNewDataNode(submittedChildDataNode, existingChildrenByXpath)) {
311                 childFragment = convertToFragmentWithAllDescendants(
312                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), submittedChildDataNode);
313             } else {
314                 childFragment = existingChildrenByXpath.get(submittedChildDataNode.getXpath());
315                 replaceDataNodeTree(childFragment, submittedChildDataNode);
316             }
317             updatedChildFragments.add(childFragment);
318         }
319         existingFragmentEntity.getChildFragments().clear();
320         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
321     }
322
323     @Override
324     @Transactional
325     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
326                                      final Collection<DataNode> replacementDataNodes) {
327         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
328         final String listNodeXpathPrefix = getListNodeXpathPrefix(replacementDataNodes);
329         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
330             extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listNodeXpathPrefix);
331         removeExistingListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
332         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
333         for (final DataNode replacementDataNode : replacementDataNodes) {
334             final FragmentEntity existingListNodeElementEntity =
335                 existingListElementFragmentEntitiesByXPath.get(replacementDataNode.getXpath());
336             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, replacementDataNode,
337                 existingListNodeElementEntity);
338
339             updatedChildFragmentEntities.add(entityToBeAdded);
340         }
341         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
342         fragmentRepository.save(parentEntity);
343     }
344
345     private static void removeExistingListElements(
346         final Collection<FragmentEntity> fragmentEntities,
347         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
348         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
349     }
350
351     private static String getListNodeXpathPrefix(final Collection<DataNode> replacementDataNodes) {
352         final String firstChildNodeXpath = replacementDataNodes.iterator().next().getXpath();
353         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[") + 1);
354     }
355
356     private static FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
357                                                             final DataNode replacementDataNode,
358                                                             final FragmentEntity existingListNodeElementEntity) {
359         if (existingListNodeElementEntity == null) {
360             return convertToFragmentWithAllDescendants(
361                 parentEntity.getDataspace(), parentEntity.getAnchor(), replacementDataNode);
362         }
363         if (replacementDataNode.getChildDataNodes().isEmpty()) {
364             copyAttributesFromReplacementDataNode(existingListNodeElementEntity, replacementDataNode);
365             existingListNodeElementEntity.getChildFragments().clear();
366         } else {
367             replaceDataNodeTree(existingListNodeElementEntity, replacementDataNode);
368         }
369         return existingListNodeElementEntity;
370     }
371
372     private static boolean isNewDataNode(final DataNode replacementDataNode,
373                                          final Map<String, FragmentEntity> existingListNodeElementsByXpath) {
374         return !existingListNodeElementsByXpath.containsKey(replacementDataNode.getXpath());
375     }
376
377     private static void copyAttributesFromReplacementDataNode(final FragmentEntity existingListNodeElementEntity,
378                                                               final DataNode replacementDataNode) {
379         final FragmentEntity replacementFragmentEntity =
380             FragmentEntity.builder().attributes(GSON.toJson(replacementDataNode.getLeaves())).build();
381         existingListNodeElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
382     }
383
384     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
385         final Set<FragmentEntity> childEntities, final String listNodeXpathPrefix) {
386         return childEntities.stream()
387             .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listNodeXpathPrefix))
388             .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
389     }
390
391     @Override
392     @Transactional
393     public void deleteListDataNodes(final String dataspaceName, final String anchorName, final String listNodeXpath) {
394         final String parentNodeXpath = listNodeXpath.substring(0, listNodeXpath.lastIndexOf('/'));
395         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
396         final String descendantNode = listNodeXpath.substring(listNodeXpath.lastIndexOf('/'));
397         final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(descendantNode);
398
399         final boolean xpathPointsToAValidChildNodeWithKey = parentEntity.getChildFragments().stream().anyMatch(
400             fragment -> fragment.getXpath().equals(listNodeXpath));
401
402         final boolean xpathPointsToAValidChildNodeWithoutKey = parentEntity.getChildFragments().stream().anyMatch(
403             fragment -> fragment.getXpath().replaceAll(REG_EX_FOR_LIST_NODE_KEY, "").equals(listNodeXpath));
404
405         if ((descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithKey)
406             ||
407             (!descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithoutKey)) {
408             removeListNodeDescendants(parentEntity, listNodeXpath);
409         } else {
410             throw new DataNodeNotFoundException(parentEntity.getDataspace().getName(),
411                 parentEntity.getAnchor().getName(), listNodeXpath);
412         }
413     }
414
415     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
416         final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(listNodeXpath);
417         final String listNodeXpathPrefix = listNodeXpath + (descendantNodeHasListNodeKey.find() ? "" : "[");
418         if (parentFragmentEntity.getChildFragments()
419             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
420             fragmentRepository.save(parentFragmentEntity);
421         }
422     }
423
424     private static boolean isRootXpath(final String xpath) {
425         return "/".equals(xpath) || "".equals(xpath);
426     }
427 }