2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl;
24 import java.util.Collection;
25 import java.util.stream.Collectors;
26 import org.onap.cps.spi.CpsAdminPersistenceService;
27 import org.onap.cps.spi.entities.AnchorEntity;
28 import org.onap.cps.spi.entities.DataspaceEntity;
29 import org.onap.cps.spi.entities.SchemaSetEntity;
30 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
31 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException;
32 import org.onap.cps.spi.model.Anchor;
33 import org.onap.cps.spi.repository.AnchorRepository;
34 import org.onap.cps.spi.repository.DataspaceRepository;
35 import org.onap.cps.spi.repository.SchemaSetRepository;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.dao.DataIntegrityViolationException;
38 import org.springframework.stereotype.Component;
41 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
44 private DataspaceRepository dataspaceRepository;
47 private AnchorRepository anchorRepository;
50 private SchemaSetRepository schemaSetRepository;
53 public void createDataspace(final String dataspaceName) {
55 dataspaceRepository.save(new DataspaceEntity(dataspaceName));
56 } catch (final DataIntegrityViolationException e) {
57 throw new DataspaceAlreadyDefinedException(dataspaceName, e);
62 public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
63 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
64 final SchemaSetEntity schemaSetEntity =
65 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
66 final AnchorEntity anchorEntity = AnchorEntity.builder()
68 .dataspace(dataspaceEntity)
69 .schemaSet(schemaSetEntity)
72 anchorRepository.save(anchorEntity);
73 } catch (final DataIntegrityViolationException e) {
74 throw new AnchorAlreadyDefinedException(dataspaceName, anchorName, e);
79 public Collection<Anchor> getAnchors(final String dataspaceName) {
80 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
81 final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
82 return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
86 public Anchor getAnchor(final String dataspaceName, final String anchorName) {
87 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
88 final AnchorEntity anchorEntity =
89 anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
90 return toAnchor(anchorEntity);
93 private static Anchor toAnchor(final AnchorEntity anchorEntity) {
94 return Anchor.builder()
95 .name(anchorEntity.getName())
96 .dataspaceName(anchorEntity.getDataspace().getName())
97 .schemaSetName(anchorEntity.getSchemaSet().getName())