2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.test.util;
11 import com.google.common.annotations.Beta;
13 import java.io.FileFilter;
14 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
22 import java.util.stream.Collectors;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
30 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
31 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
37 * Utility class which provides convenience methods for producing effective schema context based on the supplied
38 * yang/yin sources or paths to these sources.
41 public final class YangParserTestUtils {
43 private static final FileFilter YANG_FILE_FILTER = file -> {
44 final String name = file.getName().toLowerCase();
45 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
48 private YangParserTestUtils() {
49 throw new UnsupportedOperationException("Utility class should not be instantiated.");
53 * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
54 * default mode and all YANG features are supported.
56 * @param resource relative path to the YANG file to be parsed
58 * @return effective schema context
60 public static SchemaContext parseYangResource(final String resource) {
61 return parseYangResource(resource, StatementParserMode.DEFAULT_MODE);
65 * Creates a new effective schema context containing the specified YANG source. All YANG features are supported.
67 * @param resource relative path to the YANG file to be parsed
68 * @param parserMode mode of statement parser
69 * @return effective schema context
71 public static SchemaContext parseYangResource(final String resource, final StatementParserMode parserMode) {
72 return parseYangResource(resource, parserMode, null);
76 * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
79 * @param resource relative path to the YANG file to be parsed
80 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
82 * @return effective schema context
84 public static SchemaContext parseYangResource(final String resource, final Set<QName> supportedFeatures) {
85 return parseYangResource(resource, StatementParserMode.DEFAULT_MODE, supportedFeatures);
89 * Creates a new effective schema context containing the specified YANG source.
91 * @param resource relative path to the YANG file to be parsed
92 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
94 * @param parserMode mode of statement parser
95 * @return effective schema context
97 public static SchemaContext parseYangResource(final String resource, final StatementParserMode parserMode,
98 final Set<QName> supportedFeatures) {
99 final YangTextSchemaSource source = YangTextSchemaSource.forResource(YangParserTestUtils.class, resource);
100 return parseYangSources(parserMode, supportedFeatures, source);
104 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
105 * default mode and all YANG features are supported.
107 * @param files YANG files to be parsed
108 * @return effective schema context
110 public static SchemaContext parseYangFiles(final File... files) {
111 return parseYangFiles(Arrays.asList(files));
115 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
116 * default mode and all YANG features are supported.
118 * @param files collection of YANG files to be parsed
119 * @return effective schema context
121 public static SchemaContext parseYangFiles(final Collection<File> files) {
122 return parseYangFiles(StatementParserMode.DEFAULT_MODE, files);
126 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
129 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
130 * models are resolved
131 * @param files YANG files to be parsed
132 * @return effective schema context
134 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures, final File... files) {
135 return parseYangFiles(supportedFeatures, Arrays.asList(files));
138 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures, final Collection<File> files) {
139 return parseYangFiles(supportedFeatures, StatementParserMode.DEFAULT_MODE, files);
143 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
145 * @param parserMode mode of statement parser
146 * @param files YANG files to be parsed
147 * @return effective schema context
149 public static SchemaContext parseYangFiles(final StatementParserMode parserMode, final File... files) {
150 return parseYangFiles(parserMode, Arrays.asList(files));
154 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
156 * @param parserMode mode of statement parser
157 * @param files collection of YANG files to be parsed
158 * @return effective schema context
160 public static SchemaContext parseYangFiles(final StatementParserMode parserMode, final Collection<File> files) {
161 return parseYangFiles(null, parserMode, files);
165 * Creates a new effective schema context containing the specified YANG sources.
167 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
168 * models are resolved
169 * @param parserMode mode of statement parser
170 * @param files YANG files to be parsed
171 * @return effective schema context
173 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures,
174 final StatementParserMode parserMode, final File... files) {
175 return parseYangFiles(supportedFeatures, parserMode, Arrays.asList(files));
179 * Creates a new effective schema context containing the specified YANG sources.
181 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
182 * models are resolved
183 * @param parserMode mode of statement parser
184 * @param files YANG files to be parsed
185 * @return effective schema context
187 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures,
188 final StatementParserMode parserMode, final Collection<File> files) {
189 return parseYangSources(supportedFeatures, parserMode,
190 files.stream().map(YangTextSchemaSource::forFile).collect(Collectors.toList()));
194 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
195 * default mode and all YANG features are supported.
197 * @param resourcePath relative path to the directory with YANG files to be parsed
198 * @return effective schema context
200 public static SchemaContext parseYangResourceDirectory(final String resourcePath) {
201 return parseYangResourceDirectory(resourcePath, StatementParserMode.DEFAULT_MODE);
205 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
207 * @param resourcePath relative path to the directory with YANG files to be parsed
208 * @param parserMode mode of statement parser
209 * @return effective schema context
211 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
212 final StatementParserMode parserMode) {
213 return parseYangResourceDirectory(resourcePath, null, parserMode);
217 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
220 * @param resourcePath relative path to the directory with YANG files to be parsed
221 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
222 * models are resolved
223 * @return effective schema context
225 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
226 final Set<QName> supportedFeatures) {
227 return parseYangResourceDirectory(resourcePath, supportedFeatures, StatementParserMode.DEFAULT_MODE);
231 * Creates a new effective schema context containing the specified YANG sources.
233 * @param resourcePath relative path to the directory with YANG files to be parsed
234 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
235 * models are resolved
236 * @param parserMode mode of statement parser
237 * @return effective schema context
239 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
240 final Set<QName> supportedFeatures, final StatementParserMode parserMode) {
241 final URI directoryPath;
243 directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
244 } catch (URISyntaxException e) {
245 throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
247 return parseYangFiles(supportedFeatures, parserMode, new File(directoryPath).listFiles(YANG_FILE_FILTER));
251 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
252 * default mode and all YANG features are supported.
254 * @param clazz Resource lookup base
255 * @param resources Resource names to be looked up
256 * @return effective schema context
258 public static SchemaContext parseYangResources(final Class<?> clazz, final String... resources) {
259 return parseYangResources(clazz, Arrays.asList(resources));
262 public static SchemaContext parseYangResources(final Class<?> clazz, final Collection<String> resources) {
263 final List<YangTextSchemaSource> sources = new ArrayList<>(resources.size());
264 for (final String r : resources) {
265 sources.add(YangTextSchemaSource.forResource(clazz, r));
267 return parseYangSources(null, StatementParserMode.DEFAULT_MODE, sources);
271 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
274 * @param yangDirs relative paths to the directories containing YANG files to be parsed
275 * @param yangFiles relative paths to the YANG files to be parsed
276 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
277 * models are resolved
278 * @return effective schema context
280 public static SchemaContext parseYangResources(final List<String> yangDirs, final List<String> yangFiles,
281 final Set<QName> supportedFeatures) {
282 return parseYangResources(yangDirs, yangFiles, supportedFeatures, StatementParserMode.DEFAULT_MODE);
286 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
288 * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
289 * @param yangResources relative paths to the YANG files to be parsed
290 * @param statementParserMode mode of statement parser
291 * @return effective schema context
293 public static SchemaContext parseYangResources(final List<String> yangResourceDirs,
294 final List<String> yangResources, final StatementParserMode statementParserMode) {
295 return parseYangResources(yangResourceDirs, yangResources, null, statementParserMode);
299 * Creates a new effective schema context containing the specified YANG sources.
301 * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
302 * @param yangResources relative paths to the YANG files to be parsed
303 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
304 * models are resolved
305 * @param statementParserMode mode of statement parser
306 * @return effective schema context
308 public static SchemaContext parseYangResources(final List<String> yangResourceDirs,
309 final List<String> yangResources, final Set<QName> supportedFeatures,
310 final StatementParserMode statementParserMode) {
311 final List<File> allYangFiles = new ArrayList<>();
312 for (final String yangDir : yangResourceDirs) {
313 allYangFiles.addAll(getYangFiles(yangDir));
316 for (final String yangFile : yangResources) {
318 allYangFiles.add(new File(YangParserTestUtils.class.getResource(yangFile).toURI()));
319 } catch (URISyntaxException e) {
320 throw new IllegalArgumentException("Invalid resource " + yangFile, e);
324 return parseYangFiles(supportedFeatures, statementParserMode, allYangFiles);
327 public static SchemaContext parseYangSources(final StatementParserMode parserMode,
328 final Set<QName> supportedFeatures, final YangTextSchemaSource... sources) {
329 return parseYangSources(supportedFeatures, parserMode, Arrays.asList(sources));
332 public static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
333 final StatementParserMode parserMode, final Collection<? extends YangTextSchemaSource> sources) {
334 final Collection<YangStatementStreamSource> streams = new ArrayList<>(sources.size());
335 for (YangTextSchemaSource source : sources) {
337 streams.add(YangStatementStreamSource.create(source));
338 } catch (IOException e) {
339 throw new IllegalArgumentException("Failed to read source " + source, e);
340 } catch (YangSyntaxErrorException e) {
341 throw new IllegalArgumentException("Malformed source " + source, e);
345 return parseSources(supportedFeatures, parserMode, streams);
349 * Creates a new effective schema context containing the specified YIN sources. Statement parser mode is set to
350 * default mode and all YANG features are supported.
352 * @param sources YIN sources to be parsed
353 * @return effective schema context
355 public static SchemaContext parseYinSources(final YinStatementStreamSource... sources) {
356 return parseYinSources(StatementParserMode.DEFAULT_MODE, sources);
360 * Creates a new effective schema context containing the specified YIN sources. Statement parser mode is set to
363 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YIN
364 * models are resolved
365 * @param sources YIN sources to be parsed
367 * @return effective schema context
368 * @throws ReactorException if there is an error in one of the parsed YIN sources
370 public static SchemaContext parseYinSources(final Set<QName> supportedFeatures,
371 final YinStatementStreamSource... sources) throws ReactorException {
372 return parseSources(supportedFeatures, StatementParserMode.DEFAULT_MODE, sources);
376 * Creates a new effective schema context containing the specified YIN sources. All YANG features are supported.
378 * @param statementParserMode mode of statement parser
379 * @param sources YIN sources to be parsed
380 * @return effective schema context
382 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode,
383 final YinStatementStreamSource... sources) {
384 return parseSources(null, statementParserMode, sources);
388 * Creates a new effective schema context containing the specified YANG sources.
390 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
391 * models are resolved
392 * @param parserMode mode of statement parser
393 * @param sources sources to be parsed
395 * @return effective schema context
397 public static SchemaContext parseSources(final Set<QName> supportedFeatures,
398 final StatementParserMode parserMode, final StatementStreamSource... sources) {
399 return parseSources(supportedFeatures, parserMode, Arrays.asList(sources));
403 * Creates a new effective schema context containing the specified YANG sources.
405 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
406 * models are resolved
407 * @param statementParserMode mode of statement parser
408 * @param sources sources to be parsed
410 * @return effective schema context
412 public static SchemaContext parseSources(final Set<QName> supportedFeatures,
413 final StatementParserMode statementParserMode, final Collection<? extends StatementStreamSource> sources) {
414 final BuildAction reactor = DefaultReactors.defaultReactor().newBuild(statementParserMode);
415 if (supportedFeatures != null) {
416 reactor.setSupportedFeatures(supportedFeatures);
418 reactor.addSources(sources);
421 return reactor.buildEffective();
422 } catch (ReactorException e) {
423 throw new IllegalStateException(e);
427 private static Collection<File> getYangFiles(final String resourcePath) {
428 final URI directoryPath;
430 directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
431 } catch (URISyntaxException e) {
432 throw new IllegalArgumentException("Failed to open resource directory " + resourcePath, e);
434 return Arrays.asList(new File(directoryPath).listFiles(YANG_FILE_FILTER));