List all modules references in a given dataspace and schemas set name 80/116280/9
authorClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Wed, 9 Dec 2020 12:47:16 +0000 (13:47 +0100)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Wed, 16 Dec 2020 15:21:22 +0000 (16:21 +0100)
Issue-ID: CPS-21
Signed-off-by: Claudio David Gasparini <claudio.gasparini@pantheon.tech>
Change-Id: I525f780987a201f0c1583367a2c3609488f25290

cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java
cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetNotFoundException.java [new file with mode: 0644]
cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java
cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java [moved from cps-service/src/main/java/org/onap/cps/spi/model/ModuleRef.java with 88% similarity]
cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSet.java
cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java
cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy

index 3067f48..8a14926 100644 (file)
@@ -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);
+        }
+    }
 }
index f974697..035c831 100644 (file)
@@ -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));
+    }
 }
index 7feae36..df0f9f5 100644 (file)
@@ -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<String, String> yangResourcesNameToContentMap);
 
+    /**
+     * Returns Modules references per specific namespace / schemaSetName.
+     *
+     * @param namespace     module namespace
+     * @param schemaSetName schema set name
+     * @return collection of ModuleRef
+     */
+    Collection<ModuleReference> 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 (file)
index 0000000..f92ca37
--- /dev/null
@@ -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
index 8bd4047..f9eb224 100644 (file)
@@ -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<String, Object> leaves;
     private Collection<String> xpathsChildren;
 
 package org.onap.cps.spi.model;
 
+import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 @Data
 @Builder
-public class ModuleRef {
+@NoArgsConstructor
+@AllArgsConstructor
+public class ModuleReference {
 
     private String namespace;
     private String revision;
index 5c33777..32ee324 100644 (file)
 
 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<ModuleReference> getModuleReferences();
+
     /**
      *  Return SchemaContext for given YangSchema.
      * @return SchemaContext
index 1588c44..89eea97 100644 (file)
@@ -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<String, String> 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<ModuleReference> 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<String, String> yangResourceNameToContent)
-            throws IOException, ReactorException, YangSyntaxErrorException {
+            throws ReactorException, YangSyntaxErrorException {
         final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
         final List<YangTextSchemaSource> 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();
     }
index d2a69d6..39d8ec3 100644 (file)
@@ -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