Update yang-parser-api
[yangtools.git] / yang / rfc6643-parser-support / src / test / java / org / opendaylight / yangtools / rfc6643 / parser / IetfYangSmiv2ExtensionPluginTest.java
1 /*
2  * Copyright (c) 2017, 2020 PANTHEON.tech, 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 package org.opendaylight.yangtools.rfc6643.parser;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.IOException;
14 import org.junit.AfterClass;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.rfc6643.model.api.AliasSchemaNode;
18 import org.opendaylight.yangtools.rfc6643.model.api.DefValSchemaNode;
19 import org.opendaylight.yangtools.rfc6643.model.api.ImpliedSchemaNode;
20 import org.opendaylight.yangtools.rfc6643.model.api.MaxAccess;
21 import org.opendaylight.yangtools.rfc6643.model.api.MaxAccessSchemaNode;
22 import org.opendaylight.yangtools.rfc6643.model.api.ObjectIdentifier;
23 import org.opendaylight.yangtools.rfc6643.model.api.OidSchemaNode;
24 import org.opendaylight.yangtools.rfc6643.model.api.SubIdSchemaNode;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
34 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
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.ModelProcessingPhase;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
40
41 public class IetfYangSmiv2ExtensionPluginTest {
42     private static final String NS = "urn:opendaylight:yang:extension:third-party";
43     private static final String REV = "2016-06-09";
44
45     private static CrossSourceStatementReactor reactor;
46
47     @BeforeClass
48     public static void createReactor() {
49         reactor = RFC7950Reactors.defaultReactorBuilder()
50             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
51                 new DisplayHintStatementSupport(YangParserConfiguration.DEFAULT))
52             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
53                 new MaxAccessStatementSupport(YangParserConfiguration.DEFAULT))
54             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
55                 new DefValStatementSupport(YangParserConfiguration.DEFAULT))
56             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
57                 new ImpliedStatementSupport(YangParserConfiguration.DEFAULT))
58             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
59                 new AliasStatementSupport(YangParserConfiguration.DEFAULT))
60             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
61                 new OidStatementSupport(YangParserConfiguration.DEFAULT))
62             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
63                 new SubIdStatementSupport(YangParserConfiguration.DEFAULT))
64             .build();
65     }
66
67     @AfterClass
68     public static void freeReactor() {
69         reactor = null;
70     }
71
72     @Test
73     public void testExtensions() throws ReactorException, YangSyntaxErrorException, IOException {
74         SchemaContext schema = createSchema();
75
76         assertTrue(schema.getUnknownSchemaNodes().size() == 1);
77         AliasSchemaNode aliasExtEffectStmt = (AliasSchemaNode) schema.getUnknownSchemaNodes().iterator().next();
78         assertEquals(aliasExtEffectStmt.getArgument(), "ifXTable");
79         assertTrue(aliasExtEffectStmt.getUnknownSchemaNodes().size() == 1);
80         OidSchemaNode oidExtEffectStmt = (OidSchemaNode) aliasExtEffectStmt.getUnknownSchemaNodes().iterator().next();
81         assertEquals(oidExtEffectStmt.getArgument().toString(), "1.3.6.1.2.1.31.1.1");
82
83         final DataSchemaNode dataChildByName = schema.getDataChildByName(QName.create(NS, REV, "root"));
84         assertTrue(dataChildByName instanceof ContainerSchemaNode);
85         final ContainerSchemaNode root = (ContainerSchemaNode) dataChildByName;
86         assertEquals(1, root.getUnknownSchemaNodes().size());
87         OidSchemaNode oid = (OidSchemaNode) root.getUnknownSchemaNodes().iterator().next();
88         assertEquals(oid.getArgument().toString(), "1");
89
90         ListSchemaNode ifStackEntry = (ListSchemaNode) root.getDataChildByName(QName.create(NS, REV, "ifStackEntry"));
91         assertEquals(2, ifStackEntry.getUnknownSchemaNodes().size());
92         ifStackEntry.getUnknownSchemaNodes().forEach(unknownSchemaNode -> {
93             if (unknownSchemaNode instanceof OidSchemaNode) {
94                 assertEquals(((OidSchemaNode) unknownSchemaNode).getArgument().toString(),
95                         ObjectIdentifier.forString("1.1").toString());
96             } else if (unknownSchemaNode instanceof ImpliedSchemaNode) {
97                 assertEquals(((ImpliedSchemaNode) unknownSchemaNode).getArgument(), "ifStackHigherLayer");
98             }
99         });
100
101         LeafSchemaNode ifStackHigherLayer = (LeafSchemaNode)
102                 ifStackEntry.getDataChildByName(QName.create(NS, REV, "ifStackHigherLayer"));
103
104         ifStackHigherLayer.getUnknownSchemaNodes().forEach(unknownSchemaNode -> {
105             if (unknownSchemaNode instanceof MaxAccessSchemaNode) {
106                 final MaxAccessSchemaNode maxAccess = (MaxAccessSchemaNode) unknownSchemaNode;
107                 assertEquals(MaxAccess.NOT_ACCESSIBLE, maxAccess.getArgument());
108             }
109         });
110
111         LeafSchemaNode testNumberLeaf = (LeafSchemaNode)
112                 ifStackEntry.getDataChildByName(QName.create(NS, REV, "test-number"));
113         assertEquals(3, testNumberLeaf.getUnknownSchemaNodes().size());
114         testNumberLeaf.getUnknownSchemaNodes().forEach(unknownSchemaNode -> {
115             if (unknownSchemaNode instanceof SubIdSchemaNode) {
116                 assertEquals(Uint32.TWO, ((SubIdSchemaNode) unknownSchemaNode).getArgument());
117             } else if (unknownSchemaNode instanceof DefValSchemaNode) {
118                 assertEquals("0", ((DefValSchemaNode) unknownSchemaNode).getArgument());
119             }
120         });
121     }
122
123     private static SchemaContext createSchema() throws ReactorException, YangSyntaxErrorException, IOException {
124         return reactor.newBuild()
125                 .addSources(
126                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
127                         IetfYangSmiv2ExtensionPluginTest.class, "/foo.yang")),
128                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
129                         IetfYangSmiv2ExtensionPluginTest.class, "/ietf-yang-smiv2.yang")))
130                 .buildEffective();
131     }
132 }