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;
27 import java.util.stream.Collectors;
28 import javax.transaction.Transactional;
29 import org.onap.cps.spi.CpsAdminPersistenceService;
30 import org.onap.cps.spi.entities.AnchorEntity;
31 import org.onap.cps.spi.entities.DataspaceEntity;
32 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.repository.AnchorRepository;
35 import org.onap.cps.spi.repository.DataspaceRepository;
36 import org.onap.cps.spi.repository.FragmentRepository;
37 import org.onap.cps.spi.repository.SchemaSetRepository;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.dao.DataIntegrityViolationException;
40 import org.springframework.stereotype.Component;
43 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
46 private DataspaceRepository dataspaceRepository;
49 private AnchorRepository anchorRepository;
52 private SchemaSetRepository schemaSetRepository;
55 private FragmentRepository fragmentRepository;
58 public void createDataspace(final String dataspaceName) {
60 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
61 } catch (final DataIntegrityViolationException e) {
62 throw AlreadyDefinedException.forDataspace(dataspaceName, e);
67 public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
68 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
69 final var schemaSetEntity =
70 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
71 final var anchorEntity = AnchorEntity.builder()
73 .dataspace(dataspaceEntity)
74 .schemaSet(schemaSetEntity)
77 anchorRepository.save(anchorEntity);
78 } catch (final DataIntegrityViolationException e) {
79 throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
84 public Collection<Anchor> getAnchors(final String dataspaceName) {
85 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
86 final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
87 return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
91 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
92 return toAnchor(getAnchorEntity(dataspaceName, anchorName));
97 public void deleteAnchor(final String dataspaceName, final String anchorName) {
98 final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
99 fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
100 anchorRepository.delete(anchorEntity);
103 private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
104 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
105 return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
108 private static Anchor toAnchor(final AnchorEntity anchorEntity) {
109 return Anchor.builder()
110 .name(anchorEntity.getName())
111 .dataspaceName(anchorEntity.getDataspace().getName())
112 .schemaSetName(anchorEntity.getSchemaSet().getName())