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;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class GenEnumResolvingTest {
32 private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) throws IOException {
33 final YangContextParser parser = new YangParserImpl();
35 final List<File> inputFiles = new ArrayList<File>();
36 for (int i = 0; i < yangFiles.length; ++i) {
37 inputFiles.add(new File(yangFiles[i]));
40 return parser.parseFiles(inputFiles);
44 public void testLeafEnumResolving() throws URISyntaxException, IOException {
45 final URI ietfInterfacesPath = getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
47 final URI ifTypePath = getClass().getResource("/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
48 final URI yangTypesPath = getClass().getResource("/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
50 final SchemaContext context = resolveSchemaContextFromFiles(ietfInterfacesPath, ifTypePath, yangTypesPath);
51 assertTrue(context != null);
53 final BindingGenerator bindingGen = new BindingGeneratorImpl();
54 final List<Type> genTypes = bindingGen.generateTypes(context);
55 assertTrue(genTypes != null);
57 assertEquals("Expected count of all Generated Types", 20, genTypes.size());
59 GeneratedType genInterface = null;
60 for (final Type type : genTypes) {
61 if (type instanceof GeneratedType) {
62 if (type.getName().equals("Interface")) {
63 genInterface = (GeneratedType) type;
67 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
69 Enumeration linkUpDownTrapEnable = null;
70 Enumeration operStatus = null;
71 final List<Enumeration> enums = genInterface.getEnumerations();
72 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
73 assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
74 for (final Enumeration e : enums) {
75 if (e.getName().equals("LinkUpDownTrapEnable")) {
76 linkUpDownTrapEnable = e;
77 } else if (e.getName().equals("OperStatus")) {
82 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
83 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
85 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
86 linkUpDownTrapEnable.getValues());
87 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
88 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
89 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
91 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
93 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
95 assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
96 Enumeration ianaIfType = null;
97 for (final MethodSignature method : methods) {
98 if (method.getName().equals("getType")) {
99 if (method.getReturnType() instanceof Enumeration) {
100 ianaIfType = (Enumeration) method.getReturnType();
105 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
106 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
110 public void testTypedefEnumResolving() throws URISyntaxException, IOException {
111 final URI ianaIfTypePath = getClass().getResource("/leafref-test-models/iana-if-type@2012-06-05.yang").toURI();
113 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
114 assertTrue(context != null);
115 final BindingGenerator bindingGen = new BindingGeneratorImpl();
116 final List<Type> genTypes = bindingGen.generateTypes(context);
117 assertTrue(genTypes != null);
118 assertEquals(1, genTypes.size());
120 final Type type = genTypes.get(0);
121 assertTrue(type instanceof Enumeration);
123 final Enumeration enumer = (Enumeration) type;
124 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
128 public void testLeafrefEnumResolving() throws URISyntaxException, IOException {
129 final URI ietfInterfacesPath = getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
131 final URI ifTypePath = getClass().getResource("/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
132 final URI yangTypesPath = getClass().getResource("/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
133 final URI topologyPath = getClass().getResource("/enum-test-models/abstract-topology@2013-02-08.yang").toURI();
134 final URI inetTypesPath = getClass().getResource("/enum-test-models/ietf-inet-types@2010-09-24.yang").toURI();
135 final SchemaContext context = resolveSchemaContextFromFiles(ietfInterfacesPath, ifTypePath, yangTypesPath,
136 topologyPath, inetTypesPath);
138 assertNotNull(context);
139 final BindingGenerator bindingGen = new BindingGeneratorImpl();
140 final List<Type> genTypes = bindingGen.generateTypes(context);
141 assertNotNull(genTypes);
142 assertTrue(!genTypes.isEmpty());
144 GeneratedType genInterface = null;
145 for (final Type type : genTypes) {
146 if (type instanceof GeneratedType) {
147 if (type.getPackageName().equals(
148 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
149 && type.getName().equals("Interface")) {
150 genInterface = (GeneratedType) type;
154 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
156 Type linkUpDownTrapEnable = null;
157 Type operStatus = null;
158 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
159 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
160 assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
161 for (final MethodSignature method : methods) {
162 if (method.getName().equals("getLinkUpDownTrapEnable")) {
163 linkUpDownTrapEnable = method.getReturnType();
164 } else if (method.getName().equals("getOperStatus")) {
165 operStatus = method.getReturnType();
169 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
170 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
171 assertEquals(linkUpDownTrapEnable.getPackageName(),
172 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
174 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
175 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
176 assertEquals(operStatus.getPackageName(),
177 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");