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.List;
29 import java.util.stream.Collectors;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.common.YangNames;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
34 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
41 public final class YangTextSchemaSourceSetBuilder {
43 private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
45 public YangTextSchemaSourceSetBuilder() {
48 public YangTextSchemaSourceSetBuilder put(final String fileName, final String content) {
49 this.yangModelMap.put(fileName, content);
53 public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
54 this.yangModelMap.putAll(yangResourceNameToContent);
58 public YangTextSchemaSourceSet build() throws ReactorException, IOException, YangSyntaxErrorException {
59 final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
60 return new YangTextSchemaSourceSetImpl(schemaContext);
63 public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent)
64 throws ReactorException, IOException, YangSyntaxErrorException {
65 return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
68 private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
70 private final SchemaContext schemaContext;
72 public YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
73 this.schemaContext = schemaContext;
77 public SchemaContext getSchemaContext() {
83 * Parse and validate a string representing a yang model to generate a SchemaContext context.
85 * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
86 * on yangModelContent as key and the yangModelContent as value.
87 * @return the schema context
89 private SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent)
90 throws IOException, ReactorException, YangSyntaxErrorException {
91 final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
92 final List<YangTextSchemaSource> yangTextSchemaSources = forResources(yangResourceNameToContent);
93 for (final YangTextSchemaSource yangTextSchemaSource : yangTextSchemaSources) {
94 reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
96 return reactor.buildEffective();
99 private List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
100 return yangResourceNameToContent.entrySet().stream()
101 .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
102 .collect(Collectors.toList());
105 private YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
106 final Map.Entry<String, String> sourceNameParsed = YangNames.parseFilename(sourceName);
107 final RevisionSourceIdentifier revisionSourceIdentifier = RevisionSourceIdentifier
108 .create(sourceNameParsed.getKey(), Revision.ofNullable(sourceNameParsed.getValue()));
109 return new YangTextSchemaSource(revisionSourceIdentifier) {
111 protected MoreObjects.ToStringHelper addToStringAttributes(
112 final MoreObjects.ToStringHelper toStringHelper) {
113 return toStringHelper;
117 public InputStream openStream() {
118 return new ByteArrayInputStream(source.getBytes());