/* * 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.Set; 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 Set supportedFeatures) throws ReactorException, FileNotFoundException { return parseYangSources(files, StatementParserMode.DEFAULT_MODE, supportedFeatures); } static SchemaContext parseYangSources(final Collection files, final StatementParserMode statementParserMode, final Set supportedFeatures) throws ReactorException, FileNotFoundException { return parseYangSources(supportedFeatures, statementParserMode, files.toArray(new File[files.size()])); } static SchemaContext parseYangSources(final Set supportedFeatures, 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(supportedFeatures, statementParserMode, sources); } static SchemaContext parseYangSources(final Set supportedFeatures, final StatementParserMode statementParserMode, final YangStatementSourceImpl... sources) throws ReactorException { final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild( statementParserMode, supportedFeatures); reactor.addSources(sources); return reactor.buildEffective(); } static SchemaContext parseYangSources(final List yangDirs, final List yangFiles, final Set supportedFeatures) 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, supportedFeatures); } private static Collection getYangFiles(final String yangSourcesDirectoryPath) { final File testSourcesDir = new File(yangSourcesDirectoryPath); return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER)); } }