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