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.rfc8040.parser;
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.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.rfc8040.model.api.YangDataSchemaNode;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
34 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
42 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 public class YangDataExtensionTest {
46 private static final StatementStreamSource FOO_MODULE = sourceForResource(
47 "/yang-data-extension-test/foo.yang");
48 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
49 "/yang-data-extension-test/foo-invalid-1.yang");
50 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
51 "/yang-data-extension-test/foo-invalid-2.yang");
52 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
53 "/yang-data-extension-test/foo-invalid-3.yang");
54 private static final StatementStreamSource BAR_MODULE = sourceForResource(
55 "/yang-data-extension-test/bar.yang");
56 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
57 "/yang-data-extension-test/baz.yang");
58 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
59 "/yang-data-extension-test/foobar.yang");
60 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
61 "/yang-data-extension-test/ietf-restconf.yang");
63 private static final Revision REVISION = Revision.of("2017-06-01");
64 private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
65 private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
66 private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
68 private static CrossSourceStatementReactor reactor;
71 public static void createReactor() {
72 reactor = RFC7950Reactors.vanillaReactorBuilder()
73 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, YangDataStatementSupport.getInstance())
78 public static void freeReactor() {
83 public void testYangData() throws Exception {
84 final SchemaContext schemaContext = reactor.newBuild().addSources(FOO_MODULE, IETF_RESTCONF_MODULE)
86 assertNotNull(schemaContext);
88 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
89 assertEquals(1, extensions.size());
91 final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
92 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
93 assertEquals(2, unknownSchemaNodes.size());
95 YangDataSchemaNode myYangDataANode = null;
96 YangDataSchemaNode myYangDataBNode = null;
97 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
98 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
99 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
100 if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
101 myYangDataANode = yangDataSchemaNode;
102 } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
103 myYangDataBNode = yangDataSchemaNode;
107 assertNotNull(myYangDataANode);
108 assertNotNull(myYangDataBNode);
110 assertNotNull(myYangDataANode.getContainerSchemaNode());
111 assertNotNull(myYangDataBNode.getContainerSchemaNode());
115 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
116 final SchemaContext schemaContext = reactor.newBuild().addSources(BAZ_MODULE, IETF_RESTCONF_MODULE)
118 assertNotNull(schemaContext);
120 final Module baz = schemaContext.findModule("baz", REVISION).get();
121 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
122 assertEquals(1, unknownSchemaNodes.size());
124 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
125 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
126 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
127 assertNotNull(myYangDataNode);
129 final ContainerSchemaNode contInYangData = myYangDataNode.getContainerSchemaNode();
130 assertNotNull(contInYangData);
131 assertTrue(contInYangData.isConfiguration());
132 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
133 QName.create(baz.getQNameModule(), "inner-cont")).get();
134 assertNotNull(innerCont);
135 assertTrue(innerCont.isConfiguration());
136 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
137 QName.create(baz.getQNameModule(), "grp-cont")).get();
138 assertNotNull(grpCont);
139 assertTrue(grpCont.isConfiguration());
143 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
144 final SchemaContext schemaContext = reactor.newBuild().setSupportedFeatures(ImmutableSet.of())
145 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE).buildEffective();
146 assertNotNull(schemaContext);
148 final Module foobar = schemaContext.findModule("foobar", REVISION).get();
149 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
150 assertEquals(1, unknownSchemaNodes.size());
152 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
153 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
154 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
155 assertNotNull(myYangDataNode);
157 final ContainerSchemaNode contInYangData = myYangDataNode.getContainerSchemaNode();
158 assertNotNull(contInYangData);
159 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
160 QName.create(foobar.getQNameModule(), "inner-cont")).get();
161 assertNotNull(innerCont);
162 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
163 QName.create(foobar.getQNameModule(), "grp-cont")).get();
164 assertNotNull(grpCont);
168 public void testYangDataBeingIgnored() throws Exception {
169 // yang-data statement is ignored if it does not appear as a top-level statement
170 // i.e., it will not appear in the final SchemaContext
171 final SchemaContext schemaContext = reactor.newBuild().addSources(BAR_MODULE, IETF_RESTCONF_MODULE)
173 assertNotNull(schemaContext);
175 final Module bar = schemaContext.findModule("bar", REVISION).get();
176 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.findDataChildByName(
177 QName.create(bar.getQNameModule(), "cont")).get();
180 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
181 assertEquals(1, extensions.size());
183 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
184 assertEquals(0, unknownSchemaNodes.size());
188 public void testYangDataWithMissingTopLevelContainer() {
190 reactor.newBuild().addSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE).buildEffective();
191 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
192 } catch (final ReactorException ex) {
193 final Throwable cause = ex.getCause();
194 assertTrue(cause instanceof MissingSubstatementException);
195 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
200 public void testYangDataWithTwoTopLevelContainers() {
202 reactor.newBuild().addSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE).buildEffective();
203 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
204 } catch (final ReactorException ex) {
205 final Throwable cause = ex.getCause();
206 assertTrue(cause instanceof InvalidSubstatementException);
207 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
212 public void testYangDataWithInvalidToplevelNode() {
214 reactor.newBuild().addSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE).buildEffective();
215 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
216 } catch (final ReactorException ex) {
217 final Throwable cause = ex.getCause();
218 assertTrue(cause instanceof InvalidSubstatementException);
219 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));
223 private static StatementStreamSource sourceForResource(final String resourceName) {
225 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
226 } catch (IOException | YangSyntaxErrorException e) {
227 throw new IllegalArgumentException("Failed to create source", e);