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.text.ParseException;
20 import java.util.Date;
21 import java.util.List;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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.api.YangDataSchemaNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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;
41 public class YangDataExtensionTest {
43 private static final StatementStreamSource FOO_MODULE = sourceForResource(
44 "/yang-data-extension-test/foo.yang");
45 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
46 "/yang-data-extension-test/foo-invalid-1.yang");
47 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
48 "/yang-data-extension-test/foo-invalid-2.yang");
49 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
50 "/yang-data-extension-test/foo-invalid-3.yang");
51 private static final StatementStreamSource BAR_MODULE = sourceForResource(
52 "/yang-data-extension-test/bar.yang");
53 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
54 "/yang-data-extension-test/baz.yang");
55 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
56 "/yang-data-extension-test/foobar.yang");
57 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
58 "/yang-data-extension-test/ietf-restconf.yang");
60 private static Date revision;
61 private static QNameModule fooModule;
62 private static QName myYangDataA;
63 private static QName myYangDataB;
66 public static void setup() throws ParseException {
67 revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-06-01");
68 fooModule = QNameModule.create(URI.create("foo"), revision);
69 myYangDataA = QName.create(fooModule, "my-yang-data-a");
70 myYangDataB = QName.create(fooModule, "my-yang-data-b");
74 public void testYangData() throws Exception {
75 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(FOO_MODULE, IETF_RESTCONF_MODULE);
76 assertNotNull(schemaContext);
78 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
79 assertEquals(1, extensions.size());
81 final Module foo = schemaContext.findModuleByName("foo", revision);
84 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
85 assertEquals(2, unknownSchemaNodes.size());
87 YangDataSchemaNode myYangDataANode = null;
88 YangDataSchemaNode myYangDataBNode = null;
89 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
90 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
91 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
92 if (myYangDataA.equals(yangDataSchemaNode.getQName())) {
93 myYangDataANode = yangDataSchemaNode;
94 } else if (myYangDataB.equals(yangDataSchemaNode.getQName())) {
95 myYangDataBNode = yangDataSchemaNode;
99 assertNotNull(myYangDataANode);
100 assertNotNull(myYangDataBNode);
102 assertNotNull(myYangDataANode.getContainer());
103 assertNotNull(myYangDataBNode.getContainer());
107 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
108 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAZ_MODULE, IETF_RESTCONF_MODULE);
109 assertNotNull(schemaContext);
111 final Module baz = schemaContext.findModuleByName("baz", revision);
114 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
115 assertEquals(1, unknownSchemaNodes.size());
117 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
118 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
119 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
120 assertNotNull(myYangDataNode);
122 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
123 assertNotNull(contInYangData);
124 assertTrue(contInYangData.isConfiguration());
125 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
126 QName.create(baz.getQNameModule(), "inner-cont"));
127 assertNotNull(innerCont);
128 assertTrue(innerCont.isConfiguration());
129 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
130 QName.create(baz.getQNameModule(), "grp-cont"));
131 assertNotNull(grpCont);
132 assertTrue(grpCont.isConfiguration());
136 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
137 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
138 reactor.setSupportedFeatures(ImmutableSet.of());
139 reactor.addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE);
141 final SchemaContext schemaContext = reactor.buildEffective();
142 assertNotNull(schemaContext);
144 final Module foobar = schemaContext.findModuleByName("foobar", revision);
145 assertNotNull(foobar);
147 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
148 assertEquals(1, unknownSchemaNodes.size());
150 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
151 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
152 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
153 assertNotNull(myYangDataNode);
155 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
156 assertNotNull(contInYangData);
157 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
158 QName.create(foobar.getQNameModule(), "inner-cont"));
159 assertNotNull(innerCont);
160 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
161 QName.create(foobar.getQNameModule(), "grp-cont"));
162 assertNotNull(grpCont);
166 public void testYangDataBeingIgnored() throws Exception {
167 // yang-data statement is ignored if it does not appear as a top-level statement
168 // i.e., it will not appear in the final SchemaContext
169 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAR_MODULE, IETF_RESTCONF_MODULE);
170 assertNotNull(schemaContext);
172 final Module bar = schemaContext.findModuleByName("bar", revision);
174 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
175 QName.create(bar.getQNameModule(), "cont"));
178 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
179 assertEquals(1, extensions.size());
181 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
182 assertEquals(0, unknownSchemaNodes.size());
186 public void testYangDataWithMissingTopLevelContainer() {
188 StmtTestUtils.parseYangSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE);
189 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
190 } catch (final ReactorException ex) {
191 final Throwable cause = ex.getCause();
192 assertTrue(cause instanceof MissingSubstatementException);
193 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
198 public void testYangDataWithTwoTopLevelContainers() {
200 StmtTestUtils.parseYangSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE);
201 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
202 } catch (final ReactorException ex) {
203 final Throwable cause = ex.getCause();
204 assertTrue(cause instanceof InvalidSubstatementException);
205 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
210 public void testYangDataWithInvalidToplevelNode() {
212 StmtTestUtils.parseYangSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE);
213 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
214 } catch (final ReactorException ex) {
215 final Throwable cause = ex.getCause();
216 assertTrue(cause instanceof InvalidSubstatementException);
217 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));