Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.net.URI;
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;
18 import java.util.Set;
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;
37
38 public final class TestUtils {
39     private TestUtils() {
40         // Hidden on purpose
41     }
42
43     public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
44             throws Exception {
45         return loadSources(TestUtils.class, resourceDirectory);
46     }
47
48     public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
49             throws Exception {
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())));
55         }
56         return sources;
57     }
58
59     public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
60         return loadModules(TestUtils.class, resourceDirectory);
61     }
62
63     public static EffectiveModelContext loadModules(final String resourceDirectory,
64             final @Nullable Set<QName> supportedFeatures) throws Exception {
65         return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
66     }
67
68     public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
69             throws Exception {
70         return loadModules(cls, resourceDirectory, null);
71     }
72
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));
79         }
80         return action.buildEffective();
81     }
82
83     public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
84         return parseYangSource(List.of(yangSourceFilePath), null);
85     }
86
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)));
92         }
93         if (supportedFeatures != null) {
94             reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
95         }
96         return reactor.buildEffective();
97     }
98
99     public static YangTextSource assertSchemaSource(final String resourcePath) {
100         try {
101             return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
102         } catch (URISyntaxException e) {
103             throw new AssertionError(e);
104         }
105     }
106
107     // FIXME: these remain unaudited
108
109     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
110             throws ReactorException, SAXException, IOException {
111         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
112
113         for (File file : new File(resourceDirectory).listFiles()) {
114             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
115                 YinTextSource.forPath(file.toPath()))));
116         }
117
118         return reactor.buildEffective();
119     }
120
121     public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
122         return RFC7950Reactors.defaultReactor().newBuild()
123             .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
124             .buildEffective()
125             .getModules().iterator().next();
126     }
127
128     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
129         for (ModuleImport moduleImport : imports) {
130             if (moduleImport.getPrefix().equals(prefix)) {
131                 return moduleImport;
132             }
133         }
134         return null;
135     }
136
137     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
138             final String name) {
139         for (TypeDefinition<?> td : typedefs) {
140             if (td.getQName().getLocalName().equals(name)) {
141                 return td;
142             }
143         }
144         return null;
145     }
146 }