Merge "Upgrade Open daylight yang tool to version 8.0.6"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2022 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 java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.transaction.Transactional;
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> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
135         try {
136             validateDataspaceAndModuleNames(dataspaceName, inputModuleNames);
137         } catch (DataspaceNotFoundException | ModuleNamesNotFoundException  e) {
138             log.info("Module search encountered unknown dataspace or modulename, treating this as nothing found");
139             return Collections.emptySet();
140         }
141
142         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
143         final Collection<AnchorEntity> anchorEntities = anchorRepository
144             .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
145         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
146     }
147
148     @Override
149     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
150         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
151     }
152
153     @Transactional
154     @Override
155     public void deleteAnchor(final String dataspaceName, final String anchorName) {
156         final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
157         anchorRepository.delete(anchorEntity);
158     }
159
160     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
161         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
162         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
163     }
164
165     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
166         return Anchor.builder()
167             .name(anchorEntity.getName())
168             .dataspaceName(anchorEntity.getDataspace().getName())
169             .schemaSetName(anchorEntity.getSchemaSet().getName())
170             .build();
171     }
172
173     private static Dataspace toDataspace(final DataspaceEntity dataspaceEntity) {
174         return Dataspace.builder().name(dataspaceEntity.getName()).build();
175     }
176
177     private void validateDataspaceAndModuleNames(final String dataspaceName,
178         final Collection<String> inputModuleNames) {
179         final Collection<String> retrievedModuleReferences =
180             yangResourceRepository.findAllModuleReferencesByDataspaceAndModuleNames(dataspaceName, inputModuleNames)
181                 .stream().map(YangResourceModuleReference::getModuleName)
182                 .collect(Collectors.toList());
183         if (retrievedModuleReferences.isEmpty()) {
184             verifyDataspaceName(dataspaceName);
185         }
186         if (inputModuleNames.size() > retrievedModuleReferences.size()) {
187             final List<String> moduleNamesNotFound = inputModuleNames.stream()
188                 .filter(moduleName -> !retrievedModuleReferences.contains(moduleName))
189                 .collect(Collectors.toList());
190             if (!moduleNamesNotFound.isEmpty()) {
191                 throw new ModuleNamesNotFoundException(dataspaceName, moduleNamesNotFound);
192             }
193         }
194     }
195
196     private void verifyDataspaceName(final String dataspaceName) {
197         dataspaceRepository.getByName(dataspaceName);
198     }
199 }