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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.spi.impl;
25 import java.util.Collection;
26 import java.util.List;
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;
49 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
52 private DataspaceRepository dataspaceRepository;
55 private AnchorRepository anchorRepository;
58 private SchemaSetRepository schemaSetRepository;
61 private FragmentRepository fragmentRepository;
64 private YangResourceRepository yangResourceRepository;
67 public void createDataspace(final String dataspaceName) {
69 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
70 } catch (final DataIntegrityViolationException e) {
71 throw AlreadyDefinedException.forDataspace(dataspaceName, e);
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));
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));
88 dataspaceRepository.delete(dataspaceEntity);
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()
98 .dataspace(dataspaceEntity)
99 .schemaSet(schemaSetEntity)
102 anchorRepository.save(anchorEntity);
103 } catch (final DataIntegrityViolationException e) {
104 throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
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());
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());
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());
135 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
136 return toAnchor(getAnchorEntity(dataspaceName, anchorName));
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);
147 private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
148 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
149 return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
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())
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);
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);