cf334d9a430b8624dd6c4427ca313ec441d41ece
[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 Collection<File> files) throws ReactorException,
126             IOException, YangSyntaxErrorException {
127         return parseYangSources(files, YangParserConfiguration.DEFAULT);
128     }
129
130     public static EffectiveModelContext parseYangSources(final Collection<File> files,
131             final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
132         return parseYangSources(config, null, files.toArray(new File[0]));
133     }
134
135     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
136             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
137         return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
138     }
139
140     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
141             final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
142             YangSyntaxErrorException {
143         return parseYangSources(yangSourcesDirectoryPath, null, config);
144     }
145
146     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
147             final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
148             URISyntaxException, IOException, YangSyntaxErrorException {
149
150         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
151         final File testSourcesDir = new File(resourceDir.toURI());
152
153         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
154     }
155
156     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
157             throws URISyntaxException, SAXException, IOException, ReactorException {
158         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
159     }
160
161     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
162             final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
163             ReactorException {
164         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
165         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
166         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
167         for (int i = 0; i < files.length; i++) {
168             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
169
170             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
171                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
172         }
173
174         return parseYinSources(config, sources);
175     }
176
177     public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
178             final StatementStreamSource... sources) throws ReactorException {
179         return getReactor(config)
180             .newBuild()
181             .addSources(sources)
182             .buildEffective();
183     }
184
185     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
186             final String importedModuleName) {
187         ModuleImport requestedModuleImport = null;
188         for (final ModuleImport moduleImport : rootModule.getImports()) {
189             if (moduleImport.getModuleName().equals(importedModuleName)) {
190                 requestedModuleImport = moduleImport;
191                 break;
192             }
193         }
194
195         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
196                 .orElse(null);
197     }
198
199     private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
200         return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
201             : RFC7950Reactors.defaultReactorBuilder(config).build();
202     }
203 }