2 * Copyright (c) 2013 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.controller.yang.model.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.IOException;
13 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Date;
18 import java.util.List;
21 import org.antlr.v4.runtime.ANTLRInputStream;
22 import org.antlr.v4.runtime.CommonTokenStream;
23 import org.antlr.v4.runtime.tree.ParseTree;
24 import org.antlr.v4.runtime.tree.ParseTreeWalker;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
28 import org.opendaylight.controller.antlrv4.code.gen.YangParser;
29 import org.opendaylight.controller.model.util.UnknownType;
30 import org.opendaylight.controller.yang.common.QName;
31 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
33 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
34 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
36 import org.opendaylight.controller.yang.model.api.Module;
37 import org.opendaylight.controller.yang.model.api.ModuleImport;
38 import org.opendaylight.controller.yang.model.api.SchemaNode;
39 import org.opendaylight.controller.yang.model.api.SchemaPath;
40 import org.opendaylight.controller.yang.model.api.Status;
41 import org.opendaylight.controller.yang.model.api.TypeDefinition;
42 import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
44 public class YangModelParserListenerTest {
46 private final String testFile = "/test-model.yang";
47 ModuleBuilder builder;
52 public void init() throws IOException {
53 builder = getBuilder(testFile);
54 module = builder.build();
58 public void testParseModule() throws IOException {
59 Set<ModuleImport> imports = module.getImports();
60 assertEquals(3, imports.size());
62 URI namespace = module.getNamespace();
63 URI expectedNS = URI.create("urn:cisco:params:xml:ns:yang:controller:network");
64 assertEquals(expectedNS, namespace);
66 Date revision = module.getRevision();
69 String prefix = module.getPrefix();
70 String expectedPrefix = "topos";
71 assertEquals(expectedPrefix, prefix);
73 String expectedDescription = "module description";
74 assertEquals(expectedDescription, module.getDescription());
76 String expectedReference = "module reference";
77 assertEquals(expectedReference, module.getReference());
79 Set<TypeDefinition<?>> typedefs = module.getTypeDefinitions();
80 assertEquals(10, typedefs.size());
82 Set<DataSchemaNode> childNodes = module.getChildNodes();
83 assertEquals(1, childNodes.size());
85 final String containerName = "network";
86 final QName containerQName = new QName(namespace, revision, prefix, containerName);
87 ContainerSchemaNode tested = (ContainerSchemaNode) module.getChildNodes().iterator().next();
88 DataSchemaNode container1 = module.getDataChildByName(containerName);
89 DataSchemaNode container2 = module.getDataChildByName(containerQName);
91 assertEquals(tested, container1);
92 assertEquals(container1, container2);
96 public void testParseContainer() {
97 URI namespace = module.getNamespace();
98 Date revision = module.getRevision();
99 String prefix = module.getPrefix();
100 final QName containerQName = new QName(namespace, revision, prefix, "network");
102 ContainerSchemaNode tested = (ContainerSchemaNode)module.getDataChildByName(containerQName);
104 Set<DataSchemaNode> containerChildNodes = tested.getChildNodes();
105 assertEquals(3, containerChildNodes.size());
107 String expectedDescription = "network-description";
108 String expectedReference = "network-reference";
109 Status expectedStatus = Status.OBSOLETE;
110 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
112 List<QName> path = new ArrayList<QName>();
113 path.add(new QName(namespace, revision, prefix, "test-model"));
114 path.add(containerQName);
115 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
116 assertEquals(expectedSchemaPath, tested.getPath());
118 assertTrue(tested.isConfiguration());
119 assertTrue(tested.isPresenceContainer());
123 public void testParseList() {
124 URI namespace = module.getNamespace();
125 Date revision = module.getRevision();
126 String prefix = module.getPrefix();
127 final QName listQName = new QName(namespace, revision, prefix, "topology");
129 DataNodeContainer networkContainer = (DataNodeContainer)module.getDataChildByName("network");
130 DataNodeContainer topologiesContainer = (DataNodeContainer)networkContainer.getDataChildByName("topologies");
131 ListSchemaNode tested = (ListSchemaNode)topologiesContainer.getDataChildByName(listQName);
132 assertEquals(listQName, tested.getQName());
134 String expectedDescription = "Test description of list 'topology'.";
135 String expectedReference = null;
136 Status expectedStatus = Status.CURRENT;
137 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
139 List<QName> path = new ArrayList<QName>();
140 path.add(new QName(namespace, revision, prefix, "test-model"));
141 path.add(new QName(namespace, revision, prefix, "network"));
142 path.add(new QName(namespace, revision, prefix, "topologies"));
144 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
145 assertEquals(expectedSchemaPath, tested.getPath());
147 List<QName> expectedKey = new ArrayList<QName>();
148 expectedKey.add(new QName(namespace, revision, prefix, "topology-id"));
149 assertEquals(expectedKey, tested.getKeyDefinition());
151 assertEquals(Collections.EMPTY_SET, tested.getTypeDefinitions());
152 assertEquals(Collections.EMPTY_SET, tested.getUses());
153 assertEquals(Collections.EMPTY_SET, tested.getGroupings());
155 assertTrue(tested.getDataChildByName("topology-id") instanceof LeafSchemaNode);
159 public void testParseLeaf() {
160 URI namespace = module.getNamespace();
161 Date revision = module.getRevision();
162 String prefix = module.getPrefix();
163 final QName leafQName = new QName(namespace, revision, prefix, "topology-id");
165 DataNodeContainer networkContainer = (DataNodeContainer)module.getDataChildByName("network");
166 DataNodeContainer topologiesContainer = (DataNodeContainer)networkContainer.getDataChildByName("topologies");
167 DataNodeContainer topologyList = (DataNodeContainer)topologiesContainer.getDataChildByName("topology");
168 LeafSchemaNode tested = (LeafSchemaNode)topologyList.getDataChildByName(leafQName);
169 assertEquals(leafQName, tested.getQName());
171 String expectedDescription = "Test description of leaf 'topology-id'";
172 String expectedReference = null;
173 Status expectedStatus = Status.CURRENT;
174 testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
176 List<QName> path = new ArrayList<QName>();
177 path.add(new QName(namespace, revision, prefix, "test-model"));
178 path.add(new QName(namespace, revision, prefix, "network"));
179 path.add(new QName(namespace, revision, prefix, "topologies"));
180 path.add(new QName(namespace, revision, prefix, "topology"));
182 SchemaPath expectedSchemaPath = new SchemaPath(path, true);
183 assertEquals(expectedSchemaPath, tested.getPath());
185 UnknownType.Builder typeBuilder = new UnknownType.Builder(new QName(namespace, revision, prefix, "topology-id"), null, null);
186 TypeDefinition<?> expectedType = typeBuilder.build();
187 assertEquals(expectedType, tested.getType());
191 private void testDesc_Ref_Status(SchemaNode tested, String expectedDescription, String expectedReference, Status expectedStatus) {
192 assertEquals(expectedDescription, tested.getDescription());
193 assertEquals(expectedReference, tested.getReference());
194 assertEquals(expectedStatus, tested.getStatus());
197 private ModuleBuilder getBuilder(String fileName) throws IOException {
198 final InputStream inStream = getClass().getResourceAsStream(fileName);
199 ANTLRInputStream input = new ANTLRInputStream(inStream);
200 final YangLexer lexer = new YangLexer(input);
201 final CommonTokenStream tokens = new CommonTokenStream(lexer);
202 final YangParser parser = new YangParser(tokens);
204 final ParseTree tree = parser.yang();
205 final ParseTreeWalker walker = new ParseTreeWalker();
207 final YangModelParserListenerImpl modelParser = new YangModelParserListenerImpl();
208 walker.walk(modelParser, tree);
209 return modelParser.getModuleBuilder();