/*
- * ============LICENSE_START=======================================================
- * Copyright (C) 2020 Nordix Foundation
- * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020-2023 Nordix Foundation
+ * Modifications Copyright (C) 2020-2022 Bell Canada.
+ * Modifications Copyright (C) 2021 Pantheon.tech
+ * Modifications Copyright (C) 2022 TechMahindra Ltd.
* ================================================================================
* 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.
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.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.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
+import lombok.RequiredArgsConstructor;
+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.Dataspace;
-import org.onap.cps.spi.entities.SchemaSet;
-import org.onap.cps.spi.entities.YangResource;
-import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
+import org.onap.cps.spi.entities.DataspaceEntity;
+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.ModuleDefinition;
+import org.onap.cps.spi.model.ModuleReference;
+import org.onap.cps.spi.model.SchemaSet;
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.springframework.beans.factory.annotation.Autowired;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
+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.retry.support.RetrySynchronizationManager;
import org.springframework.stereotype.Component;
-import org.springframework.util.DigestUtils;
+@Slf4j
@Component
+@RequiredArgsConstructor
public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
- @Autowired
- private YangResourceRepository yangResourceRepository;
+ 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 final YangResourceRepository yangResourceRepository;
+
+ private final SchemaSetRepository schemaSetRepository;
+
+ private final DataspaceRepository dataspaceRepository;
+
+ private final CpsAdminPersistenceService cpsAdminPersistenceService;
+
+ private final ModuleReferenceRepository moduleReferenceRepository;
+
+ @Override
+ public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final SchemaSetEntity schemaSetEntity =
+ schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
+ return schemaSetEntity.getYangResources().stream().collect(
+ Collectors.toMap(YangResourceEntity::getFileName, YangResourceEntity::getContent));
+ }
+
+ @Override
+ public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
+ final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
+ yangResourceRepository.findAllModuleReferencesByDataspace(dataspaceName);
+ return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
+ .collect(Collectors.toList());
+ }
- @Autowired
- private SchemaSetRepository schemaSetRepository;
+ @Override
+ public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName,
+ final String anchorName) {
+ final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
+ yangResourceRepository
+ .findAllModuleReferencesByDataspaceAndAnchor(dataspaceName, anchorName);
+ return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
+ .collect(Collectors.toList());
+ }
- @Autowired
- private DataspaceRepository dataspaceRepository;
+ @Override
+ public Collection<ModuleDefinition> getYangResourceDefinitions(final String dataspaceName,
+ final String anchorName) {
+ final Set<YangResourceEntity> yangResourceEntities =
+ yangResourceRepository
+ .findAllModuleDefinitionsByDataspaceAndAnchor(dataspaceName, anchorName);
+ return yangResourceEntities.stream().map(CpsModulePersistenceServiceImpl::toModuleDefinition)
+ .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> yangResourcesNameToContentMap) {
-
- final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
- final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesNameToContentMap);
- final SchemaSet schemaSet = new SchemaSet();
- schemaSet.setName(schemaSetName);
- schemaSet.setDataspace(dataspace);
- schemaSet.setYangResources(yangResources);
+ final Map<String, String> moduleReferenceNameToContentMap) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(moduleReferenceNameToContentMap);
+ final SchemaSetEntity schemaSetEntity = new SchemaSetEntity();
+ schemaSetEntity.setName(schemaSetName);
+ schemaSetEntity.setDataspace(dataspaceEntity);
+ schemaSetEntity.setYangResources(yangResourceEntities);
try {
- schemaSetRepository.save(schemaSet);
+ schemaSetRepository.save(schemaSetEntity);
} catch (final DataIntegrityViolationException e) {
- throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
+ throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e);
}
}
- private Set<YangResource> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
- final Map<String, YangResource> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
+ @Override
+ public Collection<SchemaSet> getSchemaSetsByDataspaceName(final String dataspaceName) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final List<SchemaSetEntity> schemaSetEntities = schemaSetRepository.findByDataspace(dataspaceEntity);
+ return schemaSetEntities.stream()
+ .map(CpsModulePersistenceServiceImpl::toSchemaSet).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 storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
+ final Map<String, String> newModuleNameToContentMap,
+ final Collection<ModuleReference> allModuleReferences) {
+ storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap);
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final SchemaSetEntity schemaSetEntity =
+ schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
+ final List<Integer> allYangResourceIds =
+ yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences);
+ yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), allYangResourceIds);
+ }
+
+ @Override
+ @Transactional
+ public void deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final SchemaSetEntity schemaSetEntity =
+ schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
+ schemaSetRepository.delete(schemaSetEntity);
+ }
+
+ @Override
+ @Transactional
+ public void deleteSchemaSets(final String dataspaceName, final Collection<String> schemaSetNames) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ schemaSetRepository.deleteByDataspaceAndNameIn(dataspaceEntity, schemaSetNames);
+ }
+
+ @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 YangResource yangResource = new YangResource();
- yangResource.setName(entry.getKey());
- yangResource.setContent(entry.getValue());
- yangResource.setChecksum(DigestUtils.md5DigestAsHex(entry.getValue().getBytes(StandardCharsets.UTF_8)));
- return yangResource;
+ final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
+ final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(),
+ entry.getValue());
+ final YangResourceEntity yangResourceEntity = new YangResourceEntity();
+ yangResourceEntity.setFileName(entry.getKey());
+ yangResourceEntity.setContent(entry.getValue());
+ yangResourceEntity.setModuleName(moduleNameAndRevisionMap.get("moduleName"));
+ yangResourceEntity.setRevision(moduleNameAndRevisionMap.get("revision"));
+ yangResourceEntity.setChecksum(checksum);
+ return yangResourceEntity;
})
.collect(Collectors.toMap(
- YangResource::getChecksum,
+ YangResourceEntity::getChecksum,
entity -> entity
));
- final List<YangResource> existingYangResources =
+ final List<YangResourceEntity> existingYangResourceEntities =
yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
- existingYangResources.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
+ existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
- final Collection<YangResource> newYangResources = checksumToEntityMap.values();
- if (!newYangResources.isEmpty()) {
- yangResourceRepository.saveAll(newYangResources);
+ 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 -> {
+ int retryCount = RetrySynchronizationManager.getContext() == null ? 0
+ : RetrySynchronizationManager.getContext().getRetryCount();
+ log.warn("Cannot persist duplicated yang resource. System will attempt this method "
+ + "up to 5 times. Current retry count : {}", ++retryCount, e);
+ });
+ throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException;
+ }
}
- return ImmutableSet.<YangResource>builder()
- .addAll(existingYangResources)
- .addAll(newYangResources)
+ return ImmutableSet.<YangResourceEntity>builder()
+ .addAll(existingYangResourceEntities)
+ .addAll(newYangResourceEntities)
.build();
}
- @Override
- public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
- final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
- final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
- return schemaSet.getYangResources().stream().collect(
- Collectors.toMap(YangResource::getName, YangResource::getContent));
+ private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) {
+ final Map<String, String> metaDataMap = new HashMap<>();
+ final RevisionSourceIdentifier revisionSourceIdentifier =
+ createIdentifierFromSourceName(checkNotNull(sourceName));
+
+ final YangTextSchemaSource tempYangTextSchemaSource = new YangTextSchemaSource(revisionSourceIdentifier) {
+ @Override
+ public Optional<String> getSymbolicName() {
+ return Optional.empty();
+ }
+
+ @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 YangModelDependencyInfo yangModelDependencyInfo
+ = YangModelDependencyInfo.forYangText(tempYangTextSchemaSource);
+ metaDataMap.put("moduleName", yangModelDependencyInfo.getName());
+ metaDataMap.put("revision", yangModelDependencyInfo.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 Matcher 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);
+
+ }
+
+ private String getNameForChecksum(final String checksum,
+ final Collection<YangResourceEntity> yangResourceEntities) {
+ final Optional<String> optionalFileName = yangResourceEntities.stream()
+ .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
+ .findFirst()
+ .map(YangResourceEntity::getFileName);
+ if (optionalFileName.isPresent()) {
+ return optionalFileName.get();
+ }
+ return "no filename";
+ }
+
+ private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
+ final Matcher matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
+ if (matcher.find()) {
+ return matcher.group(1);
+ }
+ return "no checksum found";
+ }
+
+ private static ModuleReference toModuleReference(
+ final YangResourceModuleReference yangResourceModuleReference) {
+ return ModuleReference.builder()
+ .moduleName(yangResourceModuleReference.getModuleName())
+ .revision(yangResourceModuleReference.getRevision())
+ .build();
+ }
+
+ private static ModuleDefinition toModuleDefinition(final YangResourceEntity yangResourceEntity) {
+ return new ModuleDefinition(
+ yangResourceEntity.getModuleName(),
+ yangResourceEntity.getRevision(),
+ yangResourceEntity.getContent());
+ }
+
+ private static SchemaSet toSchemaSet(final SchemaSetEntity schemaSetEntity) {
+ return SchemaSet.builder().name(schemaSetEntity.getName())
+ .dataspaceName(schemaSetEntity.getDataspace().getName()).build();
}
}