diff options
author | 2020-12-09 13:47:16 +0100 | |
---|---|---|
committer | 2020-12-16 16:21:22 +0100 | |
commit | 7b72ea0713dbfededd1a773e9d9b90ea0b08e045 (patch) | |
tree | f5449bc99b0f12c0ab207c7da61c03c3aa927d2e /cps-ri/src/main/java/org/onap | |
parent | 966f3ab710bd1bebaa81e2394627df47a3f98909 (diff) |
List all modules references in a given dataspace and schemas set name
Issue-ID: CPS-21
Signed-off-by: Claudio David Gasparini <claudio.gasparini@pantheon.tech>
Change-Id: I525f780987a201f0c1583367a2c3609488f25290
Diffstat (limited to 'cps-ri/src/main/java/org/onap')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java | 22 | ||||
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java | 15 |
2 files changed, 36 insertions, 1 deletions
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 3067f4834..8a1492625 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<ModuleReference> getModuleReferences(final String dataspaceName, final String schemaSetName) { + final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); + final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName); + final Map<String, String> 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 f9746972f..035c831f2 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<SchemaSet, Integer> { List<SchemaSet> findAllByDataspace(@NotNull Dataspace dataspace); - Optional<SchemaSet> findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String name); + Optional<SchemaSet> 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)); + } } |