64c6ff507e52911c78bb112a1a5496c7407148cc
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
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 java.util.Collection;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import javax.transaction.Transactional;
29 import org.onap.cps.spi.CpsAdminPersistenceService;
30 import org.onap.cps.spi.entities.AnchorEntity;
31 import org.onap.cps.spi.entities.DataspaceEntity;
32 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.repository.AnchorRepository;
35 import org.onap.cps.spi.repository.DataspaceRepository;
36 import org.onap.cps.spi.repository.FragmentRepository;
37 import org.onap.cps.spi.repository.SchemaSetRepository;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.dao.DataIntegrityViolationException;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
44
45     @Autowired
46     private DataspaceRepository dataspaceRepository;
47
48     @Autowired
49     private AnchorRepository anchorRepository;
50
51     @Autowired
52     private SchemaSetRepository schemaSetRepository;
53
54     @Autowired
55     private FragmentRepository fragmentRepository;
56
57     @Override
58     public void createDataspace(final String dataspaceName) {
59         try {
60             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
61         } catch (final DataIntegrityViolationException e) {
62             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
63         }
64     }
65
66     @Override
67     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
68         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
69         final var schemaSetEntity =
70             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
71         final var anchorEntity = AnchorEntity.builder()
72             .name(anchorName)
73             .dataspace(dataspaceEntity)
74             .schemaSet(schemaSetEntity)
75             .build();
76         try {
77             anchorRepository.save(anchorEntity);
78         } catch (final DataIntegrityViolationException e) {
79             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
80         }
81     }
82
83     @Override
84     public Collection<Anchor> getAnchors(final String dataspaceName) {
85         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
86         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
87         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
88     }
89
90     @Override
91     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
92         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
93     }
94
95     @Transactional
96     @Override
97     public void deleteAnchor(final String dataspaceName, final String anchorName) {
98         final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
99         fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
100         anchorRepository.delete(anchorEntity);
101     }
102
103     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
104         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
105         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
106     }
107
108     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
109         return Anchor.builder()
110             .name(anchorEntity.getName())
111             .dataspaceName(anchorEntity.getDataspace().getName())
112             .schemaSetName(anchorEntity.getSchemaSet().getName())
113             .build();
114     }
115 }