CPS-475: Fix Sonar Qube Violations
[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 Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
95             Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
96
97     @Override
98     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
99         final DataNode dataNode) {
100         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
101         final FragmentEntity fragmentEntity =
102             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
103         parentFragment.getChildFragments().add(fragmentEntity);
104         try {
105             fragmentRepository.save(parentFragment);
106         } catch (final DataIntegrityViolationException exception) {
107             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
108         }
109     }
110
111     @Override
112     public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
113         final Collection<DataNode> dataNodes) {
114         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
115         final List<FragmentEntity> newFragmentEntities =
116             dataNodes.stream().map(
117                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
118             ).collect(Collectors.toUnmodifiableList());
119         parentFragment.getChildFragments().addAll(newFragmentEntities);
120         try {
121             fragmentRepository.save(parentFragment);
122             dataNodes.forEach(
123                 dataNode -> getChildFragments(dataspaceName, anchorName, dataNode)
124             );
125         } catch (final DataIntegrityViolationException exception) {
126             final List<String> conflictXpaths = dataNodes.stream()
127                 .map(DataNode::getXpath)
128                 .collect(Collectors.toList());
129             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
130         }
131     }
132
133     @Override
134     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
135         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
136         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
137         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
138             dataNode);
139         try {
140             fragmentRepository.save(fragmentEntity);
141         } catch (final DataIntegrityViolationException exception) {
142             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
143         }
144     }
145
146     /**
147      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
148      * for all DataNode children recursively.
149      *
150      * @param dataspaceEntity       dataspace
151      * @param anchorEntity          anchorEntity
152      * @param dataNodeToBeConverted dataNode
153      * @return a Fragment built from current DataNode
154      */
155     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
156         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
157         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
158         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
159         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
160             final FragmentEntity childFragment =
161                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
162                     childDataNode);
163             childFragmentsImmutableSetBuilder.add(childFragment);
164         }
165         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
166         return parentFragment;
167     }
168
169     private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) {
170         for (final DataNode childDataNode: dataNode.getChildDataNodes()) {
171             final FragmentEntity getChildsParentFragmentByXPath =
172                 getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
173             final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(),
174                 getChildsParentFragmentByXPath.getAnchor(), childDataNode);
175             getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity);
176             fragmentRepository.save(getChildsParentFragmentByXPath);
177         }
178     }
179
180     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
181         final AnchorEntity anchorEntity, final DataNode dataNode) {
182         return FragmentEntity.builder()
183             .dataspace(dataspaceEntity)
184             .anchor(anchorEntity)
185             .xpath(dataNode.getXpath())
186             .attributes(GSON.toJson(dataNode.getLeaves()))
187             .build();
188     }
189
190     @Override
191     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
192         final FetchDescendantsOption fetchDescendantsOption) {
193         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
194         return toDataNode(fragmentEntity, fetchDescendantsOption);
195     }
196
197     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
198         final String xpath) {
199         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
200         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
201         if (isRootXpath(xpath)) {
202             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
203         } else {
204             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
205                 xpath);
206         }
207     }
208
209     @Override
210     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
211         final FetchDescendantsOption fetchDescendantsOption) {
212         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
213         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
214         final CpsPathQuery cpsPathQuery;
215         try {
216             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
217         } catch (final IllegalStateException e) {
218             throw new CpsPathException(e.getMessage());
219         }
220         List<FragmentEntity> fragmentEntities =
221             fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
222         if (cpsPathQuery.hasAncestorAxis()) {
223             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
224             fragmentEntities = ancestorXpaths.isEmpty()
225                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
226         }
227         return fragmentEntities.stream()
228             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
229             .collect(Collectors.toUnmodifiableList());
230     }
231
232     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
233         final CpsPathQuery cpsPathQuery) {
234         final Set<String> ancestorXpath = new HashSet<>();
235         final Pattern pattern =
236             Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
237                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
238         for (final FragmentEntity fragmentEntity : fragmentEntities) {
239             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
240             if (matcher.matches()) {
241                 ancestorXpath.add(matcher.group(1));
242             }
243         }
244         return ancestorXpath;
245     }
246
247     private DataNode toDataNode(final FragmentEntity fragmentEntity,
248         final FetchDescendantsOption fetchDescendantsOption) {
249         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
250         Map<String, Object> leaves = new HashMap<>();
251         if (fragmentEntity.getAttributes() != null) {
252             try {
253                 leaves = objectMapper.readValue(fragmentEntity.getAttributes(), Map.class);
254             } catch (final JsonProcessingException jsonProcessingException) {
255                 final String message = "Parsing error occurred while processing fragmentEntity attributes.";
256                 log.error(message);
257                 throw new DataValidationException(message,
258                     jsonProcessingException.getMessage(), jsonProcessingException);
259             }
260         }
261         return new DataNodeBuilder()
262             .withXpath(fragmentEntity.getXpath())
263             .withLeaves(leaves)
264             .withChildDataNodes(childDataNodes).build();
265     }
266
267     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
268         final FetchDescendantsOption fetchDescendantsOption) {
269         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
270             return fragmentEntity.getChildFragments().stream()
271                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
272                 .collect(Collectors.toUnmodifiableList());
273         }
274         return Collections.emptyList();
275     }
276
277     @Override
278     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
279         final Map<String, Object> leaves) {
280         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
281         fragmentEntity.setAttributes(GSON.toJson(leaves));
282         fragmentRepository.save(fragmentEntity);
283     }
284
285     @Override
286     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
287         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
288         replaceDataNodeTree(fragmentEntity, dataNode);
289         try {
290             fragmentRepository.save(fragmentEntity);
291         } catch (final StaleStateException staleStateException) {
292             throw new ConcurrencyException("Concurrent Transactions",
293                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
294                     dataspaceName, anchorName, dataNode.getXpath()),
295                 staleStateException);
296         }
297     }
298
299     private static void replaceDataNodeTree(final FragmentEntity existingFragmentEntity,
300                                             final DataNode newDataNode) {
301
302         existingFragmentEntity.setAttributes(GSON.toJson(newDataNode.getLeaves()));
303
304         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
305             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
306
307         final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
308
309         for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
310             final FragmentEntity childFragment;
311             if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
312                 childFragment = convertToFragmentWithAllDescendants(
313                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
314             } else {
315                 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
316                 replaceDataNodeTree(childFragment, newDataNodeChild);
317             }
318             updatedChildFragments.add(childFragment);
319         }
320         existingFragmentEntity.getChildFragments().clear();
321         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
322     }
323
324     @Override
325     @Transactional
326     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
327                                    final Collection<DataNode> newListElements) {
328         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
329         final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
330         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
331             extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
332         deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
333         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
334         for (final DataNode newListElement : newListElements) {
335             final FragmentEntity existingListElementEntity =
336                 existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
337             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
338                 existingListElementEntity);
339
340             updatedChildFragmentEntities.add(entityToBeAdded);
341         }
342         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
343         fragmentRepository.save(parentEntity);
344     }
345
346
347     @Override
348     @Transactional
349     public void deleteListDataNode(final String dataspaceName, final String anchorName,
350                                    final String targetXpath) {
351         deleteDataNode(dataspaceName, anchorName, targetXpath, true);
352     }
353
354     @Override
355     @Transactional
356     public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
357         deleteDataNode(dataspaceName, anchorName, targetXpath, false);
358     }
359
360     private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
361                                 final boolean onlySupportListNodeDeletion) {
362         final String parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
363         final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
364         final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
365         final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE.matcher(lastXpathElement).find();
366         boolean targetExist;
367         if (isListElement) {
368             targetExist = deleteDataNode(parentFragmentEntity, targetXpath);
369         } else {
370             targetExist = deleteAllListElements(parentFragmentEntity, targetXpath);
371             final boolean tryToDeleteDataNode = !targetExist && !onlySupportListNodeDeletion;
372             if (tryToDeleteDataNode) {
373                 targetExist = deleteDataNode(parentFragmentEntity, targetXpath);
374             }
375         }
376         if (!targetExist) {
377             final String additionalInformation = onlySupportListNodeDeletion
378                 ? "The target is probably not a List." : "";
379             throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
380                 parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
381         }
382     }
383
384     private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
385         if (parentFragmentEntity.getChildFragments()
386             .removeIf(fragment -> fragment.getXpath().equals(targetXpath))) {
387             fragmentRepository.save(parentFragmentEntity);
388             return true;
389         }
390         return false;
391     }
392
393
394     private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
395         final String deleteTargetXpathPrefix = listXpath + "[";
396         if (parentFragmentEntity.getChildFragments()
397             .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
398             fragmentRepository.save(parentFragmentEntity);
399             return true;
400         }
401         return false;
402     }
403
404     private static void deleteListElements(
405         final Collection<FragmentEntity> fragmentEntities,
406         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
407         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
408     }
409
410     private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
411         final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
412         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[") + 1);
413     }
414
415     private static FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
416                                                             final DataNode newListElement,
417                                                             final FragmentEntity existingListElementEntity) {
418         if (existingListElementEntity == null) {
419             return convertToFragmentWithAllDescendants(
420                 parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
421         }
422         if (newListElement.getChildDataNodes().isEmpty()) {
423             copyAttributesFromNewListElement(existingListElementEntity, newListElement);
424             existingListElementEntity.getChildFragments().clear();
425         } else {
426             replaceDataNodeTree(existingListElementEntity, newListElement);
427         }
428         return existingListElementEntity;
429     }
430
431     private static boolean isNewDataNode(final DataNode replacementDataNode,
432                                          final Map<String, FragmentEntity> existingListElementsByXpath) {
433         return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
434     }
435
436     private static void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
437                                                          final DataNode newListElement) {
438         final FragmentEntity replacementFragmentEntity =
439             FragmentEntity.builder().attributes(GSON.toJson(newListElement.getLeaves())).build();
440         existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
441     }
442
443     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
444         final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
445         return childEntities.stream()
446             .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
447             .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
448     }
449
450     private static boolean isRootXpath(final String xpath) {
451         return "/".equals(xpath) || "".equals(xpath);
452     }
453 }