Add another Child to a Fragment that has already at least one Child
[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 final Gson GSON = new GsonBuilder().create();
50
51     @Override
52     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
53         final DataNode dataNode) {
54         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
55         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
56         final FragmentEntity parentFragment =
57             fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath);
58         final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode);
59         parentFragment.getChildFragments().add(childFragment);
60         fragmentRepository.save(parentFragment);
61     }
62
63     @Override
64     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
65         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
66         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
67         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
68             dataNode);
69         fragmentRepository.save(fragmentEntity);
70     }
71
72     /**
73      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
74      * for all DataNode children recursively.
75      *
76      * @param dataspaceEntity       dataspace
77      * @param anchorEntity          anchorEntity
78      * @param dataNodeToBeConverted dataNode
79      * @return a Fragment built from current DataNode
80      */
81     private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
82         final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
83         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
84         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
85         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
86             final FragmentEntity childFragment =
87                 convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
88                     childDataNode);
89             childFragmentsImmutableSetBuilder.add(childFragment);
90         }
91         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
92         return parentFragment;
93     }
94
95     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
96         final AnchorEntity anchorEntity,
97         final DataNode dataNode) {
98         return FragmentEntity.builder()
99             .dataspace(dataspaceEntity)
100             .anchor(anchorEntity)
101             .xpath(dataNode.getXpath())
102             .attributes(GSON.toJson(dataNode.getLeaves()))
103             .build();
104     }
105 }