From dce1d58005d90950e00b22078d82559748dcb255 Mon Sep 17 00:00:00 2001 From: shivasubedi Date: Wed, 4 Aug 2021 12:13:36 +0100 Subject: CPS-506: List all known modules and revision Issue-ID: CPS-506 Signed-off-by: shivasubedi Change-Id: Id76fecc7dd10625cc3ecb940b1181b1e8bf829a1 --- .../onap/cps/spi/entities/YangResourceEntity.java | 9 ++ .../spi/entities/YangResourceModuleReference.java | 31 ++++ .../spi/impl/CpsModulePersistenceServiceImpl.java | 164 +++++++++++++++------ .../cps/spi/repository/YangResourceRepository.java | 5 + .../main/resources/changelog/changelog-master.yaml | 2 + .../11-add-column-to-yang-resources-table.yaml | 40 +++++ 6 files changed, 205 insertions(+), 46 deletions(-) create mode 100644 cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java create mode 100644 cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml (limited to 'cps-ri/src/main') diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java index 4763d68f6..032745b5e 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech + * Modifications Copyright (C) 2021 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,6 +62,14 @@ public class YangResourceEntity implements Serializable { @Column private String content; + @NotNull + @Column + private String moduleName; + + @NotNull + @Column + private String revision; + @ManyToMany(mappedBy = "yangResources") private Set schemaSets; diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java new file mode 100644 index 000000000..3c39c6baa --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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.entities; + +import org.springframework.beans.factory.annotation.Value; + +public interface YangResourceModuleReference { + + @Value("#{target.module_name}") + String getModuleName(); + + String getRevision(); +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index 1b3dc2486..535cbe22e 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -22,9 +22,16 @@ 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; @@ -42,14 +49,22 @@ import org.onap.cps.spi.CpsModulePersistenceService; import org.onap.cps.spi.entities.AnchorEntity; 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.exceptions.SchemaSetInUseException; +import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.FragmentRepository; 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.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.retry.annotation.Backoff; @@ -63,6 +78,8 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ 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); @Autowired private YangResourceRepository yangResourceRepository; @@ -82,6 +99,29 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Autowired private CpsAdminPersistenceService cpsAdminPersistenceService; + @Override + public Map 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 getYangSchemaSetResources(final String dataspaceName, final String anchorName) { + final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName); + return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName()); + } + + @Override + public List getAllYangResourcesModuleReferences() { + final List yangResourceModuleReferenceList = + yangResourceRepository.findAllModuleNameAndRevision(); + 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 @@ -91,7 +131,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ final Map yangResourcesNameToContentMap) { final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final Set yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap); + final var yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap); final var schemaSetEntity = new SchemaSetEntity(); schemaSetEntity.setName(schemaSetName); schemaSetEntity.setDataspace(dataspaceEntity); @@ -103,13 +143,37 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ } } + @Override + @Transactional + public void deleteSchemaSet(final String dataspaceName, final String schemaSetName, + final CascadeDeleteAllowed cascadeDeleteAllowed) { + final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final var schemaSetEntity = + schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); + + final Collection anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity); + if (!anchorEntities.isEmpty()) { + if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) { + throw new SchemaSetInUseException(dataspaceName, schemaSetName); + } + fragmentRepository.deleteByAnchorIn(anchorEntities); + anchorRepository.deleteAll(anchorEntities); + } + schemaSetRepository.delete(schemaSetEntity); + yangResourceRepository.deleteOrphans(); + } + private Set synchronizeYangResources(final Map yangResourcesNameToContentMap) { final Map checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream() .map(entry -> { final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8)); + final Map 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; }) @@ -147,6 +211,42 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ .build(); } + private static Map createModuleNameAndRevisionMap(final String sourceName, final String source) { + final Map 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. @@ -178,20 +278,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ } - /** - * 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; - } - /** * Get the name of the yang resource having the specified checksum. * @param checksum the checksum. Null is supported. @@ -208,38 +294,24 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ .orElse(null); } - @Override - public Map 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 getYangSchemaSetResources(final String dataspaceName, final String anchorName) { - final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName); - return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName()); + /** + * 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; } - @Override - @Transactional - public void deleteSchemaSet(final String dataspaceName, final String schemaSetName, - final CascadeDeleteAllowed cascadeDeleteAllowed) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = - schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - - final Collection anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity); - if (!anchorEntities.isEmpty()) { - if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) { - throw new SchemaSetInUseException(dataspaceName, schemaSetName); - } - fragmentRepository.deleteByAnchorIn(anchorEntities); - anchorRepository.deleteAll(anchorEntities); - } - schemaSetRepository.delete(schemaSetEntity); - yangResourceRepository.deleteOrphans(); + private static ModuleReference toModuleReference(final YangResourceModuleReference yangResourceModuleReference) { + return ModuleReference.builder() + .name(yangResourceModuleReference.getModuleName()) + .revision(yangResourceModuleReference.getRevision()) + .build(); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index 25d76890b..fe8787f93 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech + * Modifications Copyright (C) Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ import java.util.List; import java.util.Set; import javax.validation.constraints.NotNull; import org.onap.cps.spi.entities.YangResourceEntity; +import org.onap.cps.spi.entities.YangResourceModuleReference; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -33,6 +35,9 @@ public interface YangResourceRepository extends JpaRepository findAllByChecksumIn(@NotNull Set checksum); + @Query(value = "SELECT module_name, revision FROM yang_resource", nativeQuery = true) + List findAllModuleNameAndRevision(); + @Modifying @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS " + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) diff --git a/cps-ri/src/main/resources/changelog/changelog-master.yaml b/cps-ri/src/main/resources/changelog/changelog-master.yaml index 6c0158390..14f3726d2 100644 --- a/cps-ri/src/main/resources/changelog/changelog-master.yaml +++ b/cps-ri/src/main/resources/changelog/changelog-master.yaml @@ -35,3 +35,5 @@ databaseChangeLog: file: changelog/db/changes/09-loadData-dmi-registry-schema-set.yaml - include: file: changelog/db/changes/10-loadData-dmi-registry-fragment.yaml + - include: + file: changelog/db/changes/11-add-column-to-yang-resources-table.yaml diff --git a/cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml b/cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml new file mode 100644 index 000000000..a2bfb67f9 --- /dev/null +++ b/cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml @@ -0,0 +1,40 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2021 Nordix Foundation. +# ================================================================================ +# 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========================================================= + +databaseChangeLog: + - changeSet: + id: 11 + label: add-module-name-and-revision-column + author: cps + changes: + - addColumn: + tableName: yang_resource + columns: + - column: + name: module_name + type: TEXT + - column: + name: revision + type: TEXT + - changeSet: + id: 11.1 + label: update-previous-data-module-name-and-revision + author: cps + changes: + - sql: + sql: update yang_resource set module_name = 'dummy_module_name', revision = '2021-08-04' where module_name is null and revision is null -- cgit 1.2.3-korg