Expose FileYangTextSource
[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.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Set;
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;
38
39 public final class TestUtils {
40     private TestUtils() {
41         // Hidden on purpose
42     }
43
44     public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
45             throws Exception {
46         return loadSources(TestUtils.class, resourceDirectory);
47     }
48
49     public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
50             throws Exception {
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())));
57         }
58         return sources;
59     }
60
61     public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
62         return loadModules(TestUtils.class, resourceDirectory);
63     }
64
65     public static EffectiveModelContext loadModules(final String resourceDirectory,
66             final @Nullable Set<QName> supportedFeatures) throws Exception {
67         return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
68     }
69
70     public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
71             throws Exception {
72         return loadModules(cls, resourceDirectory, null);
73     }
74
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));
81         }
82         return action.buildEffective();
83     }
84
85     public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
86         return parseYangSource(List.of(yangSourceFilePath), null);
87     }
88
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)));
94         }
95         if (supportedFeatures != null) {
96             reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
97         }
98         return reactor.buildEffective();
99     }
100
101     public static YangTextSource assertSchemaSource(final String resourcePath) {
102         return new URLYangTextSource(TestUtils.class.getResource(resourcePath));
103     }
104
105     // FIXME: these remain unaudited
106
107     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
108             throws ReactorException, SAXException, IOException {
109         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
110
111         for (File file : new File(resourceDirectory).listFiles()) {
112             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
113                 new FileYinTextSource(file.toPath()))));
114         }
115
116         return reactor.buildEffective();
117     }
118
119     public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
120         return RFC7950Reactors.defaultReactor().newBuild()
121             .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
122             .buildEffective()
123             .getModules().iterator().next();
124     }
125
126     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
127         for (ModuleImport moduleImport : imports) {
128             if (moduleImport.getPrefix().equals(prefix)) {
129                 return moduleImport;
130             }
131         }
132         return null;
133     }
134
135     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
136             final String name) {
137         for (TypeDefinition<?> td : typedefs) {
138             if (td.getQName().getLocalName().equals(name)) {
139                 return td;
140             }
141         }
142         return null;
143     }
144 }