Bug 8523: Add support for parsing restconf:yang-data extension
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangDataExtensionTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.stmt;
10
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;
16
17 import com.google.common.collect.ImmutableSet;
18 import java.net.URI;
19 import java.text.ParseException;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Set;
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;
40
41 public class YangDataExtensionTest {
42
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");
59
60     private static Date revision;
61     private static QNameModule fooModule;
62     private static QName myYangDataA;
63     private static QName myYangDataB;
64
65     @BeforeClass
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");
71     }
72
73     @Test
74     public void testYangData() throws Exception {
75         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(FOO_MODULE, IETF_RESTCONF_MODULE);
76         assertNotNull(schemaContext);
77
78         final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
79         assertEquals(1, extensions.size());
80
81         final Module foo = schemaContext.findModuleByName("foo", revision);
82         assertNotNull(foo);
83
84         final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
85         assertEquals(2, unknownSchemaNodes.size());
86
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;
96             }
97         }
98
99         assertNotNull(myYangDataANode);
100         assertNotNull(myYangDataBNode);
101
102         assertNotNull(myYangDataANode.getContainer());
103         assertNotNull(myYangDataBNode.getContainer());
104     }
105
106     @Test
107     public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
108         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(BAZ_MODULE, IETF_RESTCONF_MODULE);
109         assertNotNull(schemaContext);
110
111         final Module baz = schemaContext.findModuleByName("baz", revision);
112         assertNotNull(baz);
113
114         final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
115         assertEquals(1, unknownSchemaNodes.size());
116
117         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
118         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
119         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
120         assertNotNull(myYangDataNode);
121
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());
133     }
134
135     @Test
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);
140
141         final SchemaContext schemaContext = reactor.buildEffective();
142         assertNotNull(schemaContext);
143
144         final Module foobar = schemaContext.findModuleByName("foobar", revision);
145         assertNotNull(foobar);
146
147         final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
148         assertEquals(1, unknownSchemaNodes.size());
149
150         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
151         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
152         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
153         assertNotNull(myYangDataNode);
154
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);
163     }
164
165     @Test
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);
171
172         final Module bar = schemaContext.findModuleByName("bar", revision);
173         assertNotNull(bar);
174         final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
175                 QName.create(bar.getQNameModule(), "cont"));
176         assertNotNull(cont);
177
178         final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
179         assertEquals(1, extensions.size());
180
181         final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
182         assertEquals(0, unknownSchemaNodes.size());
183     }
184
185     @Test
186     public void testYangDataWithMissingTopLevelContainer() {
187         try {
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."));
194         }
195     }
196
197     @Test
198     public void testYangDataWithTwoTopLevelContainers() {
199         try {
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."));
206         }
207     }
208
209     @Test
210     public void testYangDataWithInvalidToplevelNode() {
211         try {
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."));
218         }
219     }
220 }