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