Refactored Delete SchemaSet functionality
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2022 Bell Canada.
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.List;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import javax.transaction.Transactional;
30 import org.onap.cps.spi.CpsAdminPersistenceService;
31 import org.onap.cps.spi.entities.AnchorEntity;
32 import org.onap.cps.spi.entities.DataspaceEntity;
33 import org.onap.cps.spi.entities.SchemaSetEntity;
34 import org.onap.cps.spi.entities.YangResourceModuleReference;
35 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
36 import org.onap.cps.spi.exceptions.DataspaceInUseException;
37 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException;
38 import org.onap.cps.spi.model.Anchor;
39 import org.onap.cps.spi.repository.AnchorRepository;
40 import org.onap.cps.spi.repository.DataspaceRepository;
41 import org.onap.cps.spi.repository.FragmentRepository;
42 import org.onap.cps.spi.repository.SchemaSetRepository;
43 import org.onap.cps.spi.repository.YangResourceRepository;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.dao.DataIntegrityViolationException;
46 import org.springframework.stereotype.Component;
47
48 @Component
49 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
50
51     @Autowired
52     private DataspaceRepository dataspaceRepository;
53
54     @Autowired
55     private AnchorRepository anchorRepository;
56
57     @Autowired
58     private SchemaSetRepository schemaSetRepository;
59
60     @Autowired
61     private FragmentRepository fragmentRepository;
62
63     @Autowired
64     private YangResourceRepository yangResourceRepository;
65
66     @Override
67     public void createDataspace(final String dataspaceName) {
68         try {
69             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
70         } catch (final DataIntegrityViolationException e) {
71             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
72         }
73     }
74
75     @Override
76     public void deleteDataspace(final String dataspaceName) {
77         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
78         final int numberOfAssociatedAnchors = anchorRepository.countByDataspace(dataspaceEntity);
79         if (numberOfAssociatedAnchors != 0) {
80             throw new DataspaceInUseException(dataspaceName,
81                 String.format("Dataspace contains %d anchor(s)", numberOfAssociatedAnchors));
82         }
83         final int numberOfAssociatedSchemaSets = schemaSetRepository.countByDataspace(dataspaceEntity);
84         if (numberOfAssociatedSchemaSets != 0) {
85             throw new DataspaceInUseException(dataspaceName,
86                 String.format("Dataspace contains %d schemaset(s)", numberOfAssociatedSchemaSets));
87         }
88         dataspaceRepository.delete(dataspaceEntity);
89     }
90
91     @Override
92     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
93         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
94         final var schemaSetEntity =
95             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
96         final var anchorEntity = AnchorEntity.builder()
97             .name(anchorName)
98             .dataspace(dataspaceEntity)
99             .schemaSet(schemaSetEntity)
100             .build();
101         try {
102             anchorRepository.save(anchorEntity);
103         } catch (final DataIntegrityViolationException e) {
104             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
105         }
106     }
107
108     @Override
109     public Collection<Anchor> getAnchors(final String dataspaceName) {
110         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
111         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
112         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
113     }
114
115     @Override
116     public Collection<Anchor> getAnchors(final String dataspaceName, final String schemaSetName) {
117         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
118         final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(
119             dataspaceEntity, schemaSetName);
120         return anchorRepository.findAllBySchemaSet(schemaSetEntity)
121             .stream().map(CpsAdminPersistenceServiceImpl::toAnchor)
122             .collect(Collectors.toSet());
123     }
124
125     @Override
126     public Collection<Anchor> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
127         validateDataspaceAndModuleNames(dataspaceName, inputModuleNames);
128         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
129         final Collection<AnchorEntity> anchorEntities = anchorRepository
130             .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
131         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
132     }
133
134     @Override
135     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
136         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
137     }
138
139     @Transactional
140     @Override
141     public void deleteAnchor(final String dataspaceName, final String anchorName) {
142         final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
143         fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
144         anchorRepository.delete(anchorEntity);
145     }
146
147     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
148         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
149         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
150     }
151
152     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
153         return Anchor.builder()
154             .name(anchorEntity.getName())
155             .dataspaceName(anchorEntity.getDataspace().getName())
156             .schemaSetName(anchorEntity.getSchemaSet().getName())
157             .build();
158     }
159
160     private void validateDataspaceAndModuleNames(final String dataspaceName,
161         final Collection<String> inputModuleNames) {
162         final Collection<String> retrievedModuleNames =
163             yangResourceRepository.findAllModuleReferences(dataspaceName, inputModuleNames)
164                 .stream().map(YangResourceModuleReference::getModuleName)
165                 .collect(Collectors.toList());
166         if (retrievedModuleNames.isEmpty()) {
167             dataspaceRepository.getByName(dataspaceName);
168         }
169         if (inputModuleNames.size() > retrievedModuleNames.size()) {
170             final List<String> moduleNamesNotFound = inputModuleNames.stream()
171                 .filter(moduleName -> !retrievedModuleNames.contains(moduleName))
172                 .collect(Collectors.toList());
173             if (!moduleNamesNotFound.isEmpty()) {
174                 throw new ModuleNamesNotFoundException(dataspaceName, moduleNamesNotFound);
175             }
176         }
177     }
178 }