2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.yang.model.parser.impl;
\r
10 import static org.junit.Assert.*;
\r
12 import java.io.IOException;
\r
13 import java.io.InputStream;
\r
14 import java.net.URI;
\r
15 import java.text.DateFormat;
\r
16 import java.text.SimpleDateFormat;
\r
17 import java.util.ArrayList;
\r
18 import java.util.Collections;
\r
19 import java.util.Date;
\r
20 import java.util.List;
\r
21 import java.util.Set;
\r
23 import org.antlr.v4.runtime.ANTLRInputStream;
\r
24 import org.antlr.v4.runtime.CommonTokenStream;
\r
25 import org.antlr.v4.runtime.tree.ParseTree;
\r
26 import org.antlr.v4.runtime.tree.ParseTreeWalker;
\r
27 import org.junit.Ignore;
\r
28 import org.junit.Test;
\r
29 import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
\r
30 import org.opendaylight.controller.antlrv4.code.gen.YangParser;
\r
31 import org.opendaylight.controller.yang.common.QName;
\r
32 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
\r
33 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
\r
34 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
\r
35 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
\r
36 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
\r
37 import org.opendaylight.controller.yang.model.api.Module;
\r
38 import org.opendaylight.controller.yang.model.api.ModuleImport;
\r
39 import org.opendaylight.controller.yang.model.api.SchemaNode;
\r
40 import org.opendaylight.controller.yang.model.api.SchemaPath;
\r
41 import org.opendaylight.controller.yang.model.api.Status;
\r
42 import org.opendaylight.controller.yang.model.api.TypeDefinition;
\r
43 import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
\r
44 import org.opendaylight.controller.yang.model.util.Leafref;
\r
45 import org.opendaylight.controller.yang.model.util.UnknownType;
\r
47 public class YangModelParserListenerTest {
\r
50 public void testParseImport() throws Exception {
\r
51 Module module = getModule("/abstract-topology.yang");
\r
53 Set<ModuleImport> imports = module.getImports();
\r
54 assertEquals(1, imports.size());
\r
55 ModuleImport moduleImport = imports.iterator().next();
\r
57 assertEquals("inet", moduleImport.getPrefix());
\r
59 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
\r
60 Date expectedDate = simpleDateFormat.parse("2010-09-24");
\r
61 assertEquals(expectedDate, moduleImport.getRevision());
\r
65 public void testParseHeaders() throws Exception {
\r
66 Module module = getModule("/abstract-topology.yang");
\r
68 URI namespace = module.getNamespace();
\r
69 URI expectedNS = URI.create("");
\r
70 assertEquals(expectedNS, namespace);
\r
72 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
\r
73 Date expectedDate = simpleDateFormat.parse("2013-02-08");
\r
74 assertEquals(expectedDate, module.getRevision());
\r
76 String prefix = module.getPrefix();
\r
77 String expectedPrefix = "tp";
\r
78 assertEquals(expectedPrefix, prefix);
\r
80 String expectedDescription = "This module contains the definitions of elements that creates network";
\r
81 assertTrue(module.getDescription().contains(expectedDescription));
\r
83 String expectedReference = "~~~ WILL BE DEFINED LATER";
\r
84 assertEquals(expectedReference, module.getReference());
\r
86 assertEquals("1", module.getYangVersion());
\r
90 public void testParseLeafref() throws Exception {
\r
91 Module module = getModule("/abstract-topology.yang");
\r
93 Set<TypeDefinition<?>> typedefs = module.getTypeDefinitions();
\r
94 assertEquals(2, typedefs.size());
\r
95 for(TypeDefinition<?> td : typedefs) {
\r
96 Leafref baseType = (Leafref)td.getBaseType();
\r
97 if(td.getQName().getLocalName().equals("node-id-ref")) {
\r
98 assertEquals("/tp:topology/tp:network-nodes/tp:network-node/tp:node-id", baseType.getPathStatement().toString());
\r
100 assertEquals("/tp:topology/tp:network-links/tp:network-link/tp:link-id", baseType.getPathStatement().toString());
\r
107 public void testParseModule() throws IOException {
\r
109 Module module = getModule("/test-model.yang");
\r
111 URI namespace = module.getNamespace();
\r
112 Date revision = module.getRevision();
\r
113 String prefix = module.getPrefix();
\r
115 String expectedDescription = "module description";
\r
116 assertEquals(expectedDescription, module.getDescription());
\r
118 String expectedReference = "module reference";
\r
119 assertEquals(expectedReference, module.getReference());
\r
121 Set<TypeDefinition<?>> typedefs = module.getTypeDefinitions();
\r
122 assertEquals(10, typedefs.size());
\r
124 Set<DataSchemaNode> childNodes = module.getChildNodes();
\r
125 assertEquals(1, childNodes.size());
\r
127 final String containerName = "network";
\r
128 final QName containerQName = new QName(namespace, revision, prefix, containerName);
\r
129 ContainerSchemaNode tested = (ContainerSchemaNode) module.getChildNodes().iterator().next();
\r
130 DataSchemaNode container1 = module.getDataChildByName(containerName);
\r
131 DataSchemaNode container2 = module.getDataChildByName(containerQName);
\r
133 assertEquals(tested, container1);
\r
134 assertEquals(container1, container2);
\r
139 public void testParseContainer() throws IOException {
\r
141 Module module = getModule("/test-model.yang");
\r
143 URI namespace = module.getNamespace();
\r
144 Date revision = module.getRevision();
\r
145 String prefix = module.getPrefix();
\r
146 final QName containerQName = new QName(namespace, revision, prefix, "network");
\r
148 ContainerSchemaNode tested = (ContainerSchemaNode)module.getDataChildByName(containerQName);
\r
150 Set<DataSchemaNode> containerChildNodes = tested.getChildNodes();
\r
151 assertEquals(3, containerChildNodes.size());
\r
153 String expectedDescription = "network-description";
\r
154 String expectedReference = "network-reference";
\r
155 Status expectedStatus = Status.OBSOLETE;
\r
156 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
\r
158 List<QName> path = new ArrayList<QName>();
\r
159 path.add(new QName(namespace, revision, prefix, "test-model"));
\r
160 path.add(containerQName);
\r
161 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
\r
162 assertEquals(expectedSchemaPath, tested.getPath());
\r
164 assertTrue(tested.isConfiguration());
\r
165 assertTrue(tested.isPresenceContainer());
\r
170 public void testParseList() throws IOException {
\r
172 Module module = getModule("/test-model.yang");
\r
174 URI namespace = module.getNamespace();
\r
175 Date revision = module.getRevision();
\r
176 String prefix = module.getPrefix();
\r
177 final QName listQName = new QName(namespace, revision, prefix, "topology");
\r
179 DataNodeContainer networkContainer = (DataNodeContainer)module.getDataChildByName("network");
\r
180 DataNodeContainer topologiesContainer = (DataNodeContainer)networkContainer.getDataChildByName("topologies");
\r
181 ListSchemaNode tested = (ListSchemaNode)topologiesContainer.getDataChildByName(listQName);
\r
182 assertEquals(listQName, tested.getQName());
\r
184 String expectedDescription = "Test description of list 'topology'.";
\r
185 String expectedReference = null;
\r
186 Status expectedStatus = Status.CURRENT;
\r
187 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
\r
189 List<QName> path = new ArrayList<QName>();
\r
190 path.add(new QName(namespace, revision, prefix, "test-model"));
\r
191 path.add(new QName(namespace, revision, prefix, "network"));
\r
192 path.add(new QName(namespace, revision, prefix, "topologies"));
\r
193 path.add(listQName);
\r
194 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
\r
195 assertEquals(expectedSchemaPath, tested.getPath());
\r
197 List<QName> expectedKey = new ArrayList<QName>();
\r
198 expectedKey.add(new QName(namespace, revision, prefix, "topology-id"));
\r
199 assertEquals(expectedKey, tested.getKeyDefinition());
\r
201 assertEquals(Collections.EMPTY_SET, tested.getTypeDefinitions());
\r
202 assertEquals(Collections.EMPTY_SET, tested.getUses());
\r
203 assertEquals(Collections.EMPTY_SET, tested.getGroupings());
\r
205 assertTrue(tested.getDataChildByName("topology-id") instanceof LeafSchemaNode);
\r
210 public void testParseLeaf() throws IOException {
\r
212 Module module = getModule("/test-model.yang");
\r
214 URI namespace = module.getNamespace();
\r
215 Date revision = module.getRevision();
\r
216 String prefix = module.getPrefix();
\r
217 final QName leafQName = new QName(namespace, revision, prefix, "topology-id");
\r
219 DataNodeContainer networkContainer = (DataNodeContainer)module.getDataChildByName("network");
\r
220 DataNodeContainer topologiesContainer = (DataNodeContainer)networkContainer.getDataChildByName("topologies");
\r
221 DataNodeContainer topologyList = (DataNodeContainer)topologiesContainer.getDataChildByName("topology");
\r
222 LeafSchemaNode tested = (LeafSchemaNode)topologyList.getDataChildByName(leafQName);
\r
223 assertEquals(leafQName, tested.getQName());
\r
225 String expectedDescription = "Test description of leaf 'topology-id'";
\r
226 String expectedReference = null;
\r
227 Status expectedStatus = Status.CURRENT;
\r
228 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
\r
230 List<QName> path = new ArrayList<QName>();
\r
231 path.add(new QName(namespace, revision, prefix, "test-model"));
\r
232 path.add(new QName(namespace, revision, prefix, "network"));
\r
233 path.add(new QName(namespace, revision, prefix, "topologies"));
\r
234 path.add(new QName(namespace, revision, prefix, "topology"));
\r
235 path.add(leafQName);
\r
236 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
\r
237 assertEquals(expectedSchemaPath, tested.getPath());
\r
239 UnknownType.Builder typeBuilder = new UnknownType.Builder(new QName(namespace, revision, prefix, "topology-id"), null, null);
\r
240 TypeDefinition<?> expectedType = typeBuilder.build();
\r
241 assertEquals(expectedType, tested.getType());
\r
245 private void testDesc_Ref_Status(SchemaNode tested, String expectedDescription, String expectedReference, Status expectedStatus) {
\r
246 assertEquals(expectedDescription, tested.getDescription());
\r
247 assertEquals(expectedReference, tested.getReference());
\r
248 assertEquals(expectedStatus, tested.getStatus());
\r
251 private Module getModule(String testFile) throws IOException {
\r
252 ModuleBuilder builder = getBuilder(testFile);
\r
253 return builder.build();
\r
256 private ModuleBuilder getBuilder(String fileName) throws IOException {
\r
257 final InputStream inStream = getClass().getResourceAsStream(fileName);
\r
258 ANTLRInputStream input = new ANTLRInputStream(inStream);
\r
259 final YangLexer lexer = new YangLexer(input);
\r
260 final CommonTokenStream tokens = new CommonTokenStream(lexer);
\r
261 final YangParser parser = new YangParser(tokens);
\r
263 final ParseTree tree = parser.yang();
\r
264 final ParseTreeWalker walker = new ParseTreeWalker();
\r
266 final YangModelParserListenerImpl modelParser = new YangModelParserListenerImpl();
\r
267 walker.walk(modelParser, tree);
\r
268 return modelParser.getModuleBuilder();
\r