Merge "Implement DMI Registration (NCMP-Side)"
[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         } catch (final DataIntegrityViolationException exception) {
112             final List<String> conflictXpaths = dataNodes.stream()
113                 .map(DataNode::getXpath)
114                 .collect(Collectors.toList());
115             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
116         }
117     }
118
119     @Override
120     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
121         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
122         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
123         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
124             dataNode);
125         try {
126             fragmentRepository.save(fragmentEntity);
127         } catch (final DataIntegrityViolationException exception) {
128             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
129         }
130     }
131
132     /**
133      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
134      * for all DataNode children recursively.
135      *
136      * @param dataspaceEntity       dataspace
137      * @param anchorEntity          anchorEntity
138      * @param dataNodeToBeConverted dataNode
139      * @return a Fragment built from current DataNode
140      */
141     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
142         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
143         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
144         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
145         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
146             final FragmentEntity childFragment =
147                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
148                     childDataNode);
149             childFragmentsImmutableSetBuilder.add(childFragment);
150         }
151         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
152         return parentFragment;
153     }
154
155     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
156         final AnchorEntity anchorEntity, final DataNode dataNode) {
157         return FragmentEntity.builder()
158             .dataspace(dataspaceEntity)
159             .anchor(anchorEntity)
160             .xpath(dataNode.getXpath())
161             .attributes(GSON.toJson(dataNode.getLeaves()))
162             .build();
163     }
164
165     @Override
166     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
167         final FetchDescendantsOption fetchDescendantsOption) {
168         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
169         return toDataNode(fragmentEntity, fetchDescendantsOption);
170     }
171
172     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
173         final String xpath) {
174         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
175         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
176         if (isRootXpath(xpath)) {
177             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
178         } else {
179             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
180                 xpath);
181         }
182     }
183
184     @Override
185     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
186         final FetchDescendantsOption fetchDescendantsOption) {
187         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
188         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
189         final CpsPathQuery cpsPathQuery;
190         try {
191             cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
192         } catch (final IllegalStateException e) {
193             throw new CpsPathException(e.getMessage());
194         }
195         List<FragmentEntity> fragmentEntities =
196             fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
197         if (cpsPathQuery.hasAncestorAxis()) {
198             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
199             fragmentEntities = ancestorXpaths.isEmpty()
200                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
201         }
202         return fragmentEntities.stream()
203             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
204             .collect(Collectors.toUnmodifiableList());
205     }
206
207     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
208         final CpsPathQuery cpsPathQuery) {
209         final Set<String> ancestorXpath = new HashSet<>();
210         final var pattern =
211             Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
212                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
213         for (final FragmentEntity fragmentEntity : fragmentEntities) {
214             final var matcher = pattern.matcher(fragmentEntity.getXpath());
215             if (matcher.matches()) {
216                 ancestorXpath.add(matcher.group(1));
217             }
218         }
219         return ancestorXpath;
220     }
221
222     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
223         final FetchDescendantsOption fetchDescendantsOption) {
224         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
225         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
226         return new DataNodeBuilder()
227             .withXpath(fragmentEntity.getXpath())
228             .withLeaves(leaves)
229             .withChildDataNodes(childDataNodes).build();
230     }
231
232     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
233         final FetchDescendantsOption fetchDescendantsOption) {
234         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
235             return fragmentEntity.getChildFragments().stream()
236                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
237                 .collect(Collectors.toUnmodifiableList());
238         }
239         return Collections.emptyList();
240     }
241
242     @Override
243     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
244         final Map<String, Object> leaves) {
245         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
246         fragmentEntity.setAttributes(GSON.toJson(leaves));
247         fragmentRepository.save(fragmentEntity);
248     }
249
250     @Override
251     public void replaceDataNodeTree(final String dataspaceName, final String anchorName,
252         final DataNode dataNode) {
253         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
254         replaceDataNodeTree(fragmentEntity, dataNode);
255         try {
256             fragmentRepository.save(fragmentEntity);
257         } catch (final StaleStateException staleStateException) {
258             throw new ConcurrencyException("Concurrent Transactions",
259                 String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
260                     dataspaceName, anchorName, dataNode.getXpath()),
261                 staleStateException);
262         }
263     }
264
265     private void replaceDataNodeTree(final FragmentEntity existingFragmentEntity, final DataNode submittedDataNode) {
266
267         existingFragmentEntity.setAttributes(GSON.toJson(submittedDataNode.getLeaves()));
268
269         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments()
270             .stream().collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
271
272         final var updatedChildFragments = new HashSet<FragmentEntity>();
273
274         for (final DataNode submittedChildDataNode : submittedDataNode.getChildDataNodes()) {
275             final FragmentEntity childFragment;
276             if (existingChildrenByXpath.containsKey(submittedChildDataNode.getXpath())) {
277                 childFragment = existingChildrenByXpath.get(submittedChildDataNode.getXpath());
278                 replaceDataNodeTree(childFragment, submittedChildDataNode);
279             } else {
280                 childFragment = convertToFragmentWithAllDescendants(
281                     existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), submittedChildDataNode);
282             }
283             updatedChildFragments.add(childFragment);
284         }
285         existingFragmentEntity.setChildFragments(updatedChildFragments);
286     }
287
288     @Override
289     @Transactional
290     public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
291         final Collection<DataNode> dataNodes) {
292         final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
293         final var firstChildNodeXpath = dataNodes.iterator().next().getXpath();
294         final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("["));
295         removeListNodeDescendants(parentEntity, listNodeXpath);
296         final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map(
297             dataNode -> convertToFragmentWithAllDescendants(
298                 parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode)
299         ).collect(Collectors.toUnmodifiableSet());
300         parentEntity.getChildFragments().addAll(childFragmentEntities);
301         fragmentRepository.save(parentEntity);
302     }
303
304     private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
305         final String listNodeXpathPrefix = listNodeXpath + "[";
306         if (parentFragmentEntity.getChildFragments()
307             .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
308             fragmentRepository.save(parentFragmentEntity);
309         }
310     }
311
312     private static boolean isRootXpath(final String xpath) {
313         return "/".equals(xpath) || "".equals(xpath);
314     }
315 }