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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.spi.impl;
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;
53 @RequiredArgsConstructor
54 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
56 private final DataspaceRepository dataspaceRepository;
57 private final AnchorRepository anchorRepository;
58 private final SchemaSetRepository schemaSetRepository;
59 private final YangResourceRepository yangResourceRepository;
62 public void createDataspace(final String dataspaceName) {
64 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
65 } catch (final DataIntegrityViolationException e) {
66 throw AlreadyDefinedException.forDataspace(dataspaceName, e);
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));
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));
83 dataspaceRepository.delete(dataspaceEntity);
87 public Dataspace getDataspace(final String dataspaceName) {
88 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
89 return toDataspace(dataspaceEntity);
93 public Collection<Dataspace> getAllDataspaces() {
94 final Collection<DataspaceEntity> dataspaceEntities = dataspaceRepository.findAll();
95 return dataspaceEntities.stream().map(CpsAdminPersistenceServiceImpl::toDataspace)
96 .collect(Collectors.toSet());
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()
106 .dataspace(dataspaceEntity)
107 .schemaSet(schemaSetEntity)
110 anchorRepository.save(anchorEntity);
111 } catch (final DataIntegrityViolationException e) {
112 throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
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());
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());
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());
141 public Collection<Anchor> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
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();
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());
156 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
157 return toAnchor(getAnchorEntity(dataspaceName, anchorName));
162 public void deleteAnchor(final String dataspaceName, final String anchorName) {
163 final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
164 anchorRepository.delete(anchorEntity);
169 public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) {
170 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
171 anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames);
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());
186 private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
187 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
188 return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
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())
199 private static Dataspace toDataspace(final DataspaceEntity dataspaceEntity) {
200 return Dataspace.builder().name(dataspaceEntity.getName()).build();
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);
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);
220 private void verifyDataspaceName(final String dataspaceName) {
221 dataspaceRepository.getByName(dataspaceName);