5cbfd6222dc05bf750a2ddee4350a4d626cd985c
[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 static com.google.common.base.Preconditions.checkNotNull;
23
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;
32 import java.util.Map;
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;
49
50 @NoArgsConstructor
51 public final class YangTextSchemaSourceSetBuilder {
52
53     private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN =
54         Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
55
56     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
57
58     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
59         this.yangModelMap.putAll(yangResourceNameToContent);
60         return this;
61     }
62
63     public YangTextSchemaSourceSet build() {
64         final var schemaContext = generateSchemaContext(yangModelMap.build());
65         return new YangTextSchemaSourceSetImpl(schemaContext);
66     }
67
68     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
69         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
70     }
71
72     /**
73      * Validates if SchemaContext can be successfully built from given yang resources.
74      *
75      * @param yangResourceNameToContent the yang resources as map where key is name and value is content
76      * @throws ModelValidationException if validation fails
77      */
78     public static void validate(final Map<String, String> yangResourceNameToContent) {
79         generateSchemaContext(yangResourceNameToContent);
80     }
81
82     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
83
84         private final SchemaContext schemaContext;
85
86         private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
87             this.schemaContext = schemaContext;
88         }
89
90         @Override
91         public List<ExtendedModuleReference> getModuleReferences() {
92             return schemaContext.getModules().stream()
93                 .map(YangTextSchemaSourceSetImpl::toModuleReference)
94                 .collect(Collectors.toList());
95         }
96
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))
102                 .build();
103         }
104
105         @Override
106         public SchemaContext getSchemaContext() {
107             return schemaContext;
108         }
109     }
110
111     /**
112      * Parse and validate a string representing a yang model to generate a SchemaContext context.
113      *
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
117      */
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();
122             try {
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.",
129                     String.format(
130                             "Yang syntax validation failed for resource %s:%n%s", resourceName, e.getMessage()), e);
131             }
132         }
133         try {
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()),
140                 e);
141         }
142     }
143
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());
148     }
149
150     private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
151         final var revisionSourceIdentifier =
152             createIdentifierFromSourceName(checkNotNull(sourceName));
153
154         return new YangTextSchemaSource(revisionSourceIdentifier) {
155             @Override
156             protected MoreObjects.ToStringHelper addToStringAttributes(
157                 final MoreObjects.ToStringHelper toStringHelper) {
158                 return toStringHelper;
159             }
160
161             @Override
162             public InputStream openStream() {
163                 return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
164             }
165         };
166     }
167
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)));
172         }
173         return RevisionSourceIdentifier.create(sourceName);
174     }
175 }