2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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;
17 import com.google.common.collect.ImmutableList;
18 import java.io.FileNotFoundException;
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;
44 public class AugmentProcessTest {
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);
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);
56 private static GroupingDefinition grp2Def;
58 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
60 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
62 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
65 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
67 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
70 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
71 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
73 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
75 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
78 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
79 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
81 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
82 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
84 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
86 private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
87 "/stmt-test/augments/multiple-augment-test.yang",false);
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);
96 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
97 "/stmt-test/augments/multiple-augment-incorrect.yang",false);
99 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
100 "/stmt-test/augments/multiple-augment-incorrect2.yang",false);
103 public void multipleAugmentsAndMultipleModulesTest()
104 throws SourceException, ReactorException {
105 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
107 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
108 MULTIPLE_AUGMENT_SUBMODULE);
110 EffectiveSchemaContext result = null;
111 result = reactor.buildEffective();
112 assertNotNull(result);
116 public void multipleAugmentTest() throws SourceException, ReactorException {
117 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
119 addSources(reactor, MULTIPLE_AUGMENT);
121 EffectiveSchemaContext result = null;
122 result = reactor.buildEffective();
123 assertNotNull(result);
126 @Test(expected=SomeModifiersUnresolvedException.class)
127 public void multipleAugmentIncorrectPathTest() throws SourceException,
129 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
131 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
133 EffectiveSchemaContext result = null;
134 result = reactor.buildEffective();
138 @Test(expected=SomeModifiersUnresolvedException.class)
139 public void multipleAugmentIncorrectPathAndGrpTest()
140 throws SourceException, ReactorException {
141 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
143 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
144 EffectiveSchemaContext result = null;
145 result = reactor.buildEffective();
152 public void readAndParseYangFileTest() throws SourceException,
155 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
157 addSources(reactor, AUGMENTED, ROOT);
159 final EffectiveSchemaContext root = reactor.buildEffective();
162 final Module augmentedModule = root.findModuleByName("augmented", null);
163 assertNotNull(augmentedModule);
165 final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
166 .getDataChildByName(augParent1);
167 final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
168 .getDataChildByName(augParent2);
169 final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
170 .getDataChildByName(contTarget);
171 assertNotNull(targetContNode);
173 assertNotNull(targetContNode.getChildNodes());
174 assertEquals(3, targetContNode.getChildNodes().size());
176 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
177 .getDataChildByName(contAdded1);
178 assertNotNull(contAdded1Node);
179 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
180 .getDataChildByName(list1);
181 assertNotNull(list1Node);
183 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
184 .getDataChildByName(contAdded2);
185 assertNotNull(contAdded2Node);
186 final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
187 .getDataChildByName(axml);
188 assertNotNull(axmlNode);
190 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
191 .getDataChildByName(contGrp);
192 assertNotNull(contGrpNode);
193 final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
194 .getDataChildByName(axmlGrp);
195 assertNotNull(axmlGrpNode);
197 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
198 .getDataChildByName(augCont1);
199 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
200 .getDataChildByName(augCont2);
201 assertNotNull(augCont2Node);
203 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
204 .getDataChildByName(grpCont2);
205 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
206 .getDataChildByName(grpCont22);
207 assertNotNull(grpCont22Node);
209 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
210 .getDataChildByName(grpAdd);
211 assertNotNull(grpAddNode);
214 private static <T extends ModelStatement<?>> T findInStatements(final QName target,
215 final ImmutableList<T> statements) {
217 for (final T statement : statements) {
219 .equals(statement.statementDefinition().getArgumentName())) {
227 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
228 final StatementStreamSource... sources) {
229 for (final StatementStreamSource source : sources) {
230 reactor.addSource(source);
235 public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException,
238 final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
240 assertNotNull(context);
242 final String rev = "2013-07-01";
243 final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
244 final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
246 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
248 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
250 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
252 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
254 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
255 .create(ns, rev, "lock-type"));
257 final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
258 assertTrue(leafAugCase.isAugmenting());
259 final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
260 assertFalse(leafAug.isAugmenting());