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.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;
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 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
107 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
108 MULTIPLE_AUGMENT_SUBMODULE);
110 EffectiveSchemaContext result = null;
112 result = reactor.buildEffective();
113 } catch (Exception e) {
116 assertNotNull(result);
120 public void multipleAugmentTest() throws SourceException, ReactorException {
121 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
123 addSources(reactor, MULTIPLE_AUGMENT);
125 EffectiveSchemaContext result = null;
127 result = reactor.buildEffective();
128 } catch (Exception e) {
131 assertNotNull(result);
135 public void multipleAugmentIncorrectPathTest() throws SourceException,
137 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
139 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
141 EffectiveSchemaContext result = null;
143 result = reactor.buildEffective();
144 } catch (Exception e) {
152 public void multipleAugmentIncorrectPathAndGrpTest()
153 throws SourceException, ReactorException {
154 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
156 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
158 EffectiveSchemaContext result = null;
160 result = reactor.buildEffective();
161 } catch (Exception e) {
168 private void log(final Throwable e, final String indent) {
169 System.out.println(indent + e.getMessage());
171 Throwable[] suppressed = e.getSuppressed();
172 for (Throwable throwable : suppressed) {
173 log(throwable, indent + " ");
178 public void readAndParseYangFileTest() throws SourceException,
181 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
183 addSources(reactor, AUGMENTED, ROOT);
185 final EffectiveSchemaContext root = reactor.buildEffective();
188 Module augmentedModule = root.findModuleByName("augmented", null);
189 assertNotNull(augmentedModule);
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);
199 assertNotNull(targetContNode.getChildNodes());
200 assertEquals(3, targetContNode.getChildNodes().size());
202 ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
203 .getDataChildByName(contAdded1);
204 assertNotNull(contAdded1Node);
205 ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
206 .getDataChildByName(list1);
207 assertNotNull(list1Node);
209 ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
210 .getDataChildByName(contAdded2);
211 assertNotNull(contAdded2Node);
212 AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
213 .getDataChildByName(axml);
214 assertNotNull(axmlNode);
216 ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
217 .getDataChildByName(contGrp);
218 assertNotNull(contGrpNode);
219 AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
220 .getDataChildByName(axmlGrp);
221 assertNotNull(axmlGrpNode);
223 ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
224 .getDataChildByName(augCont1);
225 ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
226 .getDataChildByName(augCont2);
227 assertNotNull(augCont2Node);
229 ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
230 .getDataChildByName(grpCont2);
231 ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
232 .getDataChildByName(grpCont22);
233 assertNotNull(grpCont22Node);
235 ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
236 .getDataChildByName(grpAdd);
237 assertNotNull(grpAddNode);
240 private static <T extends ModelStatement<?>> T findInStatements(final QName target,
241 final ImmutableList<T> statements) {
243 for (final T statement : statements) {
245 .equals(statement.statementDefinition().getArgumentName())) {
253 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
254 final StatementStreamSource... sources) {
255 for (StatementStreamSource source : sources) {
256 reactor.addSource(source);
261 public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
263 SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
265 assertNotNull(context);
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");
273 ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
274 assertTrue(leafAugCase.isAugmenting());
275 DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case");
276 assertFalse(leafAug.isAugmenting());