Remove unused parseYangSources() methods
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / StmtTestUtils.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 com.google.common.io.Files;
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
31 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.SAXException;
43
44 public final class StmtTestUtils {
45
46     public static final FileFilter YANG_FILE_FILTER =
47         file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
48
49     public static final FileFilter YIN_FILE_FILTER =
50         file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
51
52     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
53
54     private StmtTestUtils() {
55
56     }
57
58     public static void log(final Throwable exception, final String indent) {
59         LOG.debug("{}{}", indent, exception.getMessage());
60
61         final Throwable[] suppressed = exception.getSuppressed();
62         for (final Throwable throwable : suppressed) {
63             log(throwable, indent + "        ");
64         }
65     }
66
67     public static YangStatementStreamSource sourceForResource(final String resourceName) {
68         try {
69             return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
70                 StmtTestUtils.class.getResource(resourceName).toURI())));
71         } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
72             throw new IllegalArgumentException("Failed to create source", e);
73         }
74     }
75
76     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
77             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
78         return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
79     }
80
81     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
82             final YangParserConfiguration config, final Set<QName> supportedFeatures)
83                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
84         return parseYangSources(config, supportedFeatures,
85             new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
86     }
87
88     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
89             throws ReactorException {
90         return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
91     }
92
93     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
94             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
95         return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
96     }
97
98     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
99             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
100             throws ReactorException {
101         final BuildAction build = getReactor(config).newBuild().addSources(sources);
102         if (supportedFeatures != null) {
103             build.setSupportedFeatures(supportedFeatures);
104         }
105         return build.buildEffective();
106     }
107
108     public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
109             YangSyntaxErrorException {
110         return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
111     }
112
113     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
114             final Set<QName> supportedFeatures, final File... files) throws  ReactorException, IOException,
115             YangSyntaxErrorException {
116
117         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
118         for (File file : files) {
119             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
120         }
121
122         return parseYangSources(config, supportedFeatures, sources);
123     }
124
125     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
126             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
127         return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
128     }
129
130     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
131             final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
132             YangSyntaxErrorException {
133         return parseYangSources(yangSourcesDirectoryPath, null, config);
134     }
135
136     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
137             final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
138             URISyntaxException, IOException, YangSyntaxErrorException {
139
140         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
141         final File testSourcesDir = new File(resourceDir.toURI());
142
143         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
144     }
145
146     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
147             throws URISyntaxException, SAXException, IOException, ReactorException {
148         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
149     }
150
151     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
152             final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
153             ReactorException {
154         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
155         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
156         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
157         for (int i = 0; i < files.length; i++) {
158             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
159
160             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
161                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
162         }
163
164         return parseYinSources(config, sources);
165     }
166
167     public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
168             final StatementStreamSource... sources) throws ReactorException {
169         return getReactor(config)
170             .newBuild()
171             .addSources(sources)
172             .buildEffective();
173     }
174
175     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
176             final String importedModuleName) {
177         ModuleImport requestedModuleImport = null;
178         for (final ModuleImport moduleImport : rootModule.getImports()) {
179             if (moduleImport.getModuleName().equals(importedModuleName)) {
180                 requestedModuleImport = moduleImport;
181                 break;
182             }
183         }
184
185         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
186                 .orElse(null);
187     }
188
189     private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
190         return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
191             : RFC7950Reactors.defaultReactorBuilder(config).build();
192     }
193 }