Merge "Fix upload size to be greater than 1MB"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.spi.impl;
24
25 import com.google.common.collect.ImmutableSet;
26 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.entities.SchemaSetEntity;
33 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
34 import org.onap.cps.spi.model.Anchor;
35 import org.onap.cps.spi.repository.AnchorRepository;
36 import org.onap.cps.spi.repository.DataspaceRepository;
37 import org.onap.cps.spi.repository.FragmentRepository;
38 import org.onap.cps.spi.repository.SchemaSetRepository;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.dao.DataIntegrityViolationException;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
45
46     @Autowired
47     private DataspaceRepository dataspaceRepository;
48
49     @Autowired
50     private AnchorRepository anchorRepository;
51
52     @Autowired
53     private SchemaSetRepository schemaSetRepository;
54
55     @Autowired
56     private FragmentRepository fragmentRepository;
57
58     @Override
59     public void createDataspace(final String dataspaceName) {
60         try {
61             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
62         } catch (final DataIntegrityViolationException e) {
63             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
64         }
65     }
66
67     @Override
68     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
69         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
70         final SchemaSetEntity schemaSetEntity =
71             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
72         final AnchorEntity anchorEntity = AnchorEntity.builder()
73             .name(anchorName)
74             .dataspace(dataspaceEntity)
75             .schemaSet(schemaSetEntity)
76             .build();
77         try {
78             anchorRepository.save(anchorEntity);
79         } catch (final DataIntegrityViolationException e) {
80             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
81         }
82     }
83
84     @Override
85     public Collection<Anchor> getAnchors(final String dataspaceName) {
86         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
87         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
88         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
89     }
90
91     @Override
92     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
93         return toAnchor(getAnchorEntity(dataspaceName, anchorName));
94     }
95
96     @Transactional
97     @Override
98     public void deleteAnchor(final String dataspaceName, final String anchorName) {
99         final AnchorEntity anchorEntity = getAnchorEntity(dataspaceName, anchorName);
100         fragmentRepository.deleteByAnchorIn(ImmutableSet.of(anchorEntity));
101         anchorRepository.delete(anchorEntity);
102     }
103
104     private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
105         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
106         return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
107     }
108
109     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
110         return Anchor.builder()
111             .name(anchorEntity.getName())
112             .dataspaceName(anchorEntity.getDataspace().getName())
113             .schemaSetName(anchorEntity.getSchemaSet().getName())
114             .build();
115     }
116 }