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