2 * Copyright (c) 2015 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.sal.binding.generator.impl;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
33 public class BindingGeneratorImplTest {
35 private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
36 "/isis-topology/network-topology@2013-10-21.yang", false);
38 private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
39 "/isis-topology/isis-topology@2013-10-21.yang", false);
41 private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
42 "/isis-topology/l3-unicast-igp-topology@2013-10-21.yang", false);
45 public void isisTotpologyStatementParserTest() throws IOException,
46 YangSyntaxErrorException, URISyntaxException, SourceException,
48 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
51 reactor.addSources(ISIS_20131021, L3_20131021,
52 NETWORK_TOPOLOGY_20131021);
54 SchemaContext context = reactor.buildEffective();
55 assertNotNull(context);
57 List<Type> generateTypes = new BindingGeneratorImpl(false)
58 .generateTypes(context);
60 assertFalse(generateTypes.isEmpty());
64 public void choiceNodeGenerationTest() throws IOException,
65 YangSyntaxErrorException, URISyntaxException {
66 File resourceFile = new File(getClass().getResource(
67 "/binding-generator-impl-test/choice-test.yang").toURI());
68 File resourceDir = resourceFile.getParentFile();
70 YangParserImpl parser = YangParserImpl.getInstance();
71 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
73 List<Type> generateTypes = new BindingGeneratorImpl(false)
74 .generateTypes(context);
76 GeneratedType choiceTestData = null;
77 GeneratedType myRootContainer = null;
78 GeneratedType myList = null;
79 GeneratedType myContainer = null;
80 GeneratedType myList2 = null;
81 GeneratedType myContainer2 = null;
83 for (Type type : generateTypes) {
84 switch (type.getName()) {
85 case "ChoiceTestData":
86 choiceTestData = (GeneratedType) type;
88 case "Myrootcontainer":
89 myRootContainer = (GeneratedType) type;
92 myList = (GeneratedType) type;
95 myList2 = (GeneratedType) type;
98 myContainer = (GeneratedType) type;
101 myContainer2 = (GeneratedType) type;
106 assertNotNull(choiceTestData);
107 assertNotNull(myRootContainer);
108 assertNotNull(myList);
109 assertNotNull(myContainer);
110 assertNotNull(myList2);
111 assertNotNull(myContainer2);
113 List<Type> implements1 = myContainer.getImplements();
114 Type childOfParamType = null;
115 for (Type type : implements1) {
116 if (type.getName().equals("ChildOf")) {
117 childOfParamType = ((ParameterizedType) type)
118 .getActualTypeArguments()[0];
122 assertNotNull(childOfParamType);
123 assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
125 implements1 = myList.getImplements();
126 childOfParamType = null;
127 for (Type type : implements1) {
128 if (type.getName().equals("ChildOf")) {
129 childOfParamType = ((ParameterizedType) type)
130 .getActualTypeArguments()[0];
134 assertNotNull(childOfParamType);
135 assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
137 implements1 = myContainer2.getImplements();
138 childOfParamType = null;
139 for (Type type : implements1) {
140 if (type.getName().equals("ChildOf")) {
141 childOfParamType = ((ParameterizedType) type)
142 .getActualTypeArguments()[0];
146 assertNotNull(childOfParamType);
147 assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
149 implements1 = myList2.getImplements();
150 childOfParamType = null;
151 for (Type type : implements1) {
152 if (type.getName().equals("ChildOf")) {
153 childOfParamType = ((ParameterizedType) type)
154 .getActualTypeArguments()[0];
158 assertNotNull(childOfParamType);
159 assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
164 public void notificationGenerationTest() throws IOException, YangSyntaxErrorException, URISyntaxException {
165 File resourceFile = new File(getClass().getResource("/binding-generator-impl-test/notification-test.yang")
167 File resourceDir = resourceFile.getParentFile();
169 YangParserImpl parser = YangParserImpl.getInstance();
170 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
172 List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(context);
174 GeneratedType foo = null;
175 for (Type type : generateTypes) {
176 if (type.getName().equals("Foo")) {
177 foo = (GeneratedType) type;
183 Type dataObject = null;
184 List<Type> impl = foo.getImplements();
185 for (Type type : impl) {
186 switch (type.getName()) {
197 assertNotNull(dataObject);