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 static com.google.common.base.Preconditions.checkNotNull;
24 import com.google.common.base.MoreObjects;
25 import com.google.common.collect.ImmutableMap;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Collections;
31 import java.util.List;
33 import java.util.regex.Pattern;
34 import java.util.stream.Collectors;
35 import lombok.NoArgsConstructor;
36 import org.onap.cps.spi.exceptions.CpsException;
37 import org.onap.cps.spi.exceptions.ModelValidationException;
38 import org.onap.cps.spi.model.ExtendedModuleReference;
39 import org.opendaylight.yangtools.yang.common.Revision;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
43 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
44 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
45 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
46 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
48 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
51 public final class YangTextSchemaSourceSetBuilder {
53 private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN =
54 Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
56 private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
58 public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
59 this.yangModelMap.putAll(yangResourceNameToContent);
63 public YangTextSchemaSourceSet build() {
64 final var schemaContext = generateSchemaContext(yangModelMap.build());
65 return new YangTextSchemaSourceSetImpl(schemaContext);
68 public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
69 return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
73 * Validates if SchemaContext can be successfully built from given yang resources.
75 * @param yangResourceNameToContent the yang resources as map where key is name and value is content
76 * @throws ModelValidationException if validation fails
78 public static void validate(final Map<String, String> yangResourceNameToContent) {
79 generateSchemaContext(yangResourceNameToContent);
82 private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
84 private final SchemaContext schemaContext;
86 private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
87 this.schemaContext = schemaContext;
91 public List<ExtendedModuleReference> getModuleReferences() {
92 return schemaContext.getModules().stream()
93 .map(YangTextSchemaSourceSetImpl::toModuleReference)
94 .collect(Collectors.toList());
97 private static ExtendedModuleReference toModuleReference(final Module module) {
98 return ExtendedModuleReference.builder()
99 .name(module.getName())
100 .namespace(module.getQNameModule().getNamespace().toString())
101 .revision(module.getRevision().map(Revision::toString).orElse(null))
106 public SchemaContext getSchemaContext() {
107 return schemaContext;
112 * Parse and validate a string representing a yang model to generate a SchemaContext context.
114 * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
115 * on yangModelContent as key and the yangModelContent as value.
116 * @return the schema context
118 private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
119 final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
120 for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
121 final String resourceName = yangTextSchemaSource.getIdentifier().getName();
123 reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
124 } catch (final IOException e) {
125 throw new CpsException("Failed to read yang resource.",
126 String.format("Exception occurred on reading resource %s.", resourceName), e);
127 } catch (final YangSyntaxErrorException e) {
128 throw new ModelValidationException("Yang resource is invalid.",
130 "Yang syntax validation failed for resource %s:%n%s", resourceName, e.getMessage()), e);
134 return reactor.buildEffective();
135 } catch (final ReactorException e) {
136 final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
137 Collections.sort(resourceNames);
138 throw new ModelValidationException("Invalid schema set.",
139 String.format("Effective schema context build failed for resources %s.", resourceNames.toString()),
144 private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
145 return yangResourceNameToContent.entrySet().stream()
146 .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
147 .collect(Collectors.toList());
150 private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
151 final var revisionSourceIdentifier =
152 createIdentifierFromSourceName(checkNotNull(sourceName));
154 return new YangTextSchemaSource(revisionSourceIdentifier) {
156 protected MoreObjects.ToStringHelper addToStringAttributes(
157 final MoreObjects.ToStringHelper toStringHelper) {
158 return toStringHelper;
162 public InputStream openStream() {
163 return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
168 private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
169 final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
170 if (matcher.matches()) {
171 return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
173 return RevisionSourceIdentifier.create(sourceName);