2 * Copyright (c) 2016 Cisco Systems, Inc. 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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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;
32 public class ControllerStmtParserTest {
35 public void test() throws Exception {
36 final SchemaContext context = StmtTestUtils.parseYangSources("/sal-broker-impl");
37 assertNotNull(context);
39 salDomBrokerImplModuleTest(context);
40 configModuleTest(context);
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();
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());
60 final Collection<? extends UnrecognizedStatement> unknownSchemaNodes =
61 containerNode.asEffectiveStatement()
62 .findFirstEffectiveSubstatement(UsesEffectiveStatement.class).orElseThrow()
63 .findFirstEffectiveSubstatement(RefineEffectiveStatement.class).orElseThrow()
64 .getDeclared().declaredSubstatements(UnrecognizedStatement.class);
67 final UnrecognizedStatement unknownSchemaNode = unknownSchemaNodes.iterator().next();
68 assertEquals("sal:dom-async-data-broker", unknownSchemaNode.argument());
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();
80 final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(),
82 assertTrue(dataNode instanceof ContainerSchemaNode);
84 final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
85 final DataSchemaNode dataChildList = moduleContainer
86 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
88 assertTrue(dataChildList instanceof ListSchemaNode);
90 final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
91 final DataSchemaNode dataChildChoice = listModule
92 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
94 assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
96 final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
97 final CaseSchemaNode caseNodeByName = confChoice.findCaseNodes("dom-broker-impl").iterator().next();
99 assertNotNull(caseNodeByName);
100 final DataSchemaNode dataNode2 = caseNodeByName
101 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
102 assertTrue(dataNode2 instanceof ContainerSchemaNode);
104 final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
105 final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type"));
106 assertEquals(0, leaf.getUnknownSchemaNodes().size());
108 final CaseSchemaNode domInmemoryDataBroker = confChoice.findCaseNodes("dom-inmemory-data-broker").iterator()
111 assertNotNull(domInmemoryDataBroker);
112 final DataSchemaNode schemaService = domInmemoryDataBroker
113 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
114 assertTrue(schemaService instanceof ContainerSchemaNode);
116 final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
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",
122 assertEquals(groupingQName, uses.getSourceGrouping().getQName());
123 assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer));
125 final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(),
127 assertEquals(0, type.getUnknownSchemaNodes().size());
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());
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());
143 private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
145 for (final DataSchemaNode dsn : csn.getChildNodes()) {
146 if (!dsn.isAddedByUses()) {