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.source.YinTextSource;
27 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
28 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
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 final var files = new File(cls.getResource(resourceDirectory).toURI())
52 .listFiles(StmtTestUtils.YANG_FILE_FILTER);
53 final var sources = new ArrayList<StatementStreamSource>(files.length);
54 for (var file : files) {
55 sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
60 public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
61 return loadModules(TestUtils.class, resourceDirectory);
64 public static EffectiveModelContext loadModules(final String resourceDirectory,
65 final @Nullable Set<QName> supportedFeatures) throws Exception {
66 return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
69 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
71 return loadModules(cls, resourceDirectory, null);
74 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory,
75 final @Nullable Set<QName> supportedFeatures) throws Exception {
76 final var action = RFC7950Reactors.defaultReactor().newBuild()
77 .addSources(loadSources(cls, resourceDirectory));
78 if (supportedFeatures != null) {
79 action.setSupportedFeatures(FeatureSet.of(supportedFeatures));
81 return action.buildEffective();
84 public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
85 return parseYangSource(List.of(yangSourceFilePath), null);
88 public static EffectiveModelContext parseYangSource(final List<String> yangSourceFilePath,
89 final @Nullable Set<QName> supportedFeatures) throws Exception {
90 final var reactor = RFC7950Reactors.defaultReactor().newBuild();
91 for (var resourcePath : yangSourceFilePath) {
92 reactor.addSource(YangStatementStreamSource.create(assertSchemaSource(resourcePath)));
94 if (supportedFeatures != null) {
95 reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
97 return reactor.buildEffective();
100 public static YangTextSource assertSchemaSource(final String resourcePath) {
102 return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
103 } catch (URISyntaxException e) {
104 throw new AssertionError(e);
108 // FIXME: these remain unaudited
110 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
111 throws ReactorException, SAXException, IOException {
112 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
114 for (File file : new File(resourceDirectory).listFiles()) {
115 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
116 new FileYinTextSource(file.toPath()))));
119 return reactor.buildEffective();
122 public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
123 return RFC7950Reactors.defaultReactor().newBuild()
124 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
126 .getModules().iterator().next();
129 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
130 for (ModuleImport moduleImport : imports) {
131 if (moduleImport.getPrefix().equals(prefix)) {
138 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
140 for (TypeDefinition<?> td : typedefs) {
141 if (td.getQName().getLocalName().equals(name)) {