Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / StmtTestUtils.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.stmt.test;
10
11 import java.net.URISyntaxException;
12
13 import java.net.URL;
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
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.rfc6020.YangInferencePipeline;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import java.util.Collection;
27 import java.util.Set;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
33
34 public class StmtTestUtils {
35
36     private static final Logger LOG = LoggerFactory
37             .getLogger(StmtTestUtils.class);
38
39     private StmtTestUtils() {
40
41     }
42
43     public static void log(Throwable e, String indent) {
44         LOG.debug(indent + e.getMessage());
45
46         Throwable[] suppressed = e.getSuppressed();
47         for (Throwable throwable : suppressed) {
48             log(throwable, indent + "        ");
49         }
50     }
51
52     public static List<Module> findModules(final Set<Module> modules,
53             final String moduleName) {
54         List<Module> result = new ArrayList<>();
55         for (Module module : modules) {
56             if (module.getName().equals(moduleName)) {
57                 result.add(module);
58             }
59         }
60         return result;
61     }
62
63     public static void addSources(
64             CrossSourceStatementReactor.BuildAction reactor,
65             YangStatementSourceImpl... sources) {
66         for (YangStatementSourceImpl source : sources) {
67             reactor.addSource(source);
68         }
69     }
70
71     public static void printReferences(Module module, boolean isSubmodule,
72             String indent) {
73         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ")
74                 + module.getName());
75         Set<Module> submodules = module.getSubmodules();
76         for (Module submodule : submodules) {
77             printReferences(submodule, true, indent + "      ");
78             printChilds(submodule.getChildNodes(), indent + "            ");
79         }
80     }
81
82     public static void printChilds(Collection<DataSchemaNode> childNodes,
83             String indent) {
84
85         for (DataSchemaNode child : childNodes) {
86             LOG.debug(indent + "Child " + child.getQName().getLocalName());
87             if (child instanceof DataNodeContainer) {
88                 printChilds(((DataNodeContainer) child).getChildNodes(), indent
89                         + "      ");
90             }
91         }
92     }
93
94     public static SchemaContext parseYangSources(
95             StatementStreamSource... sources) throws SourceException,
96             ReactorException {
97
98         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
99                 .newBuild();
100         reactor.addSources(sources);
101
102         return reactor.buildEffective();
103     }
104
105     public static SchemaContext parseYangSources(File... files)
106             throws SourceException, ReactorException, FileNotFoundException {
107
108         StatementStreamSource[] sources = new StatementStreamSource[files.length];
109
110         for (int i = 0; i < files.length; i++) {
111             sources[i] = new YangStatementSourceImpl(new FileInputStream(
112                     files[i]));
113         }
114
115         return parseYangSources(sources);
116     }
117
118     public static SchemaContext parseYangSources(Collection<File> files)
119             throws SourceException, ReactorException, FileNotFoundException {
120         return parseYangSources(files.toArray(new File[files.size()]));
121     }
122
123     public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
124             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
125
126         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
127         File testSourcesDir = new File(resourceDir.toURI());
128
129         return parseYangSources(testSourcesDir.listFiles());
130     }
131 }