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.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
25 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
26 import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
27 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
28 import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
29 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
37 import org.xml.sax.SAXException;
39 public final class TestUtils {
44 public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
46 return loadSources(TestUtils.class, resourceDirectory);
49 public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
51 // FIXME: use Path instead
52 final var files = new File(cls.getResource(resourceDirectory).toURI())
53 .listFiles(StmtTestUtils.YANG_FILE_FILTER);
54 final var sources = new ArrayList<StatementStreamSource>(files.length);
55 for (var file : files) {
56 sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
61 public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
62 return loadModules(TestUtils.class, resourceDirectory);
65 public static EffectiveModelContext loadModules(final String resourceDirectory,
66 final @Nullable Set<QName> supportedFeatures) throws Exception {
67 return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
70 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
72 return loadModules(cls, resourceDirectory, null);
75 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory,
76 final @Nullable Set<QName> supportedFeatures) throws Exception {
77 final var action = RFC7950Reactors.defaultReactor().newBuild()
78 .addSources(loadSources(cls, resourceDirectory));
79 if (supportedFeatures != null) {
80 action.setSupportedFeatures(FeatureSet.of(supportedFeatures));
82 return action.buildEffective();
85 public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
86 return parseYangSource(List.of(yangSourceFilePath), null);
89 public static EffectiveModelContext parseYangSource(final List<String> yangSourceFilePath,
90 final @Nullable Set<QName> supportedFeatures) throws Exception {
91 final var reactor = RFC7950Reactors.defaultReactor().newBuild();
92 for (var resourcePath : yangSourceFilePath) {
93 reactor.addSource(YangStatementStreamSource.create(assertSchemaSource(resourcePath)));
95 if (supportedFeatures != null) {
96 reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
98 return reactor.buildEffective();
101 public static YangTextSource assertSchemaSource(final String resourcePath) {
102 return new URLYangTextSource(TestUtils.class.getResource(resourcePath));
105 // FIXME: these remain unaudited
107 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
108 throws ReactorException, SAXException, IOException {
109 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
111 for (File file : new File(resourceDirectory).listFiles()) {
112 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
113 new FileYinTextSource(file.toPath()))));
116 return reactor.buildEffective();
119 public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
120 return RFC7950Reactors.defaultReactor().newBuild()
121 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
123 .getModules().iterator().next();
126 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
127 for (ModuleImport moduleImport : imports) {
128 if (moduleImport.getPrefix().equals(prefix)) {
135 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
137 for (TypeDefinition<?> td : typedefs) {
138 if (td.getQName().getLocalName().equals(name)) {