2fb08d2c63b94a366acdfbff94df5ef66c78efbb
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2023 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.Collections;
29 import java.util.List;
30 import java.util.stream.Collectors;
31 import lombok.RequiredArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.spi.CpsAdminPersistenceService;
34 import org.onap.cps.spi.entities.AnchorEntity;
35 import org.onap.cps.spi.entities.DataspaceEntity;
36 import org.onap.cps.spi.entities.SchemaSetEntity;
37 import org.onap.cps.spi.entities.YangResourceModuleReference;
38 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
39 import org.onap.cps.spi.exceptions.DataspaceInUseException;
40 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
41 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException;
42 import org.onap.cps.spi.model.Anchor;
43 import org.onap.cps.spi.model.Dataspace;
44 import org.onap.cps.spi.repository.AnchorRepository;
45 import org.onap.cps.spi.repository.DataspaceRepository;
46 import org.onap.cps.spi.repository.SchemaSetRepository;
47 import org.onap.cps.spi.repository.YangResourceRepository;
48 import org.springframework.dao.DataIntegrityViolationException;
49 import org.springframework.stereotype.Component;
50
51 @Slf4j
52 @Component
53 @RequiredArgsConstructor
54 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
55
56     private final DataspaceRepository dataspaceRepository;
57     private final AnchorRepository anchorRepository;
58     private final SchemaSetRepository schemaSetRepository;
59     private final YangResourceRepository yangResourceRepository;
60
61     @Override
62     public void createDataspace(final String dataspaceName) {
63         try {
64             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
65         } catch (final DataIntegrityViolationException e) {
66             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
67         }
68     }
69
70     @Override
71     public void deleteDataspace(final String dataspaceName) {
72         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
73         final int numberOfAssociatedAnchors = anchorRepository.countByDataspace(dataspaceEntity);
74         if (numberOfAssociatedAnchors != 0) {
75             throw new DataspaceInUseException(dataspaceName,
76                 String.format("Dataspace contains %d anchor(s)", numberOfAssociatedAnchors));
77         }
78         final int numberOfAssociatedSchemaSets = schemaSetRepository.countByDataspace(dataspaceEntity);
79         if (numberOfAssociatedSchemaSets != 0) {
80             throw new DataspaceInUseException(dataspaceName,
81                 String.format("Dataspace contains %d schemaset(s)", numberOfAssociatedSchemaSets));
82         }
83         dataspaceRepository.delete(dataspaceEntity);
84     }
85
86     @Override
87     public Dataspace getDataspace(final String dataspaceName) {
88         final DataspaceEntity dataspaceEntity =  dataspaceRepository.getByName(dataspaceName);
89         return toDataspace(dataspaceEntity);
90     }
91
92     @Override
93     public Collection<Dataspace> getAllDataspaces() {
94         final Collection<DataspaceEntity> dataspaceEntities = dataspaceRepository.findAll();
95         return dataspaceEntities.stream().map(CpsAdminPersistenceServiceImpl::toDataspace)
96                 .collect(Collectors.toSet());
97     }
98
99     @Override
100     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
101         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
102         final var schemaSetEntity =
103             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
104         final var anchorEntity = AnchorEntity.builder()
105             .name(anchorName)
106             .dataspace(dataspaceEntity)
107             .schemaSet(schemaSetEntity)
108             .build();
109         try {
110             anchorRepository.save(anchorEntity);
111         } catch (final DataIntegrityViolationException e) {
112             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
113         }
114     }
115
116     @Override
117     public Collection<Anchor> getAnchors(final String dataspaceName) {
118         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
119         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
120         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
121     }
122
123     @Override
124     public Collection<Anchor> getAnchors(final String dataspaceName, final String schemaSetName) {
125         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
126         final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(
127             dataspaceEntity, schemaSetName);
128         return anchorRepository.findAllBySchemaSet(schemaSetEntity)
129             .stream().map(CpsAdminPersistenceServiceImpl::toAnchor)
130             .collect(Collectors.toSet());
131     }
132
133     @Override
134     public Collection<Anchor> getAnchors(final String dataspaceName, final Collection<String> schemaSetNames) {
135         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
136         return anchorRepository.findAllByDataspaceAndSchemaSetNameIn(dataspaceEntity, schemaSetNames)
137             .stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
138     }
139
140     @Override
141     public Collection<Anchor> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
142         try {
143             validateDataspaceAndModuleNames(dataspaceName, inputModuleNames);
144         } catch (DataspaceNotFoundException | ModuleNamesNotFoundException  e) {
145             log.info("Module search encountered unknown dataspace or modulename, treating this as nothing found");
146             return Collections.emptySet();
147         }
148
149         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
150         final Collection<AnchorEntity> anchorEntities = anchorRepository
151             .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
152         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
153     }
154
155     @Override
156     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
157         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
158     }
159
160     @Transactional
161     @Override
162     public void deleteAnchor(final String dataspaceName, final String anchorName) {
163         final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
164         anchorRepository.delete(anchorEntity);
165     }
166
167     @Transactional
168     @Override
169     public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) {
170         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
171         anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames);
172     }
173
174     @Transactional
175     @Override
176     public void updateAnchorSchemaSet(final String dataspaceName,
177                                          final String anchorName,
178                                          final String schemaSetName) {
179         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
180         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
181         final SchemaSetEntity schemaSetEntity = schemaSetRepository
182                 .getByDataspaceAndName(dataspaceEntity, schemaSetName);
183         anchorRepository.updateAnchorSchemaSetId(schemaSetEntity.getId(), anchorEntity.getId());
184     }
185
186     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
187         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
188         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
189     }
190
191     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
192         return Anchor.builder()
193             .name(anchorEntity.getName())
194             .dataspaceName(anchorEntity.getDataspace().getName())
195             .schemaSetName(anchorEntity.getSchemaSet().getName())
196             .build();
197     }
198
199     private static Dataspace toDataspace(final DataspaceEntity dataspaceEntity) {
200         return Dataspace.builder().name(dataspaceEntity.getName()).build();
201     }
202
203     private void validateDataspaceAndModuleNames(final String dataspaceName,
204         final Collection<String> inputModuleNames) {
205         final Collection<String> retrievedModuleReferences =
206             yangResourceRepository.findAllModuleReferencesByDataspaceAndModuleNames(dataspaceName, inputModuleNames)
207                 .stream().map(YangResourceModuleReference::getModuleName)
208                 .collect(Collectors.toList());
209         if (retrievedModuleReferences.isEmpty()) {
210             verifyDataspaceName(dataspaceName);
211         }
212         if (inputModuleNames.size() > retrievedModuleReferences.size()) {
213             final List<String> unknownModules = inputModuleNames.stream()
214                 .filter(moduleName -> !retrievedModuleReferences.contains(moduleName))
215                 .collect(Collectors.toList());
216             throw new ModuleNamesNotFoundException(dataspaceName, unknownModules);
217         }
218     }
219
220     private void verifyDataspaceName(final String dataspaceName) {
221         dataspaceRepository.getByName(dataspaceName);
222     }
223 }