aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
blob: ec720b8a96b80f45ce5dbcf9e2cc8713fd3c3a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2020-2022 Nordix Foundation
 *  Modifications Copyright (C) 2020-2022 Bell Canada.
 *  Modifications Copyright (C) 2021 Pantheon.tech
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.spi.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.onap.cps.spi.CpsAdminPersistenceService;
import org.onap.cps.spi.CpsModulePersistenceService;
import org.onap.cps.spi.entities.SchemaSetEntity;
import org.onap.cps.spi.entities.YangResourceEntity;
import org.onap.cps.spi.entities.YangResourceModuleReference;
import org.onap.cps.spi.exceptions.AlreadyDefinedException;
import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
import org.onap.cps.spi.exceptions.ModelValidationException;
import org.onap.cps.spi.model.ModuleReference;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.ModuleReferenceRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
import org.onap.cps.spi.repository.YangResourceRepository;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {

    private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key";
    private static final Pattern CHECKSUM_EXCEPTION_PATTERN = Pattern.compile(".*\\(checksum\\)=\\((\\w+)\\).*");
    private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern
            .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);

    private YangResourceRepository yangResourceRepository;

    private SchemaSetRepository schemaSetRepository;

    private DataspaceRepository dataspaceRepository;

    private CpsAdminPersistenceService cpsAdminPersistenceService;

    private ModuleReferenceRepository moduleReferenceRepository;

    @Override
    public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
        final var schemaSetEntity =
            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
        return schemaSetEntity.getYangResources().stream().collect(
            Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
    }

    @Override
    public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
        final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
        return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
    }

    @Override
    public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
        final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
            yangResourceRepository.findAllModuleReferences(dataspaceName);
        return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
            .collect(Collectors.toList());
    }

    @Override
    public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName,
        final String anchorName) {
        final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
            yangResourceRepository
                .findAllModuleReferences(dataspaceName, anchorName);
        return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
            .collect(Collectors.toList());
    }

    @Override
    @Transactional
    // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
    // can occur in case of specific concurrent requests.
    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff =
        @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2))
    public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
        final Map<String, String> moduleReferenceNameToContentMap) {
        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
        final var yangResourceEntities = synchronizeYangResources(moduleReferenceNameToContentMap);
        final var schemaSetEntity = new SchemaSetEntity();
        schemaSetEntity.setName(schemaSetName);
        schemaSetEntity.setDataspace(dataspaceEntity);
        schemaSetEntity.setYangResources(yangResourceEntities);
        try {
            schemaSetRepository.save(schemaSetEntity);
        } catch (final DataIntegrityViolationException e) {
            throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e);
        }
    }

    @Override
    @Transactional
    // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
    // can occur in case of specific concurrent requests.
    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff =
        @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2))
    public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
                                          final Map<String, String> newModuleNameToContentMap,
                                          final Collection<ModuleReference> moduleReferences) {
        storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap);
        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
        final var schemaSetEntity =
                schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
        final List<Long> listOfYangResourceIds = new ArrayList<>();
        moduleReferences.forEach(moduleReference ->
                listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision(
                        moduleReference.getModuleName(), moduleReference.getRevision())));
        yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds);
    }

    @Override
    @Transactional
    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
        final var schemaSetEntity =
            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
        schemaSetRepository.delete(schemaSetEntity);
    }

    @Override
    @Transactional
    public void deleteUnusedYangResourceModules() {
        yangResourceRepository.deleteOrphans();
    }

    @Override
    public Collection<ModuleReference> identifyNewModuleReferences(
        final Collection<ModuleReference> moduleReferencesToCheck) {
        return moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck);
    }

    private Set<YangResourceEntity> synchronizeYangResources(
        final Map<String, String> moduleReferenceNameToContentMap) {
        final Map<String, YangResourceEntity> checksumToEntityMap = moduleReferenceNameToContentMap.entrySet().stream()
            .map(entry -> {
                final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
                final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(),
                            entry.getValue());
                final var yangResourceEntity = new YangResourceEntity();
                yangResourceEntity.setName(entry.getKey());
                yangResourceEntity.setContent(entry.getValue());
                yangResourceEntity.setModuleName(moduleNameAndRevisionMap.get("moduleName"));
                yangResourceEntity.setRevision(moduleNameAndRevisionMap.get("revision"));
                yangResourceEntity.setChecksum(checksum);
                return yangResourceEntity;
            })
            .collect(Collectors.toMap(
                YangResourceEntity::getChecksum,
                entity -> entity
            ));

        final List<YangResourceEntity> existingYangResourceEntities =
            yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
        existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));

        final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
        if (!newYangResourceEntities.isEmpty()) {
            try {
                yangResourceRepository.saveAll(newYangResourceEntities);
            } catch (final DataIntegrityViolationException dataIntegrityViolationException) {
                // Throw a CPS duplicated Yang resource exception if the cause of the error is a yang checksum
                // database constraint violation.
                // If it is not, then throw the original exception
                final Optional<DuplicatedYangResourceException> convertedException =
                        convertToDuplicatedYangResourceException(
                                dataIntegrityViolationException, newYangResourceEntities);
                convertedException.ifPresent(
                    e ->  log.warn(
                                "Cannot persist duplicated yang resource. "
                                        + "System will attempt this method up to 5 times.", e));
                throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException;
            }
        }

        return ImmutableSet.<YangResourceEntity>builder()
            .addAll(existingYangResourceEntities)
            .addAll(newYangResourceEntities)
            .build();
    }

    private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) {
        final Map<String, String> metaDataMap = new HashMap<>();
        final var revisionSourceIdentifier =
                createIdentifierFromSourceName(checkNotNull(sourceName));

        final var tempYangTextSchemaSource = new YangTextSchemaSource(revisionSourceIdentifier) {
            @Override
            protected MoreObjects.ToStringHelper addToStringAttributes(
                    final MoreObjects.ToStringHelper toStringHelper) {
                return toStringHelper;
            }

            @Override
            public InputStream openStream() {
                return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
            }
        };
        try {
            final var dependencyInfo = YangModelDependencyInfo.forYangText(tempYangTextSchemaSource);
            metaDataMap.put("moduleName", dependencyInfo.getName());
            metaDataMap.put("revision", dependencyInfo.getFormattedRevision());
        } catch (final YangSyntaxErrorException | IOException e) {
            throw new ModelValidationException("Yang resource is invalid.",
                   String.format("Yang syntax validation failed for resource %s:%n%s", sourceName, e.getMessage()), e);
        }
        return metaDataMap;
    }

    private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
        final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
        if (matcher.matches()) {
            return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
        }
        return RevisionSourceIdentifier.create(sourceName);
    }

    /**
     * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
     * if the cause of the error is a yang checksum database constraint violation.
     *
     * @param originalException the original db exception.
     * @param yangResourceEntities the collection of Yang resources involved in the db failure.
     * @return an optional converted CPS duplicated Yang resource exception. The optional is empty if the original
     *      cause of the error is not a yang checksum database constraint violation.
     */
    private Optional<DuplicatedYangResourceException> convertToDuplicatedYangResourceException(
            final DataIntegrityViolationException originalException,
            final Collection<YangResourceEntity> yangResourceEntities) {

        // The exception result
        DuplicatedYangResourceException duplicatedYangResourceException = null;

        final Throwable cause = originalException.getCause();
        if (cause instanceof ConstraintViolationException) {
            final ConstraintViolationException constraintException = (ConstraintViolationException) cause;
            if (YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME.equals(constraintException.getConstraintName())) {
                // Db constraint related to yang resource checksum uniqueness is not respected
                final String checksumInError = getDuplicatedChecksumFromException(constraintException);
                final String nameInError = getNameForChecksum(checksumInError, yangResourceEntities);
                duplicatedYangResourceException =
                        new DuplicatedYangResourceException(nameInError, checksumInError, constraintException);
            }
        }

        return Optional.ofNullable(duplicatedYangResourceException);

    }

    /**
     * Get the name of the yang resource having the specified checksum.
     *
     * @param checksum the checksum. Null is supported.
     * @param yangResourceEntities the list of yang resources to search among.
     * @return the name found or null if none.
     */
    private String getNameForChecksum(
            final String checksum, final Collection<YangResourceEntity> yangResourceEntities) {
        return
                yangResourceEntities.stream()
                        .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
                        .findFirst()
                        .map(YangResourceEntity::getName)
                        .orElse(null);
    }

    /**
     * Get the checksum that caused the constraint violation exception.
     *
     * @param exception the exception having the checksum in error.
     * @return the checksum in error or null if not found.
     */
    private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
        String checksum = null;
        final var matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
        if (matcher.find() && matcher.groupCount() == 1) {
            checksum = matcher.group(1);
        }
        return checksum;
    }

    private static ModuleReference toModuleReference(
        final YangResourceModuleReference yangResourceModuleReference) {
        return ModuleReference.builder()
            .moduleName(yangResourceModuleReference.getModuleName())
            .revision(yangResourceModuleReference.getRevision())
            .build();
    }
}