a09f17d4a259593c6f30adad5e717ab3ab446e81
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / TestUtils.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.yangtools.yang.data.impl;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.InputStream;
15 import java.util.Collection;
16 import java.util.List;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
24
25 public class TestUtils {
26
27     private TestUtils() {
28         throw new UnsupportedOperationException("Utility class");
29     }
30
31     public static SchemaContext parseYangSources(StatementStreamSource... sources) throws SourceException,
32             ReactorException {
33
34         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
35         reactor.addSources(sources);
36
37         return reactor.buildEffective();
38     }
39
40     public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException,
41             FileNotFoundException {
42
43         StatementStreamSource[] sources = new StatementStreamSource[files.length];
44
45         for (int i = 0; i < files.length; i++) {
46             sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
47         }
48
49         return parseYangSources(sources);
50     }
51
52     public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException,
53             FileNotFoundException {
54         return parseYangSources(files.toArray(new File[files.size()]));
55     }
56
57     public static SchemaContext parseYangStreams(List<InputStream> streams)
58             throws SourceException, ReactorException {
59
60         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
61                 .newBuild();
62         return reactor.buildEffective(streams);
63     }
64 }