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;
15 import java.net.URISyntaxException;
16 import java.text.ParseException;
17 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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.UsesNode;
35 public class ControllerStmtParserTest {
38 public void test() throws Exception {
39 final SchemaContext context = StmtTestUtils.parseYangSources("/sal-broker-impl");
40 assertNotNull(context);
42 salDomBrokerImplModuleTest(context);
43 configModuleTest(context);
46 private static void salDomBrokerImplModuleTest(final SchemaContext context)
47 throws ParseException {
48 final Module module = context.findModuleByName(
49 "opendaylight-sal-dom-broker-impl", SimpleDateFormatUtil
50 .getRevisionFormat().parse("2013-10-28"));
51 assertNotNull(module);
53 final Set<AugmentationSchema> augmentations = module.getAugmentations();
54 boolean checked = false;
55 for (final AugmentationSchema augmentationSchema : augmentations) {
56 final DataSchemaNode dataNode = augmentationSchema
57 .getDataChildByName(QName.create(module.getQNameModule(), "dom-broker-impl"));
58 if (dataNode instanceof ChoiceCaseNode) {
59 final ChoiceCaseNode caseNode = (ChoiceCaseNode) dataNode;
60 final DataSchemaNode dataNode2 = caseNode
61 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
62 if (dataNode2 instanceof ContainerSchemaNode) {
63 final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
64 final DataSchemaNode leaf = containerNode
65 .getDataChildByName(QName.create(module.getQNameModule(), "type"));
66 final List<UnknownSchemaNode> unknownSchemaNodes = leaf
67 .getUnknownSchemaNodes();
68 assertEquals(1, unknownSchemaNodes.size());
70 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes
72 assertEquals("dom-async-data-broker", unknownSchemaNode
73 .getQName().getLocalName());
74 assertEquals(unknownSchemaNode.getQName(),
75 unknownSchemaNode.getPath().getLastComponent());
84 private static void configModuleTest(final SchemaContext context) throws ParseException,
86 final Module configModule = context.findModuleByName("config",
87 SimpleDateFormatUtil.getRevisionFormat().parse("2013-04-05"));
88 assertNotNull(configModule);
90 final Module module = context.findModuleByName(
91 "opendaylight-sal-dom-broker-impl", SimpleDateFormatUtil
92 .getRevisionFormat().parse("2013-10-28"));
93 assertNotNull(module);
95 final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(), "modules"));
96 assertTrue(dataNode instanceof ContainerSchemaNode);
98 final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
99 final DataSchemaNode dataChildList = moduleContainer
100 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
102 assertTrue(dataChildList instanceof ListSchemaNode);
104 final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
105 final DataSchemaNode dataChildChoice = listModule
106 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
108 assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
110 final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
111 final ChoiceCaseNode caseNodeByName = confChoice
112 .getCaseNodeByName("dom-broker-impl");
114 assertNotNull(caseNodeByName);
115 final DataSchemaNode dataNode2 = caseNodeByName
116 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
117 assertTrue(dataNode2 instanceof ContainerSchemaNode);
119 final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
120 final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type"));
121 final List<UnknownSchemaNode> unknownSchemaNodes = leaf
122 .getUnknownSchemaNodes();
124 assertEquals(1, unknownSchemaNodes.size());
126 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
128 assertEquals(unknownSchemaNode.getQName(), unknownSchemaNode.getPath()
129 .getLastComponent());
130 assertEquals("dom-async-data-broker", unknownSchemaNode.getQName()
133 final ChoiceCaseNode domInmemoryDataBroker = confChoice
134 .getCaseNodeByName("dom-inmemory-data-broker");
136 assertNotNull(domInmemoryDataBroker);
137 final DataSchemaNode schemaService = domInmemoryDataBroker
138 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
139 assertTrue(schemaService instanceof ContainerSchemaNode);
141 final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
143 assertEquals(1, schemaServiceContainer.getUses().size());
144 final UsesNode uses = schemaServiceContainer.getUses().iterator().next();
145 final QName groupingQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:config","2013-04-05","service-ref");
146 final QName usesGroupingPathLastComponent = uses.getGroupingPath().getLastComponent();
147 assertEquals(groupingQName, usesGroupingPathLastComponent);
148 assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer));
150 final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(), "type"));
151 final List<UnknownSchemaNode> typeUnknownSchemaNodes = type
152 .getUnknownSchemaNodes();
154 assertEquals(1, typeUnknownSchemaNodes.size());
156 final UnknownSchemaNode typeUnknownSchemaNode = typeUnknownSchemaNodes.get(0);
158 final QNameModule qNameModule = QNameModule
160 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom"),
161 SimpleDateFormatUtil.getRevisionFormat().parse(
163 final QName qName = QName.create(qNameModule, "schema-service");
165 assertEquals(qName, typeUnknownSchemaNode.getQName());
166 assertEquals(typeUnknownSchemaNode.getQName(), typeUnknownSchemaNode
167 .getPath().getLastComponent());
170 private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
172 for (final DataSchemaNode dsn : csn.getChildNodes()) {
173 if (!dsn.isAddedByUses()) {