Introduce YangTextSchemaSourceSet
[cps.git] / cps-service / src / main / java / org / onap / cps / yang / YangTextSchemaSourceSetBuilder.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.yang;
21
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;
28 import java.util.Map;
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;
40
41 public final class YangTextSchemaSourceSetBuilder {
42
43     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
44
45     public YangTextSchemaSourceSetBuilder() {
46     }
47
48     public YangTextSchemaSourceSetBuilder put(final String fileName, final String content) {
49         this.yangModelMap.put(fileName, content);
50         return this;
51     }
52
53     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
54         this.yangModelMap.putAll(yangResourceNameToContent);
55         return this;
56     }
57
58     public YangTextSchemaSourceSet build() throws ReactorException, IOException, YangSyntaxErrorException {
59         final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
60         return new YangTextSchemaSourceSetImpl(schemaContext);
61     }
62
63     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent)
64             throws ReactorException, IOException, YangSyntaxErrorException {
65         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
66     }
67
68     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
69
70         private final SchemaContext schemaContext;
71
72         public YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
73             this.schemaContext = schemaContext;
74         }
75
76         @Override
77         public SchemaContext getSchemaContext() {
78             return schemaContext;
79         }
80     }
81
82     /**
83      * Parse and validate a string representing a yang model to generate a SchemaContext context.
84      *
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
88      */
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));
95         }
96         return reactor.buildEffective();
97     }
98
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());
103     }
104
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) {
110             @Override
111             protected MoreObjects.ToStringHelper addToStringAttributes(
112                     final MoreObjects.ToStringHelper toStringHelper) {
113                 return toStringHelper;
114             }
115
116             @Override
117             public InputStream openStream() {
118                 return new ByteArrayInputStream(source.getBytes());
119             }
120         };
121     }
122 }