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
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 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;
47 @RequiredArgsConstructor
48 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
50 private final DataspaceRepository dataspaceRepository;
51 private final AnchorRepository anchorRepository;
52 private final SchemaSetRepository schemaSetRepository;
55 public void createDataspace(final String dataspaceName) {
57 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
58 } catch (final DataIntegrityViolationException e) {
59 throw AlreadyDefinedException.forDataspace(dataspaceName, e);
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));
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));
76 dataspaceRepository.delete(dataspaceEntity);
80 public Dataspace getDataspace(final String dataspaceName) {
81 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
82 return toDataspace(dataspaceEntity);
86 public Collection<Dataspace> getAllDataspaces() {
87 final Collection<DataspaceEntity> dataspaceEntities = dataspaceRepository.findAll();
88 return dataspaceEntities.stream().map(CpsAdminPersistenceServiceImpl::toDataspace)
89 .collect(Collectors.toSet());
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()
99 .dataspace(dataspaceEntity)
100 .schemaSet(schemaSetEntity)
103 anchorRepository.save(anchorEntity);
104 } catch (final DataIntegrityViolationException e) {
105 throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
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());
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());
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());
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());
141 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
142 return toAnchor(getAnchorEntity(dataspaceName, anchorName));
147 public void deleteAnchor(final String dataspaceName, final String anchorName) {
148 final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
149 anchorRepository.delete(anchorEntity);
154 public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) {
155 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
156 anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames);
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());
171 private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
172 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
173 return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
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())
184 private static Dataspace toDataspace(final DataspaceEntity dataspaceEntity) {
185 return Dataspace.builder().name(dataspaceEntity.getName()).build();