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.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.Arrays;
18 import java.util.Collections;
19 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
23 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
24 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class GenEnumResolvingTest {
33 public void testLeafEnumResolving() throws URISyntaxException, IOException {
34 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
36 File ianaIfTypeModel = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
38 final SchemaContext context = new YangParserImpl().parseFiles(Arrays.asList(ietfInterfaces, ianaIfTypeModel));
39 assertTrue(context != null);
41 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
42 final List<Type> genTypes = bindingGen.generateTypes(context);
43 assertTrue(genTypes != null);
45 assertEquals("Expected count of all Generated Types", 6, genTypes.size());
47 GeneratedType genInterface = null;
48 for (final Type type : genTypes) {
49 if (type instanceof GeneratedType) {
50 if (type.getName().equals("Interface")) {
51 genInterface = (GeneratedType) type;
55 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
57 Enumeration linkUpDownTrapEnable = null;
58 Enumeration operStatus = null;
59 final List<Enumeration> enums = genInterface.getEnumerations();
60 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
61 assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
62 for (final Enumeration e : enums) {
63 if (e.getName().equals("LinkUpDownTrapEnable")) {
64 linkUpDownTrapEnable = e;
65 } else if (e.getName().equals("OperStatus")) {
70 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
71 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
73 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
74 linkUpDownTrapEnable.getValues());
75 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
76 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
77 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
79 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
81 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
83 assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
84 Enumeration ianaIfType = null;
85 for (final MethodSignature method : methods) {
86 if (method.getName().equals("getType")) {
87 if (method.getReturnType() instanceof Enumeration) {
88 ianaIfType = (Enumeration) method.getReturnType();
93 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
94 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
98 public void testTypedefEnumResolving() throws URISyntaxException, IOException {
99 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
100 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(ianaIfType));
101 assertTrue(context != null);
102 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
103 final List<Type> genTypes = bindingGen.generateTypes(context);
104 assertTrue(genTypes != null);
105 assertEquals(1, genTypes.size());
107 final Type type = genTypes.get(0);
108 assertTrue(type instanceof Enumeration);
110 final Enumeration enumer = (Enumeration) type;
111 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
115 public void testLeafrefEnumResolving() throws URISyntaxException, IOException {
116 File abstractTopology = new File(getClass().getResource("/enum-test-models/abstract-topology@2013-02-08.yang")
118 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
120 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
122 final SchemaContext context = new YangParserImpl().parseFiles(Arrays.asList(abstractTopology, ietfInterfaces,
124 assertNotNull(context);
125 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
126 final List<Type> genTypes = bindingGen.generateTypes(context);
127 assertNotNull(genTypes);
128 assertTrue(!genTypes.isEmpty());
130 GeneratedType genInterface = null;
131 for (final Type type : genTypes) {
132 if (type instanceof GeneratedType) {
133 if (type.getPackageName().equals(
134 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
135 && type.getName().equals("Interface")) {
136 genInterface = (GeneratedType) type;
140 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
142 Type linkUpDownTrapEnable = null;
143 Type operStatus = null;
144 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
145 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
146 assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
147 for (final MethodSignature method : methods) {
148 if (method.getName().equals("getLinkUpDownTrapEnable")) {
149 linkUpDownTrapEnable = method.getReturnType();
150 } else if (method.getName().equals("getOperStatus")) {
151 operStatus = method.getReturnType();
155 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
156 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
157 assertEquals(linkUpDownTrapEnable.getPackageName(),
158 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
160 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
161 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
162 assertEquals(operStatus.getPackageName(),
163 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");