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;
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;
.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);
+ }
+ }
}
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;
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));
+ }
}
package org.onap.cps.spi;
+import java.util.Collection;
import java.util.Map;
+import org.onap.cps.spi.model.ModuleReference;
/**
* Service to manage modules.
*/
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);
}
--- /dev/null
+/*
+ * ============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
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;
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
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;
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();
}
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;
* @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();
}
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