Create list-node elements (part1): CPS service and persistence layers
[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  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl;
23
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
25
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.ImmutableSet.Builder;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import org.onap.cps.spi.CpsDataPersistenceService;
39 import org.onap.cps.spi.FetchDescendantsOption;
40 import org.onap.cps.spi.entities.AnchorEntity;
41 import org.onap.cps.spi.entities.DataspaceEntity;
42 import org.onap.cps.spi.entities.FragmentEntity;
43 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
44 import org.onap.cps.spi.model.DataNode;
45 import org.onap.cps.spi.model.DataNodeBuilder;
46 import org.onap.cps.spi.query.CpsPathQuery;
47 import org.onap.cps.spi.query.CpsPathQueryType;
48 import org.onap.cps.spi.repository.AnchorRepository;
49 import org.onap.cps.spi.repository.DataspaceRepository;
50 import org.onap.cps.spi.repository.FragmentRepository;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.dao.DataIntegrityViolationException;
53 import org.springframework.stereotype.Service;
54
55 @Service
56 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
57
58     @Autowired
59     private DataspaceRepository dataspaceRepository;
60
61     @Autowired
62     private AnchorRepository anchorRepository;
63
64     @Autowired
65     private FragmentRepository fragmentRepository;
66
67     private static final Gson GSON = new GsonBuilder().create();
68     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})";
69
70     @Override
71     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
72         final DataNode dataNode) {
73         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
74         final var fragmentEntity =
75             toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
76         parentFragment.getChildFragments().add(fragmentEntity);
77         try {
78             fragmentRepository.save(parentFragment);
79         } catch (final DataIntegrityViolationException exception) {
80             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
81         }
82     }
83
84     @Override
85     public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
86         final Collection<DataNode> dataNodes) {
87         final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
88         final List<FragmentEntity> newFragmentEntities =
89             dataNodes.stream().map(
90                 dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode)
91             ).collect(Collectors.toUnmodifiableList());
92         parentFragment.getChildFragments().addAll(newFragmentEntities);
93         try {
94             fragmentRepository.save(parentFragment);
95         } catch (final DataIntegrityViolationException exception) {
96             final List<String> conflictXpaths = dataNodes.stream()
97                 .map(DataNode::getXpath)
98                 .collect(Collectors.toList());
99             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
100         }
101     }
102
103     @Override
104     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
105         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
106         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
107         final var fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
108             dataNode);
109         try {
110             fragmentRepository.save(fragmentEntity);
111         } catch (final DataIntegrityViolationException exception) {
112             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
113         }
114     }
115
116     /**
117      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
118      * for all DataNode children recursively.
119      *
120      * @param dataspaceEntity       dataspace
121      * @param anchorEntity          anchorEntity
122      * @param dataNodeToBeConverted dataNode
123      * @return a Fragment built from current DataNode
124      */
125     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
126         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
127         final var parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
128         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
129         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
130             final FragmentEntity childFragment =
131                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
132                     childDataNode);
133             childFragmentsImmutableSetBuilder.add(childFragment);
134         }
135         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
136         return parentFragment;
137     }
138
139     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
140         final AnchorEntity anchorEntity, final DataNode dataNode) {
141         return FragmentEntity.builder()
142             .dataspace(dataspaceEntity)
143             .anchor(anchorEntity)
144             .xpath(dataNode.getXpath())
145             .attributes(GSON.toJson(dataNode.getLeaves()))
146             .build();
147     }
148
149     @Override
150     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
151         final FetchDescendantsOption fetchDescendantsOption) {
152         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
153         return toDataNode(fragmentEntity, fetchDescendantsOption);
154     }
155
156     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
157         final String xpath) {
158         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
159         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
160         if (isRootXpath(xpath)) {
161             return fragmentRepository.getFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
162         } else {
163             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
164                 xpath);
165         }
166     }
167
168     @Override
169     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
170         final FetchDescendantsOption fetchDescendantsOption) {
171         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
172         final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
173         final var cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
174         List<FragmentEntity> fragmentEntities;
175         if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
176             fragmentEntities = fragmentRepository
177                 .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
178                     cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
179         } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
180             final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
181             fragmentEntities = fragmentRepository
182                 .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
183                     leafDataAsJson);
184         } else {
185             fragmentEntities = fragmentRepository
186                 .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
187         }
188         if (cpsPathQuery.hasAncestorAxis()) {
189             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
190             fragmentEntities = ancestorXpaths.isEmpty()
191                 ? Collections.emptyList() : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
192         }
193         return fragmentEntities.stream()
194             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
195             .collect(Collectors.toUnmodifiableList());
196     }
197
198     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
199         final CpsPathQuery cpsPathQuery) {
200         final Set<String> ancestorXpath = new HashSet<>();
201         final var pattern =
202             Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
203                 + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
204         for (final FragmentEntity fragmentEntity : fragmentEntities) {
205             final var matcher = pattern.matcher(fragmentEntity.getXpath());
206             if (matcher.matches()) {
207                 ancestorXpath.add(matcher.group(1));
208             }
209         }
210         return ancestorXpath;
211     }
212
213     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
214         final FetchDescendantsOption fetchDescendantsOption) {
215         final Map<String, Object> leaves = GSON.fromJson(fragmentEntity.getAttributes(), Map.class);
216         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
217         return new DataNodeBuilder()
218             .withXpath(fragmentEntity.getXpath())
219             .withLeaves(leaves)
220             .withChildDataNodes(childDataNodes).build();
221     }
222
223     private static List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
224         final FetchDescendantsOption fetchDescendantsOption) {
225         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
226             return fragmentEntity.getChildFragments().stream()
227                 .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
228                 .collect(Collectors.toUnmodifiableList());
229         }
230         return Collections.emptyList();
231     }
232
233     @Override
234     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
235         final Map<String, Object> leaves) {
236         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
237         fragmentEntity.setAttributes(GSON.toJson(leaves));
238         fragmentRepository.save(fragmentEntity);
239     }
240
241     @Override
242     public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
243         final var fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
244         removeExistingDescendants(fragmentEntity);
245
246         fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
247         final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
248             childDataNode -> convertToFragmentWithAllDescendants(
249                 fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
250         ).collect(Collectors.toUnmodifiableSet());
251         fragmentEntity.setChildFragments(childFragmentEntities);
252
253         fragmentRepository.save(fragmentEntity);
254     }
255
256     private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
257         fragmentEntity.setChildFragments(Collections.emptySet());
258         fragmentRepository.save(fragmentEntity);
259     }
260
261     private boolean isRootXpath(final String xpath) {
262         return "/".equals(xpath) || "".equals(xpath);
263     }
264 }