Fixed implementation of TypeProviderImpl for Leafref resolving. 03/203/3
authorlsedlak <lsedlak@cisco.com>
Wed, 17 Apr 2013 14:21:26 +0000 (16:21 +0200)
committerlsedlak <lsedlak@cisco.com>
Tue, 23 Apr 2013 14:09:40 +0000 (16:09 +0200)
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 <lsedlak@cisco.com>
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/yang/types/test/GeneratedTypesTest.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java
opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/SchemaContext.java

index 54abd3d..bb5e80a 100644 (file)
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.controller.sal.binding.yang.types;\r
-\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.Queue;\r
-import java.util.Set;\r
-\r
-import org.opendaylight.controller.binding.generator.util.Types;\r
-import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;\r
-import org.opendaylight.controller.sal.binding.model.api.Type;\r
-import org.opendaylight.controller.yang.common.QName;\r
-import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.DataNodeContainer;\r
-import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.LeafSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.ListSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.Module;\r
-import org.opendaylight.controller.yang.model.api.ModuleImport;\r
-import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;\r
-import org.opendaylight.controller.yang.model.api.SchemaContext;\r
-import org.opendaylight.controller.yang.model.api.SchemaPath;\r
-import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
-import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition;\r
-import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;\r
-import org.opendaylight.controller.yang.model.util.Leafref;\r
-\r
-public class TypeProviderImpl implements TypeProvider {\r
-\r
-    private SchemaContext schemaContext;\r
-\r
-    public TypeProviderImpl(SchemaContext schemaContext) {\r
-        this.schemaContext = schemaContext;\r
-    }\r
-\r
-    /*\r
-     * (non-Javadoc)\r
-     * \r
-     * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#\r
-     * javaTypeForYangType(java.lang.String)\r
-     */\r
-    @Override\r
-    public Type javaTypeForYangType(String type) {\r
-        Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
-                .javaTypeForYangType(type);\r
-        return t;\r
-    }\r
-\r
-    @Override\r
-    public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type) {\r
-        Type returnType = null;\r
-        if (type != null) {\r
-            if (type instanceof Leafref) {\r
-                final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) type;\r
-                returnType = provideTypeForLeafref(leafref);\r
-            } else if (type instanceof IdentityrefTypeDefinition) {\r
-\r
-            } else {\r
-                returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
-                        .javaTypeForSchemaDefinitionType(type);\r
-            }\r
-        }\r
-        return returnType;\r
-    }\r
-\r
-    public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {\r
-        Type returnType = null;\r
-        if ((leafrefType != null) && (leafrefType.getPathStatement() != null)) {\r
-            final RevisionAwareXPath xpath = leafrefType.getPathStatement();\r
-            final String strXPath = xpath.toString();\r
-\r
-            if (strXPath != null) {\r
-                if (strXPath.matches(".*//[.* | .*//].*")) {\r
-                    returnType = Types.typeForClass(Object.class);\r
-                } else {\r
-                    final Module module = resolveModuleFromSchemaContext(leafrefType\r
-                            .getPath());\r
-                    if (module != null) {\r
-                        Queue<String> leafrefPath;\r
-                        if (!xpath.isAbsolute()) {\r
-                            leafrefPath = resolveRelativeXPath(xpath,\r
-                                    leafrefType.getPath());\r
-                        } else {\r
-                            leafrefPath = xpathToPrefixedPath(strXPath, module.getName());\r
-                        }\r
-\r
-                        if (leafrefPath != null) {\r
-                            final DataSchemaNode dataNode = findSchemaNodeForGivenPath(\r
-                                    module, leafrefPath);\r
-                            returnType = resolveTypeFromDataSchemaNode(dataNode);\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return returnType;\r
-    }\r
-\r
-    private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {\r
-        Type returnType = null;\r
-        if (dataNode != null) {\r
-            if (dataNode instanceof LeafSchemaNode) {\r
-                final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;\r
-                returnType = javaTypeForSchemaDefinitionType(leaf.getType());\r
-            } else if (dataNode instanceof LeafListSchemaNode) {\r
-                final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;\r
-                returnType = javaTypeForSchemaDefinitionType(leafList.getType());\r
-            }\r
-        }\r
-        return returnType;\r
-    }\r
-\r
-    /**\r
-     * Search which starts from root of Module.\r
-     * \r
-     * @param module\r
-     * @param prefixedPath\r
-     * @return\r
-     */\r
-    private DataSchemaNode findSchemaNodeForGivenPath(final Module module,\r
-            final Queue<String> prefixedPath) {\r
-        if ((module != null) && (prefixedPath != null)) {\r
-            final String modulePrefix = module.getPrefix();\r
-            String childNodeName = prefixedPath.poll();\r
-            DataNodeContainer nextContainer = null;\r
-\r
-            if ((childNodeName != null)\r
-                    && childNodeName.equals(module.getName())) {\r
-                nextContainer = module;\r
-            }\r
-\r
-            DataSchemaNode schemaNode = null;\r
-            while ((nextContainer != null) && (prefixedPath.size() > 0)) {\r
-                childNodeName = prefixedPath.poll();\r
-                if (childNodeName.contains(":")) {\r
-                    final String[] prefixedChildNode = childNodeName.split(":");\r
-                    if ((modulePrefix != null)\r
-                            && modulePrefix.equals(prefixedChildNode[0])) {\r
-                        \r
-                        childNodeName = prefixedChildNode[1];\r
-                    } else {\r
-                        final Module nextModule = resolveModuleForPrefix(\r
-                                prefixedChildNode[0], module);\r
-                        final Queue<String> nextModulePrefixedPath = new LinkedList<String>();\r
-                        \r
-                        nextModulePrefixedPath.add(nextModule.getName());\r
-                        nextModulePrefixedPath.add(childNodeName);\r
-                        nextModulePrefixedPath.addAll(prefixedPath);\r
-                        prefixedPath.clear();\r
-                        \r
-                        schemaNode = findSchemaNodeForGivenPath(nextModule,\r
-                                nextModulePrefixedPath);\r
-                        \r
-                        return schemaNode;\r
-                    }\r
-                }\r
-                \r
-                schemaNode = nextContainer.getDataChildByName(childNodeName);\r
-                if (schemaNode instanceof ContainerSchemaNode) {\r
-                    nextContainer = (ContainerSchemaNode) schemaNode;\r
-                } else if (schemaNode instanceof ListSchemaNode) {\r
-                    nextContainer = (ListSchemaNode) schemaNode;\r
-                } else {\r
-                    return schemaNode;\r
-                }\r
-            }\r
-        }\r
-\r
-        return null;\r
-    }\r
-\r
-    private Module resolveModuleFromSchemaContext(final SchemaPath schemaPath) {\r
-        final Set<Module> modules = schemaContext.getModules();\r
-        final String moduleName = resolveModuleName(schemaPath);\r
-        if ((moduleName != null) && (modules != null)) {\r
-            for (final Module module : modules) {\r
-                if (module.getName().equals(moduleName)) {\r
-                    return module;\r
-                }\r
-            }\r
-        }\r
-        return null;\r
-    }\r
-\r
-    private String resolveModuleName(final SchemaPath schemaPath) {\r
-        if ((schemaPath != null) && (schemaPath.getPath() != null)) {\r
-            final QName qname = schemaPath.getPath().get(0);\r
-            if ((qname != null) && (qname.getLocalName() != null)) {\r
-                return qname.getLocalName();\r
-            }\r
-        }\r
-        return "";\r
-    }\r
-\r
-    private Queue<String> xpathToPrefixedPath(final String xpath, final String moduleName) {\r
-        final Queue<String> retQueue = new LinkedList<String>();\r
-        if ((xpath != null) && (moduleName != null)) {\r
-            final String[] prefixedPath = xpath.split("/");\r
-            \r
-            retQueue.add(moduleName);\r
-            if (prefixedPath != null) {\r
-                for (int i = 0; i < prefixedPath.length; ++i) {\r
-                    if (!prefixedPath[i].isEmpty()) {\r
-                        retQueue.add(prefixedPath[i]);\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return retQueue;\r
-    }\r
-\r
-    private Module resolveModuleForPrefix(final String prefix,\r
-            final Module parent) {\r
-        if ((prefix != null) && (parent != null)) {\r
-            final Set<ModuleImport> imports = parent.getImports();\r
-\r
-            if (imports != null) {\r
-                for (final ModuleImport impModule : imports) {\r
-                    final String impModPrefix = impModule.getPrefix();\r
-                    if ((impModPrefix != null) && prefix.equals(impModPrefix)) {\r
-                        return resolveModuleFromContext(prefix,\r
-                                impModule.getModuleName());\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return null;\r
-    }\r
-\r
-    private Module resolveModuleFromContext(final String prefix,\r
-            final String moduleName) {\r
-        final Set<Module> modules = schemaContext.getModules();\r
-\r
-        if ((prefix != null) && (moduleName != null) && (modules != null)) {\r
-            for (Module module : modules) {\r
-                if ((module != null) && prefix.equals(module.getPrefix())\r
-                        && moduleName.equals(module.getName())) {\r
-                    return module;\r
-                }\r
-            }\r
-        }\r
-        return null;\r
-    }\r
-\r
-    private Queue<String> resolveRelativeXPath(\r
-            final RevisionAwareXPath relativeXPath,\r
-            final SchemaPath leafrefSchemaPath) {\r
-        final Queue<String> absolutePath = new LinkedList<String>();\r
-\r
-        if ((relativeXPath != null) && !relativeXPath.isAbsolute()\r
-                && (leafrefSchemaPath != null)) {\r
-            final String strXPath = relativeXPath.toString();\r
-            if (strXPath != null) {\r
-                final String[] xpaths = strXPath.split("/");\r
-\r
-                if (xpaths != null) {\r
-                    int colCount = 0;\r
-                    while (xpaths[colCount].contains("..")) {\r
-                        ++colCount;\r
-                    }\r
-                    final List<QName> path = leafrefSchemaPath.getPath();\r
-                    if (path != null) {\r
-                        int lenght = path.size() - colCount;\r
-                        for (int i = 0; i < lenght; ++i) {\r
-                            absolutePath.add(path.get(i).getLocalName());\r
-                        }\r
-                        for (int i = colCount; i < xpaths.length; ++i) {\r
-                            absolutePath.add(xpaths[i]);\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return absolutePath;\r
-    }\r
-}\r
+/*
+ * 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<String> 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<String> 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<String> nextModulePrefixedPath = new LinkedList<String>();
+                        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<String> xpathToPrefixedPath(final String xpath,
+            final String moduleName) {
+        final Queue<String> retQueue = new LinkedList<String>();
+        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<ModuleImport> 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<Module> 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<String> resolveRelativeXPath(
+            final RevisionAwareXPath relativeXPath,
+            final SchemaPath leafrefSchemaPath) {
+        final Queue<String> absolutePath = new LinkedList<String>();
+
+        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<QName> 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;
+    }
+}
index 4480f25..425c232 100644 (file)
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.controller.sal.binding.yang.types.test;\r
-\r
-import static org.junit.Assert.*;\r
-\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.junit.Ignore;\r
-import org.junit.Test;\r
-import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;\r
-import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;\r
-import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;\r
-import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
-import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
-import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
-import org.opendaylight.controller.sal.binding.model.api.Type;\r
-import org.opendaylight.controller.yang.model.api.Module;\r
-import org.opendaylight.controller.yang.model.api.SchemaContext;\r
-import org.opendaylight.controller.yang.model.parser.api.YangModelParser;\r
-import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;\r
-\r
-public class GeneratedTypesTest {\r
-\r
-    private SchemaContext resolveSchemaContextFromFiles(\r
-            final String... yangFiles) {\r
-        final YangModelParser parser = new YangModelParserImpl();\r
-        final Set<Module> modules = parser.parseYangModels(yangFiles);\r
-\r
-        return parser.resolveSchemaContext(modules);\r
-    }\r
-\r
-    @Test\r
-    public void testMultipleModulesResolving() {\r
-        final String topologyPath = getClass().getResource(\r
-                "/abstract-topology.yang").getPath();\r
-        final String typesPath = getClass().getResource(\r
-                "/ietf-inet-types@2010-09-24.yang").getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(\r
-                topologyPath, typesPath);\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(11, genTypes.size());\r
-    }\r
-\r
-    @Ignore\r
-    @Test\r
-    public void testLeafrefResolving() {\r
-        final String topologyPath = getClass().getResource(\r
-                "/leafref-test-models/abstract-topology@2013-02-08.yang")\r
-                .getPath();\r
-        final String interfacesPath = getClass().getResource(\r
-                "/leafref-test-models/ietf-interfaces@2012-11-15.yang")\r
-                .getPath();\r
-//        final String ifTypePath = getClass().getResource(\r
-//                "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();\r
-        final String inetTypesPath = getClass().getResource(\r
-                "/leafref-test-models/ietf-inet-types@2010-09-24.yang")\r
-                .getPath();\r
-        final String yangTypesPath = getClass().getResource(\r
-                "/leafref-test-models/ietf-yang-types@2010-09-24.yang")\r
-                .getPath();\r
-\r
-        assertTrue(topologyPath != null);\r
-        assertTrue(interfacesPath != null);\r
-//        assertTrue(ifTypePath != null);\r
-        assertTrue(inetTypesPath != null);\r
-        assertTrue(yangTypesPath != null);\r
-\r
-//        final SchemaContext context = resolveSchemaContextFromFiles(\r
-//                topologyPath, interfacesPath, ifTypePath, inetTypesPath, yangTypesPath);\r
-        final SchemaContext context = resolveSchemaContextFromFiles(\r
-                topologyPath, interfacesPath, inetTypesPath, yangTypesPath);\r
-        assertTrue(context != null);\r
-        assertEquals(4, context.getModules().size());\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertEquals(21, genTypes.size());\r
-        assertTrue(genTypes != null);\r
-\r
-        for (final Type genType : genTypes) {\r
-            if (genType.getName().equals("Interface") && genType instanceof GeneratedType) {\r
-//                System.out.println(((GeneratedType)genType).getMethodDefinitions().toString());\r
-            } else if (genType.getName().equals("NetworkLink") && genType instanceof GeneratedType) {\r
-//                System.out.println(((GeneratedType)genType).getMethodDefinitions().toString());\r
-            }\r
-        }\r
-    }\r
-\r
-    @Test\r
-    public void testContainerResolving() {\r
-        final String filePath = getClass().getResource(\r
-                "/simple-container-demo.yang").getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(2, genTypes.size());\r
-\r
-        final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);\r
-        final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);\r
-\r
-        assertEquals("SimpleContainer", simpleContainer.getName());\r
-        assertEquals("NestedContainer", nestedContainer.getName());\r
-        assertEquals(4, simpleContainer.getMethodDefinitions().size());\r
-        assertEquals(4, nestedContainer.getMethodDefinitions().size());\r
-\r
-        int methodsCount = 0;\r
-        for (final MethodSignature method : simpleContainer\r
-                .getMethodDefinitions()) {\r
-            if (method.getName().equals("getFoo")) {\r
-                method.getReturnType().getName().equals("Integer");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("setFoo")) {\r
-                methodsCount++;\r
-                final MethodSignature.Parameter param = method.getParameters()\r
-                        .get(0);\r
-                assertEquals("foo", param.getName());\r
-                assertEquals("Integer", param.getType().getName());\r
-            }\r
-\r
-            if (method.getName().equals("getBar")) {\r
-                method.getReturnType().getName().equals("String");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("getNestedContainer")) {\r
-                method.getReturnType().getName().equals("NestedContainer");\r
-                methodsCount++;\r
-            }\r
-        }\r
-        assertEquals(4, methodsCount);\r
-\r
-        methodsCount = 0;\r
-        for (final MethodSignature method : nestedContainer\r
-                .getMethodDefinitions()) {\r
-            if (method.getName().equals("getFoo")) {\r
-                method.getReturnType().getName().equals("Short");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("setFoo")) {\r
-                methodsCount++;\r
-                final MethodSignature.Parameter param = method.getParameters()\r
-                        .get(0);\r
-                assertEquals("foo", param.getName());\r
-                assertEquals("Short", param.getType().getName());\r
-            }\r
-\r
-            if (method.getName().equals("getBar")) {\r
-                method.getReturnType().getName().equals("String");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("setBar")) {\r
-                method.getReturnType().getName().equals("String");\r
-                methodsCount++;\r
-            }\r
-        }\r
-        assertEquals(4, methodsCount);\r
-    }\r
-\r
-    @Test\r
-    public void testLeafListResolving() {\r
-        final String filePath = getClass().getResource(\r
-                "/simple-leaf-list-demo.yang").getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(2, genTypes.size());\r
-\r
-        final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);\r
-        final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);\r
-\r
-        assertEquals("SimpleContainer", simpleContainer.getName());\r
-        assertEquals("NestedContainer", nestedContainer.getName());\r
-        assertEquals(4, simpleContainer.getMethodDefinitions().size());\r
-        assertEquals(3, nestedContainer.getMethodDefinitions().size());\r
-\r
-        int methodsCount = 0;\r
-        for (final MethodSignature method : simpleContainer\r
-                .getMethodDefinitions()) {\r
-            if (method.getName().equals("getFoo")) {\r
-                method.getReturnType().getName().equals("List");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("setFoo")) {\r
-                methodsCount++;\r
-                final MethodSignature.Parameter param = method.getParameters()\r
-                        .get(0);\r
-                assertEquals("foo", param.getName());\r
-                assertEquals("List", param.getType().getName());\r
-            }\r
-\r
-            if (method.getName().equals("getBar")) {\r
-                method.getReturnType().getName().equals("String");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("getNestedContainer")) {\r
-                method.getReturnType().getName().equals("NestedContainer");\r
-                methodsCount++;\r
-            }\r
-        }\r
-        assertEquals(4, methodsCount);\r
-\r
-        methodsCount = 0;\r
-        for (final MethodSignature method : nestedContainer\r
-                .getMethodDefinitions()) {\r
-            if (method.getName().equals("getFoo")) {\r
-                method.getReturnType().getName().equals("Short");\r
-                methodsCount++;\r
-            }\r
-\r
-            if (method.getName().equals("setFoo")) {\r
-                methodsCount++;\r
-                final MethodSignature.Parameter param = method.getParameters()\r
-                        .get(0);\r
-                assertEquals("foo", param.getName());\r
-                assertEquals("Short", param.getType().getName());\r
-            }\r
-\r
-            if (method.getName().equals("getBar")) {\r
-                method.getReturnType().getName().equals("List");\r
-                methodsCount++;\r
-            }\r
-        }\r
-        assertEquals(3, methodsCount);\r
-    }\r
-\r
-    @Test\r
-    public void testListResolving() {\r
-        final String filePath = getClass()\r
-                .getResource("/simple-list-demo.yang").getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(4, genTypes.size());\r
-\r
-        int genTypesCount = 0;\r
-        int genTOsCount = 0;\r
-        for (final Type type : genTypes) {\r
-            if (type instanceof GeneratedType) {\r
-                final GeneratedType genType = (GeneratedType) type;\r
-                if (genType.getName().equals("ListParentContainer")) {\r
-                    assertEquals(2, genType.getMethodDefinitions().size());\r
-                    genTypesCount++;\r
-                } else if (genType.getName().equals("SimpleList")) {\r
-                    assertEquals(7, genType.getMethodDefinitions().size());\r
-                    final List<MethodSignature> methods = genType\r
-                            .getMethodDefinitions();\r
-                    int methodsCount = 0;\r
-                    for (final MethodSignature method : methods) {\r
-                        if (method.getName().equals("getSimpleListKey")) {\r
-                            assertEquals("SimpleListKey", method\r
-                                    .getReturnType().getName());\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals(\r
-                                "getListChildContainer")) {\r
-                            assertEquals("ListChildContainer", method\r
-                                    .getReturnType().getName());\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals("getFoo")) {\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals("setFoo")) {\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals("getSimpleLeafList")) {\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals("setSimpleLeafList")) {\r
-                            methodsCount++;\r
-                        } else if (method.getName().equals("getBar")) {\r
-                            methodsCount++;\r
-                        }\r
-                    }\r
-                    assertEquals(7, methodsCount);\r
-                    genTypesCount++;\r
-                } else if (genType.getName().equals("ListChildContainer")) {\r
-                    assertEquals(2, genType.getMethodDefinitions().size());\r
-                    genTypesCount++;\r
-                }\r
-            } else if (type instanceof GeneratedTransferObject) {\r
-                genTOsCount++;\r
-                final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
-                final List<GeneratedProperty> properties = genTO\r
-                        .getProperties();\r
-                final List<GeneratedProperty> hashProps = genTO\r
-                        .getHashCodeIdentifiers();\r
-                final List<GeneratedProperty> equalProps = genTO\r
-                        .getEqualsIdentifiers();\r
-\r
-                assertEquals(1, properties.size());\r
-                assertEquals("ListKey", properties.get(0).getName());\r
-                assertEquals("Byte", properties.get(0).getReturnType()\r
-                        .getName());\r
-                assertEquals(true, properties.get(0).isReadOnly());\r
-                assertEquals(1, hashProps.size());\r
-                assertEquals("ListKey", hashProps.get(0).getName());\r
-                assertEquals("Byte", hashProps.get(0).getReturnType().getName());\r
-                assertEquals(1, equalProps.size());\r
-                assertEquals("ListKey", equalProps.get(0).getName());\r
-                assertEquals("Byte", equalProps.get(0).getReturnType()\r
-                        .getName());\r
-            }\r
-        }\r
-        assertEquals(3, genTypesCount);\r
-        assertEquals(1, genTOsCount);\r
-    }\r
-\r
-    @Test\r
-    public void testListCompositeKeyResolving() {\r
-        final String filePath = getClass().getResource(\r
-                "/list-composite-key.yang").getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(6, genTypes.size());\r
-\r
-        int genTypesCount = 0;\r
-        int genTOsCount = 0;\r
-        for (final Type type : genTypes) {\r
-            if (type instanceof GeneratedType) {\r
-                genTypesCount++;\r
-            } else if (type instanceof GeneratedTransferObject) {\r
-                final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
-\r
-                if (genTO.getName().equals("CompositeKeyListKey")) {\r
-                    final List<GeneratedProperty> properties = genTO\r
-                            .getProperties();\r
-                    int propertyCount = 0;\r
-                    for (final GeneratedProperty prop : properties) {\r
-                        if (prop.getName().equals("Key1")) {\r
-                            propertyCount++;\r
-                        } else if (prop.getName().equals("Key2")) {\r
-                            propertyCount++;\r
-                        }\r
-                    }\r
-                    assertEquals(2, propertyCount);\r
-                    genTOsCount++;\r
-                } else if (genTO.getName().equals("InnerListKey")) {\r
-                    final List<GeneratedProperty> properties = genTO\r
-                            .getProperties();\r
-                    assertEquals(1, properties.size());\r
-                    genTOsCount++;\r
-                }\r
-            }\r
-        }\r
-\r
-        assertEquals(4, genTypesCount);\r
-        assertEquals(2, genTOsCount);\r
-    }\r
-\r
-    @Test\r
-    public void testGeneratedTypes() {\r
-        final String filePath = getClass().getResource("/demo-topology.yang")\r
-                .getPath();\r
-        final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-        assertTrue(context != null);\r
-\r
-        final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
-        final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
-        assertTrue(genTypes != null);\r
-        assertEquals(13, genTypes.size());\r
-\r
-        int genTypesCount = 0;\r
-        int genTOsCount = 0;\r
-        for (final Type type : genTypes) {\r
-            if (type instanceof GeneratedType) {\r
-                genTypesCount++;\r
-            } else if (type instanceof GeneratedTransferObject) {\r
-                genTOsCount++;\r
-            }\r
-        }\r
-\r
-        assertEquals(10, genTypesCount);\r
-        assertEquals(3, genTOsCount);\r
-    }\r
-}\r
+/*
+ * 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<Module> 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<Type> 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<Type> 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<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++;
+                    }
+                }
+            }
+        }
+        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<Type> 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<Type> 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<Type> 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<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(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<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(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<Type> 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);
+    }
+}
index 5490223..acde379 100644 (file)
@@ -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<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(
             String... yangFiles) {
-        InputStream[] streams = new InputStream[yangFiles.length];
-        FileInputStream inStream = null;
+        final InputStream[] streams = loadStreams(yangFiles);
+
+        if (streams != null) {
+            final Map<String, TreeMap<Date, ModuleBuilder>> result = resolveModuleBuildersFromStreams(streams);
+            cloaseStreams(streams);
+
+            if (result != null) {
+                return result;
+            }
+        }
+        return new HashMap<String, TreeMap<Date, ModuleBuilder>>();
+    }
+
+    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<String, TreeMap<Date, ModuleBuilder>> 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;
+        }
     }
 
 }
index 765220b..6675b80 100644 (file)
@@ -13,6 +13,7 @@ import java.net.URI;
 import java.text.DateFormat;\r
 import java.text.ParseException;\r
 import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
 import java.util.Collections;\r
 import java.util.Date;\r
 import java.util.List;\r
@@ -321,7 +322,9 @@ final class YangModelParserListenerImpl extends YangParserBaseListener {
                 if ("union".equals(typeName)) {\r
                     moduleBuilder.addUnionType(getActualPath());\r
                 } else {\r
-                    type = parseTypeBody(typeName, typeBody, getActualPath(),\r
+                    List<String> typePath = new ArrayList<String>(actualPath);\r
+                    typePath.remove(0);\r
+                    type = parseTypeBody(typeName, typeBody, typePath,\r
                             namespace, revision, yangModelPrefix);\r
                     moduleBuilder.setType(type, getActualPath());\r
                 }\r
index cbeed09..8a032ca 100644 (file)
@@ -193,7 +193,7 @@ public final class YangModelBuilderUtil {
             URI namespace, Date revision, String prefix) {
         final List<QName> path = new ArrayList<QName>();
         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<Byte> bytes = Collections.emptyList();
             type = new BinaryType(bytes, lengthStatements, null);
index c497c3d..e449dfa 100644 (file)
@@ -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<RpcDefinition> getOperations();
     
     Set<ExtensionDefinition> getExtensions();
+    
+    Module findModuleByName(final String name, final Date revision);
+    
+    Module findModuleByNamespace(final URI namespace);
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.