ae0f2cd3e893e7aa78d109c467c9e038d0d01385
[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.Collections;
28 import java.util.List;
29 import java.util.Map;
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;
46
47 @NoArgsConstructor
48 public final class YangTextSchemaSourceSetBuilder {
49
50     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
51
52     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
53         this.yangModelMap.putAll(yangResourceNameToContent);
54         return this;
55     }
56
57     public YangTextSchemaSourceSet build() {
58         final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
59         return new YangTextSchemaSourceSetImpl(schemaContext);
60     }
61
62     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
63         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
64     }
65
66     /**
67      * Validates if SchemaContext can be successfully built from given yang resources.
68      *
69      * @param yangResourceNameToContent the yang resources as map where key is name and value is content
70      * @throws ModelValidationException if validation fails
71      */
72     public static void validate(final Map<String, String> yangResourceNameToContent) {
73         generateSchemaContext(yangResourceNameToContent);
74     }
75
76     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
77
78         private final SchemaContext schemaContext;
79
80         private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
81             this.schemaContext = schemaContext;
82         }
83
84         @Override
85         public List<ModuleReference> getModuleReferences() {
86             return schemaContext.getModules().stream()
87                 .map(YangTextSchemaSourceSetImpl::toModuleReference)
88                 .collect(Collectors.toList());
89         }
90
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))
96                 .build();
97         }
98
99         @Override
100         public SchemaContext getSchemaContext() {
101             return schemaContext;
102         }
103     }
104
105     /**
106      * Parse and validate a string representing a yang model to generate a SchemaContext context.
107      *
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
111      */
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();
116             try {
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);
124             }
125         }
126         try {
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()),
133                 e);
134         }
135     }
136
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());
141     }
142
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()));
147
148         return new YangTextSchemaSource(revisionSourceIdentifier) {
149             @Override
150             protected MoreObjects.ToStringHelper addToStringAttributes(
151                 final MoreObjects.ToStringHelper toStringHelper) {
152                 return toStringHelper;
153             }
154
155             @Override
156             public InputStream openStream() {
157                 return new ByteArrayInputStream(source.getBytes());
158             }
159         };
160     }
161 }