Bug 2366 - Effective statments impl merge, retest & bugfix
[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.util.ArrayList;
12 import java.util.List;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import java.util.Collection;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
23
24 public class StmtTestUtils {
25
26     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
27
28     private StmtTestUtils() {
29
30     }
31
32     public static void log(Throwable e, String indent) {
33         LOG.debug(indent + e.getMessage());
34
35         Throwable[] suppressed = e.getSuppressed();
36         for (Throwable throwable : suppressed) {
37             log(throwable, indent + "        ");
38         }
39     }
40
41     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
42         List<Module> result = new ArrayList<>();
43         for (Module module : modules) {
44             if (module.getName().equals(moduleName)) {
45                 result.add(module);
46             }
47         }
48         return result;
49     }
50
51     public static void addSources(
52             CrossSourceStatementReactor.BuildAction reactor,
53             YangStatementSourceImpl... sources) {
54         for (YangStatementSourceImpl source : sources) {
55             reactor.addSource(source);
56         }
57     }
58
59     public static void printReferences(Module module, boolean isSubmodule,
60             String indent) {
61         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ")
62                 + module.getName());
63         Set<Module> submodules = module.getSubmodules();
64         for (Module submodule : submodules) {
65             printReferences(submodule, true, indent + "      ");
66             printChilds(submodule.getChildNodes(), indent + "            ");
67         }
68     }
69
70     public static void printChilds(Collection<DataSchemaNode> childNodes,
71             String indent) {
72
73         for (DataSchemaNode child : childNodes) {
74             LOG.debug(indent + "Child "
75                     + child.getQName().getLocalName());
76             if (child instanceof DataNodeContainer) {
77                 printChilds(((DataNodeContainer) child).getChildNodes(), indent
78                         + "      ");
79             }
80         }
81     }
82 }