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;
16 import com.google.common.collect.ImmutableSet;
17 import java.io.IOException;
19 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.rfc8040.model.api.YangDataSchemaNode;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
40 public class YangDataExtensionTest {
42 private static final StatementStreamSource FOO_MODULE = sourceForResource(
43 "/yang-data-extension-test/foo.yang");
44 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
45 "/yang-data-extension-test/foo-invalid-1.yang");
46 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
47 "/yang-data-extension-test/foo-invalid-2.yang");
48 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
49 "/yang-data-extension-test/foo-invalid-3.yang");
50 private static final StatementStreamSource BAR_MODULE = sourceForResource(
51 "/yang-data-extension-test/bar.yang");
52 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
53 "/yang-data-extension-test/baz.yang");
54 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
55 "/yang-data-extension-test/foobar.yang");
56 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
57 "/yang-data-extension-test/ietf-restconf.yang");
59 private static final Revision REVISION = Revision.of("2017-06-01");
60 private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
61 private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
62 private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
65 public void testYangData() throws Exception {
66 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
67 .addSources(FOO_MODULE, IETF_RESTCONF_MODULE)
69 assertNotNull(schemaContext);
71 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
72 assertEquals(1, extensions.size());
74 final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
75 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
76 assertEquals(2, unknownSchemaNodes.size());
78 YangDataSchemaNode myYangDataANode = null;
79 YangDataSchemaNode myYangDataBNode = null;
80 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
81 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
82 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
83 if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
84 myYangDataANode = yangDataSchemaNode;
85 } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
86 myYangDataBNode = yangDataSchemaNode;
90 assertNotNull(myYangDataANode);
91 assertNotNull(myYangDataBNode);
93 assertNotNull(myYangDataANode.getContainer());
94 assertNotNull(myYangDataBNode.getContainer());
98 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
99 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
100 .addSources(BAZ_MODULE, IETF_RESTCONF_MODULE)
102 assertNotNull(schemaContext);
104 final Module baz = schemaContext.findModule("baz", REVISION).get();
105 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
106 assertEquals(1, unknownSchemaNodes.size());
108 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
109 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
110 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
111 assertNotNull(myYangDataNode);
113 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
114 assertNotNull(contInYangData);
115 assertTrue(contInYangData.isConfiguration());
116 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
117 QName.create(baz.getQNameModule(), "inner-cont"));
118 assertNotNull(innerCont);
119 assertTrue(innerCont.isConfiguration());
120 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
121 QName.create(baz.getQNameModule(), "grp-cont"));
122 assertNotNull(grpCont);
123 assertTrue(grpCont.isConfiguration());
127 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
128 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
129 .setSupportedFeatures(ImmutableSet.of())
130 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE)
132 assertNotNull(schemaContext);
134 final Module foobar = schemaContext.findModule("foobar", REVISION).get();
135 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
136 assertEquals(1, unknownSchemaNodes.size());
138 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
139 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
140 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
141 assertNotNull(myYangDataNode);
143 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
144 assertNotNull(contInYangData);
145 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
146 QName.create(foobar.getQNameModule(), "inner-cont"));
147 assertNotNull(innerCont);
148 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
149 QName.create(foobar.getQNameModule(), "grp-cont"));
150 assertNotNull(grpCont);
154 public void testYangDataBeingIgnored() throws Exception {
155 // yang-data statement is ignored if it does not appear as a top-level statement
156 // i.e., it will not appear in the final SchemaContext
157 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
158 .addSources(BAR_MODULE, IETF_RESTCONF_MODULE)
160 assertNotNull(schemaContext);
162 final Module bar = schemaContext.findModule("bar", REVISION).get();
163 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
164 QName.create(bar.getQNameModule(), "cont"));
167 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
168 assertEquals(1, extensions.size());
170 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
171 assertEquals(0, unknownSchemaNodes.size());
175 public void testYangDataWithMissingTopLevelContainer() {
177 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE)
179 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
180 } catch (final ReactorException ex) {
181 final Throwable cause = ex.getCause();
182 assertTrue(cause instanceof MissingSubstatementException);
183 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
188 public void testYangDataWithTwoTopLevelContainers() {
190 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE)
192 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
193 } catch (final ReactorException ex) {
194 final Throwable cause = ex.getCause();
195 assertTrue(cause instanceof InvalidSubstatementException);
196 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
201 public void testYangDataWithInvalidToplevelNode() {
203 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE)
205 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
206 } catch (final ReactorException ex) {
207 final Throwable cause = ex.getCause();
208 assertTrue(cause instanceof InvalidSubstatementException);
209 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));
213 private static StatementStreamSource sourceForResource(final String resourceName) {
215 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
216 } catch (IOException | YangSyntaxErrorException e) {
217 throw new IllegalArgumentException("Failed to create source", e);