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.stmt;
11 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.nio.file.Path;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
27 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
28 import org.opendaylight.yangtools.yang.model.spi.source.YinTextSource;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
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;
36 import org.xml.sax.SAXException;
38 public final class TestUtils {
43 public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
45 return loadSources(TestUtils.class, resourceDirectory);
48 public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
50 final var files = new File(cls.getResource(resourceDirectory).toURI())
51 .listFiles(StmtTestUtils.YANG_FILE_FILTER);
52 final var sources = new ArrayList<StatementStreamSource>(files.length);
53 for (var file : files) {
54 sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
59 public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
60 return loadModules(TestUtils.class, resourceDirectory);
63 public static EffectiveModelContext loadModules(final String resourceDirectory,
64 final @Nullable Set<QName> supportedFeatures) throws Exception {
65 return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
68 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
70 return loadModules(cls, resourceDirectory, null);
73 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory,
74 final @Nullable Set<QName> supportedFeatures) throws Exception {
75 final var action = RFC7950Reactors.defaultReactor().newBuild()
76 .addSources(loadSources(cls, resourceDirectory));
77 if (supportedFeatures != null) {
78 action.setSupportedFeatures(FeatureSet.of(supportedFeatures));
80 return action.buildEffective();
83 public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
84 return parseYangSource(List.of(yangSourceFilePath), null);
87 public static EffectiveModelContext parseYangSource(final List<String> yangSourceFilePath,
88 final @Nullable Set<QName> supportedFeatures) throws Exception {
89 final var reactor = RFC7950Reactors.defaultReactor().newBuild();
90 for (var resourcePath : yangSourceFilePath) {
91 reactor.addSource(YangStatementStreamSource.create(assertSchemaSource(resourcePath)));
93 if (supportedFeatures != null) {
94 reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
96 return reactor.buildEffective();
99 public static YangTextSource assertSchemaSource(final String resourcePath) {
101 return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
102 } catch (URISyntaxException e) {
103 throw new AssertionError(e);
107 // FIXME: these remain unaudited
109 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
110 throws ReactorException, SAXException, IOException {
111 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
113 for (File file : new File(resourceDirectory).listFiles()) {
114 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
115 YinTextSource.forPath(file.toPath()))));
118 return reactor.buildEffective();
121 public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
122 return RFC7950Reactors.defaultReactor().newBuild()
123 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
125 .getModules().iterator().next();
128 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
129 for (ModuleImport moduleImport : imports) {
130 if (moduleImport.getPrefix().equals(prefix)) {
137 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
139 for (TypeDefinition<?> td : typedefs) {
140 if (td.getQName().getLocalName().equals(name)) {