*/
package org.opendaylight.controller.sal.binding.yang.types.test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
-import java.io.IOException;
-import java.io.InputStream;
import java.util.List;
+import java.util.Set;
-import org.antlr.v4.runtime.ANTLRInputStream;
-import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.tree.ParseTree;
-import org.antlr.v4.runtime.tree.ParseTreeWalker;
+import org.junit.Ignore;
import org.junit.Test;
-import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
-import org.opendaylight.controller.antlrv4.code.gen.YangParser;
-import org.opendaylight.controller.model.parser.builder.ModuleBuilder;
-import org.opendaylight.controller.model.parser.impl.YangModelParserImpl;
import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
+import org.opendaylight.controller.sal.binding.model.api.Enumeration;
+import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
+import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
+import org.opendaylight.controller.sal.binding.model.api.Type;
import org.opendaylight.controller.yang.model.api.Module;
+import org.opendaylight.controller.yang.model.api.SchemaContext;
+import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
+import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
public class GeneratedTypesTest {
- private Module resolveModuleFromFile(final String filePath) {
- try {
- final InputStream inStream = getClass().getResourceAsStream(
- filePath);
- if (inStream != null) {
- ANTLRInputStream input = new ANTLRInputStream(inStream);
- final YangLexer lexer = new YangLexer(input);
- final CommonTokenStream tokens = new CommonTokenStream(lexer);
- final YangParser parser = new YangParser(tokens);
+ private SchemaContext resolveSchemaContextFromFiles(
+ final String... yangFiles) {
+ final YangModelParser parser = new YangModelParserImpl();
+ final Set<Module> modules = parser.parseYangModels(yangFiles);
- final ParseTree tree = parser.yang();
- final ParseTreeWalker walker = new ParseTreeWalker();
+ return parser.resolveSchemaContext(modules);
+ }
+
+ @Ignore
+ @Test
+ public void testLeafEnumResolving() {
+ final String ietfInterfacesPath = getClass().getResource(
+ "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
+ final String ifTypePath = getClass().getResource(
+ "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
+ final String yangTypesPath = getClass().getResource(
+ "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
+
+ final SchemaContext context = resolveSchemaContextFromFiles(
+ ietfInterfacesPath, ifTypePath, yangTypesPath);
+ assertTrue(context != null);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+ assertTrue(genTypes != null);
+ }
+
+ @Test
+ public void testTypedefEnumResolving() {
+ final String ianaIfTypePath = getClass().getResource(
+ "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
+
+ final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
+ assertTrue(context != null);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+ assertTrue(genTypes != null);
+ assertEquals(1, genTypes.size());
+
+ final Type type = genTypes.get(0);
+ assertTrue(type instanceof GeneratedType);
+
+ final GeneratedType genType = (GeneratedType) type;
+ assertEquals(1, genType.getEnumDefintions().size());
+
+ final Enumeration enumer = genType.getEnumDefintions().get(0);
+ assertEquals(272, enumer.getValues().size());
+ }
+
+ @Test
+ public void testMultipleModulesResolving() {
+ final String topologyPath = getClass().getResource(
+ "/abstract-topology.yang").getPath();
+ final String typesPath = getClass().getResource(
+ "/ietf-inet-types@2010-09-24.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(
+ topologyPath, typesPath);
+ assertTrue(context != null);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertTrue(genTypes != null);
+ assertEquals(13, genTypes.size());
+ }
+
+ @Test
+ public void testLeafrefResolving() {
+ final String topologyPath = getClass().getResource(
+ "/leafref-test-models/abstract-topology@2013-02-08.yang")
+ .getPath();
+ final String interfacesPath = getClass().getResource(
+ "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
+ .getPath();
+ // final String ifTypePath = getClass().getResource(
+ // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
+ final String inetTypesPath = getClass().getResource(
+ "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
+ .getPath();
+ final String yangTypesPath = getClass().getResource(
+ "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
+ .getPath();
+
+ assertTrue(topologyPath != null);
+ assertTrue(interfacesPath != null);
+ // assertTrue(ifTypePath != null);
+ assertTrue(inetTypesPath != null);
+ assertTrue(yangTypesPath != null);
+
+ // final SchemaContext context = resolveSchemaContextFromFiles(
+ // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
+ // yangTypesPath);
+ final SchemaContext context = resolveSchemaContextFromFiles(
+ topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
+ assertTrue(context != null);
+ assertEquals(4, context.getModules().size());
- final YangModelParserImpl modelParser = new YangModelParserImpl();
- walker.walk(modelParser, tree);
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> genTypes = bindingGen.generateTypes(context);
- final ModuleBuilder genModule = modelParser.getModuleBuilder();
- final Module module = genModule.build();
+ assertEquals(25, genTypes.size());
+ assertTrue(genTypes != null);
- return module;
+ int resolvedLeafrefCount = 0;
+ for (final Type type : genTypes) {
+ if (type.getName().equals("InterfaceKey")
+ && type instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+ final List<GeneratedProperty> properties = genTO
+ .getProperties();
+
+ assertTrue(properties != null);
+ for (final GeneratedProperty property : properties) {
+ if (property.getName().equals("InterfaceId")) {
+ assertTrue(property.getReturnType() != null);
+ assertFalse(property.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(property.getReturnType().getName()
+ .equals("String"));
+ resolvedLeafrefCount++;
+ }
+ }
+
+ } else if (type.getName().equals("Interface")
+ && type instanceof GeneratedType) {
+ final GeneratedType genType = (GeneratedType) type;
+ final List<MethodSignature> methods = genType
+ .getMethodDefinitions();
+
+ assertTrue(methods != null);
+ for (final MethodSignature method : methods) {
+ if (method.getName().equals("getInterfaceKey")) {
+ assertTrue(method.getReturnType() != null);
+ assertFalse(method.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(method.getReturnType().getName()
+ .equals("InterfaceKey"));
+ resolvedLeafrefCount++;
+ } else if (method.getName().equals("getHigherLayerIf")) {
+ assertTrue(method.getReturnType() != null);
+ assertFalse(method.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(method.getReturnType().getName()
+ .equals("List"));
+ resolvedLeafrefCount++;
+ }
+ }
+ } else if (type.getName().equals("NetworkLink")
+ && type instanceof GeneratedType) {
+ final GeneratedType genType = (GeneratedType) type;
+ final List<MethodSignature> methods = genType
+ .getMethodDefinitions();
+ assertTrue(methods != null);
+ for (MethodSignature method : methods) {
+ if (method.getName().equals("getInterface")) {
+ assertTrue(method.getReturnType() != null);
+ assertFalse(method.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(method.getReturnType().getName()
+ .equals("String"));
+ resolvedLeafrefCount++;
+ }
+ }
+ } else if ((type.getName().equals("SourceNode") || type.getName()
+ .equals("DestinationNode"))
+ && type instanceof GeneratedType) {
+ final GeneratedType genType = (GeneratedType) type;
+ final List<MethodSignature> methods = genType
+ .getMethodDefinitions();
+ assertTrue(methods != null);
+ for (MethodSignature method : methods) {
+ if (method.getName().equals("getId")) {
+ assertTrue(method.getReturnType() != null);
+ assertFalse(method.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(method.getReturnType().getName()
+ .equals("String"));
+ resolvedLeafrefCount++;
+ }
+ }
+ } else if (type.getName().equals("Tunnel")
+ && type instanceof GeneratedType) {
+ final GeneratedType genType = (GeneratedType) type;
+ final List<MethodSignature> methods = genType
+ .getMethodDefinitions();
+ assertTrue(methods != null);
+ for (MethodSignature method : methods) {
+ if (method.getName().equals("getTunnelKey")) {
+ assertTrue(method.getReturnType() != null);
+ assertFalse(method.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(method.getReturnType().getName()
+ .equals("TunnelKey"));
+ resolvedLeafrefCount++;
+ }
+ }
+ } else if (type.getName().equals("TunnelKey")
+ && type instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+ final List<GeneratedProperty> properties = genTO
+ .getProperties();
+
+ assertTrue(properties != null);
+ for (final GeneratedProperty property : properties) {
+ if (property.getName().equals("TunnelId")) {
+ assertTrue(property.getReturnType() != null);
+ assertFalse(property.getReturnType().equals(
+ "java.lang.Void"));
+ assertTrue(property.getReturnType().getName()
+ .equals("String"));
+ resolvedLeafrefCount++;
+ }
+ }
}
- } catch (IOException e) {
- e.printStackTrace();
}
- return null;
+ assertEquals(10, resolvedLeafrefCount);
}
+ @Ignore
@Test
public void testContainerResolving() {
- final Module module = resolveModuleFromFile("/simple-container-demo.yang");
- assertTrue(module != null);
+ final String filePath = getClass().getResource(
+ "/simple-container-demo.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(filePath);
+ assertTrue(context != null);
final BindingGenerator bindingGen = new BindingGeneratorImpl();
- final List<GeneratedType> genTypes = bindingGen.generateTypes(module);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
assertTrue(genTypes != null);
- assertEquals(genTypes.size(), 2);
-
- final GeneratedType simpleContainer = genTypes.get(0);
- final GeneratedType nestedContainer = genTypes.get(1);
+ assertEquals(3, genTypes.size());
- assertEquals(simpleContainer.getName(), "SimpleContainer");
- assertEquals(nestedContainer.getName(), "NestedContainer");
+ final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
+ final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
- assertEquals(simpleContainer.getMethodDefinitions().size(), 4);
- assertEquals(nestedContainer.getMethodDefinitions().size(), 4);
+ assertEquals("SimpleContainer", simpleContainer.getName());
+ assertEquals("NestedContainer", nestedContainer.getName());
+ assertEquals(4, simpleContainer.getMethodDefinitions().size());
+ assertEquals(4, nestedContainer.getMethodDefinitions().size());
int methodsCount = 0;
for (final MethodSignature method : simpleContainer
methodsCount++;
final MethodSignature.Parameter param = method.getParameters()
.get(0);
- assertEquals(param.getName(), "foo");
- assertEquals(param.getType().getName(), "Integer");
+ assertEquals("foo", param.getName());
+ assertEquals("Integer", param.getType().getName());
}
if (method.getName().equals("getBar")) {
methodsCount++;
}
}
- assertEquals(methodsCount, 4);
+ assertEquals(4, methodsCount);
methodsCount = 0;
for (final MethodSignature method : nestedContainer
methodsCount++;
final MethodSignature.Parameter param = method.getParameters()
.get(0);
- assertEquals(param.getName(), "foo");
- assertEquals(param.getType().getName(), "Short");
+ assertEquals("foo", param.getName());
+ assertEquals("Short", param.getType().getName());
}
if (method.getName().equals("getBar")) {
methodsCount++;
}
}
- assertEquals(methodsCount, 4);
+ assertEquals(4, methodsCount);
}
+ @Ignore
@Test
public void testLeafListResolving() {
- final Module module = resolveModuleFromFile("/simple-leaf-list-demo.yang");
- assertTrue(module != null);
+ final String filePath = getClass().getResource(
+ "/simple-leaf-list-demo.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(filePath);
+ assertTrue(context != null);
final BindingGenerator bindingGen = new BindingGeneratorImpl();
- final List<GeneratedType> genTypes = bindingGen.generateTypes(module);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
assertTrue(genTypes != null);
- assertEquals(genTypes.size(), 2);
+ assertEquals(3, genTypes.size());
- final GeneratedType simpleContainer = genTypes.get(0);
- final GeneratedType nestedContainer = genTypes.get(1);
+ final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
+ final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
- assertEquals(simpleContainer.getName(), "SimpleContainer");
- assertEquals(nestedContainer.getName(), "NestedContainer");
-
- // FIXME: uncomment after fix in DOM tree parser - LeafSchemaNode bad
- // isConfig resolving
- assertEquals(simpleContainer.getMethodDefinitions().size(), 4);
- assertEquals(nestedContainer.getMethodDefinitions().size(), 3);
+ assertEquals("SimpleContainer", simpleContainer.getName());
+ assertEquals("NestedContainer", nestedContainer.getName());
+ assertEquals(4, simpleContainer.getMethodDefinitions().size());
+ assertEquals(3, nestedContainer.getMethodDefinitions().size());
int methodsCount = 0;
for (final MethodSignature method : simpleContainer
methodsCount++;
final MethodSignature.Parameter param = method.getParameters()
.get(0);
- assertEquals(param.getName(), "foo");
- assertEquals(param.getType().getName(), "List");
+ assertEquals("foo", param.getName());
+ assertEquals("List", param.getType().getName());
}
if (method.getName().equals("getBar")) {
methodsCount++;
}
}
- assertEquals(methodsCount, 4);
+ assertEquals(4, methodsCount);
methodsCount = 0;
for (final MethodSignature method : nestedContainer
methodsCount++;
final MethodSignature.Parameter param = method.getParameters()
.get(0);
- assertEquals(param.getName(), "foo");
- assertEquals(param.getType().getName(), "Short");
+ assertEquals("foo", param.getName());
+ assertEquals("Short", param.getType().getName());
}
if (method.getName().equals("getBar")) {
methodsCount++;
}
}
- assertEquals(methodsCount, 3);
+ assertEquals(3, methodsCount);
}
+ @Ignore
@Test
public void testListResolving() {
- final Module module = resolveModuleFromFile("/simple-list-demo.yang");
- assertTrue(module != null);
+ final String filePath = getClass()
+ .getResource("/simple-list-demo.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(filePath);
+ assertTrue(context != null);
final BindingGenerator bindingGen = new BindingGeneratorImpl();
- final List<GeneratedType> genTypes = bindingGen.generateTypes(module);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
assertTrue(genTypes != null);
- assertEquals(genTypes.size(), 3);
+ assertEquals(5, genTypes.size());
+
+ int genTypesCount = 0;
+ int genTOsCount = 0;
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType) {
+ final GeneratedType genType = (GeneratedType) type;
+ if (genType.getName().equals("ListParentContainer")) {
+ assertEquals(2, genType.getMethodDefinitions().size());
+ genTypesCount++;
+ } else if (genType.getName().equals("SimpleList")) {
+ assertEquals(7, genType.getMethodDefinitions().size());
+ final List<MethodSignature> methods = genType
+ .getMethodDefinitions();
+ int methodsCount = 0;
+ for (final MethodSignature method : methods) {
+ if (method.getName().equals("getSimpleListKey")) {
+ assertEquals("SimpleListKey", method
+ .getReturnType().getName());
+ methodsCount++;
+ } else if (method.getName().equals(
+ "getListChildContainer")) {
+ assertEquals("ListChildContainer", method
+ .getReturnType().getName());
+ methodsCount++;
+ } else if (method.getName().equals("getFoo")) {
+ methodsCount++;
+ } else if (method.getName().equals("setFoo")) {
+ methodsCount++;
+ } else if (method.getName().equals("getSimpleLeafList")) {
+ methodsCount++;
+ } else if (method.getName().equals("setSimpleLeafList")) {
+ methodsCount++;
+ } else if (method.getName().equals("getBar")) {
+ methodsCount++;
+ }
+ }
+ assertEquals(7, methodsCount);
+ genTypesCount++;
+ } else if (genType.getName().equals("ListChildContainer")) {
+ assertEquals(2, genType.getMethodDefinitions().size());
+ genTypesCount++;
+ }
+ } else if (type instanceof GeneratedTransferObject) {
+ genTOsCount++;
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+ final List<GeneratedProperty> properties = genTO
+ .getProperties();
+ final List<GeneratedProperty> hashProps = genTO
+ .getHashCodeIdentifiers();
+ final List<GeneratedProperty> equalProps = genTO
+ .getEqualsIdentifiers();
+
+ assertEquals(1, properties.size());
+ assertEquals("ListKey", properties.get(0).getName());
+ assertEquals("Byte", properties.get(0).getReturnType()
+ .getName());
+ assertEquals(true, properties.get(0).isReadOnly());
+ assertEquals(1, hashProps.size());
+ assertEquals("ListKey", hashProps.get(0).getName());
+ assertEquals("Byte", hashProps.get(0).getReturnType().getName());
+ assertEquals(1, equalProps.size());
+ assertEquals("ListKey", equalProps.get(0).getName());
+ assertEquals("Byte", equalProps.get(0).getReturnType()
+ .getName());
+ }
+ }
+ assertEquals(3, genTypesCount);
+ assertEquals(1, genTOsCount);
+ }
+
+ @Test
+ public void testListCompositeKeyResolving() {
+ final String filePath = getClass().getResource(
+ "/list-composite-key.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(filePath);
+
+ assertTrue(context != null);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertTrue(genTypes != null);
+ assertEquals(7, genTypes.size());
+
+ int genTypesCount = 0;
+ int genTOsCount = 0;
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType) {
+ genTypesCount++;
+ } else if (type instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+
+ if (genTO.getName().equals("CompositeKeyListKey")) {
+ final List<GeneratedProperty> properties = genTO
+ .getProperties();
+ int propertyCount = 0;
+ for (final GeneratedProperty prop : properties) {
+ if (prop.getName().equals("Key1")) {
+ propertyCount++;
+ } else if (prop.getName().equals("Key2")) {
+ propertyCount++;
+ }
+ }
+ assertEquals(2, propertyCount);
+ genTOsCount++;
+ } else if (genTO.getName().equals("InnerListKey")) {
+ final List<GeneratedProperty> properties = genTO
+ .getProperties();
+ assertEquals(1, properties.size());
+ genTOsCount++;
+ }
+ }
+ }
+
+ assertEquals(5, genTypesCount);
+ assertEquals(2, genTOsCount);
}
@Test
public void testGeneratedTypes() {
- final Module module = resolveModuleFromFile("/demo-topology.yang");
- assertTrue(module != null);
+ final String filePath = getClass().getResource("/demo-topology.yang")
+ .getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(filePath);
+ assertTrue(context != null);
final BindingGenerator bindingGen = new BindingGeneratorImpl();
- final List<GeneratedType> genTypes = bindingGen.generateTypes(module);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
assertTrue(genTypes != null);
- assertEquals(genTypes.size(), 10);
+ assertEquals(14, genTypes.size());
+
+ int genTypesCount = 0;
+ int genTOsCount = 0;
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType) {
+ genTypesCount++;
+ } else if (type instanceof GeneratedTransferObject) {
+ genTOsCount++;
+ }
+ }
+
+ assertEquals(11, genTypesCount);
+ assertEquals(3, genTOsCount);
}
}