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