Create schema set REST API and service level
[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 put(final String fileName, final String content) {
53         this.yangModelMap.put(fileName, content);
54         return this;
55     }
56
57     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
58         this.yangModelMap.putAll(yangResourceNameToContent);
59         return this;
60     }
61
62     public YangTextSchemaSourceSet build() {
63         final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
64         return new YangTextSchemaSourceSetImpl(schemaContext);
65     }
66
67     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
68         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
69     }
70
71     /**
72      * Validates if SchemaContext can be successfully built from given yang resources.
73      *
74      * @param yangResourceNameToContent the yang resources as map where key is name and value is content
75      * @throws ModelValidationException if validation fails
76      */
77     public static void validate(final Map<String, String> yangResourceNameToContent) {
78         generateSchemaContext(yangResourceNameToContent);
79     }
80
81     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
82
83         private final SchemaContext schemaContext;
84
85         private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
86             this.schemaContext = schemaContext;
87         }
88
89         @Override
90         public List<ModuleReference> getModuleReferences() {
91             return schemaContext.getModules().stream()
92                 .map(YangTextSchemaSourceSetImpl::toModuleReference)
93                 .collect(Collectors.toList());
94         }
95
96         private static ModuleReference toModuleReference(final Module module) {
97             return ModuleReference.builder()
98                 .namespace(module.getNamespace().toString())
99                 .revision(module.getRevision().map(Revision::toString).orElse(null))
100                 .build();
101         }
102
103         @Override
104         public SchemaContext getSchemaContext() {
105             return schemaContext;
106         }
107     }
108
109     /**
110      * Parse and validate a string representing a yang model to generate a SchemaContext context.
111      *
112      * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
113      *                                  on yangModelContent as key and the yangModelContent as value.
114      * @return the schema context
115      */
116     private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
117         final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
118         for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
119             final String resourceName = yangTextSchemaSource.getIdentifier().getName();
120             try {
121                 reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
122             } catch (final IOException e) {
123                 throw new CpsException("Failed to read yang resource.",
124                     String.format("Exception occurred on reading resource %s.", resourceName), e);
125             } catch (final YangSyntaxErrorException e) {
126                 throw new ModelValidationException("Yang resource is invalid.",
127                     String.format("Yang syntax validation failed for resource %s.", resourceName), e);
128             }
129         }
130         try {
131             return reactor.buildEffective();
132         } catch (final ReactorException e) {
133             final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
134             Collections.sort(resourceNames);
135             throw new ModelValidationException("Invalid schema set.",
136                 String.format("Effective schema context build failed for resources %s.", resourceNames.toString()),
137                 e);
138         }
139     }
140
141     private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
142         return yangResourceNameToContent.entrySet().stream()
143             .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
144             .collect(Collectors.toList());
145     }
146
147     private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
148         final Map.Entry<String, String> sourceNameParsed = YangNames.parseFilename(sourceName);
149         final RevisionSourceIdentifier revisionSourceIdentifier = RevisionSourceIdentifier
150             .create(sourceNameParsed.getKey(), Revision.ofNullable(sourceNameParsed.getValue()));
151
152         return new YangTextSchemaSource(revisionSourceIdentifier) {
153             @Override
154             protected MoreObjects.ToStringHelper addToStringAttributes(
155                 final MoreObjects.ToStringHelper toStringHelper) {
156                 return toStringHelper;
157             }
158
159             @Override
160             public InputStream openStream() {
161                 return new ByteArrayInputStream(source.getBytes());
162             }
163         };
164     }
165 }