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.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.UsesNode;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 public class ControllerStmtParserTest {
41 public void test() throws SourceException, FileNotFoundException,
42 ReactorException, URISyntaxException, ParseException {
43 final SchemaContext context = StmtTestUtils
44 .parseYangSources("/sal-broker-impl");
45 assertNotNull(context);
47 salDomBrokerImplModuleTest(context);
48 configModuleTest(context);
51 private static void salDomBrokerImplModuleTest(final SchemaContext context)
52 throws ParseException {
53 final Module module = context.findModuleByName(
54 "opendaylight-sal-dom-broker-impl", SimpleDateFormatUtil
55 .getRevisionFormat().parse("2013-10-28"));
56 assertNotNull(module);
58 final Set<AugmentationSchema> augmentations = module.getAugmentations();
59 boolean checked = false;
60 for (final AugmentationSchema augmentationSchema : augmentations) {
61 final DataSchemaNode dataNode = augmentationSchema
62 .getDataChildByName(QName.create(module.getQNameModule(), "dom-broker-impl"));
63 if (dataNode instanceof ChoiceCaseNode) {
64 final ChoiceCaseNode caseNode = (ChoiceCaseNode) dataNode;
65 final DataSchemaNode dataNode2 = caseNode
66 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
67 if (dataNode2 instanceof ContainerSchemaNode) {
68 final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
69 final DataSchemaNode leaf = containerNode
70 .getDataChildByName(QName.create(module.getQNameModule(), "type"));
71 final List<UnknownSchemaNode> unknownSchemaNodes = leaf
72 .getUnknownSchemaNodes();
73 assertEquals(1, unknownSchemaNodes.size());
75 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes
77 assertEquals("dom-async-data-broker", unknownSchemaNode
78 .getQName().getLocalName());
79 assertEquals(unknownSchemaNode.getQName(),
80 unknownSchemaNode.getPath().getLastComponent());
89 private static void configModuleTest(final SchemaContext context) throws ParseException,
91 final Module configModule = context.findModuleByName("config",
92 SimpleDateFormatUtil.getRevisionFormat().parse("2013-04-05"));
93 assertNotNull(configModule);
95 final Module module = context.findModuleByName(
96 "opendaylight-sal-dom-broker-impl", SimpleDateFormatUtil
97 .getRevisionFormat().parse("2013-10-28"));
98 assertNotNull(module);
100 final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(), "modules"));
101 assertTrue(dataNode instanceof ContainerSchemaNode);
103 final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
104 final DataSchemaNode dataChildList = moduleContainer
105 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
107 assertTrue(dataChildList instanceof ListSchemaNode);
109 final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
110 final DataSchemaNode dataChildChoice = listModule
111 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
113 assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
115 final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
116 final ChoiceCaseNode caseNodeByName = confChoice
117 .getCaseNodeByName("dom-broker-impl");
119 assertNotNull(caseNodeByName);
120 final DataSchemaNode dataNode2 = caseNodeByName
121 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
122 assertTrue(dataNode2 instanceof ContainerSchemaNode);
124 final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
125 final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type"));
126 final List<UnknownSchemaNode> unknownSchemaNodes = leaf
127 .getUnknownSchemaNodes();
129 assertEquals(1, unknownSchemaNodes.size());
131 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
133 assertEquals(unknownSchemaNode.getQName(), unknownSchemaNode.getPath()
134 .getLastComponent());
135 assertEquals("dom-async-data-broker", unknownSchemaNode.getQName()
138 final ChoiceCaseNode domInmemoryDataBroker = confChoice
139 .getCaseNodeByName("dom-inmemory-data-broker");
141 assertNotNull(domInmemoryDataBroker);
142 final DataSchemaNode schemaService = domInmemoryDataBroker
143 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
144 assertTrue(schemaService instanceof ContainerSchemaNode);
146 final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
148 assertEquals(1, schemaServiceContainer.getUses().size());
149 final UsesNode uses = schemaServiceContainer.getUses().iterator().next();
150 final QName groupingQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:config","2013-04-05","service-ref");
151 final QName usesGroupingPathLastComponent = uses.getGroupingPath().getLastComponent();
152 assertEquals(groupingQName, usesGroupingPathLastComponent);
153 assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer));
155 final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(), "type"));
156 final List<UnknownSchemaNode> typeUnknownSchemaNodes = type
157 .getUnknownSchemaNodes();
159 assertEquals(1, typeUnknownSchemaNodes.size());
161 final UnknownSchemaNode typeUnknownSchemaNode = typeUnknownSchemaNodes.get(0);
163 final QNameModule qNameModule = QNameModule
165 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom"),
166 SimpleDateFormatUtil.getRevisionFormat().parse(
168 final QName qName = QName.create(qNameModule, "schema-service");
170 assertEquals(qName, typeUnknownSchemaNode.getQName());
171 assertEquals(typeUnknownSchemaNode.getQName(), typeUnknownSchemaNode
172 .getPath().getLastComponent());
175 private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
177 for (final DataSchemaNode dsn : csn.getChildNodes()) {
178 if (!dsn.isAddedByUses()) {