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.validator;
11 import com.google.common.base.Preconditions;
13 import java.io.FileFilter;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.nio.charset.StandardCharsets;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.YangConstants;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
32 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
37 final class SystemTestUtils {
39 private static final Pattern MODULE_PATTERN = Pattern.compile("module(.*?)\\{");
40 private static final Pattern WHITESPACES = Pattern.compile("\\s+");
42 private SystemTestUtils() {
43 throw new UnsupportedOperationException();
46 static final FileFilter YANG_FILE_FILTER = file -> {
47 final String name = file.getName().toLowerCase();
48 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
51 static SchemaContext parseYangSources(final List<String> yangLibDirs, final List<String> yangTestFiles,
52 final Set<QName> supportedFeatures, final boolean recursiveSearch)
53 throws ReactorException, IOException, YangSyntaxErrorException {
55 * Current dir "." should be always present implicitly in the list of
56 * directories where dependencies are searched for
58 if (!yangLibDirs.contains(".")) {
62 final List<File> libFiles = new ArrayList<>();
63 for (final String yangLibDir : yangLibDirs) {
64 libFiles.addAll(getYangFiles(yangLibDir, recursiveSearch));
67 final List<File> testFiles = new ArrayList<>();
68 for (final String yangTestFile : yangTestFiles) {
69 if (!yangTestFile.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
70 testFiles.add(findInFiles(libFiles, yangTestFile));
72 testFiles.add(new File(yangTestFile));
76 return parseYangSources(supportedFeatures, testFiles, libFiles);
79 static SchemaContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles,
80 final List<File> libFiles) throws ReactorException, IOException, YangSyntaxErrorException {
81 final List<StatementStreamSource> testSources = getYangStatementSources(testFiles);
82 final List<StatementStreamSource> libSources = getYangStatementSources(libFiles);
83 return parseYangSources(testSources, libSources, supportedFeatures);
86 static SchemaContext parseYangSources(final List<StatementStreamSource> testSources,
87 final List<StatementStreamSource> libSources, final Set<QName> supportedFeatures) throws ReactorException {
88 Preconditions.checkArgument(testSources != null && !testSources.isEmpty(), "No yang sources");
90 final BuildAction reactor = DefaultReactors.defaultReactor().newBuild()
91 .addLibSources(libSources).addSources(testSources);
93 if (supportedFeatures != null) {
94 reactor.setSupportedFeatures(supportedFeatures);
97 return reactor.buildEffective();
100 private static File findInFiles(final List<File> libFiles, final String yangTestFile) throws IOException {
101 for (final File file : libFiles) {
102 if (WHITESPACES.matcher(getModelNameFromFile(file)).replaceAll("").equals(yangTestFile)) {
106 throw new FileNotFoundException("Model with specific module-name does not exist : " + yangTestFile);
109 private static String getModelNameFromFile(final File file) throws IOException {
110 final String fileAsString = readFile(file.getAbsolutePath());
111 final Matcher matcher = MODULE_PATTERN.matcher(fileAsString);
112 if (matcher.find()) {
113 return matcher.group(1);
119 private static String readFile(final String path) throws IOException {
120 return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
123 private static List<StatementStreamSource> getYangStatementSources(final List<File> yangFiles)
124 throws IOException, YangSyntaxErrorException {
125 final List<StatementStreamSource> yangSources = new ArrayList<>(yangFiles.size());
126 for (final File file : yangFiles) {
127 yangSources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
132 private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath, final boolean recursiveSearch)
133 throws FileNotFoundException {
134 final File testSourcesDir = new File(yangSourcesDirectoryPath);
135 if (!testSourcesDir.isDirectory()) {
136 throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath));
139 return recursiveSearch ? searchYangFiles(testSourcesDir)
140 : Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));
143 private static List<File> searchYangFiles(final File dir) {
144 Preconditions.checkNotNull(dir);
145 Preconditions.checkArgument(dir.isDirectory(), "File %s is not a directory", dir.getPath());
147 final List<File> yangFiles = new ArrayList<>();
148 for (final File file : dir.listFiles()) {
149 if (file.isDirectory()) {
150 yangFiles.addAll(searchYangFiles(file));
151 } else if (file.isFile()
152 && file.getName().toLowerCase().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {