2 * Copyright (c) 2016 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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.mdsal.binding.model.api.Enumeration;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 public class GenEnumResolvingTest {
32 public void testLeafEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
33 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
35 File ianaIfTypeModel = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
37 final SchemaContext context = YangParserTestUtils.parseYangSources(ietfInterfaces, ianaIfTypeModel);
38 assertTrue(context != null);
40 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
41 final List<Type> genTypes = bindingGen.generateTypes(context);
42 assertTrue(genTypes != null);
44 assertEquals("Expected count of all Generated Types", 6, genTypes.size());
46 GeneratedType genInterface = null;
47 for (final Type type : genTypes) {
48 if (type instanceof GeneratedType) {
49 if (type.getName().equals("Interface")) {
50 genInterface = (GeneratedType) type;
54 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
56 Enumeration linkUpDownTrapEnable = null;
57 Enumeration operStatus = null;
58 final List<Enumeration> enums = genInterface.getEnumerations();
59 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
60 assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
61 for (final Enumeration e : enums) {
62 if (e.getName().equals("LinkUpDownTrapEnable")) {
63 linkUpDownTrapEnable = e;
64 } else if (e.getName().equals("OperStatus")) {
69 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
70 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
72 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
73 linkUpDownTrapEnable.getValues());
74 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
75 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
76 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
78 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
80 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
82 assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
83 Enumeration ianaIfType = null;
84 for (final MethodSignature method : methods) {
85 if (method.getName().equals("getType")) {
86 if (method.getReturnType() instanceof Enumeration) {
87 ianaIfType = (Enumeration) method.getReturnType();
92 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
93 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
97 public void testTypedefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
98 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
99 final SchemaContext context = YangParserTestUtils.parseYangSources(ianaIfType);
100 assertTrue(context != null);
101 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
102 final List<Type> genTypes = bindingGen.generateTypes(context);
103 assertTrue(genTypes != null);
104 assertEquals(1, genTypes.size());
106 final Type type = genTypes.get(0);
107 assertTrue(type instanceof Enumeration);
109 final Enumeration enumer = (Enumeration) type;
110 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
114 public void testLeafrefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
115 File abstractTopology = new File(getClass().getResource("/enum-test-models/abstract-topology@2013-02-08.yang")
117 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
119 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
121 final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInterfaces,
123 assertNotNull(context);
124 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
125 final List<Type> genTypes = bindingGen.generateTypes(context);
126 assertNotNull(genTypes);
127 assertTrue(!genTypes.isEmpty());
129 GeneratedType genInterface = null;
130 for (final Type type : genTypes) {
131 if (type instanceof GeneratedType) {
132 if (type.getPackageName().equals(
133 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
134 && type.getName().equals("Interface")) {
135 genInterface = (GeneratedType) type;
139 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
141 Type linkUpDownTrapEnable = null;
142 Type operStatus = null;
143 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
144 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
145 assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
146 for (final MethodSignature method : methods) {
147 if (method.getName().equals("getLinkUpDownTrapEnable")) {
148 linkUpDownTrapEnable = method.getReturnType();
149 } else if (method.getName().equals("getOperStatus")) {
150 operStatus = method.getReturnType();
154 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
155 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
156 assertEquals(linkUpDownTrapEnable.getPackageName(),
157 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
159 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
160 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
161 assertEquals(operStatus.getPackageName(),
162 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");