Merge "Direct schema node lookup in SchemaUtils"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / ImportBasicTestStatementSource.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 package org.opendaylight.yangtools.yang.stmt.test;
9
10 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.IMPORT;
11 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.MODULE;
12 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.NAMESPACE;
13 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.*;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
19 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
20 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
25
26 class ImportBasicTestStatementSource implements StatementStreamSource {
27
28     private static final String NS_PREFIX = "urn:org:opendaylight:yangtools:test:";
29
30     private final String name;
31     private final List<String> imports;
32     private StatementWriter writer;
33     private StatementSourceReference REF = new StatementSourceReference() {
34
35         @Override
36         public StatementSource getStatementSource() {
37             return StatementSource.DECLARATION;
38         }
39     };
40
41
42     public ImportBasicTestStatementSource(String name, String... imports) {
43         this.name = name;
44         this.imports = Arrays.asList(imports);
45     }
46
47     @Override
48     public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes)
49             throws SourceException {
50         this.writer = writer;
51         header();
52         extensions();
53         body();
54         end();
55
56     }
57
58
59
60     @Override
61     public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) throws SourceException {
62         this.writer = writer;
63         header().end();
64     }
65
66     @Override
67     public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
68             PrefixToModule prefixes) throws SourceException {
69         this.writer = writer;
70         header();
71         extensions();
72         end();
73
74     }
75
76     protected void extensions() throws SourceException {
77
78     }
79
80     protected void body() throws SourceException {
81 //        stmt(YangVersion).arg("1");end();
82 //        stmt(Description).arg("Here goes description of this module");end();
83 //        stmt(Contact).arg("Here goes our address...");end();
84 //
85 //        stmt(Revision).arg("2015-03-12");
86 //            stmt(Description).arg("This revision brings this and that...");end();
87 //            stmt(Reference).arg("Learn more here...");end();
88 //        end();
89
90         stmt(CONTAINER).arg(name+":my-container-with-prefix");
91 //            stmt(Leaf).arg("MyContainerLeaf");
92 //                stmt(Type).arg("string");end();
93 //                stmt(Config).arg("TRUE");end();
94 //            end();
95 //            stmt(Choice).arg("choose-your-destiny");
96 //                stmt(Default).arg("destiny-one");end();
97 //                stmt(Case).arg("destiny-one");
98 //                    stmt(Leaf).arg("destiny-one-leaf");end();
99 //                end();
100 //                stmt(Case).arg("destiny-two");
101 //                    stmt(Mandatory).arg("true");
102 //                    stmt(Container).arg("InnerContainer");
103 //                        stmt(Leaf).arg("leaf-of-deep");end();
104 //                    end();
105 //                end();
106 //            end();
107 //            stmt(Container).arg("MyContainerInContainer");
108 //                stmt(Must).arg("xpath expression");
109 //                stmt(Leaf).arg("MyContainerInContainerLeaf");
110 //                    stmt(Type).arg("string");end();
111 //                end();
112 //                stmt(Deviation).arg("/tst:test/tst:element");
113 //                    stmt(Deviate).arg("add");end();
114 //                end();
115 //            end();
116         end();
117         stmt(CONTAINER).arg("my-container-without-prefix").end();
118
119         int i = 0;
120         for(String imp : imports)  {
121             i++;
122             stmt(CONTAINER).arg(imp+":my-container-with-imported-prefix"+i).end();
123         }
124
125     }
126
127     ImportBasicTestStatementSource header() throws SourceException {
128         stmt(MODULE).arg(name); {
129             stmt(NAMESPACE).arg(getNamespace()).end();
130             stmt(PREFIX).arg(name).end();
131             stmt(REVISION).arg("2000-01-01").end();
132             for(String imp : imports)  {
133                 stmt(IMPORT).arg(imp);
134                     stmt(PREFIX).arg(imp).end();
135                     stmt(REVISION_DATE).arg("2000-01-01").end();
136                 end();
137             }
138         }
139         return this;
140     }
141
142     private String getNamespace() {
143         return NS_PREFIX + name;
144     }
145
146     protected ImportBasicTestStatementSource arg(String arg) throws SourceException {
147         writer.argumentValue(arg, REF);
148         return this;
149     }
150
151     protected ImportBasicTestStatementSource stmt(Rfc6020Mapping stmt) throws SourceException {
152         writer.startStatement(stmt.getStatementName(), REF);
153         return this;
154     }
155
156     protected ImportBasicTestStatementSource end() throws SourceException {
157         writer.endStatement(REF);
158         return this;
159     }
160
161
162 }