2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 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.YangResourceModuleReference;
34 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
35 import org.onap.cps.spi.exceptions.DataspaceInUseException;
36 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException;
37 import org.onap.cps.spi.model.Anchor;
38 import org.onap.cps.spi.repository.AnchorRepository;
39 import org.onap.cps.spi.repository.DataspaceRepository;
40 import org.onap.cps.spi.repository.FragmentRepository;
41 import org.onap.cps.spi.repository.SchemaSetRepository;
42 import org.onap.cps.spi.repository.YangResourceRepository;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.dao.DataIntegrityViolationException;
45 import org.springframework.stereotype.Component;
48 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
51 private DataspaceRepository dataspaceRepository;
54 private AnchorRepository anchorRepository;
57 private SchemaSetRepository schemaSetRepository;
60 private FragmentRepository fragmentRepository;
63 private YangResourceRepository yangResourceRepository;
66 public void createDataspace(final String dataspaceName) {
68 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
69 } catch (final DataIntegrityViolationException e) {
70 throw AlreadyDefinedException.forDataspace(dataspaceName, e);
75 public void deleteDataspace(final String dataspaceName) {
76 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
77 final int numberOfAssociatedAnchors = anchorRepository.countByDataspace(dataspaceEntity);
78 if (numberOfAssociatedAnchors != 0) {
79 throw new DataspaceInUseException(dataspaceName,
80 String.format("Dataspace contains %d anchor(s)", numberOfAssociatedAnchors));
82 final int numberOfAssociatedSchemaSets = schemaSetRepository.countByDataspace(dataspaceEntity);
83 if (numberOfAssociatedSchemaSets != 0) {
84 throw new DataspaceInUseException(dataspaceName,
85 String.format("Dataspace contains %d schemaset(s)", numberOfAssociatedSchemaSets));
87 dataspaceRepository.delete(dataspaceEntity);
91 public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
92 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
93 final var schemaSetEntity =
94 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
95 final var anchorEntity = AnchorEntity.builder()
97 .dataspace(dataspaceEntity)
98 .schemaSet(schemaSetEntity)
101 anchorRepository.save(anchorEntity);
102 } catch (final DataIntegrityViolationException e) {
103 throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
108 public Collection<Anchor> getAnchors(final String dataspaceName) {
109 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
110 final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
111 return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
115 public Collection<Anchor> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
116 validateDataspaceAndModuleNames(dataspaceName, inputModuleNames);
117 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
118 final Collection<AnchorEntity> anchorEntities = anchorRepository
119 .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
120 return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
124 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
125 return toAnchor(getAnchorEntity(dataspaceName, anchorName));
130 public void deleteAnchor(final String dataspaceName, final String anchorName) {
131 final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
132 fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
133 anchorRepository.delete(anchorEntity);
136 private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
137 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
138 return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
141 private static Anchor toAnchor(final AnchorEntity anchorEntity) {
142 return Anchor.builder()
143 .name(anchorEntity.getName())
144 .dataspaceName(anchorEntity.getDataspace().getName())
145 .schemaSetName(anchorEntity.getSchemaSet().getName())
149 private void validateDataspaceAndModuleNames(final String dataspaceName,
150 final Collection<String> inputModuleNames) {
151 final Collection<String> retrievedModuleNames =
152 yangResourceRepository.findAllModuleReferences(dataspaceName, inputModuleNames)
153 .stream().map(YangResourceModuleReference::getModuleName)
154 .collect(Collectors.toList());
155 if (retrievedModuleNames.isEmpty()) {
156 dataspaceRepository.getByName(dataspaceName);
158 if (inputModuleNames.size() > retrievedModuleNames.size()) {
159 final List<String> moduleNamesNotFound = inputModuleNames.stream()
160 .filter(moduleName -> !retrievedModuleNames.contains(moduleName))
161 .collect(Collectors.toList());
162 if (!moduleNamesNotFound.isEmpty()) {
163 throw new ModuleNamesNotFoundException(dataspaceName, moduleNamesNotFound);