Fix sonar qube violations
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsModulePersistenceServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2021 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
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.nio.charset.StandardCharsets;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.Set;
32 import java.util.regex.Pattern;
33 import java.util.stream.Collectors;
34 import javax.transaction.Transactional;
35 import lombok.extern.slf4j.Slf4j;
36 import org.apache.commons.codec.digest.DigestUtils;
37 import org.apache.commons.lang3.StringUtils;
38 import org.hibernate.exception.ConstraintViolationException;
39 import org.onap.cps.spi.CascadeDeleteAllowed;
40 import org.onap.cps.spi.CpsAdminPersistenceService;
41 import org.onap.cps.spi.CpsModulePersistenceService;
42 import org.onap.cps.spi.entities.AnchorEntity;
43 import org.onap.cps.spi.entities.SchemaSetEntity;
44 import org.onap.cps.spi.entities.YangResourceEntity;
45 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
46 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
47 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
48 import org.onap.cps.spi.repository.AnchorRepository;
49 import org.onap.cps.spi.repository.DataspaceRepository;
50 import org.onap.cps.spi.repository.FragmentRepository;
51 import org.onap.cps.spi.repository.SchemaSetRepository;
52 import org.onap.cps.spi.repository.YangResourceRepository;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.dao.DataIntegrityViolationException;
55 import org.springframework.retry.annotation.Backoff;
56 import org.springframework.retry.annotation.Retryable;
57 import org.springframework.stereotype.Component;
58
59
60 @Component
61 @Slf4j
62 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
63
64     private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key";
65     private static final Pattern CHECKSUM_EXCEPTION_PATTERN = Pattern.compile(".*\\(checksum\\)=\\((\\w+)\\).*");
66
67     @Autowired
68     private YangResourceRepository yangResourceRepository;
69
70     @Autowired
71     private SchemaSetRepository schemaSetRepository;
72
73     @Autowired
74     private DataspaceRepository dataspaceRepository;
75
76     @Autowired
77     private AnchorRepository anchorRepository;
78
79     @Autowired
80     private FragmentRepository fragmentRepository;
81
82     @Autowired
83     private CpsAdminPersistenceService cpsAdminPersistenceService;
84
85     @Override
86     @Transactional
87     // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
88     // can occur in case of specific concurrent requests.
89     @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 2, backoff = @Backoff(delay = 500))
90     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
91         final Map<String, String> yangResourcesNameToContentMap) {
92
93         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
94         final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
95         final var schemaSetEntity = new SchemaSetEntity();
96         schemaSetEntity.setName(schemaSetName);
97         schemaSetEntity.setDataspace(dataspaceEntity);
98         schemaSetEntity.setYangResources(yangResourceEntities);
99         try {
100             schemaSetRepository.save(schemaSetEntity);
101         } catch (final DataIntegrityViolationException e) {
102             throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e);
103         }
104     }
105
106     private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
107         final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
108             .map(entry -> {
109                 final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
110                 final var yangResourceEntity = new YangResourceEntity();
111                 yangResourceEntity.setName(entry.getKey());
112                 yangResourceEntity.setContent(entry.getValue());
113                 yangResourceEntity.setChecksum(checksum);
114                 return yangResourceEntity;
115             })
116             .collect(Collectors.toMap(
117                 YangResourceEntity::getChecksum,
118                 entity -> entity
119             ));
120
121         final List<YangResourceEntity> existingYangResourceEntities =
122             yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
123         existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
124
125         final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
126         if (!newYangResourceEntities.isEmpty()) {
127             try {
128                 yangResourceRepository.saveAll(newYangResourceEntities);
129             } catch (final DataIntegrityViolationException dataIntegrityViolationException) {
130                 // Throw a CPS duplicated Yang resource exception if the cause of the error is a yang checksum
131                 // database constraint violation.
132                 // If it is not, then throw the original exception
133                 final Optional<DuplicatedYangResourceException> convertedException =
134                         convertToDuplicatedYangResourceException(
135                                 dataIntegrityViolationException, newYangResourceEntities);
136                 convertedException.ifPresent(
137                     e ->  log.warn(
138                                 "Cannot persist duplicated yang resource. "
139                                         + "A total of 2 attempts to store the schema set are planned.", e));
140                 throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException;
141             }
142         }
143
144         return ImmutableSet.<YangResourceEntity>builder()
145             .addAll(existingYangResourceEntities)
146             .addAll(newYangResourceEntities)
147             .build();
148     }
149
150     /**
151      * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
152      * if the cause of the error is a yang checksum database constraint violation.
153      * @param originalException the original db exception.
154      * @param yangResourceEntities the collection of Yang resources involved in the db failure.
155      * @return an optional converted CPS duplicated Yang resource exception. The optional is empty if the original
156      *      cause of the error is not a yang checksum database constraint violation.
157      */
158     private Optional<DuplicatedYangResourceException> convertToDuplicatedYangResourceException(
159             final DataIntegrityViolationException originalException,
160             final Collection<YangResourceEntity> yangResourceEntities) {
161
162         // The exception result
163         DuplicatedYangResourceException duplicatedYangResourceException = null;
164
165         final Throwable cause = originalException.getCause();
166         if (cause instanceof ConstraintViolationException) {
167             final ConstraintViolationException constraintException = (ConstraintViolationException) cause;
168             if (YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME.equals(constraintException.getConstraintName())) {
169                 // Db constraint related to yang resource checksum uniqueness is not respected
170                 final String checksumInError = getDuplicatedChecksumFromException(constraintException);
171                 final String nameInError = getNameForChecksum(checksumInError, yangResourceEntities);
172                 duplicatedYangResourceException =
173                         new DuplicatedYangResourceException(nameInError, checksumInError, constraintException);
174             }
175         }
176
177         return Optional.ofNullable(duplicatedYangResourceException);
178
179     }
180
181     /**
182      * Get the checksum that caused the constraint violation exception.
183      * @param exception the exception having the checksum in error.
184      * @return the checksum in error or null if not found.
185      */
186     private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
187         String checksum = null;
188         final var matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
189         if (matcher.find() && matcher.groupCount() == 1) {
190             checksum = matcher.group(1);
191         }
192         return checksum;
193     }
194
195     /**
196      * Get the name of the yang resource having the specified checksum.
197      * @param checksum the checksum. Null is supported.
198      * @param yangResourceEntities the list of yang resources to search among.
199      * @return the name found or null if none.
200      */
201     private String getNameForChecksum(
202             final String checksum, final Collection<YangResourceEntity> yangResourceEntities) {
203         return
204                 yangResourceEntities.stream()
205                         .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
206                         .findFirst()
207                         .map(YangResourceEntity::getName)
208                         .orElse(null);
209     }
210
211     @Override
212     public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
213         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
214         final var schemaSetEntity =
215             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
216         return schemaSetEntity.getYangResources().stream().collect(
217             Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
218     }
219
220     @Override
221     public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
222         final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
223         return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
224     }
225
226     @Override
227     @Transactional
228     public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
229         final CascadeDeleteAllowed cascadeDeleteAllowed) {
230         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
231         final var schemaSetEntity =
232             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
233
234         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
235         if (!anchorEntities.isEmpty()) {
236             if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
237                 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
238             }
239             fragmentRepository.deleteByAnchorIn(anchorEntities);
240             anchorRepository.deleteAll(anchorEntities);
241         }
242         schemaSetRepository.delete(schemaSetEntity);
243         yangResourceRepository.deleteOrphans();
244     }
245 }