f797f347d0cec88e31c24bf5a2c0e8386bd80ada
[yangtools.git] / yang / yang-parser-impl / 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.net.URI;
15 import java.net.URISyntaxException;
16 import java.text.ParseException;
17 import java.util.List;
18 import java.util.Set;
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;
34
35 public class ControllerStmtParserTest {
36
37     @Test
38     public void test() throws Exception {
39         final SchemaContext context = StmtTestUtils.parseYangSources("/sal-broker-impl");
40         assertNotNull(context);
41
42         salDomBrokerImplModuleTest(context);
43         configModuleTest(context);
44     }
45
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);
52
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());
69
70                     final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes
71                             .get(0);
72                     assertEquals("dom-async-data-broker", unknownSchemaNode
73                             .getQName().getLocalName());
74                     assertEquals(unknownSchemaNode.getQName(),
75                             unknownSchemaNode.getPath().getLastComponent());
76
77                     checked = true;
78                 }
79             }
80         }
81         assertTrue(checked);
82     }
83
84     private static void configModuleTest(final SchemaContext context) throws ParseException,
85             URISyntaxException {
86         final Module configModule = context.findModuleByName("config",
87                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-04-05"));
88         assertNotNull(configModule);
89
90         final Module module = context.findModuleByName(
91                 "opendaylight-sal-dom-broker-impl", SimpleDateFormatUtil
92                         .getRevisionFormat().parse("2013-10-28"));
93         assertNotNull(module);
94
95         final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(), "modules"));
96         assertTrue(dataNode instanceof ContainerSchemaNode);
97
98         final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
99         final DataSchemaNode dataChildList = moduleContainer
100                 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
101
102         assertTrue(dataChildList instanceof ListSchemaNode);
103
104         final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
105         final DataSchemaNode dataChildChoice = listModule
106                 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
107
108         assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
109
110         final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
111         final ChoiceCaseNode caseNodeByName = confChoice
112                 .getCaseNodeByName("dom-broker-impl");
113
114         assertNotNull(caseNodeByName);
115         final DataSchemaNode dataNode2 = caseNodeByName
116                 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
117         assertTrue(dataNode2 instanceof ContainerSchemaNode);
118
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();
123
124         assertEquals(1, unknownSchemaNodes.size());
125
126         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
127
128         assertEquals(unknownSchemaNode.getQName(), unknownSchemaNode.getPath()
129                 .getLastComponent());
130         assertEquals("dom-async-data-broker", unknownSchemaNode.getQName()
131                 .getLocalName());
132
133         final ChoiceCaseNode domInmemoryDataBroker = confChoice
134                 .getCaseNodeByName("dom-inmemory-data-broker");
135
136         assertNotNull(domInmemoryDataBroker);
137         final DataSchemaNode schemaService = domInmemoryDataBroker
138                 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
139         assertTrue(schemaService instanceof ContainerSchemaNode);
140
141         final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
142
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));
149
150         final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(), "type"));
151         final List<UnknownSchemaNode> typeUnknownSchemaNodes = type
152                 .getUnknownSchemaNodes();
153
154         assertEquals(1, typeUnknownSchemaNodes.size());
155
156         final UnknownSchemaNode typeUnknownSchemaNode = typeUnknownSchemaNodes.get(0);
157
158         final QNameModule qNameModule = QNameModule
159                 .create(new URI(
160                         "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom"),
161                         SimpleDateFormatUtil.getRevisionFormat().parse(
162                                 "2013-10-28"));
163         final QName qName = QName.create(qNameModule, "schema-service");
164
165         assertEquals(qName, typeUnknownSchemaNode.getQName());
166         assertEquals(typeUnknownSchemaNode.getQName(), typeUnknownSchemaNode
167                 .getPath().getLastComponent());
168     }
169
170     private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
171         int result = 0;
172         for (final DataSchemaNode dsn : csn.getChildNodes()) {
173             if (!dsn.isAddedByUses()) {
174                 result++;
175             }
176         }
177         return result;
178     }
179
180 }