From 7b72ea0713dbfededd1a773e9d9b90ea0b08e045 Mon Sep 17 00:00:00 2001 From: Claudio David Gasparini Date: Wed, 9 Dec 2020 13:47:16 +0100 Subject: List all modules references in a given dataspace and schemas set name Issue-ID: CPS-21 Signed-off-by: Claudio David Gasparini Change-Id: I525f780987a201f0c1583367a2c3609488f25290 --- .../spi/impl/CpsModulePersistenceServiceImpl.java | 22 ++++++++++++++++++++++ .../cps/spi/repository/SchemaSetRepository.java | 15 ++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'cps-ri/src/main') 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 3067f48345..8a14926255 100644 --- 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 @@ -21,6 +21,7 @@ package org.onap.cps.spi.impl; import com.google.common.collect.ImmutableSet; +import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; @@ -31,10 +32,17 @@ 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.CpsException; +import org.onap.cps.spi.exceptions.ModelValidationException; import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException; +import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.onap.cps.spi.repository.YangResourceRepository; +import org.onap.cps.yang.YangTextSchemaSourceSet; +import org.onap.cps.yang.YangTextSchemaSourceSetBuilder; +import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; @@ -106,4 +114,18 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ .build(); } + @Override + public Collection getModuleReferences(final String dataspaceName, final String schemaSetName) { + final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); + final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName); + final Map yangResourceNameToContent = schemaSet.getYangResources().stream().collect( + Collectors.toMap(YangResource::getName, YangResource::getContent)); + try { + final YangTextSchemaSourceSet schemaSourceSet = YangTextSchemaSourceSetBuilder + .of(yangResourceNameToContent); + return schemaSourceSet.getModuleReferences(); + } catch (final ReactorException | YangSyntaxErrorException e) { + throw new ModelValidationException("Yang file validation failed", e.getMessage(), e); + } + } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java index f9746972f3..035c831f2f 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java @@ -24,6 +24,7 @@ import java.util.Optional; import javax.validation.constraints.NotNull; import org.onap.cps.spi.entities.Dataspace; import org.onap.cps.spi.entities.SchemaSet; +import org.onap.cps.spi.exceptions.SchemaSetNotFoundException; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -32,5 +33,17 @@ public interface SchemaSetRepository extends JpaRepository { List findAllByDataspace(@NotNull Dataspace dataspace); - Optional findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String name); + Optional findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String schemaSetName); + + /** + * This method gets a SchemaSet by dataspace, namespace and schemaSetName. + * + * @param dataspace the dataspace + * @param schemaSetName the schemaSet Name + * @return schemaSet + */ + default SchemaSet getByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String schemaSetName) { + return findByDataspaceAndName(dataspace, schemaSetName) + .orElseThrow(() -> new SchemaSetNotFoundException(dataspace.getName(), schemaSetName)); + } } -- cgit 1.2.3-korg