From: Igor Foltin Date: Mon, 19 Dec 2016 15:50:33 +0000 (+0100) Subject: Bug 7395: yang-system-test is unable to access yang files X-Git-Tag: release/carbon~198 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=3c4247bdf49066d73009b3a764ab7aa02a648a87;p=yangtools.git Bug 7395: yang-system-test is unable to access yang files - yang-system-test should now be able to access yang files Change-Id: I49535fdf9d39e66f7578a52491aa90ecfc5bb5a4 Signed-off-by: Igor Foltin --- diff --git a/yang/yang-system-test/pom.xml b/yang/yang-system-test/pom.xml index fa8ea6bd8f..7e889d3b20 100644 --- a/yang/yang-system-test/pom.xml +++ b/yang/yang-system-test/pom.xml @@ -21,9 +21,17 @@ ${project.groupId} - yang-test-util + yang-parser-impl ${project.version} + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + commons-cli commons-cli diff --git a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java index 537a1f8d1a..256ac65a1a 100644 --- a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java +++ b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java @@ -23,10 +23,10 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.log4j.BasicConfigurator; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates; -import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; /** * Main class of Yang parser system test. @@ -77,6 +77,8 @@ public class Main { public static void main(final String[] args) { + BasicConfigurator.configure(); + final HelpFormatter formatter = new HelpFormatter(); final Options options = createOptions(); final CommandLine arguments = parseArguments(args, options, formatter); @@ -113,7 +115,7 @@ public class Main { final Stopwatch stopWatch = Stopwatch.createStarted(); try { - context = YangParserTestUtils.parseYangSources(yangDirs, yangFiles, isFeatureSupported); + context = SystemTestUtils.parseYangSources(yangDirs, yangFiles, isFeatureSupported); } catch (final Exception e) { LOG.log(Level.SEVERE, "Failed to create SchemaContext.", e); System.exit(1); diff --git a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java new file mode 100644 index 0000000000..7ffcfce5c3 --- /dev/null +++ b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.system.test; + +import java.io.File; +import java.io.FileFilter; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.function.Predicate; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; +import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; + +class SystemTestUtils { + + static final FileFilter YANG_FILE_FILTER = new FileFilter() { + @Override + public boolean accept(final File file) { + final String name = file.getName().toLowerCase(); + return name.endsWith(".yang") && file.isFile(); + } + }; + + static SchemaContext parseYangSources(final Collection files, final Predicate isFeatureSupported) + throws ReactorException, FileNotFoundException { + return parseYangSources(files, StatementParserMode.DEFAULT_MODE, isFeatureSupported); + } + + static SchemaContext parseYangSources(final Collection files, + final StatementParserMode statementParserMode, final Predicate isFeatureSupported) + throws ReactorException, FileNotFoundException { + return parseYangSources(isFeatureSupported, statementParserMode, files.toArray(new File[files.size()])); + } + + static SchemaContext parseYangSources(final Predicate isFeatureSupported, + final StatementParserMode statementParserMode, final File... files) throws ReactorException, + FileNotFoundException { + final YangStatementSourceImpl [] sources = new YangStatementSourceImpl[files.length]; + + for (int i = 0; i < files.length; i++) { + sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath())); + } + + return parseYangSources(isFeatureSupported, statementParserMode, sources); + } + + static SchemaContext parseYangSources(final Predicate isFeatureSupported, + final StatementParserMode statementParserMode, final YangStatementSourceImpl... sources) + throws ReactorException { + final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild( + statementParserMode, isFeatureSupported); + reactor.addSources(sources); + + return reactor.buildEffective(); + } + + static SchemaContext parseYangSources(final List yangDirs, final List yangFiles, + final Predicate isFeatureSupported) throws FileNotFoundException, ReactorException { + final List allYangFiles = new ArrayList<>(); + for (final String yangDir : yangDirs) { + allYangFiles.addAll(getYangFiles(yangDir)); + } + + for (final String yangFile : yangFiles) { + allYangFiles.add(new File(yangFile)); + } + + return parseYangSources(allYangFiles, isFeatureSupported); + } + + private static Collection getYangFiles(final String yangSourcesDirectoryPath) { + final File testSourcesDir = new File(yangSourcesDirectoryPath); + return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER)); + } +}