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 / AugmentProcessTest.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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.net.URISyntaxException;
18
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import java.io.FileNotFoundException;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import com.google.common.collect.ImmutableList;
25 import java.net.URI;
26 import org.junit.Test;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
42 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
43
44 public class AugmentProcessTest {
45
46     private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
47             "/stmt-test/augments/augmented.yang", false);
48     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
49             "/stmt-test/augments/aug-root.yang", false);
50
51     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
52             URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
53     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
54             .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
55
56     private static GroupingDefinition grp2Def;
57
58     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
59             "aug-parent1");
60     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
61             "aug-parent2");
62     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
63             "cont-target");
64
65     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
66             "cont-added1");
67     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
68             "cont-added2");
69
70     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
71     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
72
73     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
74             "cont-grp");
75     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
76             "axml-grp");
77
78     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
79     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
80
81     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
82     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
83             "grp-cont22");
84     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
85
86     private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
87             "/stmt-test/augments/multiple-augment-test.yang",false);
88
89     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_ROOT = new YangStatementSourceImpl(
90             "/stmt-test/augments/multiple-augment-root.yang",false);
91     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_IMPORTED = new YangStatementSourceImpl(
92             "/stmt-test/augments/multiple-augment-imported.yang",false);
93     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_SUBMODULE = new YangStatementSourceImpl(
94             "/stmt-test/augments/multiple-augment-submodule.yang",false);
95
96     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
97             "/stmt-test/augments/multiple-augment-incorrect.yang",false);
98
99     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
100             "/stmt-test/augments/multiple-augment-incorrect2.yang",false);
101
102     @Test
103     public void multipleAugmentsAndMultipleModulesTest()
104             throws SourceException, ReactorException {
105         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
106                 .newBuild();
107         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
108                 MULTIPLE_AUGMENT_SUBMODULE);
109
110         EffectiveSchemaContext result = null;
111         try {
112             result = reactor.buildEffective();
113         } catch (Exception e) {
114             log(e, "");
115         }
116         assertNotNull(result);
117     }
118
119     @Test
120     public void multipleAugmentTest() throws SourceException, ReactorException {
121         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
122                 .newBuild();
123         addSources(reactor, MULTIPLE_AUGMENT);
124
125         EffectiveSchemaContext result = null;
126         try {
127             result = reactor.buildEffective();
128         } catch (Exception e) {
129             log(e, "");
130         }
131         assertNotNull(result);
132     }
133
134     @Test
135     public void multipleAugmentIncorrectPathTest() throws SourceException,
136             ReactorException {
137         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
138                 .newBuild();
139         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
140
141         EffectiveSchemaContext result = null;
142         try {
143             result = reactor.buildEffective();
144         } catch (Exception e) {
145             log(e, "");
146         }
147
148         assertNull(result);
149     }
150
151     @Test
152     public void multipleAugmentIncorrectPathAndGrpTest()
153             throws SourceException, ReactorException {
154         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
155                 .newBuild();
156         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
157
158         EffectiveSchemaContext result = null;
159         try {
160             result = reactor.buildEffective();
161         } catch (Exception e) {
162             log(e, "");
163         }
164
165         assertNull(result);
166     }
167
168     private void log(final Throwable e, final String indent) {
169         System.out.println(indent + e.getMessage());
170
171         Throwable[] suppressed = e.getSuppressed();
172         for (Throwable throwable : suppressed) {
173             log(throwable, indent + "        ");
174         }
175     }
176
177     @Test
178     public void readAndParseYangFileTest() throws SourceException,
179             ReactorException {
180
181         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
182                 .newBuild();
183         addSources(reactor, AUGMENTED, ROOT);
184
185         final EffectiveSchemaContext root = reactor.buildEffective();
186         assertNotNull(root);
187
188         Module augmentedModule = root.findModuleByName("augmented", null);
189         assertNotNull(augmentedModule);
190
191         ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
192                 .getDataChildByName(augParent1);
193         ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
194                 .getDataChildByName(augParent2);
195         ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
196                 .getDataChildByName(contTarget);
197         assertNotNull(targetContNode);
198
199         assertNotNull(targetContNode.getChildNodes());
200         assertEquals(3, targetContNode.getChildNodes().size());
201
202         ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
203                 .getDataChildByName(contAdded1);
204         assertNotNull(contAdded1Node);
205         ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
206                 .getDataChildByName(list1);
207         assertNotNull(list1Node);
208
209         ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
210                 .getDataChildByName(contAdded2);
211         assertNotNull(contAdded2Node);
212         AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
213                 .getDataChildByName(axml);
214         assertNotNull(axmlNode);
215
216         ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
217                 .getDataChildByName(contGrp);
218         assertNotNull(contGrpNode);
219         AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
220                 .getDataChildByName(axmlGrp);
221         assertNotNull(axmlGrpNode);
222
223         ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
224                 .getDataChildByName(augCont1);
225         ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
226                 .getDataChildByName(augCont2);
227         assertNotNull(augCont2Node);
228
229         ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
230                 .getDataChildByName(grpCont2);
231         ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
232                 .getDataChildByName(grpCont22);
233         assertNotNull(grpCont22Node);
234
235         ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
236                 .getDataChildByName(grpAdd);
237         assertNotNull(grpAddNode);
238     }
239
240     private static <T extends ModelStatement<?>> T findInStatements(final QName target,
241             final ImmutableList<T> statements) {
242
243         for (final T statement : statements) {
244             if (target
245                     .equals(statement.statementDefinition().getArgumentName())) {
246                 return statement;
247             }
248         }
249
250         return null;
251     }
252
253     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
254             final StatementStreamSource... sources) {
255         for (StatementStreamSource source : sources) {
256             reactor.addSource(source);
257         }
258     }
259
260     @Test
261     public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
262
263         SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
264
265         assertNotNull(context);
266
267         ContainerSchemaNode netconf =  (ContainerSchemaNode) context.getDataChildByName("netconf-state");
268         ContainerSchemaNode datastores =  (ContainerSchemaNode) netconf.getDataChildByName("datastores");
269         ListSchemaNode datastore =  (ListSchemaNode) datastores.getDataChildByName("datastore");
270         ContainerSchemaNode locks =  (ContainerSchemaNode) datastore.getDataChildByName("locks");
271         ChoiceSchemaNode lockType =  (ChoiceSchemaNode)locks.getDataChildByName("lock-type");
272
273         ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
274         assertTrue(leafAugCase.isAugmenting());
275         DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case");
276         assertFalse(leafAug.isAugmenting());
277     }
278
279 }