From 2a42a9c68c2149801f3c26d0f4c46cc857cffb00 Mon Sep 17 00:00:00 2001 From: lsedlak Date: Wed, 17 Apr 2013 16:21:26 +0200 Subject: [PATCH] Fixed implementation of TypeProviderImpl for Leafref resolving. Since the modification of resolving of inner type definitions as ExtendedTypes there had to be done minor modifications in javaTypeForSchemaDefinitionType and baseTypeForExtendedType methods. Also the SchemaPath no longer contains the module name as first element so the module name is now retrieved directly from SchemaContext via findModuleByNamespace method; Added findModuleByName and findModuleByNamespace into SchemaContext in yang-model-api; Added implementation of findModuleByName and findModuleByNamespace for private implementation of SchemaContextImpl in YangModelParserImpl; Fixed possible resource leak in resolveModuleBuildersFromStreams in YangModelBuilderImpl - implemented loadStreams and closeStreams methods; Added implementation of test case for resolving of leafrefs and leafref types from multiple modules; Fixed wrong constructor call of Leafref Type Definition in parseTypeBody method in YangModelBuilderUtil; Fixed resolving of SchemaPath for Type statement in YangModelParserListenerImpl; Change-Id: I6a037a746562d856d12ea71d3fb095e902e1c204 Signed-off-by: Lukas Sedlak --- .../binding/yang/types/TypeProviderImpl.java | 568 +++++------ .../yang/types/test/GeneratedTypesTest.java | 925 ++++++++++-------- .../parser/impl/YangModelParserImpl.java | 83 +- .../impl/YangModelParserListenerImpl.java | 5 +- .../parser/util/YangModelBuilderUtil.java | 4 +- .../yang/model/api/SchemaContext.java | 6 + 6 files changed, 881 insertions(+), 710 deletions(-) diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java index 54abd3daea..bb5e80a525 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java @@ -1,283 +1,285 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.yang.types; - -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Set; - -import org.opendaylight.controller.binding.generator.util.Types; -import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider; -import org.opendaylight.controller.sal.binding.model.api.Type; -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; -import org.opendaylight.controller.yang.model.api.DataNodeContainer; -import org.opendaylight.controller.yang.model.api.DataSchemaNode; -import org.opendaylight.controller.yang.model.api.LeafListSchemaNode; -import org.opendaylight.controller.yang.model.api.LeafSchemaNode; -import org.opendaylight.controller.yang.model.api.ListSchemaNode; -import org.opendaylight.controller.yang.model.api.Module; -import org.opendaylight.controller.yang.model.api.ModuleImport; -import org.opendaylight.controller.yang.model.api.RevisionAwareXPath; -import org.opendaylight.controller.yang.model.api.SchemaContext; -import org.opendaylight.controller.yang.model.api.SchemaPath; -import org.opendaylight.controller.yang.model.api.TypeDefinition; -import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.controller.yang.model.util.Leafref; - -public class TypeProviderImpl implements TypeProvider { - - private SchemaContext schemaContext; - - public TypeProviderImpl(SchemaContext schemaContext) { - this.schemaContext = schemaContext; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider# - * javaTypeForYangType(java.lang.String) - */ - @Override - public Type javaTypeForYangType(String type) { - Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER - .javaTypeForYangType(type); - return t; - } - - @Override - public Type javaTypeForSchemaDefinitionType(final TypeDefinition type) { - Type returnType = null; - if (type != null) { - if (type instanceof Leafref) { - final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) type; - returnType = provideTypeForLeafref(leafref); - } else if (type instanceof IdentityrefTypeDefinition) { - - } else { - returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER - .javaTypeForSchemaDefinitionType(type); - } - } - return returnType; - } - - public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) { - Type returnType = null; - if ((leafrefType != null) && (leafrefType.getPathStatement() != null)) { - final RevisionAwareXPath xpath = leafrefType.getPathStatement(); - final String strXPath = xpath.toString(); - - if (strXPath != null) { - if (strXPath.matches(".*//[.* | .*//].*")) { - returnType = Types.typeForClass(Object.class); - } else { - final Module module = resolveModuleFromSchemaContext(leafrefType - .getPath()); - if (module != null) { - Queue leafrefPath; - if (!xpath.isAbsolute()) { - leafrefPath = resolveRelativeXPath(xpath, - leafrefType.getPath()); - } else { - leafrefPath = xpathToPrefixedPath(strXPath, module.getName()); - } - - if (leafrefPath != null) { - final DataSchemaNode dataNode = findSchemaNodeForGivenPath( - module, leafrefPath); - returnType = resolveTypeFromDataSchemaNode(dataNode); - } - } - } - } - } - return returnType; - } - - private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) { - Type returnType = null; - if (dataNode != null) { - if (dataNode instanceof LeafSchemaNode) { - final LeafSchemaNode leaf = (LeafSchemaNode) dataNode; - returnType = javaTypeForSchemaDefinitionType(leaf.getType()); - } else if (dataNode instanceof LeafListSchemaNode) { - final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode; - returnType = javaTypeForSchemaDefinitionType(leafList.getType()); - } - } - return returnType; - } - - /** - * Search which starts from root of Module. - * - * @param module - * @param prefixedPath - * @return - */ - private DataSchemaNode findSchemaNodeForGivenPath(final Module module, - final Queue prefixedPath) { - if ((module != null) && (prefixedPath != null)) { - final String modulePrefix = module.getPrefix(); - String childNodeName = prefixedPath.poll(); - DataNodeContainer nextContainer = null; - - if ((childNodeName != null) - && childNodeName.equals(module.getName())) { - nextContainer = module; - } - - DataSchemaNode schemaNode = null; - while ((nextContainer != null) && (prefixedPath.size() > 0)) { - childNodeName = prefixedPath.poll(); - if (childNodeName.contains(":")) { - final String[] prefixedChildNode = childNodeName.split(":"); - if ((modulePrefix != null) - && modulePrefix.equals(prefixedChildNode[0])) { - - childNodeName = prefixedChildNode[1]; - } else { - final Module nextModule = resolveModuleForPrefix( - prefixedChildNode[0], module); - final Queue nextModulePrefixedPath = new LinkedList(); - - nextModulePrefixedPath.add(nextModule.getName()); - nextModulePrefixedPath.add(childNodeName); - nextModulePrefixedPath.addAll(prefixedPath); - prefixedPath.clear(); - - schemaNode = findSchemaNodeForGivenPath(nextModule, - nextModulePrefixedPath); - - return schemaNode; - } - } - - schemaNode = nextContainer.getDataChildByName(childNodeName); - if (schemaNode instanceof ContainerSchemaNode) { - nextContainer = (ContainerSchemaNode) schemaNode; - } else if (schemaNode instanceof ListSchemaNode) { - nextContainer = (ListSchemaNode) schemaNode; - } else { - return schemaNode; - } - } - } - - return null; - } - - private Module resolveModuleFromSchemaContext(final SchemaPath schemaPath) { - final Set modules = schemaContext.getModules(); - final String moduleName = resolveModuleName(schemaPath); - if ((moduleName != null) && (modules != null)) { - for (final Module module : modules) { - if (module.getName().equals(moduleName)) { - return module; - } - } - } - return null; - } - - private String resolveModuleName(final SchemaPath schemaPath) { - if ((schemaPath != null) && (schemaPath.getPath() != null)) { - final QName qname = schemaPath.getPath().get(0); - if ((qname != null) && (qname.getLocalName() != null)) { - return qname.getLocalName(); - } - } - return ""; - } - - private Queue xpathToPrefixedPath(final String xpath, final String moduleName) { - final Queue retQueue = new LinkedList(); - if ((xpath != null) && (moduleName != null)) { - final String[] prefixedPath = xpath.split("/"); - - retQueue.add(moduleName); - if (prefixedPath != null) { - for (int i = 0; i < prefixedPath.length; ++i) { - if (!prefixedPath[i].isEmpty()) { - retQueue.add(prefixedPath[i]); - } - } - } - } - return retQueue; - } - - private Module resolveModuleForPrefix(final String prefix, - final Module parent) { - if ((prefix != null) && (parent != null)) { - final Set imports = parent.getImports(); - - if (imports != null) { - for (final ModuleImport impModule : imports) { - final String impModPrefix = impModule.getPrefix(); - if ((impModPrefix != null) && prefix.equals(impModPrefix)) { - return resolveModuleFromContext(prefix, - impModule.getModuleName()); - } - } - } - } - return null; - } - - private Module resolveModuleFromContext(final String prefix, - final String moduleName) { - final Set modules = schemaContext.getModules(); - - if ((prefix != null) && (moduleName != null) && (modules != null)) { - for (Module module : modules) { - if ((module != null) && prefix.equals(module.getPrefix()) - && moduleName.equals(module.getName())) { - return module; - } - } - } - return null; - } - - private Queue resolveRelativeXPath( - final RevisionAwareXPath relativeXPath, - final SchemaPath leafrefSchemaPath) { - final Queue absolutePath = new LinkedList(); - - if ((relativeXPath != null) && !relativeXPath.isAbsolute() - && (leafrefSchemaPath != null)) { - final String strXPath = relativeXPath.toString(); - if (strXPath != null) { - final String[] xpaths = strXPath.split("/"); - - if (xpaths != null) { - int colCount = 0; - while (xpaths[colCount].contains("..")) { - ++colCount; - } - final List path = leafrefSchemaPath.getPath(); - if (path != null) { - int lenght = path.size() - colCount; - for (int i = 0; i < lenght; ++i) { - absolutePath.add(path.get(i).getLocalName()); - } - for (int i = colCount; i < xpaths.length; ++i) { - absolutePath.add(xpaths[i]); - } - } - } - } - } - return absolutePath; - } -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.yang.types; + +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; + +import org.opendaylight.controller.binding.generator.util.Types; +import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider; +import org.opendaylight.controller.sal.binding.model.api.Type; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; +import org.opendaylight.controller.yang.model.api.DataNodeContainer; +import org.opendaylight.controller.yang.model.api.DataSchemaNode; +import org.opendaylight.controller.yang.model.api.LeafListSchemaNode; +import org.opendaylight.controller.yang.model.api.LeafSchemaNode; +import org.opendaylight.controller.yang.model.api.ListSchemaNode; +import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.ModuleImport; +import org.opendaylight.controller.yang.model.api.RevisionAwareXPath; +import org.opendaylight.controller.yang.model.api.SchemaContext; +import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.controller.yang.model.util.ExtendedType; +import org.opendaylight.controller.yang.model.util.Leafref; + +public class TypeProviderImpl implements TypeProvider { + + private SchemaContext schemaContext; + + public TypeProviderImpl(SchemaContext schemaContext) { + this.schemaContext = schemaContext; + } + + /* + * (non-Javadoc) + * + * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider# + * javaTypeForYangType(java.lang.String) + */ + @Override + public Type javaTypeForYangType(String type) { + Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER + .javaTypeForYangType(type); + return t; + } + + @Override + public Type javaTypeForSchemaDefinitionType(final TypeDefinition typeDefinition) { + Type returnType = null; + if (typeDefinition != null) { + if (typeDefinition instanceof Leafref) { + final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition; + returnType = provideTypeForLeafref(leafref); + } else if (typeDefinition instanceof IdentityrefTypeDefinition) { + + } else if (typeDefinition instanceof ExtendedType) { + final TypeDefinition baseType = typeDefinition.getBaseType(); + return javaTypeForSchemaDefinitionType(baseType); + } + else { + returnType = baseTypeForExtendedType(typeDefinition); + } + } + return returnType; + } + + public Type baseTypeForExtendedType(final TypeDefinition typeDefinition) { + Type returnType = null; + if (typeDefinition != null) { + if (typeDefinition instanceof ExtendedType) { + final TypeDefinition extType = typeDefinition.getBaseType(); + return baseTypeForExtendedType(extType); + } else { + returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER + .javaTypeForSchemaDefinitionType(typeDefinition); + } + } + return returnType; + } + + public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) { + Type returnType = null; + if ((leafrefType != null) && (leafrefType.getPathStatement() != null) + && (leafrefType.getPath() != null)) { + + final RevisionAwareXPath xpath = leafrefType.getPathStatement(); + final String strXPath = xpath.toString(); + + if (strXPath != null) { + if (strXPath.matches(".*//[.* | .*//].*")) { + returnType = Types.typeForClass(Object.class); + } else { + final Module module = resolveModuleFromSchemaPath(leafrefType + .getPath()); + if (module != null) { + Queue leafrefPath; + if (!xpath.isAbsolute()) { + leafrefPath = resolveRelativeXPath(xpath, + leafrefType.getPath()); + } else { + leafrefPath = xpathToPrefixedPath(strXPath, + module.getName()); + } + if (leafrefPath != null) { + final DataSchemaNode dataNode = findSchemaNodeForGivenPath( + module, leafrefPath); + returnType = resolveTypeFromDataSchemaNode(dataNode); + } + } + } + } + } + return returnType; + } + + private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) { + Type returnType = null; + if (dataNode != null) { + if (dataNode instanceof LeafSchemaNode) { + final LeafSchemaNode leaf = (LeafSchemaNode) dataNode; + returnType = javaTypeForSchemaDefinitionType(leaf.getType()); + } else if (dataNode instanceof LeafListSchemaNode) { + final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode; + returnType = javaTypeForSchemaDefinitionType(leafList.getType()); + } + } + return returnType; + } + + /** + * Search which starts from root of Module. + * + * @param module + * @param prefixedPath + * @return + */ + private DataSchemaNode findSchemaNodeForGivenPath(final Module module, + final Queue prefixedPath) { + if ((module != null) && (prefixedPath != null)) { + DataNodeContainer nextContainer = module; + final String modulePrefix = module.getPrefix(); + + String childNodeName = null; + DataSchemaNode schemaNode = null; + while ((nextContainer != null) && (prefixedPath.size() > 0)) { + childNodeName = prefixedPath.poll(); + if (childNodeName.contains(":")) { + final String[] prefixedChildNode = childNodeName.split(":"); + if ((modulePrefix != null) + && modulePrefix.equals(prefixedChildNode[0])) { + + childNodeName = prefixedChildNode[1]; + } else { + final Module nextModule = resolveModuleForPrefix( + prefixedChildNode[0], module); + final Queue nextModulePrefixedPath = new LinkedList(); + nextModulePrefixedPath.add(childNodeName); + nextModulePrefixedPath.addAll(prefixedPath); + prefixedPath.clear(); + + schemaNode = findSchemaNodeForGivenPath(nextModule, + nextModulePrefixedPath); + + return schemaNode; + } + } + + schemaNode = nextContainer.getDataChildByName(childNodeName); + if (schemaNode instanceof ContainerSchemaNode) { + nextContainer = (ContainerSchemaNode) schemaNode; + } else if (schemaNode instanceof ListSchemaNode) { + nextContainer = (ListSchemaNode) schemaNode; + } else { + return schemaNode; + } + } + } + + return null; + } + + private Module resolveModuleFromSchemaPath(final SchemaPath schemaPath) { + if ((schemaPath != null) && (schemaPath.getPath() != null)) { + final QName qname = schemaPath.getPath().get(0); + + if ((qname != null) && (qname.getNamespace() != null)) { + return schemaContext + .findModuleByNamespace(qname.getNamespace()); + } + } + return null; + } + + private Queue xpathToPrefixedPath(final String xpath, + final String moduleName) { + final Queue retQueue = new LinkedList(); + if ((xpath != null) && (moduleName != null)) { + final String[] prefixedPath = xpath.split("/"); + + if (prefixedPath != null) { + for (int i = 0; i < prefixedPath.length; ++i) { + if (!prefixedPath[i].isEmpty()) { + retQueue.add(prefixedPath[i]); + } + } + } + } + return retQueue; + } + + private Module resolveModuleForPrefix(final String prefix, + final Module parent) { + if ((prefix != null) && (parent != null)) { + final Set imports = parent.getImports(); + + if (imports != null) { + for (final ModuleImport impModule : imports) { + final String impModPrefix = impModule.getPrefix(); + if ((impModPrefix != null) && prefix.equals(impModPrefix)) { + return resolveModuleFromContext(prefix, + impModule.getModuleName()); + } + } + } + } + return null; + } + + private Module resolveModuleFromContext(final String prefix, + final String moduleName) { + final Set modules = schemaContext.getModules(); + + if ((prefix != null) && (moduleName != null) && (modules != null)) { + for (Module module : modules) { + if ((module != null) && prefix.equals(module.getPrefix()) + && moduleName.equals(module.getName())) { + return module; + } + } + } + return null; + } + + private Queue resolveRelativeXPath( + final RevisionAwareXPath relativeXPath, + final SchemaPath leafrefSchemaPath) { + final Queue absolutePath = new LinkedList(); + + if ((relativeXPath != null) && !relativeXPath.isAbsolute() + && (leafrefSchemaPath != null)) { + final String strXPath = relativeXPath.toString(); + if (strXPath != null) { + final String[] xpaths = strXPath.split("/"); + + if (xpaths != null) { + int colCount = 0; + while (xpaths[colCount].contains("..")) { + ++colCount; + } + final List path = leafrefSchemaPath.getPath(); + if (path != null) { + int lenght = path.size() - colCount; + for (int i = 0; i < lenght; ++i) { + absolutePath.add(path.get(i).getLocalName()); + } + for (int i = colCount; i < xpaths.length; ++i) { + absolutePath.add(xpaths[i]); + } + } + } + } + } + return absolutePath; + } +} diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/yang/types/test/GeneratedTypesTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/yang/types/test/GeneratedTypesTest.java index 4480f25316..425c23246d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/yang/types/test/GeneratedTypesTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/yang/types/test/GeneratedTypesTest.java @@ -1,409 +1,516 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.yang.types.test; - -import static org.junit.Assert.*; - -import java.util.List; -import java.util.Set; - -import org.junit.Ignore; -import org.junit.Test; -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.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 SchemaContext resolveSchemaContextFromFiles( - final String... yangFiles) { - final YangModelParser parser = new YangModelParserImpl(); - final Set modules = parser.parseYangModels(yangFiles); - - return parser.resolveSchemaContext(modules); - } - - @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 genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(11, genTypes.size()); - } - - @Ignore - @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 BindingGenerator bindingGen = new BindingGeneratorImpl(); - final List genTypes = bindingGen.generateTypes(context); - - assertEquals(21, genTypes.size()); - assertTrue(genTypes != null); - - for (final Type genType : genTypes) { - if (genType.getName().equals("Interface") && genType instanceof GeneratedType) { -// System.out.println(((GeneratedType)genType).getMethodDefinitions().toString()); - } else if (genType.getName().equals("NetworkLink") && genType instanceof GeneratedType) { -// System.out.println(((GeneratedType)genType).getMethodDefinitions().toString()); - } - } - } - - @Test - public void testContainerResolving() { - 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 genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(2, genTypes.size()); - - final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0); - final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1); - - 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 - .getMethodDefinitions()) { - if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Integer"); - methodsCount++; - } - - if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Integer", param.getType().getName()); - } - - if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; - } - - if (method.getName().equals("getNestedContainer")) { - method.getReturnType().getName().equals("NestedContainer"); - methodsCount++; - } - } - assertEquals(4, methodsCount); - - methodsCount = 0; - for (final MethodSignature method : nestedContainer - .getMethodDefinitions()) { - if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Short"); - methodsCount++; - } - - if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Short", param.getType().getName()); - } - - if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; - } - - if (method.getName().equals("setBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; - } - } - assertEquals(4, methodsCount); - } - - @Test - public void testLeafListResolving() { - 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 genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(2, genTypes.size()); - - final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0); - final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1); - - 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 - .getMethodDefinitions()) { - if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("List"); - methodsCount++; - } - - if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("List", param.getType().getName()); - } - - if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; - } - - if (method.getName().equals("getNestedContainer")) { - method.getReturnType().getName().equals("NestedContainer"); - methodsCount++; - } - } - assertEquals(4, methodsCount); - - methodsCount = 0; - for (final MethodSignature method : nestedContainer - .getMethodDefinitions()) { - if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Short"); - methodsCount++; - } - - if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Short", param.getType().getName()); - } - - if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("List"); - methodsCount++; - } - } - assertEquals(3, methodsCount); - } - - @Test - public void testListResolving() { - 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 genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(4, 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 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 properties = genTO - .getProperties(); - final List hashProps = genTO - .getHashCodeIdentifiers(); - final List 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 genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(6, 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 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 properties = genTO - .getProperties(); - assertEquals(1, properties.size()); - genTOsCount++; - } - } - } - - assertEquals(4, genTypesCount); - assertEquals(2, genTOsCount); - } - - @Test - public void testGeneratedTypes() { - final String filePath = getClass().getResource("/demo-topology.yang") - .getPath(); - final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); - - final BindingGenerator bindingGen = new BindingGeneratorImpl(); - final List genTypes = bindingGen.generateTypes(context); - - assertTrue(genTypes != null); - assertEquals(13, 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(10, genTypesCount); - assertEquals(3, genTOsCount); - } -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.yang.types.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.Set; + +import org.junit.Test; +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.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 SchemaContext resolveSchemaContextFromFiles( + final String... yangFiles) { + final YangModelParser parser = new YangModelParserImpl(); + final Set modules = parser.parseYangModels(yangFiles); + + return parser.resolveSchemaContext(modules); + } + + @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 genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(11, 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 BindingGenerator bindingGen = new BindingGeneratorImpl(); + final List genTypes = bindingGen.generateTypes(context); + + assertEquals(21, genTypes.size()); + assertTrue(genTypes != null); + + int resolvedLeafrefCount = 0; + for (final Type type : genTypes) { + if (type.getName().equals("InterfaceKey") + && type instanceof GeneratedTransferObject) { + final GeneratedTransferObject genTO = (GeneratedTransferObject) type; + final List 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 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 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 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 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 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++; + } + } + } + } + assertEquals(10, resolvedLeafrefCount); + } + + @Test + public void testContainerResolving() { + 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 genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(2, genTypes.size()); + + final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0); + final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1); + + 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 + .getMethodDefinitions()) { + if (method.getName().equals("getFoo")) { + method.getReturnType().getName().equals("Integer"); + methodsCount++; + } + + if (method.getName().equals("setFoo")) { + methodsCount++; + final MethodSignature.Parameter param = method.getParameters() + .get(0); + assertEquals("foo", param.getName()); + assertEquals("Integer", param.getType().getName()); + } + + if (method.getName().equals("getBar")) { + method.getReturnType().getName().equals("String"); + methodsCount++; + } + + if (method.getName().equals("getNestedContainer")) { + method.getReturnType().getName().equals("NestedContainer"); + methodsCount++; + } + } + assertEquals(4, methodsCount); + + methodsCount = 0; + for (final MethodSignature method : nestedContainer + .getMethodDefinitions()) { + if (method.getName().equals("getFoo")) { + method.getReturnType().getName().equals("Short"); + methodsCount++; + } + + if (method.getName().equals("setFoo")) { + methodsCount++; + final MethodSignature.Parameter param = method.getParameters() + .get(0); + assertEquals("foo", param.getName()); + assertEquals("Short", param.getType().getName()); + } + + if (method.getName().equals("getBar")) { + method.getReturnType().getName().equals("String"); + methodsCount++; + } + + if (method.getName().equals("setBar")) { + method.getReturnType().getName().equals("String"); + methodsCount++; + } + } + assertEquals(4, methodsCount); + } + + @Test + public void testLeafListResolving() { + 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 genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(2, genTypes.size()); + + final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0); + final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1); + + 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 + .getMethodDefinitions()) { + if (method.getName().equals("getFoo")) { + method.getReturnType().getName().equals("List"); + methodsCount++; + } + + if (method.getName().equals("setFoo")) { + methodsCount++; + final MethodSignature.Parameter param = method.getParameters() + .get(0); + assertEquals("foo", param.getName()); + assertEquals("List", param.getType().getName()); + } + + if (method.getName().equals("getBar")) { + method.getReturnType().getName().equals("String"); + methodsCount++; + } + + if (method.getName().equals("getNestedContainer")) { + method.getReturnType().getName().equals("NestedContainer"); + methodsCount++; + } + } + assertEquals(4, methodsCount); + + methodsCount = 0; + for (final MethodSignature method : nestedContainer + .getMethodDefinitions()) { + if (method.getName().equals("getFoo")) { + method.getReturnType().getName().equals("Short"); + methodsCount++; + } + + if (method.getName().equals("setFoo")) { + methodsCount++; + final MethodSignature.Parameter param = method.getParameters() + .get(0); + assertEquals("foo", param.getName()); + assertEquals("Short", param.getType().getName()); + } + + if (method.getName().equals("getBar")) { + method.getReturnType().getName().equals("List"); + methodsCount++; + } + } + assertEquals(3, methodsCount); + } + + @Test + public void testListResolving() { + 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 genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(4, 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 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 properties = genTO + .getProperties(); + final List hashProps = genTO + .getHashCodeIdentifiers(); + final List 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 genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(6, 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 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 properties = genTO + .getProperties(); + assertEquals(1, properties.size()); + genTOsCount++; + } + } + } + + assertEquals(4, genTypesCount); + assertEquals(2, genTOsCount); + } + + @Test + public void testGeneratedTypes() { + final String filePath = getClass().getResource("/demo-topology.yang") + .getPath(); + final SchemaContext context = resolveSchemaContextFromFiles(filePath); + assertTrue(context != null); + + final BindingGenerator bindingGen = new BindingGeneratorImpl(); + final List genTypes = bindingGen.generateTypes(context); + + assertTrue(genTypes != null); + assertEquals(13, 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(10, genTypesCount); + assertEquals(3, genTOsCount); + } +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java index 54902232ea..acde37953b 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java @@ -12,6 +12,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -96,20 +97,47 @@ public class YangModelParserImpl implements YangModelParser { private Map> resolveModuleBuildersFromStreams( String... yangFiles) { - InputStream[] streams = new InputStream[yangFiles.length]; - FileInputStream inStream = null; + final InputStream[] streams = loadStreams(yangFiles); + + if (streams != null) { + final Map> result = resolveModuleBuildersFromStreams(streams); + cloaseStreams(streams); + + if (result != null) { + return result; + } + } + return new HashMap>(); + } + + private InputStream[] loadStreams(final String... yangFiles) { + final InputStream[] streams = new InputStream[yangFiles.length]; for (int i = 0; i < yangFiles.length; i++) { final String yangFileName = yangFiles[i]; final File yangFile = new File(yangFileName); try { - inStream = new FileInputStream(yangFile); + streams[i] = new FileInputStream(yangFile); } catch (FileNotFoundException e) { - logger.warn("Exception while reading yang stream: " + inStream, - e); + logger.warn("Exception while reading yang stream: " + + streams[i], e); + } + } + return streams; + } + + private void cloaseStreams(final InputStream[] streams) { + if (streams != null) { + for (int i = 0; i < streams.length; ++i) { + try { + if (streams[i] != null) { + streams[i].close(); + } + } catch (IOException e) { + logger.warn("Exception while closing yang stream: " + + streams[i], e); + } } - streams[i] = inStream; } - return resolveModuleBuildersFromStreams(streams); } private Map> resolveModuleBuildersFromStreams( @@ -218,7 +246,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Search for dirty nodes (node which contains UnknownType) and resolve * unknown types. - * + * * @param modules * all available modules * @param module @@ -531,7 +559,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Go through all typedef statements from given module and search for one * with given name - * + * * @param typedefs * typedef statements to search * @param name @@ -556,7 +584,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Pull restriction from referenced type and add them to given constraints - * + * * @param referencedType * @param constraints */ @@ -586,7 +614,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Go through all augmentation definitions and resolve them. This means find * referenced node and add child nodes to it. - * + * * @param modules * all available modules * @param module @@ -624,7 +652,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Add all augment's child nodes to given target. - * + * * @param augment * @param target */ @@ -639,7 +667,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Go through identity statements defined in current module and resolve * their 'base' statement if present. - * + * * @param modules * all modules * @param module @@ -679,7 +707,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Find dependent module based on given prefix - * + * * @param modules * all available modules * @param module @@ -727,7 +755,7 @@ public class YangModelParserImpl implements YangModelParser { /** * Get module import referenced by given prefix. - * + * * @param builder * module to search * @param prefix @@ -798,6 +826,31 @@ public class YangModelParserImpl implements YangModelParser { } return extensions; } + + @Override + public Module findModuleByName(final String name, final Date revision) { + if ((name != null) && (revision != null)) { + for (final Module module : modules) { + if (module.getName().equals(name) + && module.getRevision().equals(revision)) { + return module; + } + } + } + return null; + } + + @Override + public Module findModuleByNamespace(URI namespace) { + if (namespace != null) { + for (final Module module : modules) { + if (module.getNamespace().equals(namespace)) { + return module; + } + } + } + return null; + } } } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java index 765220bf2a..6675b8068d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java @@ -13,6 +13,7 @@ import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; @@ -321,7 +322,9 @@ final class YangModelParserListenerImpl extends YangParserBaseListener { if ("union".equals(typeName)) { moduleBuilder.addUnionType(getActualPath()); } else { - type = parseTypeBody(typeName, typeBody, getActualPath(), + List typePath = new ArrayList(actualPath); + typePath.remove(0); + type = parseTypeBody(typeName, typeBody, typePath, namespace, revision, yangModelPrefix); moduleBuilder.setType(type, getActualPath()); } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java index cbeed0959e..8a032ca12c 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java @@ -193,7 +193,7 @@ public final class YangModelBuilderUtil { URI namespace, Date revision, String prefix) { final List path = new ArrayList(); QName qname; - // start from index 1 - module name ommited + // start from index 1 - module name omitted for (int i = 1; i < actualPath.size(); i++) { qname = new QName(namespace, revision, prefix, actualPath.get(i)); path.add(qname); @@ -974,7 +974,7 @@ public final class YangModelBuilderUtil { final boolean absolute = path.startsWith("/"); RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, absolute); - type = new Leafref(xpath); + type = new Leafref(actualPath, namespace, revision, xpath); } else if ("binary".equals(typeName)) { List bytes = Collections.emptyList(); type = new BinaryType(bytes, lengthStatements, null); diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/SchemaContext.java b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/SchemaContext.java index c497c3d169..e449dfaea0 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/SchemaContext.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/SchemaContext.java @@ -7,6 +7,8 @@ */ package org.opendaylight.controller.yang.model.api; +import java.net.URI; +import java.util.Date; import java.util.Set; @@ -25,4 +27,8 @@ public interface SchemaContext { Set getOperations(); Set getExtensions(); + + Module findModuleByName(final String name, final Date revision); + + Module findModuleByNamespace(final URI namespace); } -- 2.36.6