Merge "RTD change to document migration to Spring Boot 3.0"
[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.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import lombok.NoArgsConstructor;
39 import org.onap.cps.spi.exceptions.ModelValidationException;
40 import org.onap.cps.spi.model.ModuleReference;
41 import org.opendaylight.yangtools.yang.common.Revision;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
45 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
46 import org.opendaylight.yangtools.yang.parser.api.YangParser;
47 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
48 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
49 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
50 import org.opendaylight.yangtools.yang.xpath.impl.di.DefaultXPathParserFactory;
51
52 @NoArgsConstructor
53 public final class YangTextSchemaSourceSetBuilder {
54
55     private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN =
56         Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
57
58     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
59
60     private static final YangParserFactory YANG_PARSER_FACTORY =
61             new DefaultYangParserFactory(new DefaultXPathParserFactory());
62
63     /**
64      * Add Yang resource context.
65      *
66      * @param yangResourceNameToContent the resource content
67      * @return this builder
68      */
69     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
70         this.yangModelMap.putAll(yangResourceNameToContent);
71         return this;
72     }
73
74     /**
75      * Build a YangTextSchemaSourceSet.
76      *
77      * @return the YangTextSchemaSourceSet
78      */
79     public YangTextSchemaSourceSet build() {
80         final var schemaContext = generateSchemaContext(yangModelMap.build());
81         return new YangTextSchemaSourceSetImpl(schemaContext);
82     }
83
84     /**
85      * Add yangResourceNameToContent and build a YangTextSchemaSourceSet.
86      *
87      * @param yangResourceNameToContent the resource content
88      * @return the YangTextSchemaSourceSet
89      */
90
91     @Timed(value = "cps.yang.schemasourceset.build", description = "Time taken to build a ODL yang Model")
92     public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
93         return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
94     }
95
96     /**
97      * Validates if SchemaContext can be successfully built from given yang resources.
98      *
99      * @param yangResourceNameToContent the yang resources as map where key is name and value is content
100      * @throws ModelValidationException if validation fails
101      */
102     public static void validate(final Map<String, String> yangResourceNameToContent) {
103         generateSchemaContext(yangResourceNameToContent);
104     }
105
106     private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {
107
108         private final SchemaContext schemaContext;
109
110         private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
111             this.schemaContext = schemaContext;
112         }
113
114         @Override
115         public List<ModuleReference> getModuleReferences() {
116             return schemaContext.getModules().stream()
117                 .map(YangTextSchemaSourceSetImpl::toModuleReference)
118                 .collect(Collectors.toList());
119         }
120
121         private static ModuleReference toModuleReference(final Module module) {
122             return ModuleReference.builder()
123                 .moduleName(module.getName())
124                 .namespace(module.getQNameModule().getNamespace().toString())
125                 .revision(module.getRevision().map(Revision::toString).orElse(null))
126                 .build();
127         }
128
129         @Override
130         public SchemaContext getSchemaContext() {
131             return schemaContext;
132         }
133     }
134
135     /**
136      * Parse and validate a string representing a yang model to generate a SchemaContext context.
137      *
138      * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
139      *                                  on yangModelContent as key and the yangModelContent as value.
140      * @return the schema context
141      */
142     private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
143         final YangParser yangParser = YANG_PARSER_FACTORY.createParser();
144         for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
145             final String resourceName = yangTextSchemaSource.getIdentifier().getName();
146             try {
147                 yangParser.addSource(yangTextSchemaSource);
148             } catch (final Exception exception) {
149                 throw new ModelValidationException("Yang resource processing exception.",
150                     String.format("Could not process resource %s:%n%s", resourceName, exception.getMessage()),
151                     exception);
152             }
153         }
154         try {
155             return yangParser.buildEffectiveModel();
156         } catch (final YangParserException yangParserException) {
157             final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
158             Collections.sort(resourceNames);
159             throw new ModelValidationException("Invalid schema set.",
160                 String.format("Effective schema context build failed for resources %s.", resourceNames),
161                 yangParserException);
162         }
163     }
164
165     private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
166         return yangResourceNameToContent.entrySet().stream()
167             .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
168             .collect(Collectors.toList());
169     }
170
171     private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
172         final var revisionSourceIdentifier =
173             createIdentifierFromSourceName(checkNotNull(sourceName));
174
175         return new YangTextSchemaSource(revisionSourceIdentifier) {
176             @Override
177             public Optional<String> getSymbolicName() {
178                 return Optional.empty();
179             }
180
181             @Override
182             protected MoreObjects.ToStringHelper addToStringAttributes(
183                 final MoreObjects.ToStringHelper toStringHelper) {
184                 return toStringHelper;
185             }
186
187             @Override
188             public InputStream openStream() {
189                 return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
190             }
191         };
192     }
193
194     private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
195         final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
196         if (matcher.matches()) {
197             return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
198         }
199         return RevisionSourceIdentifier.create(sourceName);
200     }
201 }