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 --- .../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 -- 8 files changed, 124 insertions(+), 39 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 (limited to 'cps-service') 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 7feae367e8..df0f9f5a24 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 0000000000..f92ca37298 --- /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 8bd4047007..f9eb22493c 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 1f4e64a949..0000000000 --- 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 0000000000..5af355dcdb --- /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 5c337770dd..32ee324c54 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 1588c44d9c..89eea97f61 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 d2a69d6552..39d8ec3bca 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