2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.yang;
22 import com.google.common.base.MoreObjects;
23 import com.google.common.collect.ImmutableMap;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import lombok.NoArgsConstructor;
32 import org.onap.cps.spi.exceptions.CpsException;
33 import org.onap.cps.spi.exceptions.ModelValidationException;
34 import org.onap.cps.spi.model.ModuleReference;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.common.YangNames;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
40 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
41 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
42 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
43 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
45 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
48 public final class YangTextSchemaSourceSetBuilder {
50 private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
52 public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
53 this.yangModelMap.putAll(yangResourceNameToContent);
57 public YangTextSchemaSourceSet build() {
58 final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
59 return new YangTextSchemaSourceSetImpl(schemaContext);
62 public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
63 return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
67 * Validates if SchemaContext can be successfully built from given yang resources.
69 * @param yangResourceNameToContent the yang resources as map where key is name and value is content
70 * @throws ModelValidationException if validation fails
72 public static void validate(final Map<String, String> yangResourceNameToContent) {
73 generateSchemaContext(yangResourceNameToContent);
76 private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
78 private final SchemaContext schemaContext;
80 private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
81 this.schemaContext = schemaContext;
85 public List<ModuleReference> getModuleReferences() {
86 return schemaContext.getModules().stream()
87 .map(YangTextSchemaSourceSetImpl::toModuleReference)
88 .collect(Collectors.toList());
91 private static ModuleReference toModuleReference(final Module module) {
92 return ModuleReference.builder()
93 .name(module.getName())
94 .namespace(module.getNamespace().toString())
95 .revision(module.getRevision().map(Revision::toString).orElse(null))
100 public SchemaContext getSchemaContext() {
101 return schemaContext;
106 * Parse and validate a string representing a yang model to generate a SchemaContext context.
108 * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
109 * on yangModelContent as key and the yangModelContent as value.
110 * @return the schema context
112 private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
113 final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
114 for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
115 final String resourceName = yangTextSchemaSource.getIdentifier().getName();
117 reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
118 } catch (final IOException e) {
119 throw new CpsException("Failed to read yang resource.",
120 String.format("Exception occurred on reading resource %s.", resourceName), e);
121 } catch (final YangSyntaxErrorException e) {
122 throw new ModelValidationException("Yang resource is invalid.",
123 String.format("Yang syntax validation failed for resource %s.", resourceName), e);
127 return reactor.buildEffective();
128 } catch (final ReactorException e) {
129 final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
130 Collections.sort(resourceNames);
131 throw new ModelValidationException("Invalid schema set.",
132 String.format("Effective schema context build failed for resources %s.", resourceNames.toString()),
137 private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
138 return yangResourceNameToContent.entrySet().stream()
139 .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
140 .collect(Collectors.toList());
143 private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
144 final Map.Entry<String, String> sourceNameParsed = YangNames.parseFilename(sourceName);
145 final RevisionSourceIdentifier revisionSourceIdentifier = RevisionSourceIdentifier
146 .create(sourceNameParsed.getKey(), Revision.ofNullable(sourceNameParsed.getValue()));
148 return new YangTextSchemaSource(revisionSourceIdentifier) {
150 protected MoreObjects.ToStringHelper addToStringAttributes(
151 final MoreObjects.ToStringHelper toStringHelper) {
152 return toStringHelper;
156 public InputStream openStream() {
157 return new ByteArrayInputStream(source.getBytes());