2 * Copyright (c) 2015 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.test;
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 java.net.URISyntaxException;
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;
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.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 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
45 public class AugmentProcessTest {
47 private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
48 "/stmt-test/augments/augmented.yang", false);
49 private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
50 "/stmt-test/augments/aug-root.yang", false);
52 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
53 URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
54 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
55 .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
57 private static GroupingDefinition grp2Def;
59 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
61 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
63 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
66 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
68 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
71 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
72 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
74 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
76 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
79 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
80 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
82 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
83 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
85 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
87 private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
88 "/stmt-test/augments/multiple-augment-test.yang",false);
90 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_ROOT = new YangStatementSourceImpl(
91 "/stmt-test/augments/multiple-augment-root.yang",false);
92 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_IMPORTED = new YangStatementSourceImpl(
93 "/stmt-test/augments/multiple-augment-imported.yang",false);
94 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_SUBMODULE = new YangStatementSourceImpl(
95 "/stmt-test/augments/multiple-augment-submodule.yang",false);
97 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
98 "/stmt-test/augments/multiple-augment-incorrect.yang",false);
100 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
101 "/stmt-test/augments/multiple-augment-incorrect2.yang",false);
104 public void multipleAugmentsAndMultipleModulesTest()
105 throws SourceException, ReactorException {
106 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
108 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
109 MULTIPLE_AUGMENT_SUBMODULE);
111 EffectiveSchemaContext result = null;
112 result = reactor.buildEffective();
113 assertNotNull(result);
117 public void multipleAugmentTest() throws SourceException, ReactorException {
118 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
120 addSources(reactor, MULTIPLE_AUGMENT);
122 EffectiveSchemaContext result = null;
123 result = reactor.buildEffective();
124 assertNotNull(result);
127 @Test(expected=SomeModifiersUnresolvedException.class)
128 public void multipleAugmentIncorrectPathTest() throws SourceException,
130 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
132 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
134 EffectiveSchemaContext result = null;
135 result = reactor.buildEffective();
139 @Test(expected=SomeModifiersUnresolvedException.class)
140 public void multipleAugmentIncorrectPathAndGrpTest()
141 throws SourceException, ReactorException {
142 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
144 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
145 EffectiveSchemaContext result = null;
146 result = reactor.buildEffective();
153 public void readAndParseYangFileTest() throws SourceException,
156 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
158 addSources(reactor, AUGMENTED, ROOT);
160 final EffectiveSchemaContext root = reactor.buildEffective();
163 Module augmentedModule = root.findModuleByName("augmented", null);
164 assertNotNull(augmentedModule);
166 ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
167 .getDataChildByName(augParent1);
168 ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
169 .getDataChildByName(augParent2);
170 ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
171 .getDataChildByName(contTarget);
172 assertNotNull(targetContNode);
174 assertNotNull(targetContNode.getChildNodes());
175 assertEquals(3, targetContNode.getChildNodes().size());
177 ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
178 .getDataChildByName(contAdded1);
179 assertNotNull(contAdded1Node);
180 ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
181 .getDataChildByName(list1);
182 assertNotNull(list1Node);
184 ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
185 .getDataChildByName(contAdded2);
186 assertNotNull(contAdded2Node);
187 AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
188 .getDataChildByName(axml);
189 assertNotNull(axmlNode);
191 ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
192 .getDataChildByName(contGrp);
193 assertNotNull(contGrpNode);
194 AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
195 .getDataChildByName(axmlGrp);
196 assertNotNull(axmlGrpNode);
198 ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
199 .getDataChildByName(augCont1);
200 ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
201 .getDataChildByName(augCont2);
202 assertNotNull(augCont2Node);
204 ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
205 .getDataChildByName(grpCont2);
206 ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
207 .getDataChildByName(grpCont22);
208 assertNotNull(grpCont22Node);
210 ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
211 .getDataChildByName(grpAdd);
212 assertNotNull(grpAddNode);
215 private static <T extends ModelStatement<?>> T findInStatements(final QName target,
216 final ImmutableList<T> statements) {
218 for (final T statement : statements) {
220 .equals(statement.statementDefinition().getArgumentName())) {
228 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
229 final StatementStreamSource... sources) {
230 for (StatementStreamSource source : sources) {
231 reactor.addSource(source);
236 public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
238 SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
240 assertNotNull(context);
242 ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName("netconf-state");
243 ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName("datastores");
244 ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName("datastore");
245 ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName("locks");
246 ChoiceSchemaNode lockType = (ChoiceSchemaNode)locks.getDataChildByName("lock-type");
248 ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
249 assertTrue(leafAugCase.isAugmenting());
250 DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case");
251 assertFalse(leafAug.isAugmenting());