6c3f9a0da891cf824fc4c528436ee27ca4e6615f
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / ControllerStmtParserTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Collection;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Revision;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.UsesNode;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RefineEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
31
32 public class ControllerStmtParserTest {
33
34     @Test
35     public void test() throws Exception {
36         final SchemaContext context = StmtTestUtils.parseYangSources("/sal-broker-impl");
37         assertNotNull(context);
38
39         salDomBrokerImplModuleTest(context);
40         configModuleTest(context);
41     }
42
43     private static void salDomBrokerImplModuleTest(final SchemaContext context) {
44         final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get();
45
46         boolean checked = false;
47         for (final AugmentationSchemaNode augmentationSchema : module.getAugmentations()) {
48             final DataSchemaNode dataNode = augmentationSchema.dataChildByName(
49                 QName.create(module.getQNameModule(), "dom-broker-impl"));
50             if (dataNode instanceof CaseSchemaNode) {
51                 final CaseSchemaNode caseNode = (CaseSchemaNode) dataNode;
52                 final DataSchemaNode dataNode2 = caseNode.dataChildByName(
53                     QName.create(module.getQNameModule(), "async-data-broker"));
54                 if (dataNode2 instanceof ContainerSchemaNode) {
55                     final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
56                     final DataSchemaNode leaf = containerNode
57                             .getDataChildByName(QName.create(module.getQNameModule(), "type"));
58                     assertEquals(0, leaf.getUnknownSchemaNodes().size());
59
60                     final Collection<? extends UnrecognizedStatement> unknownSchemaNodes =
61                         containerNode.asEffectiveStatement()
62                             .findFirstEffectiveSubstatement(UsesEffectiveStatement.class).orElseThrow()
63                             .findFirstEffectiveSubstatement(RefineEffectiveStatement.class).orElseThrow()
64                             .getDeclared().declaredSubstatements(UnrecognizedStatement.class);
65
66
67                     final UnrecognizedStatement unknownSchemaNode = unknownSchemaNodes.iterator().next();
68                     assertEquals("sal:dom-async-data-broker", unknownSchemaNode.argument());
69                     checked = true;
70                 }
71             }
72         }
73         assertTrue(checked);
74     }
75
76     private static void configModuleTest(final SchemaContext context) {
77         final Module configModule = context.findModule("config", Revision.of("2013-04-05")).get();
78         final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get();
79
80         final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(),
81             "modules"));
82         assertTrue(dataNode instanceof ContainerSchemaNode);
83
84         final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
85         final DataSchemaNode dataChildList = moduleContainer
86                 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
87
88         assertTrue(dataChildList instanceof ListSchemaNode);
89
90         final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
91         final DataSchemaNode dataChildChoice = listModule
92                 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
93
94         assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
95
96         final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
97         final CaseSchemaNode caseNodeByName = confChoice.findCaseNodes("dom-broker-impl").iterator().next();
98
99         assertNotNull(caseNodeByName);
100         final DataSchemaNode dataNode2 = caseNodeByName
101                 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
102         assertTrue(dataNode2 instanceof ContainerSchemaNode);
103
104         final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
105         final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type"));
106         assertEquals(0, leaf.getUnknownSchemaNodes().size());
107
108         final CaseSchemaNode domInmemoryDataBroker = confChoice.findCaseNodes("dom-inmemory-data-broker").iterator()
109                 .next();
110
111         assertNotNull(domInmemoryDataBroker);
112         final DataSchemaNode schemaService = domInmemoryDataBroker
113                 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
114         assertTrue(schemaService instanceof ContainerSchemaNode);
115
116         final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
117
118         assertEquals(1, schemaServiceContainer.getUses().size());
119         final UsesNode uses = schemaServiceContainer.getUses().iterator().next();
120         final QName groupingQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:config", "2013-04-05",
121             "service-ref");
122         assertEquals(groupingQName, uses.getSourceGrouping().getQName());
123         assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer));
124
125         final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(),
126             "type"));
127         assertEquals(0, type.getUnknownSchemaNodes().size());
128
129         final Collection<? extends UnrecognizedStatement> typeUnknownSchemaNodes =
130             schemaServiceContainer.asEffectiveStatement()
131                 .findFirstEffectiveSubstatement(UsesEffectiveStatement.class).orElseThrow()
132                 .findFirstEffectiveSubstatement(RefineEffectiveStatement.class).orElseThrow()
133                 .getDeclared().declaredSubstatements(UnrecognizedStatement.class);
134         assertEquals(1, typeUnknownSchemaNodes.size());
135
136
137         final UnrecognizedStatement typeUnknownSchemaNode = typeUnknownSchemaNodes.iterator().next();
138         assertEquals("sal:schema-service", typeUnknownSchemaNode.argument());
139         assertEquals(QName.create(groupingQName, "required-identity"),
140             typeUnknownSchemaNode.statementDefinition().getStatementName());
141     }
142
143     private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
144         int result = 0;
145         for (final DataSchemaNode dsn : csn.getChildNodes()) {
146             if (!dsn.isAddedByUses()) {
147                 result++;
148             }
149         }
150         return result;
151     }
152 }