Use constants for magic numbers in perf tests
[cps.git] / cps-service / src / main / java / org / onap / cps / yang / YangTextSchemaSourceSetBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  Modifications Copyright (C) 2022-2023 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.yang;
23
24 import static com.google.common.base.Preconditions.checkNotNull;
25
26 import com.google.common.base.MoreObjects;
27 import com.google.common.collect.ImmutableMap;
28 import io.micrometer.core.annotation.Timed;
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import lombok.NoArgsConstructor;
40 import org.onap.cps.spi.exceptions.CpsException;
41 import org.onap.cps.spi.exceptions.ModelValidationException;
42 import org.onap.cps.spi.model.ModuleReference;
43 import org.opendaylight.yangtools.yang.common.Revision;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
47 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
48 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
49 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
50 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
53
54 @NoArgsConstructor
55 public final class YangTextSchemaSourceSetBuilder {
56
57     private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN =
58         Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
59
60     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
61
62     /**
63      * Add Yang resource context.
64      *
65      * @param yangResourceNameToContent the resource content
66      * @return this builder
67      */
68     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
69         this.yangModelMap.putAll(yangResourceNameToContent);
70         return this;
71     }
72
73     /**
74      * Build a YangTextSchemaSourceSet.
75      *
76      * @return the YangTextSchemaSourceSet
77      */
78     public YangTextSchemaSourceSet build() {
79         final var schemaContext = generateSchemaContext(yangModelMap.build());
80         return new YangTextSchemaSourceSetImpl(schemaContext);
81     }
82
83     /**
84      * Add yangResourceNameToContent and build a YangTextSchemaSourceSet.
85      *
86      * @param yangResourceNameToContent the resource content
87      * @return the YangTextSchemaSourceSet
88      */
89
90     @Timed(value = "cps.yang.schemasourceset.build", description = "Time taken to build a ODL yang Model")
91     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
92         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
93     }
94
95     /**
96      * Validates if SchemaContext can be successfully built from given yang resources.
97      *
98      * @param yangResourceNameToContent the yang resources as map where key is name and value is content
99      * @throws ModelValidationException if validation fails
100      */
101     public static void validate(final Map<String, String> yangResourceNameToContent) {
102         generateSchemaContext(yangResourceNameToContent);
103     }
104
105     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
106
107         private final SchemaContext schemaContext;
108
109         private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
110             this.schemaContext = schemaContext;
111         }
112
113         @Override
114         public List<ModuleReference> getModuleReferences() {
115             return schemaContext.getModules().stream()
116                 .map(YangTextSchemaSourceSetImpl::toModuleReference)
117                 .collect(Collectors.toList());
118         }
119
120         private static ModuleReference toModuleReference(final Module module) {
121             return ModuleReference.builder()
122                 .moduleName(module.getName())
123                 .namespace(module.getQNameModule().getNamespace().toString())
124                 .revision(module.getRevision().map(Revision::toString).orElse(null))
125                 .build();
126         }
127
128         @Override
129         public SchemaContext getSchemaContext() {
130             return schemaContext;
131         }
132     }
133
134     /**
135      * Parse and validate a string representing a yang model to generate a SchemaContext context.
136      *
137      * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
138      *                                  on yangModelContent as key and the yangModelContent as value.
139      * @return the schema context
140      */
141     private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
142         final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
143         for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
144             final String resourceName = yangTextSchemaSource.getIdentifier().getName();
145             try {
146                 reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
147             } catch (final IOException e) {
148                 throw new CpsException("Failed to read yang resource.",
149                     String.format("Exception occurred on reading resource %s.", resourceName), e);
150             } catch (final YangSyntaxErrorException e) {
151                 throw new ModelValidationException("Yang resource is invalid.",
152                     String.format(
153                             "Yang syntax validation failed for resource %s:%n%s", resourceName, e.getMessage()), e);
154             }
155         }
156         try {
157             return reactor.buildEffective();
158         } catch (final ReactorException e) {
159             final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
160             Collections.sort(resourceNames);
161             throw new ModelValidationException("Invalid schema set.",
162                 String.format("Effective schema context build failed for resources %s.", resourceNames.toString()),
163                 e);
164         }
165     }
166
167     private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
168         return yangResourceNameToContent.entrySet().stream()
169             .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
170             .collect(Collectors.toList());
171     }
172
173     private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
174         final var revisionSourceIdentifier =
175             createIdentifierFromSourceName(checkNotNull(sourceName));
176
177         return new YangTextSchemaSource(revisionSourceIdentifier) {
178             @Override
179             public Optional<String> getSymbolicName() {
180                 return Optional.empty();
181             }
182
183             @Override
184             protected MoreObjects.ToStringHelper addToStringAttributes(
185                 final MoreObjects.ToStringHelper toStringHelper) {
186                 return toStringHelper;
187             }
188
189             @Override
190             public InputStream openStream() {
191                 return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
192             }
193         };
194     }
195
196     private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
197         final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
198         if (matcher.matches()) {
199             return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
200         }
201         return RevisionSourceIdentifier.create(sourceName);
202     }
203 }