2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import com.google.common.collect.ImmutableSet;
19 import java.util.Date;
20 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.YangDataSchemaNode;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
38 public class YangDataExtensionTest {
40 private static final StatementStreamSource FOO_MODULE = sourceForResource(
41 "/yang-data-extension-test/foo.yang");
42 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
43 "/yang-data-extension-test/foo-invalid-1.yang");
44 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
45 "/yang-data-extension-test/foo-invalid-2.yang");
46 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
47 "/yang-data-extension-test/foo-invalid-3.yang");
48 private static final StatementStreamSource BAR_MODULE = sourceForResource(
49 "/yang-data-extension-test/bar.yang");
50 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
51 "/yang-data-extension-test/baz.yang");
52 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
53 "/yang-data-extension-test/foobar.yang");
54 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
55 "/yang-data-extension-test/ietf-restconf.yang");
57 private static final Date REVISION = QName.parseRevision("2017-06-01");
58 private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
59 private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
60 private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
63 public void testYangData() throws Exception {
64 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(FOO_MODULE, IETF_RESTCONF_MODULE);
65 assertNotNull(schemaContext);
67 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
68 assertEquals(1, extensions.size());
70 final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
71 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
72 assertEquals(2, unknownSchemaNodes.size());
74 YangDataSchemaNode myYangDataANode = null;
75 YangDataSchemaNode myYangDataBNode = null;
76 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
77 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
78 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
79 if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
80 myYangDataANode = yangDataSchemaNode;
81 } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
82 myYangDataBNode = yangDataSchemaNode;
86 assertNotNull(myYangDataANode);
87 assertNotNull(myYangDataBNode);
89 assertNotNull(myYangDataANode.getContainer());
90 assertNotNull(myYangDataBNode.getContainer());
94 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
95 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAZ_MODULE, IETF_RESTCONF_MODULE);
96 assertNotNull(schemaContext);
98 final Module baz = schemaContext.findModule("baz", REVISION).get();
99 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
100 assertEquals(1, unknownSchemaNodes.size());
102 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
103 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
104 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
105 assertNotNull(myYangDataNode);
107 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
108 assertNotNull(contInYangData);
109 assertTrue(contInYangData.isConfiguration());
110 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
111 QName.create(baz.getQNameModule(), "inner-cont"));
112 assertNotNull(innerCont);
113 assertTrue(innerCont.isConfiguration());
114 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
115 QName.create(baz.getQNameModule(), "grp-cont"));
116 assertNotNull(grpCont);
117 assertTrue(grpCont.isConfiguration());
121 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
122 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
123 reactor.setSupportedFeatures(ImmutableSet.of());
124 reactor.addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE);
126 final SchemaContext schemaContext = reactor.buildEffective();
127 assertNotNull(schemaContext);
129 final Module foobar = schemaContext.findModule("foobar", REVISION).get();
130 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
131 assertEquals(1, unknownSchemaNodes.size());
133 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
134 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
135 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
136 assertNotNull(myYangDataNode);
138 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
139 assertNotNull(contInYangData);
140 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
141 QName.create(foobar.getQNameModule(), "inner-cont"));
142 assertNotNull(innerCont);
143 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
144 QName.create(foobar.getQNameModule(), "grp-cont"));
145 assertNotNull(grpCont);
149 public void testYangDataBeingIgnored() throws Exception {
150 // yang-data statement is ignored if it does not appear as a top-level statement
151 // i.e., it will not appear in the final SchemaContext
152 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAR_MODULE, IETF_RESTCONF_MODULE);
153 assertNotNull(schemaContext);
155 final Module bar = schemaContext.findModule("bar", REVISION).get();
156 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
157 QName.create(bar.getQNameModule(), "cont"));
160 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
161 assertEquals(1, extensions.size());
163 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
164 assertEquals(0, unknownSchemaNodes.size());
168 public void testYangDataWithMissingTopLevelContainer() {
170 StmtTestUtils.parseYangSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE);
171 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
172 } catch (final ReactorException ex) {
173 final Throwable cause = ex.getCause();
174 assertTrue(cause instanceof MissingSubstatementException);
175 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
180 public void testYangDataWithTwoTopLevelContainers() {
182 StmtTestUtils.parseYangSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE);
183 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
184 } catch (final ReactorException ex) {
185 final Throwable cause = ex.getCause();
186 assertTrue(cause instanceof InvalidSubstatementException);
187 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
192 public void testYangDataWithInvalidToplevelNode() {
194 StmtTestUtils.parseYangSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE);
195 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
196 } catch (final ReactorException ex) {
197 final Throwable cause = ex.getCause();
198 assertTrue(cause instanceof InvalidSubstatementException);
199 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));