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() throws SourceException, ReactorException {
104 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
106 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
107 MULTIPLE_AUGMENT_SUBMODULE);
109 EffectiveSchemaContext result = null;
110 result = reactor.buildEffective();
111 assertNotNull(result);
115 public void multipleAugmentTest() throws SourceException, ReactorException {
116 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
118 addSources(reactor, MULTIPLE_AUGMENT);
120 EffectiveSchemaContext result = null;
121 result = reactor.buildEffective();
122 assertNotNull(result);
125 @Test(expected = SomeModifiersUnresolvedException.class)
126 public void multipleAugmentIncorrectPathTest() throws SourceException, ReactorException {
127 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
129 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
131 EffectiveSchemaContext result = null;
132 result = reactor.buildEffective();
136 @Test(expected = SomeModifiersUnresolvedException.class)
137 public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException, ReactorException {
138 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
140 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
141 EffectiveSchemaContext result = null;
142 result = reactor.buildEffective();
147 public void readAndParseYangFileTest() throws SourceException, ReactorException {
148 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
150 addSources(reactor, AUGMENTED, ROOT);
152 final EffectiveSchemaContext root = reactor.buildEffective();
155 final Module augmentedModule = root.findModuleByName("augmented", null);
156 assertNotNull(augmentedModule);
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);
166 assertNotNull(targetContNode.getChildNodes());
167 assertEquals(3, targetContNode.getChildNodes().size());
169 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
170 .getDataChildByName(contAdded1);
171 assertNotNull(contAdded1Node);
172 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
173 .getDataChildByName(list1);
174 assertNotNull(list1Node);
176 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
177 .getDataChildByName(contAdded2);
178 assertNotNull(contAdded2Node);
179 final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
180 .getDataChildByName(axml);
181 assertNotNull(axmlNode);
183 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
184 .getDataChildByName(contGrp);
185 assertNotNull(contGrpNode);
186 final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
187 .getDataChildByName(axmlGrp);
188 assertNotNull(axmlGrpNode);
190 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
191 .getDataChildByName(augCont1);
192 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
193 .getDataChildByName(augCont2);
194 assertNotNull(augCont2Node);
196 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
197 .getDataChildByName(grpCont2);
198 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
199 .getDataChildByName(grpCont22);
200 assertNotNull(grpCont22Node);
202 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
203 .getDataChildByName(grpAdd);
204 assertNotNull(grpAddNode);
207 private static <T extends ModelStatement<?>> T findInStatements(final QName target,
208 final ImmutableList<T> statements) {
210 for (final T statement : statements) {
212 .equals(statement.statementDefinition().getArgumentName())) {
220 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
221 final StatementStreamSource... sources) {
222 for (final StatementStreamSource source : sources) {
223 reactor.addSource(source);
228 public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException,
231 final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
233 assertNotNull(context);
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";
239 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
241 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
243 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
245 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
247 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
248 .create(ns, rev, "lock-type"));
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());