Fixed inconsistent data issue with replaceNode
[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.Pattern;
38 import java.util.stream.Collectors;
39 import javax.transaction.Transactional;
40 import lombok.extern.slf4j.Slf4j;
41 import org.hibernate.StaleStateException;
42 import org.onap.cps.cpspath.parser.CpsPathQuery;
43 import org.onap.cps.cpspath.parser.CpsPathQueryType;
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.model.DataNode;
53 import org.onap.cps.spi.model.DataNodeBuilder;
54 import org.onap.cps.spi.repository.AnchorRepository;
55 import org.onap.cps.spi.repository.DataspaceRepository;
56 import org.onap.cps.spi.repository.FragmentRepository;
57 import org.springframework.dao.DataIntegrityViolationException;
58 import org.springframework.stereotype.Service;
59
60 @Service
61 @Slf4j
62 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
63
64     private DataspaceRepository dataspaceRepository;
65
66     private AnchorRepository anchorRepository;
67
68     private FragmentRepository fragmentRepository;
69
70     /**
71      * Constructor.
72      *
73      * @param dataspaceRepository dataspaceRepository
74      * @param anchorRepository    anchorRepository
75      * @param fragmentRepository  fragmentRepository
76      */
77     public CpsDataPersistenceServiceImpl(final DataspaceRepository dataspaceRepository,
78         final AnchorRepository anchorRepository, final FragmentRepository fragmentRepository) {
79         this.dataspaceRepository = dataspaceRepository;
80         this.anchorRepository = anchorRepository;
81         this.fragmentRepository = fragmentRepository;
82     }
83
84     private static final Gson GSON = new GsonBuilder().create();
85     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
86
87     @Override
88     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
89         final DataNode dataNode) {
90         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
91         final var fragmentEntity =
92             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
93         parentFragment.getChildFragments().add(fragmentEntity);
94         try {
95             fragmentRepository.save(parentFragment);
96         } catch (final DataIntegrityViolationException exception) {
97             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
98         }
99     }
100
101     @Override
102     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
103         final Collection<DataNode> dataNodes) {
104         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
105         final List<FragmentEntity> newFragmentEntities =
106             dataNodes.stream().map(
107                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
108             ).collect(Collectors.toUnmodifiableList());
109         parentFragment.getChildFragments().addAll(newFragmentEntities);
110         try {
111             fragmentRepository.save(parentFragment);
112         } catch (final DataIntegrityViolationException exception) {
113             final List<String> conflictXpaths = dataNodes.stream()
114                 .map(DataNode::getXpath)
115                 .collect(Collectors.toList());
116             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
117         }
118     }
119
120     @Override
121     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
122         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
123         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
124         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
125             dataNode);
126         try {
127             fragmentRepository.save(fragmentEntity);
128         } catch (final DataIntegrityViolationException exception) {
129             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
130         }
131     }
132
133     /**
134      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
135      * for all DataNode children recursively.
136      *
137      * @param dataspaceEntity       dataspace
138      * @param anchorEntity          anchorEntity
139      * @param dataNodeToBeConverted dataNode
140      * @return a Fragment built from current DataNode
141      */
142     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
143         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
144         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
145         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
146         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
147             final FragmentEntity childFragment =
148                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
149                     childDataNode);
150             childFragmentsImmutableSetBuilder.add(childFragment);
151         }
152         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
153         return parentFragment;
154     }
155
156     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
157         final AnchorEntity anchorEntity, final DataNode dataNode) {
158         return FragmentEntity.builder()
159             .dataspace(dataspaceEntity)
160             .anchor(anchorEntity)
161             .xpath(dataNode.getXpath())
162             .attributes(GSON.toJson(dataNode.getLeaves()))
163             .build();
164     }
165
166     @Override
167     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
168         final FetchDescendantsOption fetchDescendantsOption) {
169         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
170         return toDataNode(fragmentEntity, fetchDescendantsOption);
171     }
172
173     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
174         final String xpath) {
175         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
176         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
177         if (isRootXpath(xpath)) {
178             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
179         } else {
180             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
181                 xpath);
182         }
183     }
184
185     @Override
186     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
187         final FetchDescendantsOption fetchDescendantsOption) {
188         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
189         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
190         final CpsPathQuery cpsPathQuery;
191         try {
192             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
193         } catch (final IllegalStateException e) {
194             throw new CpsPathException(e.getMessage());
195         }
196         List<FragmentEntity> fragmentEntities;
197         if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
198             fragmentEntities = fragmentRepository
199                 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
200                     cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
201         } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
202             final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
203             fragmentEntities = fragmentRepository
204                 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
205                     leafDataAsJson);
206         } else {
207             fragmentEntities = fragmentRepository
208                 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
209         }
210         if (cpsPathQuery.hasAncestorAxis()) {
211             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
212             fragmentEntities = ancestorXpaths.isEmpty()
213                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
214         }
215         return fragmentEntities.stream()
216             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
217             .collect(Collectors.toUnmodifiableList());
218     }
219
220     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
221         final CpsPathQuery cpsPathQuery) {
222         final Set<String> ancestorXpath = new HashSet<>();
223         final var pattern =
224             Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
225                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
226         for (final FragmentEntity fragmentEntity : fragmentEntities) {
227             final var matcher = pattern.matcher(fragmentEntity.getXpath());
228             if (matcher.matches()) {
229                 ancestorXpath.add(matcher.group(1));
230             }
231         }
232         return ancestorXpath;
233     }
234
235     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
236         final FetchDescendantsOption fetchDescendantsOption) {
237         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
238         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
239         return new DataNodeBuilder()
240             .withXpath(fragmentEntity.getXpath())
241             .withLeaves(leaves)
242             .withChildDataNodes(childDataNodes).build();
243     }
244
245     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
246         final FetchDescendantsOption fetchDescendantsOption) {
247         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
248             return fragmentEntity.getChildFragments().stream()
249                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
250                 .collect(Collectors.toUnmodifiableList());
251         }
252         return Collections.emptyList();
253     }
254
255     @Override
256     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
257         final Map<String, Object> leaves) {
258         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
259         fragmentEntity.setAttributes(GSON.toJson(leaves));
260         fragmentRepository.save(fragmentEntity);
261     }
262
263     @Override
264     public void replaceDataNodeTree(final String dataspaceName, final String anchorName,
265         final DataNode dataNode) {
266         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
267         replaceDataNodeTree(fragmentEntity, dataNode);
268         try {
269             fragmentRepository.save(fragmentEntity);
270         } catch (final StaleStateException staleStateException) {
271             throw new ConcurrencyException("Concurrent Transactions",
272                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
273                     dataspaceName, anchorName, dataNode.getXpath()),
274                 staleStateException);
275         }
276     }
277
278     private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity, final DataNode submittedDataNode) {
279
280         existingFragmentEntity.setAttributes(GSON.toJson(submittedDataNode.getLeaves()));
281
282         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
283             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
284
285         final var updatedChildFragments = new HashSet<FragmentEntity>();
286
287         for (final DataNode submittedChildDataNode : submittedDataNode.getChildDataNodes()) {
288             final FragmentEntity childFragment;
289             if (existingChildrenByXpath.containsKey(submittedChildDataNode.getXpath())) {
290                 childFragment = existingChildrenByXpath.get(submittedChildDataNode.getXpath());
291                 replaceDataNodeTree(childFragment, submittedChildDataNode);
292             } else {
293                 childFragment = convertToFragmentWithAllDescendants(
294                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), submittedChildDataNode);
295             }
296             updatedChildFragments.add(childFragment);
297         }
298         existingFragmentEntity.setChildFragments(updatedChildFragments);
299     }
300
301     @Override
302     @Transactional
303     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
304         final Collection<DataNode> dataNodes) {
305         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
306         final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
307         final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
308         removeListNodeDescendants(parentEntity, listNodeXpath);
309         final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
310             dataNode -> convertToFragmentWithAllDescendants(
311                 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
312         ).collect(Collectors.toUnmodifiableSet());
313         parentEntity.getChildFragments().addAll(childFragmentEntities);
314         fragmentRepository.save(parentEntity);
315     }
316
317     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
318         final String listNodeXpathPrefix = listNodeXpath + "[";
319         if (parentFragmentEntity.getChildFragments()
320             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
321             fragmentRepository.save(parentFragmentEntity);
322         }
323     }
324
325     private static boolean isRootXpath(final String xpath) {
326         return "/".equals(xpath) || "".equals(xpath);
327     }
328 }