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.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.parser.impl.DefaultReactors;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
37 public class YangDataExtensionTest {
39 private static final StatementStreamSource FOO_MODULE = sourceForResource(
40 "/yang-data-extension-test/foo.yang");
41 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
42 "/yang-data-extension-test/foo-invalid-1.yang");
43 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
44 "/yang-data-extension-test/foo-invalid-2.yang");
45 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
46 "/yang-data-extension-test/foo-invalid-3.yang");
47 private static final StatementStreamSource BAR_MODULE = sourceForResource(
48 "/yang-data-extension-test/bar.yang");
49 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
50 "/yang-data-extension-test/baz.yang");
51 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
52 "/yang-data-extension-test/foobar.yang");
53 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
54 "/yang-data-extension-test/ietf-restconf.yang");
56 private static final Revision REVISION = Revision.of("2017-06-01");
57 private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
58 private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
59 private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
62 public void testYangData() throws Exception {
63 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(FOO_MODULE, IETF_RESTCONF_MODULE);
64 assertNotNull(schemaContext);
66 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
67 assertEquals(1, extensions.size());
69 final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
70 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
71 assertEquals(2, unknownSchemaNodes.size());
73 YangDataSchemaNode myYangDataANode = null;
74 YangDataSchemaNode myYangDataBNode = null;
75 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
76 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
77 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
78 if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
79 myYangDataANode = yangDataSchemaNode;
80 } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
81 myYangDataBNode = yangDataSchemaNode;
85 assertNotNull(myYangDataANode);
86 assertNotNull(myYangDataBNode);
88 assertNotNull(myYangDataANode.getContainer());
89 assertNotNull(myYangDataBNode.getContainer());
93 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
94 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAZ_MODULE, IETF_RESTCONF_MODULE);
95 assertNotNull(schemaContext);
97 final Module baz = schemaContext.findModule("baz", REVISION).get();
98 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
99 assertEquals(1, unknownSchemaNodes.size());
101 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
102 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
103 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
104 assertNotNull(myYangDataNode);
106 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
107 assertNotNull(contInYangData);
108 assertTrue(contInYangData.isConfiguration());
109 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
110 QName.create(baz.getQNameModule(), "inner-cont"));
111 assertNotNull(innerCont);
112 assertTrue(innerCont.isConfiguration());
113 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
114 QName.create(baz.getQNameModule(), "grp-cont"));
115 assertNotNull(grpCont);
116 assertTrue(grpCont.isConfiguration());
120 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
121 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
122 .setSupportedFeatures(ImmutableSet.of())
123 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE)
125 assertNotNull(schemaContext);
127 final Module foobar = schemaContext.findModule("foobar", REVISION).get();
128 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
129 assertEquals(1, unknownSchemaNodes.size());
131 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
132 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
133 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
134 assertNotNull(myYangDataNode);
136 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
137 assertNotNull(contInYangData);
138 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
139 QName.create(foobar.getQNameModule(), "inner-cont"));
140 assertNotNull(innerCont);
141 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
142 QName.create(foobar.getQNameModule(), "grp-cont"));
143 assertNotNull(grpCont);
147 public void testYangDataBeingIgnored() throws Exception {
148 // yang-data statement is ignored if it does not appear as a top-level statement
149 // i.e., it will not appear in the final SchemaContext
150 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAR_MODULE, IETF_RESTCONF_MODULE);
151 assertNotNull(schemaContext);
153 final Module bar = schemaContext.findModule("bar", REVISION).get();
154 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
155 QName.create(bar.getQNameModule(), "cont"));
158 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
159 assertEquals(1, extensions.size());
161 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
162 assertEquals(0, unknownSchemaNodes.size());
166 public void testYangDataWithMissingTopLevelContainer() {
168 StmtTestUtils.parseYangSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE);
169 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
170 } catch (final ReactorException ex) {
171 final Throwable cause = ex.getCause();
172 assertTrue(cause instanceof MissingSubstatementException);
173 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
178 public void testYangDataWithTwoTopLevelContainers() {
180 StmtTestUtils.parseYangSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE);
181 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
182 } catch (final ReactorException ex) {
183 final Throwable cause = ex.getCause();
184 assertTrue(cause instanceof InvalidSubstatementException);
185 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
190 public void testYangDataWithInvalidToplevelNode() {
192 StmtTestUtils.parseYangSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE);
193 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
194 } catch (final ReactorException ex) {
195 final Throwable cause = ex.getCause();
196 assertTrue(cause instanceof InvalidSubstatementException);
197 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));