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;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
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 List<String> yangLibDirs, final List<String> yangTestFiles,
39 final Set<QName> supportedFeatures) throws FileNotFoundException, ReactorException {
41 * Current dir "." should be always present implicitly in the list of
42 * directories where dependencies are searched for
44 if (!yangLibDirs.contains(".")) {
48 final List<File> libFiles = new ArrayList<>();
49 for (final String yangLibDir : yangLibDirs) {
50 libFiles.addAll(getYangFiles(yangLibDir));
53 final List<File> testFiles = new ArrayList<>();
54 for (final String yangTestFile : yangTestFiles) {
55 testFiles.add(new File(yangTestFile));
58 return parseYangSources(supportedFeatures, testFiles, libFiles);
61 static SchemaContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles,
62 final List<File> libFiles) throws FileNotFoundException, ReactorException {
63 final StatementStreamSource[] testSources = getYangStatementSources(testFiles);
64 final StatementStreamSource[] libSources = getYangStatementSources(libFiles);
65 return parseYangSources(testSources, libSources, supportedFeatures);
68 static SchemaContext parseYangSources(final StatementStreamSource[] testSources,
69 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
71 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
72 .newBuild(supportedFeatures);
73 reactor.addSources(testSources);
74 reactor.addLibSources(libSources);
76 return reactor.buildEffective();
79 private static StatementStreamSource[] getYangStatementSources(final List<File> yangFiles)
80 throws FileNotFoundException {
81 final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.size()];
82 for (int i = 0; i < yangFiles.size(); i++) {
83 yangSources[i] = new YangStatementSourceImpl(new NamedFileInputStream(yangFiles.get(i), yangFiles.get(i)
89 private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath) throws FileNotFoundException {
90 final File testSourcesDir = new File(yangSourcesDirectoryPath);
91 if (testSourcesDir == null || !testSourcesDir.isDirectory()) {
92 throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath));
94 return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));