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 ++++++++- .../onap/cps/spi/CpsModulePersistenceService.java | 10 ++++++ .../spi/exceptions/SchemaSetNotFoundException.java | 36 ++++++++++++++++++++++ .../main/java/org/onap/cps/spi/model/DataNode.java | 6 +++- .../java/org/onap/cps/spi/model/ModuleRef.java | 32 ------------------- .../org/onap/cps/spi/model/ModuleReference.java | 36 ++++++++++++++++++++++ .../org/onap/cps/yang/YangTextSchemaSourceSet.java | 11 +++++++ .../cps/yang/YangTextSchemaSourceSetBuilder.java | 30 +++++++++++++++--- .../CpsModulePersistenceServiceImplSpec.groovy | 2 -- 10 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetNotFoundException.java delete mode 100644 cps-service/src/main/java/org/onap/cps/spi/model/ModuleRef.java create mode 100644 cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java 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 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 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 { 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)); + } } diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java index 7feae367e..df0f9f5a2 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java @@ -20,7 +20,9 @@ package org.onap.cps.spi; +import java.util.Collection; import java.util.Map; +import org.onap.cps.spi.model.ModuleReference; /** * Service to manage modules. @@ -52,4 +54,12 @@ public interface CpsModulePersistenceService { */ void storeSchemaSet(String dataspaceName, String schemaSetName, Map yangResourcesNameToContentMap); + /** + * Returns Modules references per specific namespace / schemaSetName. + * + * @param namespace module namespace + * @param schemaSetName schema set name + * @return collection of ModuleRef + */ + Collection getModuleReferences(String namespace, String schemaSetName); } diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetNotFoundException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetNotFoundException.java new file mode 100644 index 000000000..f92ca3729 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetNotFoundException.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.exceptions; + +public class SchemaSetNotFoundException extends CpsAdminException { + + private static final long serialVersionUID = 7422782395935450035L; + + /** + * Constructor. + * + * @param dataspaceName dataspace name + * @param schemaSetName schema set name + */ + public SchemaSetNotFoundException(final String dataspaceName, final String schemaSetName) { + super("Schema Set not found.", + String.format("Schema Set with name %s was not found for dataspace %s.", schemaSetName, dataspaceName)); + } +} \ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java b/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java index 8bd404700..f9eb22493 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java +++ b/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java @@ -22,16 +22,20 @@ package org.onap.cps.spi.model; import java.util.Collection; import java.util.Map; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; @Data @Builder +@NoArgsConstructor +@AllArgsConstructor public class DataNode { private String dataspace; private String moduleSetName; - private ModuleRef moduleRef; + private ModuleReference moduleReference; private String xpath; private Map leaves; private Collection xpathsChildren; diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/ModuleRef.java b/cps-service/src/main/java/org/onap/cps/spi/model/ModuleRef.java deleted file mode 100644 index 1f4e64a94..000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/model/ModuleRef.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. All rights reserved. - * ================================================================================ - * 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.model; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class ModuleRef { - - private String namespace; - private String revision; -} diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java b/cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java new file mode 100644 index 000000000..5af355dcd --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * ================================================================================ + * 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.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ModuleReference { + + private String namespace; + private String revision; +} diff --git a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSet.java b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSet.java index 5c337770d..32ee324c5 100644 --- a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSet.java +++ b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSet.java @@ -19,13 +19,24 @@ package org.onap.cps.yang; +import java.util.List; import org.checkerframework.checker.nullness.qual.NonNull; +import org.onap.cps.spi.model.ModuleReference; import org.opendaylight.yangtools.yang.model.api.SchemaContext; /** * CPS YangTextSchemaSource. */ public interface YangTextSchemaSourceSet { + + /** + * Returns list of modules references for given YangSchema. + * + * @return list of ModuleRef + */ + @NonNull + List getModuleReferences(); + /** * Return SchemaContext for given YangSchema. * @return SchemaContext diff --git a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java index 1588c44d9..89eea97f6 100644 --- a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java +++ b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java @@ -27,8 +27,11 @@ import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.model.ModuleReference; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.common.YangNames; +import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; @@ -55,13 +58,13 @@ public final class YangTextSchemaSourceSetBuilder { return this; } - public YangTextSchemaSourceSet build() throws ReactorException, IOException, YangSyntaxErrorException { + public YangTextSchemaSourceSet build() throws ReactorException, YangSyntaxErrorException { final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build()); return new YangTextSchemaSourceSetImpl(schemaContext); } public static YangTextSchemaSourceSet of(final Map yangResourceNameToContent) - throws ReactorException, IOException, YangSyntaxErrorException { + throws ReactorException, YangSyntaxErrorException { return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build(); } @@ -73,6 +76,20 @@ public final class YangTextSchemaSourceSetBuilder { this.schemaContext = schemaContext; } + @Override + public List getModuleReferences() { + return schemaContext.getModules().stream() + .map(YangTextSchemaSourceSetImpl::toModuleReference) + .collect(Collectors.toList()); + } + + private static ModuleReference toModuleReference(final Module module) { + return ModuleReference.builder() + .namespace(module.getName()) + .revision(module.getRevision().map(Revision::toString).orElse(null)) + .build(); + } + @Override public SchemaContext getSchemaContext() { return schemaContext; @@ -87,11 +104,16 @@ public final class YangTextSchemaSourceSetBuilder { * @return the schema context */ private SchemaContext generateSchemaContext(final Map yangResourceNameToContent) - throws IOException, ReactorException, YangSyntaxErrorException { + throws ReactorException, YangSyntaxErrorException { final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild(); final List yangTextSchemaSources = forResources(yangResourceNameToContent); for (final YangTextSchemaSource yangTextSchemaSource : yangTextSchemaSources) { - reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource)); + try { + reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource)); + } catch (final IOException e) { + throw new CpsException("Failed to read yangTextSchemaSource %s.", + yangTextSchemaSource.getIdentifier().getName(), e); + } } return reactor.buildEffective(); } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy index d2a69d655..39d8ec3bc 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy @@ -20,9 +20,7 @@ package org.onap.cps.api.impl -import org.onap.cps.TestUtils import org.onap.cps.spi.CpsModulePersistenceService -import org.onap.cps.spi.exceptions.CpsException import org.opendaylight.yangtools.yang.common.Revision import org.opendaylight.yangtools.yang.model.api.SchemaContext import spock.lang.Specification -- cgit 1.2.3-korg