Merge "Schedule response to client"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2024 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2022 Bell Canada.
5  * Modifications Copyright (C) 2021 Pantheon.tech
6  * Modifications Copyright (C) 2022 TechMahindra Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.spi.impl;
25
26 import jakarta.transaction.Transactional;
27 import java.util.Collection;
28 import java.util.stream.Collectors;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.spi.CpsAdminPersistenceService;
32 import org.onap.cps.spi.entities.AnchorEntity;
33 import org.onap.cps.spi.entities.DataspaceEntity;
34 import org.onap.cps.spi.entities.SchemaSetEntity;
35 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
36 import org.onap.cps.spi.exceptions.DataspaceInUseException;
37 import org.onap.cps.spi.model.Anchor;
38 import org.onap.cps.spi.model.Dataspace;
39 import org.onap.cps.spi.repository.AnchorRepository;
40 import org.onap.cps.spi.repository.DataspaceRepository;
41 import org.onap.cps.spi.repository.SchemaSetRepository;
42 import org.springframework.dao.DataIntegrityViolationException;
43 import org.springframework.stereotype.Component;
44
45 @Slf4j
46 @Component
47 @RequiredArgsConstructor
48 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
49
50     private final DataspaceRepository dataspaceRepository;
51     private final AnchorRepository anchorRepository;
52     private final SchemaSetRepository schemaSetRepository;
53
54     @Override
55     public void createDataspace(final String dataspaceName) {
56         try {
57             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
58         } catch (final DataIntegrityViolationException e) {
59             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
60         }
61     }
62
63     @Override
64     public void deleteDataspace(final String dataspaceName) {
65         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
66         final int numberOfAssociatedAnchors = anchorRepository.countByDataspace(dataspaceEntity);
67         if (numberOfAssociatedAnchors != 0) {
68             throw new DataspaceInUseException(dataspaceName,
69                 String.format("Dataspace contains %d anchor(s)", numberOfAssociatedAnchors));
70         }
71         final int numberOfAssociatedSchemaSets = schemaSetRepository.countByDataspace(dataspaceEntity);
72         if (numberOfAssociatedSchemaSets != 0) {
73             throw new DataspaceInUseException(dataspaceName,
74                 String.format("Dataspace contains %d schemaset(s)", numberOfAssociatedSchemaSets));
75         }
76         dataspaceRepository.delete(dataspaceEntity);
77     }
78
79     @Override
80     public Dataspace getDataspace(final String dataspaceName) {
81         final DataspaceEntity dataspaceEntity =  dataspaceRepository.getByName(dataspaceName);
82         return toDataspace(dataspaceEntity);
83     }
84
85     @Override
86     public Collection<Dataspace> getAllDataspaces() {
87         final Collection<DataspaceEntity> dataspaceEntities = dataspaceRepository.findAll();
88         return dataspaceEntities.stream().map(CpsAdminPersistenceServiceImpl::toDataspace)
89                 .collect(Collectors.toSet());
90     }
91
92     @Override
93     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
94         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
95         final var schemaSetEntity =
96             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
97         final var anchorEntity = AnchorEntity.builder()
98             .name(anchorName)
99             .dataspace(dataspaceEntity)
100             .schemaSet(schemaSetEntity)
101             .build();
102         try {
103             anchorRepository.save(anchorEntity);
104         } catch (final DataIntegrityViolationException e) {
105             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
106         }
107     }
108
109     @Override
110     public Collection<Anchor> getAnchors(final String dataspaceName) {
111         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
112         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
113         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
114     }
115
116     @Override
117     public Collection<Anchor> getAnchors(final String dataspaceName, final String schemaSetName) {
118         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
119         final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(
120             dataspaceEntity, schemaSetName);
121         return anchorRepository.findAllBySchemaSet(schemaSetEntity)
122             .stream().map(CpsAdminPersistenceServiceImpl::toAnchor)
123             .collect(Collectors.toSet());
124     }
125
126     @Override
127     public Collection<Anchor> getAnchors(final String dataspaceName, final Collection<String> schemaSetNames) {
128         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
129         return anchorRepository.findAllByDataspaceAndSchemaSetNameIn(dataspaceEntity, schemaSetNames)
130             .stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
131     }
132
133     @Override
134     public Collection<String> queryAnchorNames(final String dataspaceName, final Collection<String> inputModuleNames) {
135         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
136         return anchorRepository.getAnchorNamesByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames,
137                 inputModuleNames.size());
138     }
139
140     @Override
141     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
142         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
143     }
144
145     @Transactional
146     @Override
147     public void deleteAnchor(final String dataspaceName, final String anchorName) {
148         final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
149         anchorRepository.delete(anchorEntity);
150     }
151
152     @Transactional
153     @Override
154     public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) {
155         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
156         anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames);
157     }
158
159     @Transactional
160     @Override
161     public void updateAnchorSchemaSet(final String dataspaceName,
162                                          final String anchorName,
163                                          final String schemaSetName) {
164         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
165         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
166         final SchemaSetEntity schemaSetEntity = schemaSetRepository
167                 .getByDataspaceAndName(dataspaceEntity, schemaSetName);
168         anchorRepository.updateAnchorSchemaSetId(schemaSetEntity.getId(), anchorEntity.getId());
169     }
170
171     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
172         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
173         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
174     }
175
176     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
177         return Anchor.builder()
178             .name(anchorEntity.getName())
179             .dataspaceName(anchorEntity.getDataspace().getName())
180             .schemaSetName(anchorEntity.getSchemaSet().getName())
181             .build();
182     }
183
184     private static Dataspace toDataspace(final DataspaceEntity dataspaceEntity) {
185         return Dataspace.builder().name(dataspaceEntity.getName()).build();
186     }
187 }