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