eec1dff6987733821e2004fbbcd66ca931556397
[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.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;
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         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())));
56         }
57         return sources;
58     }
59
60     public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
61         return loadModules(TestUtils.class, resourceDirectory);
62     }
63
64     public static EffectiveModelContext loadModules(final String resourceDirectory,
65             final @Nullable Set<QName> supportedFeatures) throws Exception {
66         return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
67     }
68
69     public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
70             throws Exception {
71         return loadModules(cls, resourceDirectory, null);
72     }
73
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));
80         }
81         return action.buildEffective();
82     }
83
84     public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
85         return parseYangSource(List.of(yangSourceFilePath), null);
86     }
87
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)));
93         }
94         if (supportedFeatures != null) {
95             reactor.setSupportedFeatures(FeatureSet.of(supportedFeatures));
96         }
97         return reactor.buildEffective();
98     }
99
100     public static YangTextSource assertSchemaSource(final String resourcePath) {
101         try {
102             return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
103         } catch (URISyntaxException e) {
104             throw new AssertionError(e);
105         }
106     }
107
108     // FIXME: these remain unaudited
109
110     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
111             throws ReactorException, SAXException, IOException {
112         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
113
114         for (File file : new File(resourceDirectory).listFiles()) {
115             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
116                 new FileYinTextSource(file.toPath()))));
117         }
118
119         return reactor.buildEffective();
120     }
121
122     public static Module loadYinModule(final YinTextSource source) throws ReactorException, SAXException, IOException {
123         return RFC7950Reactors.defaultReactor().newBuild()
124             .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
125             .buildEffective()
126             .getModules().iterator().next();
127     }
128
129     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
130         for (ModuleImport moduleImport : imports) {
131             if (moduleImport.getPrefix().equals(prefix)) {
132                 return moduleImport;
133             }
134         }
135         return null;
136     }
137
138     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
139             final String name) {
140         for (TypeDefinition<?> td : typedefs) {
141             if (td.getQName().getLocalName().equals(name)) {
142                 return td;
143             }
144         }
145         return null;
146     }
147 }