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.parser.system.test;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.function.Predicate;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
26 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
28 class SystemTestUtils {
30 static final FileFilter YANG_FILE_FILTER = new FileFilter() {
32 public boolean accept(final File file) {
33 final String name = file.getName().toLowerCase();
34 return name.endsWith(".yang") && file.isFile();
38 static SchemaContext parseYangSources(final Collection<File> files, final Predicate<QName> isFeatureSupported)
39 throws ReactorException, FileNotFoundException {
40 return parseYangSources(files, StatementParserMode.DEFAULT_MODE, isFeatureSupported);
43 static SchemaContext parseYangSources(final Collection<File> files,
44 final StatementParserMode statementParserMode, final Predicate<QName> isFeatureSupported)
45 throws ReactorException, FileNotFoundException {
46 return parseYangSources(isFeatureSupported, statementParserMode, files.toArray(new File[files.size()]));
49 static SchemaContext parseYangSources(final Predicate<QName> isFeatureSupported,
50 final StatementParserMode statementParserMode, final File... files) throws ReactorException,
51 FileNotFoundException {
52 final YangStatementSourceImpl [] sources = new YangStatementSourceImpl[files.length];
54 for (int i = 0; i < files.length; i++) {
55 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
58 return parseYangSources(isFeatureSupported, statementParserMode, sources);
61 static SchemaContext parseYangSources(final Predicate<QName> isFeatureSupported,
62 final StatementParserMode statementParserMode, final YangStatementSourceImpl... sources)
63 throws ReactorException {
64 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
65 statementParserMode, isFeatureSupported);
66 reactor.addSources(sources);
68 return reactor.buildEffective();
71 static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles,
72 final Predicate<QName> isFeatureSupported) throws FileNotFoundException, ReactorException {
73 final List<File> allYangFiles = new ArrayList<>();
74 for (final String yangDir : yangDirs) {
75 allYangFiles.addAll(getYangFiles(yangDir));
78 for (final String yangFile : yangFiles) {
79 allYangFiles.add(new File(yangFile));
82 return parseYangSources(allYangFiles, isFeatureSupported);
85 private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath) {
86 final File testSourcesDir = new File(yangSourcesDirectoryPath);
87 return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));