2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.test.util;
11 import com.google.common.annotations.Beta;
13 import java.io.FileFilter;
14 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.ServiceLoader;
24 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.YangConstants;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
34 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
35 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
38 * Utility class which provides convenience methods for producing effective schema context based on the supplied
39 * yang/yin sources or paths to these sources.
42 public final class YangParserTestUtils {
44 private static final FileFilter YANG_FILE_FILTER = file -> {
45 final String name = file.getName().toLowerCase();
46 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
49 private static final @NonNull YangParserFactory PARSER_FACTORY;
52 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
54 throw new IllegalStateException("No YangParserFactory found");
56 PARSER_FACTORY = it.next();
59 private YangParserTestUtils() {
60 throw new UnsupportedOperationException("Utility class should not be instantiated.");
64 * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
65 * default mode and all YANG features are supported.
67 * @param resource relative path to the YANG file to be parsed
69 * @return effective schema context
71 public static SchemaContext parseYangResource(final String resource) {
72 return parseYangResource(resource, StatementParserMode.DEFAULT_MODE);
76 * Creates a new effective schema context containing the specified YANG source. All YANG features are supported.
78 * @param resource relative path to the YANG file to be parsed
79 * @param parserMode mode of statement parser
80 * @return effective schema context
82 public static SchemaContext parseYangResource(final String resource, final StatementParserMode parserMode) {
83 return parseYangResource(resource, parserMode, null);
87 * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
90 * @param resource relative path to the YANG file to be parsed
91 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
93 * @return effective schema context
95 public static SchemaContext parseYangResource(final String resource, final Set<QName> supportedFeatures) {
96 return parseYangResource(resource, StatementParserMode.DEFAULT_MODE, supportedFeatures);
100 * Creates a new effective schema context containing the specified YANG source.
102 * @param resource relative path to the YANG file to be parsed
103 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
105 * @param parserMode mode of statement parser
106 * @return effective schema context
108 public static SchemaContext parseYangResource(final String resource, final StatementParserMode parserMode,
109 final Set<QName> supportedFeatures) {
110 final YangTextSchemaSource source = YangTextSchemaSource.forResource(YangParserTestUtils.class, resource);
111 return parseYangSources(parserMode, supportedFeatures, source);
115 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
116 * default mode and all YANG features are supported.
118 * @param files YANG files to be parsed
119 * @return effective schema context
121 public static SchemaContext parseYangFiles(final File... files) {
122 return parseYangFiles(Arrays.asList(files));
126 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
127 * default mode and all YANG features are supported.
129 * @param files collection of YANG files to be parsed
130 * @return effective schema context
132 public static SchemaContext parseYangFiles(final Collection<File> files) {
133 return parseYangFiles(StatementParserMode.DEFAULT_MODE, files);
137 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
140 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
141 * models are resolved
142 * @param files YANG files to be parsed
143 * @return effective schema context
145 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures, final File... files) {
146 return parseYangFiles(supportedFeatures, Arrays.asList(files));
149 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures, final Collection<File> files) {
150 return parseYangFiles(supportedFeatures, StatementParserMode.DEFAULT_MODE, files);
154 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
156 * @param parserMode mode of statement parser
157 * @param files YANG files to be parsed
158 * @return effective schema context
160 public static SchemaContext parseYangFiles(final StatementParserMode parserMode, final File... files) {
161 return parseYangFiles(parserMode, Arrays.asList(files));
165 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
167 * @param parserMode mode of statement parser
168 * @param files collection of YANG files to be parsed
169 * @return effective schema context
171 public static SchemaContext parseYangFiles(final StatementParserMode parserMode, final Collection<File> files) {
172 return parseYangFiles(null, parserMode, files);
176 * Creates a new effective schema context containing the specified YANG sources.
178 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
179 * models are resolved
180 * @param parserMode mode of statement parser
181 * @param files YANG files to be parsed
182 * @return effective schema context
184 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures,
185 final StatementParserMode parserMode, final File... files) {
186 return parseYangFiles(supportedFeatures, parserMode, Arrays.asList(files));
190 * Creates a new effective schema context containing the specified YANG sources.
192 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
193 * models are resolved
194 * @param parserMode mode of statement parser
195 * @param files YANG files to be parsed
196 * @return effective schema context
198 public static SchemaContext parseYangFiles(final Set<QName> supportedFeatures,
199 final StatementParserMode parserMode, final Collection<File> files) {
200 return parseSources(parserMode, supportedFeatures,
201 files.stream().map(YangTextSchemaSource::forFile).collect(Collectors.toList()));
205 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
206 * default mode and all YANG features are supported.
208 * @param resourcePath relative path to the directory with YANG files to be parsed
209 * @return effective schema context
211 public static SchemaContext parseYangResourceDirectory(final String resourcePath) {
212 return parseYangResourceDirectory(resourcePath, StatementParserMode.DEFAULT_MODE);
216 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
218 * @param resourcePath relative path to the directory with YANG files to be parsed
219 * @param parserMode mode of statement parser
220 * @return effective schema context
222 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
223 final StatementParserMode parserMode) {
224 return parseYangResourceDirectory(resourcePath, null, parserMode);
228 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
231 * @param resourcePath relative path to the directory with YANG files to be parsed
232 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
233 * models are resolved
234 * @return effective schema context
236 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
237 final Set<QName> supportedFeatures) {
238 return parseYangResourceDirectory(resourcePath, supportedFeatures, StatementParserMode.DEFAULT_MODE);
242 * Creates a new effective schema context containing the specified YANG sources.
244 * @param resourcePath relative path to the directory with YANG files to be parsed
245 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
246 * models are resolved
247 * @param parserMode mode of statement parser
248 * @return effective schema context
250 public static SchemaContext parseYangResourceDirectory(final String resourcePath,
251 final Set<QName> supportedFeatures, final StatementParserMode parserMode) {
252 final URI directoryPath;
254 directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
255 } catch (URISyntaxException e) {
256 throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
258 return parseYangFiles(supportedFeatures, parserMode, new File(directoryPath).listFiles(YANG_FILE_FILTER));
262 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
263 * default mode and all YANG features are supported.
265 * @param clazz Resource lookup base
266 * @param resources Resource names to be looked up
267 * @return effective schema context
269 public static SchemaContext parseYangResources(final Class<?> clazz, final String... resources) {
270 return parseYangResources(clazz, Arrays.asList(resources));
273 public static SchemaContext parseYangResources(final Class<?> clazz, final Collection<String> resources) {
274 final List<YangTextSchemaSource> sources = new ArrayList<>(resources.size());
275 for (final String r : resources) {
276 sources.add(YangTextSchemaSource.forResource(clazz, r));
278 return parseSources(StatementParserMode.DEFAULT_MODE, null, sources);
282 * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
285 * @param yangDirs relative paths to the directories containing YANG files to be parsed
286 * @param yangFiles relative paths to the YANG files to be parsed
287 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
288 * models are resolved
289 * @return effective schema context
291 public static SchemaContext parseYangResources(final List<String> yangDirs, final List<String> yangFiles,
292 final Set<QName> supportedFeatures) {
293 return parseYangResources(yangDirs, yangFiles, supportedFeatures, StatementParserMode.DEFAULT_MODE);
297 * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
299 * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
300 * @param yangResources relative paths to the YANG files to be parsed
301 * @param statementParserMode mode of statement parser
302 * @return effective schema context
304 public static SchemaContext parseYangResources(final List<String> yangResourceDirs,
305 final List<String> yangResources, final StatementParserMode statementParserMode) {
306 return parseYangResources(yangResourceDirs, yangResources, null, statementParserMode);
310 * Creates a new effective schema context containing the specified YANG sources.
312 * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
313 * @param yangResources relative paths to the YANG files to be parsed
314 * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
315 * models are resolved
316 * @param statementParserMode mode of statement parser
317 * @return effective schema context
319 public static SchemaContext parseYangResources(final List<String> yangResourceDirs,
320 final List<String> yangResources, final Set<QName> supportedFeatures,
321 final StatementParserMode statementParserMode) {
322 final List<File> allYangFiles = new ArrayList<>();
323 for (final String yangDir : yangResourceDirs) {
324 allYangFiles.addAll(getYangFiles(yangDir));
327 for (final String yangFile : yangResources) {
329 allYangFiles.add(new File(YangParserTestUtils.class.getResource(yangFile).toURI()));
330 } catch (URISyntaxException e) {
331 throw new IllegalArgumentException("Invalid resource " + yangFile, e);
335 return parseYangFiles(supportedFeatures, statementParserMode, allYangFiles);
338 public static SchemaContext parseYangSources(final StatementParserMode parserMode,
339 final Set<QName> supportedFeatures, final YangTextSchemaSource... sources) {
340 return parseSources(parserMode, supportedFeatures, Arrays.asList(sources));
343 public static SchemaContext parseSources(final StatementParserMode parserMode, final Set<QName> supportedFeatures,
344 final Collection<? extends SchemaSourceRepresentation> sources) {
345 final YangParser parser = PARSER_FACTORY.createParser(parserMode);
346 if (supportedFeatures != null) {
347 parser.setSupportedFeatures(supportedFeatures);
351 parser.addSources(sources);
352 } catch (YangSyntaxErrorException e) {
353 throw new IllegalArgumentException("Malformed source", e);
354 } catch (IOException e) {
355 throw new IllegalArgumentException("Failed to read a source", e);
359 return parser.buildSchemaContext();
360 } catch (YangParserException e) {
361 throw new IllegalStateException("Failed to assemble SchemaContext", e);
365 private static Collection<File> getYangFiles(final String resourcePath) {
366 final URI directoryPath;
368 directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
369 } catch (URISyntaxException e) {
370 throw new IllegalArgumentException("Failed to open resource directory " + resourcePath, e);
372 return Arrays.asList(new File(directoryPath).listFiles(YANG_FILE_FILTER));