BUG-7052: move ModuleIdentifierImpl
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentProcessTest.java
1 /*
2  * Copyright (c) 2016 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;
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 com.google.common.collect.ImmutableList;
18 import java.io.FileNotFoundException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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.meta.SomeModifiersUnresolvedException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
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() throws SourceException, ReactorException {
104         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
105                 .newBuild();
106         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
107                 MULTIPLE_AUGMENT_SUBMODULE);
108
109         EffectiveSchemaContext result = null;
110         result = reactor.buildEffective();
111         assertNotNull(result);
112     }
113
114     @Test
115     public void multipleAugmentTest() throws SourceException, ReactorException {
116         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
117                 .newBuild();
118         addSources(reactor, MULTIPLE_AUGMENT);
119
120         EffectiveSchemaContext result = null;
121         result = reactor.buildEffective();
122         assertNotNull(result);
123     }
124
125     @Test(expected = SomeModifiersUnresolvedException.class)
126     public void multipleAugmentIncorrectPathTest() throws SourceException, ReactorException {
127         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
128                 .newBuild();
129         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
130
131         EffectiveSchemaContext result = null;
132         result = reactor.buildEffective();
133         assertNull(result);
134     }
135
136     @Test(expected = SomeModifiersUnresolvedException.class)
137     public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException, ReactorException {
138         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
139                 .newBuild();
140         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
141         EffectiveSchemaContext result = null;
142         result = reactor.buildEffective();
143         assertNull(result);
144     }
145
146     @Test
147     public void readAndParseYangFileTest() throws SourceException, ReactorException {
148         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
149                 .newBuild();
150         addSources(reactor, AUGMENTED, ROOT);
151
152         final EffectiveSchemaContext root = reactor.buildEffective();
153         assertNotNull(root);
154
155         final Module augmentedModule = root.findModuleByName("augmented", null);
156         assertNotNull(augmentedModule);
157
158         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
159                 .getDataChildByName(augParent1);
160         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
161                 .getDataChildByName(augParent2);
162         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
163                 .getDataChildByName(contTarget);
164         assertNotNull(targetContNode);
165
166         assertNotNull(targetContNode.getChildNodes());
167         assertEquals(3, targetContNode.getChildNodes().size());
168
169         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
170                 .getDataChildByName(contAdded1);
171         assertNotNull(contAdded1Node);
172         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
173                 .getDataChildByName(list1);
174         assertNotNull(list1Node);
175
176         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
177                 .getDataChildByName(contAdded2);
178         assertNotNull(contAdded2Node);
179         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
180                 .getDataChildByName(axml);
181         assertNotNull(axmlNode);
182
183         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
184                 .getDataChildByName(contGrp);
185         assertNotNull(contGrpNode);
186         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
187                 .getDataChildByName(axmlGrp);
188         assertNotNull(axmlGrpNode);
189
190         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
191                 .getDataChildByName(augCont1);
192         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
193                 .getDataChildByName(augCont2);
194         assertNotNull(augCont2Node);
195
196         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
197                 .getDataChildByName(grpCont2);
198         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
199                 .getDataChildByName(grpCont22);
200         assertNotNull(grpCont22Node);
201
202         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
203                 .getDataChildByName(grpAdd);
204         assertNotNull(grpAddNode);
205     }
206
207     private static <T extends ModelStatement<?>> T findInStatements(final QName target,
208             final ImmutableList<T> statements) {
209
210         for (final T statement : statements) {
211             if (target
212                     .equals(statement.statementDefinition().getArgumentName())) {
213                 return statement;
214             }
215         }
216
217         return null;
218     }
219
220     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
221             final StatementStreamSource... sources) {
222         for (final StatementStreamSource source : sources) {
223             reactor.addSource(source);
224         }
225     }
226
227     @Test
228     public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException,
229             URISyntaxException {
230
231         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
232
233         assertNotNull(context);
234
235         final String rev = "2013-07-01";
236         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
237         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
238
239         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
240                 "netconf-state"));
241         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
242                 "datastores"));
243         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
244                 "datastore"));
245         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
246                 "locks"));
247         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
248                 .create(ns, rev, "lock-type"));
249
250         final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
251         assertTrue(leafAugCase.isAugmenting());
252         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
253         assertFalse(leafAug.isAugmenting());
254     }
255
256 }