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
8 package org.opendaylight.yangtools.yang.validator;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.FileFilter;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.nio.charset.StandardCharsets;
19 import java.nio.file.Files;
20 import java.nio.file.Paths;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.ServiceLoader;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.YangConstants;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
36 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
37 import org.opendaylight.yangtools.yang.parser.api.YangParser;
38 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
39 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
40 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
42 final class SystemTestUtils {
44 private static final Pattern MODULE_PATTERN = Pattern.compile("module(.*?)\\{");
45 private static final Pattern WHITESPACES = Pattern.compile("\\s+");
46 private static final @NonNull YangParserFactory PARSER_FACTORY;
49 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
51 throw new IllegalStateException("No YangParserFactory found");
53 PARSER_FACTORY = it.next();
56 private SystemTestUtils() {
60 static final FileFilter YANG_FILE_FILTER = file -> {
61 final String name = file.getName().toLowerCase(Locale.ENGLISH);
62 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
65 static EffectiveModelContext parseYangSources(final List<String> yangLibDirs, final List<String> yangTestFiles,
66 final Set<QName> supportedFeatures, final boolean recursiveSearch,
67 final boolean warnForUnkeyedLists) throws IOException, YangParserException {
69 * Current dir "." should be always present implicitly in the list of
70 * directories where dependencies are searched for
72 if (!yangLibDirs.contains(".")) {
76 final List<File> libFiles = new ArrayList<>();
77 for (final String yangLibDir : yangLibDirs) {
78 libFiles.addAll(getYangFiles(yangLibDir, recursiveSearch));
81 final List<File> testFiles = new ArrayList<>();
82 for (final String yangTestFile : yangTestFiles) {
83 if (!yangTestFile.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
84 testFiles.add(findInFiles(libFiles, yangTestFile));
86 testFiles.add(new File(yangTestFile));
90 return parseYangSources(supportedFeatures, testFiles, libFiles, warnForUnkeyedLists);
93 static EffectiveModelContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles,
94 final List<File> libFiles, final boolean warnForUnkeyedLists) throws IOException, YangParserException {
95 checkArgument(!testFiles.isEmpty(), "No yang sources");
97 final YangParserConfiguration configuration = YangParserConfiguration.builder()
98 .warnForUnkeyedLists(warnForUnkeyedLists).build();
99 final YangParser parser = PARSER_FACTORY.createParser(configuration);
100 if (supportedFeatures != null) {
101 parser.setSupportedFeatures(FeatureSet.of(supportedFeatures));
104 for (File file : testFiles) {
105 parser.addSource(YangTextSource.forPath(file.toPath()));
107 for (File file : libFiles) {
108 parser.addLibSource(YangTextSource.forPath(file.toPath()));
111 return parser.buildEffectiveModel();
114 private static File findInFiles(final List<File> libFiles, final String yangTestFile) throws IOException {
115 for (final File file : libFiles) {
116 if (WHITESPACES.matcher(getModelNameFromFile(file)).replaceAll("").equals(yangTestFile)) {
120 throw new FileNotFoundException("Model with specific module-name does not exist : " + yangTestFile);
123 private static String getModelNameFromFile(final File file) throws IOException {
124 final String fileAsString = readFile(file.getAbsolutePath());
125 final Matcher matcher = MODULE_PATTERN.matcher(fileAsString);
126 return matcher.find() ? matcher.group(1) : "";
129 private static String readFile(final String path) throws IOException {
130 return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
133 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
134 private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath, final boolean recursiveSearch)
135 throws FileNotFoundException {
136 final File testSourcesDir = new File(yangSourcesDirectoryPath);
137 if (!testSourcesDir.isDirectory()) {
138 throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath));
141 return recursiveSearch ? searchYangFiles(testSourcesDir)
142 : Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));
145 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
146 private static List<File> searchYangFiles(final File dir) {
148 checkArgument(dir.isDirectory(), "File %s is not a directory", dir.getPath());
150 final List<File> yangFiles = new ArrayList<>();
151 for (final File file : dir.listFiles()) {
152 if (file.isDirectory()) {
153 yangFiles.addAll(searchYangFiles(file));
154 } else if (file.isFile()
155 && file.getName().toLowerCase(Locale.ENGLISH).endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {