Merge from development repository.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / YangModelParserListenerTest.java
1 /*
2  * Copyright (c) 2013 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.controller.yang.model.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Set;
20
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;
43
44 public class YangModelParserListenerTest {
45
46     private final String testFile = "/test-model.yang";
47     ModuleBuilder builder;
48     Module module;
49
50
51     @Before
52     public void init() throws IOException {
53         builder = getBuilder(testFile);
54         module = builder.build();
55     }
56
57     @Test
58     public void testParseModule() throws IOException {
59         Set<ModuleImport> imports = module.getImports();
60         assertEquals(3, imports.size());
61
62         URI namespace = module.getNamespace();
63         URI expectedNS = URI.create("urn:cisco:params:xml:ns:yang:controller:network");
64         assertEquals(expectedNS, namespace);
65
66         Date revision = module.getRevision();
67         assertNull(revision);
68
69         String prefix = module.getPrefix();
70         String expectedPrefix = "topos";
71         assertEquals(expectedPrefix, prefix);
72
73         String expectedDescription = "module description";
74         assertEquals(expectedDescription, module.getDescription());
75
76         String expectedReference = "module reference";
77         assertEquals(expectedReference, module.getReference());
78
79         Set<TypeDefinition<?>> typedefs = module.getTypeDefinitions();
80         assertEquals(10, typedefs.size());
81
82         Set<DataSchemaNode> childNodes = module.getChildNodes();
83         assertEquals(1, childNodes.size());
84
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);
90
91         assertEquals(tested, container1);
92         assertEquals(container1, container2);
93     }
94
95     @Test
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");
101
102         ContainerSchemaNode tested = (ContainerSchemaNode)module.getDataChildByName(containerQName);
103
104         Set<DataSchemaNode> containerChildNodes = tested.getChildNodes();
105         assertEquals(3, containerChildNodes.size());
106
107         String expectedDescription = "network-description";
108         String expectedReference = "network-reference";
109         Status expectedStatus = Status.OBSOLETE;
110         testDesc_Ref_Status(tested, expectedDescription, expectedReference, expectedStatus);
111
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());
117
118         assertTrue(tested.isConfiguration());
119         assertTrue(tested.isPresenceContainer());
120     }
121
122     @Test
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");
128
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());
133
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);
138
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"));
143         path.add(listQName);
144         SchemaPath expectedSchemaPath = new SchemaPath(path, true);
145         assertEquals(expectedSchemaPath, tested.getPath());
146
147         List<QName> expectedKey = new ArrayList<QName>();
148         expectedKey.add(new QName(namespace, revision, prefix, "topology-id"));
149         assertEquals(expectedKey, tested.getKeyDefinition());
150
151         assertEquals(Collections.EMPTY_SET, tested.getTypeDefinitions());
152         assertEquals(Collections.EMPTY_SET, tested.getUses());
153         assertEquals(Collections.EMPTY_SET, tested.getGroupings());
154
155         assertTrue(tested.getDataChildByName("topology-id") instanceof LeafSchemaNode);
156     }
157
158     @Test
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");
164
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());
170
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);
175
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"));
181         path.add(leafQName);
182         SchemaPath expectedSchemaPath = new SchemaPath(path, true);
183         assertEquals(expectedSchemaPath, tested.getPath());
184
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());
188     }
189
190
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());
195     }
196
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);
203
204         final ParseTree tree = parser.yang();
205         final ParseTreeWalker walker = new ParseTreeWalker();
206
207         final YangModelParserListenerImpl modelParser = new YangModelParserListenerImpl();
208         walker.walk(modelParser, tree);
209         return modelParser.getModuleBuilder();
210     }
211
212 }