Persisting data nodes (fragments tree structure)
[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  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.spi.impl;
21
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.ImmutableSet.Builder;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import org.onap.cps.spi.CpsDataPersistenceService;
27 import org.onap.cps.spi.entities.AnchorEntity;
28 import org.onap.cps.spi.entities.DataspaceEntity;
29 import org.onap.cps.spi.entities.FragmentEntity;
30 import org.onap.cps.spi.model.DataNode;
31 import org.onap.cps.spi.repository.AnchorRepository;
32 import org.onap.cps.spi.repository.DataspaceRepository;
33 import org.onap.cps.spi.repository.FragmentRepository;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
39
40     @Autowired
41     private DataspaceRepository dataspaceRepository;
42
43     @Autowired
44     private AnchorRepository anchorRepository;
45
46     @Autowired
47     private FragmentRepository fragmentRepository;
48
49     private static Gson GSON = new GsonBuilder().create();
50
51     @Override
52     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
53         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
54         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
55         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
56             dataNode);
57         fragmentRepository.save(fragmentEntity);
58     }
59
60     /**
61      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
62      * for all DataNode children recursively.
63      *
64      * @param dataspaceEntity       dataspace
65      * @param anchorEntity          anchorEntity
66      * @param dataNodeToBeConverted dataNode
67      * @return a Fragment built from current DataNode
68      */
69     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
70         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
71         final FragmentEntity parentFragment = FragmentEntity.builder()
72             .dataspace(dataspaceEntity)
73             .anchor(anchorEntity)
74             .xpath(dataNodeToBeConverted.getXpath())
75             .attributes(GSON.toJson(dataNodeToBeConverted.getLeaves()))
76             .build();
77
78         final Builder<FragmentEntity> fragmentEntityBuilder = ImmutableSet.builder();
79         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
80             final FragmentEntity childFragment =
81                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
82                     childDataNode);
83             fragmentEntityBuilder.add(childFragment);
84         }
85         parentFragment.setChildFragments(fragmentEntityBuilder.build());
86         return parentFragment;
87     }
88 }