Update the API generation code and code generation sample 39/139/3
authorLukas Sedlak <lsedlak@cisco.com>
Thu, 11 Apr 2013 21:47:40 +0000 (14:47 -0700)
committerGerrit Code Review <gerrit@opendaylight.org>
Sat, 13 Apr 2013 03:04:28 +0000 (03:04 +0000)
Fixed bug in generation of package names.

Change-Id: I8314f44ddf08aceb29b0ccccbb81d2c0eaf54d1d
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
27 files changed:
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/pom.xml
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/BindingGeneratorImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/EnumerationBuilderImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTOBuilderImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypeBuilderImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java
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/BaseTypeProvider.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/binding-generator-impl/src/test/resources/abstract-topology.yang
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/CompositeKeyGenerator.java
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/InterfaceGenerator.java
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/package-info.java
opendaylight/sal/yang-prototype/code-generator/binding-model-api/src/main/java/org/opendaylight/controller/sal/binding/model/api/CodeGenerator.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/UsesNodeBuilderImpl.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/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/TypesResolutionTest.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerTest.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java
opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/NotificationModule.java
opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/UsesNode.java
opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/BaseTypes.java
opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/Leafref.java

index 9b178c0..950db94 100644 (file)
           <artifactId>binding-generator-spi</artifactId>\r
           <version>1.0</version>\r
       </dependency>\r
+      <dependency>\r
+       <groupId>jaxen</groupId>\r
+       <artifactId>jaxen</artifactId>\r
+       <version>1.1.4</version>\r
+      </dependency>\r
   </dependencies>\r
 </project>
\ No newline at end of file
index 4f87adb..3a9398a 100644 (file)
-/*
- * 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.generator.impl;
-
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.opendaylight.controller.binding.generator.util.CodeGeneratorHelper;
-import org.opendaylight.controller.binding.generator.util.Types;
-import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
-import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;
-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.Type;
-import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
-import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
-import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
-import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
-import org.opendaylight.controller.sal.binding.yang.types.TypeProviderImpl;
-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.SchemaContext;
-import org.opendaylight.controller.yang.model.api.SchemaPath;
-import org.opendaylight.controller.yang.model.api.TypeDefinition;
-
-public class BindingGeneratorImpl implements BindingGenerator {
-
-    private static Calendar calendar = new GregorianCalendar();
-    private final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
-    private final List<ContainerSchemaNode> schemaContainers;
-    private final List<ListSchemaNode> schemaLists;
-
-    private final TypeProvider typeProvider;
-
-    private String basePackageName;
-
-    public BindingGeneratorImpl() {
-        super();
-        genTypeBuilders = new HashMap<String, Map<String, GeneratedTypeBuilder>>();
-        schemaContainers = new ArrayList<ContainerSchemaNode>();
-        schemaLists = new ArrayList<ListSchemaNode>();
-
-        // TODO: reimplement in better way
-        typeProvider = new TypeProviderImpl();
-    }
-
-    @Override
-    public List<Type> generateTypes(final SchemaContext context) {
-        final List<Type> genTypes = new ArrayList<Type>();
-        
-        if (context != null) {
-            final Set<Module> modules = context.getModules();
-            
-            if (modules != null) {
-                for (final Module module : modules) {
-                    basePackageName = resolveBasePackageName(module.getNamespace(),
-                            module.getYangVersion());
-
-                    traverseModule(module);
-                    if (schemaContainers.size() > 0) {
-                        for (final ContainerSchemaNode container : schemaContainers) {
-                            genTypes.add(containerToGenType(container));
-                        }
-                    }
-
-                    if (schemaLists.size() > 0) {
-                        for (final ListSchemaNode list : schemaLists) {
-                            genTypes.addAll(listToGenType(list));
-                        }
-                    }
-                }
-            }
-        }
-
-        return genTypes;
-    }
-    
-    private String resolveGeneratedTypePackageName(final SchemaPath schemaPath) {
-        final StringBuilder builder = new StringBuilder();
-        builder.append(basePackageName);
-        if ((schemaPath != null) && (schemaPath.getPath() != null)) {
-            final List<QName> pathToNode = schemaPath.getPath();
-            for (int i = 0; i < pathToNode.size(); ++i) {
-                builder.append(".");
-                String nodeLocalName = pathToNode.get(i).getLocalName();
-
-                // TODO: create method
-                nodeLocalName = nodeLocalName.replace(":", ".");
-                nodeLocalName = nodeLocalName.replace("-", ".");
-                builder.append(nodeLocalName);
-            }
-            return builder.toString();
-        }
-        return null;
-    }
-
-    private GeneratedType containerToGenType(ContainerSchemaNode container) {
-        if (container == null) {
-            return null;
-        }
-        final Set<DataSchemaNode> schemaNodes = container.getChildNodes();
-        final GeneratedTypeBuilder typeBuilder = addRawInterfaceDefinition(container);
-
-        for (final DataSchemaNode node : schemaNodes) {
-            if (node instanceof LeafSchemaNode) {
-                resolveLeafSchemaNodeAsMethod(typeBuilder,
-                        (LeafSchemaNode) node);
-            } else if (node instanceof LeafListSchemaNode) {
-                resolveLeafListSchemaNode(typeBuilder,
-                        (LeafListSchemaNode) node);
-
-            } else if (node instanceof ContainerSchemaNode) {
-                resolveContainerSchemaNode(typeBuilder,
-                        (ContainerSchemaNode) node);
-            } else if (node instanceof ListSchemaNode) {
-                resolveListSchemaNode(typeBuilder, (ListSchemaNode) node);
-            }
-        }
-        return typeBuilder.toInstance();
-    }
-
-    private boolean resolveLeafSchemaNodeAsMethod(
-            final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {
-        if ((leaf != null) && (typeBuilder != null)) {
-            final String leafName = leaf.getQName().getLocalName();
-            String leafDesc = leaf.getDescription();
-            if (leafDesc == null) {
-                leafDesc = "";
-            }
-
-            if (leafName != null) {
-                final TypeDefinition<?> typeDef = leaf.getType();
-                final Type javaType = typeProvider
-                        .javaTypeForSchemaDefinitionType(typeDef);
-
-                constructGetter(typeBuilder, leafName, leafDesc, javaType);
-                if (!leaf.isConfiguration()) {
-                    constructSetter(typeBuilder, leafName, leafDesc, javaType);
-                }
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private boolean resolveLeafSchemaNodeAsProperty(
-            final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
-            boolean isReadOnly) {
-        if ((leaf != null) && (toBuilder != null)) {
-            final String leafName = leaf.getQName().getLocalName();
-            String leafDesc = leaf.getDescription();
-            if (leafDesc == null) {
-                leafDesc = "";
-            }
-
-            if (leafName != null) {
-                final TypeDefinition<?> typeDef = leaf.getType();
-                final Type javaType = typeProvider
-                        .javaTypeForSchemaDefinitionType(typeDef);
-
-                final GeneratedPropertyBuilder propBuilder = toBuilder
-                        .addProperty(CodeGeneratorHelper
-                                .parseToClassName(leafName));
-
-                propBuilder.setReadOnly(isReadOnly);
-                propBuilder.addReturnType(javaType);
-                propBuilder.addComment(leafDesc);
-
-                toBuilder.addEqualsIdentity(propBuilder);
-                toBuilder.addHashIdentity(propBuilder);
-                toBuilder.addToStringProperty(propBuilder);
-
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private boolean resolveLeafListSchemaNode(
-            final GeneratedTypeBuilder typeBuilder,
-            final LeafListSchemaNode node) {
-        if ((node != null) && (typeBuilder != null)) {
-            final String nodeName = node.getQName().getLocalName();
-            String nodeDesc = node.getDescription();
-            if (nodeDesc == null) {
-                nodeDesc = "";
-            }
-
-            if (nodeName != null) {
-                final TypeDefinition<?> type = node.getType();
-                final Type listType = Types.listTypeFor(typeProvider
-                        .javaTypeForSchemaDefinitionType(type));
-
-                constructGetter(typeBuilder, nodeName, nodeDesc, listType);
-                if (!node.isConfiguration()) {
-                    constructSetter(typeBuilder, nodeName, nodeDesc, listType);
-                }
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private boolean resolveContainerSchemaNode(
-            final GeneratedTypeBuilder typeBuilder,
-            final ContainerSchemaNode node) {
-        if ((node != null) && (typeBuilder != null)) {
-            final String nodeName = node.getQName().getLocalName();
-
-            if (nodeName != null) {
-                final GeneratedTypeBuilder rawGenType = addRawInterfaceDefinition(node);
-                constructGetter(typeBuilder, nodeName, "", rawGenType);
-
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private boolean resolveListSchemaNode(
-            final GeneratedTypeBuilder typeBuilder, final ListSchemaNode node) {
-        if ((node != null) && (typeBuilder != null)) {
-            final String nodeName = node.getQName().getLocalName();
-
-            if (nodeName != null) {
-                final GeneratedTypeBuilder rawGenType = addRawInterfaceDefinition(node);
-                constructGetter(typeBuilder, nodeName, "",
-                        Types.listTypeFor(rawGenType));
-                if (!node.isConfiguration()) {
-                    constructSetter(typeBuilder, nodeName, "",
-                            Types.listTypeFor(rawGenType));
-                }
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private GeneratedTypeBuilder addRawInterfaceDefinition(
-            final DataSchemaNode schemaNode) {
-        if (schemaNode == null) {
-            return null;
-        }
-
-        final String packageName = resolveGeneratedTypePackageName(schemaNode
-                .getPath());
-        final String schemaNodeName = schemaNode.getQName().getLocalName();
-
-        if ((packageName != null) && (schemaNode != null)
-                && (schemaNodeName != null)) {
-            final String genTypeName = CodeGeneratorHelper
-                    .parseToClassName(schemaNodeName);
-            final GeneratedTypeBuilder newType = new GeneratedTypeBuilderImpl(
-                    packageName, genTypeName);
-
-            if (!genTypeBuilders.containsKey(packageName)) {
-                final Map<String, GeneratedTypeBuilder> builders = new HashMap<String, GeneratedTypeBuilder>();
-                builders.put(genTypeName, newType);
-                genTypeBuilders.put(packageName, builders);
-            } else {
-                final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders
-                        .get(packageName);
-                if (!builders.containsKey(genTypeName)) {
-                    builders.put(genTypeName, newType);
-                }
-            }
-
-            return newType;
-        }
-        return null;
-    }
-
-    private String getterMethodName(final String methodName) {
-        final StringBuilder method = new StringBuilder();
-        method.append("get");
-        method.append(CodeGeneratorHelper.parseToClassName(methodName));
-        return method.toString();
-    }
-
-    private String setterMethodName(final String methodName) {
-        final StringBuilder method = new StringBuilder();
-        method.append("set");
-        method.append(CodeGeneratorHelper.parseToClassName(methodName));
-        return method.toString();
-    }
-
-    private MethodSignatureBuilder constructGetter(
-            final GeneratedTypeBuilder interfaceBuilder,
-            final String schemaNodeName, final String comment,
-            final Type returnType) {
-        final MethodSignatureBuilder getMethod = interfaceBuilder
-                .addMethod(getterMethodName(schemaNodeName));
-
-        getMethod.addComment(comment);
-        getMethod.addReturnType(returnType);
-
-        return getMethod;
-    }
-
-    private MethodSignatureBuilder constructSetter(
-            final GeneratedTypeBuilder interfaceBuilder,
-            final String schemaNodeName, final String comment,
-            final Type parameterType) {
-        final MethodSignatureBuilder setMethod = interfaceBuilder
-                .addMethod(setterMethodName(schemaNodeName));
-
-        setMethod.addComment(comment);
-        setMethod.addParameter(parameterType,
-                CodeGeneratorHelper.parseToParamName(schemaNodeName));
-        setMethod.addReturnType(Types.voidType());
-
-        return setMethod;
-    }
-
-    private String resolveBasePackageName(final URI moduleNamespace,
-            final String yangVersion) {
-        final StringBuilder packageNameBuilder = new StringBuilder();
-
-        packageNameBuilder.append("com.cisco.yang.gen.v");
-        packageNameBuilder.append(yangVersion);
-        packageNameBuilder.append(".rev");
-        packageNameBuilder.append(calendar.get(Calendar.YEAR));
-        packageNameBuilder.append((calendar.get(Calendar.MONTH) + 1));
-        packageNameBuilder.append(calendar.get(Calendar.DAY_OF_MONTH));
-        packageNameBuilder.append(".");
-
-        String namespace = moduleNamespace.toString();
-        namespace = namespace.replace(":", ".");
-        namespace = namespace.replace("-", ".");
-
-        packageNameBuilder.append(namespace);
-
-        return packageNameBuilder.toString();
-    }
-
-    private List<Type> listToGenType(final ListSchemaNode list) {
-        if (list == null) {
-            return null;
-        }
-        final GeneratedTypeBuilder typeBuilder = resolveListTypeBuilder(list);
-        final List<String> listKeys = listKeys(list);
-        GeneratedTOBuilder genTOBuilder = null;
-        if (listKeys.size() > 0) {
-            genTOBuilder = resolveListKey(list);
-        }
-
-        final Set<DataSchemaNode> schemaNodes = list.getChildNodes();
-        for (final DataSchemaNode node : schemaNodes) {
-
-            if (node instanceof LeafSchemaNode) {
-                final LeafSchemaNode leaf = (LeafSchemaNode) node;
-                if (!isPartOfListKey(leaf, listKeys)) {
-                    resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
-                } else {
-                    resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true);
-                }
-            } else if (node instanceof LeafListSchemaNode) {
-                resolveLeafListSchemaNode(typeBuilder,
-                        (LeafListSchemaNode) node);
-            } else if (node instanceof ContainerSchemaNode) {
-                resolveContainerSchemaNode(typeBuilder,
-                        (ContainerSchemaNode) node);
-            } else if (node instanceof ListSchemaNode) {
-                resolveListSchemaNode(typeBuilder, (ListSchemaNode) node);
-            }
-        }
-
-        final List<Type> genTypes = new ArrayList<Type>();
-        if (genTOBuilder != null) {
-            final GeneratedTransferObject genTO = genTOBuilder.toInstance();
-            constructGetter(typeBuilder, genTO.getName(), "Returns Primary Key of Yang List Type", genTO);
-            genTypes.add(genTO);
-        }
-        genTypes.add(typeBuilder.toInstance());
-        return genTypes;
-    }
-
-    /**
-     * @param list
-     * @return
-     */
-    private GeneratedTOBuilder resolveListKey(final ListSchemaNode list) {
-        final String packageName = resolveGeneratedTypePackageName(list
-                .getPath());
-        final String listName = list.getQName().getLocalName() + "Key";
-
-        if ((packageName != null) && (list != null) && (listName != null)) {
-            final String genTOName = CodeGeneratorHelper
-                    .parseToClassName(listName);
-            final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(
-                    packageName, genTOName);
-
-            return newType;
-        }
-        return null;
-    }
-
-    private boolean isPartOfListKey(final LeafSchemaNode leaf,
-            final List<String> keys) {
-        if ((leaf != null) && (keys != null) && (leaf.getQName() != null)) {
-            final String leafName = leaf.getQName().getLocalName();
-            if (keys.contains(leafName)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private List<String> listKeys(final ListSchemaNode list) {
-        final List<String> listKeys = new ArrayList<String>();
-
-        if (list.getKeyDefinition() != null) {
-            final List<QName> keyDefinitions = list.getKeyDefinition();
-            
-            for (final QName keyDefinition : keyDefinitions) {
-                listKeys.add(keyDefinition.getLocalName());
-            }
-        }
-        return listKeys;
-    }
-
-    private GeneratedTypeBuilder resolveListTypeBuilder(
-            final ListSchemaNode list) {
-        final String packageName = resolveGeneratedTypePackageName(list
-                .getPath());
-        final String schemaNodeName = list.getQName().getLocalName();
-        final String genTypeName = CodeGeneratorHelper
-                .parseToClassName(schemaNodeName);
-
-        GeneratedTypeBuilder typeBuilder = null;
-        if (genTypeBuilders.containsKey(packageName)) {
-            final Map<String, GeneratedTypeBuilder> builders = new HashMap<String, GeneratedTypeBuilder>();
-            typeBuilder = builders.get(genTypeName);
-
-            if (null == typeBuilder) {
-                typeBuilder = addRawInterfaceDefinition(list);
-            }
-        }
-        return typeBuilder;
-    }
-
-    private void traverseModule(final Module module) {
-        final Set<DataSchemaNode> schemaNodes = module.getChildNodes();
-
-        for (DataSchemaNode node : schemaNodes) {
-            if (node instanceof ContainerSchemaNode) {
-                schemaContainers.add((ContainerSchemaNode) node);
-                traverse((ContainerSchemaNode) node);
-            }
-        }
-    }
-
-    private void traverse(final DataNodeContainer dataNode) {
-        if (!containChildDataNodeContainer(dataNode)) {
-            return;
-        }
-
-        final Set<DataSchemaNode> childs = dataNode.getChildNodes();
-        if (childs != null) {
-            for (DataSchemaNode childNode : childs) {
-                if (childNode instanceof ContainerSchemaNode) {
-                    final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
-                    schemaContainers.add(container);
-                    traverse(container);
-                }
-
-                if (childNode instanceof ListSchemaNode) {
-                    final ListSchemaNode list = (ListSchemaNode) childNode;
-                    schemaLists.add(list);
-                    traverse(list);
-                }
-            }
-        }
-    }
-
-    /**
-     * Returns <code>true</code> if and only if the child node contain at least
-     * one child container schema node or child list schema node, otherwise will
-     * always returns <code>false</code>
-     * 
-     * @param container
-     * @return <code>true</code> if and only if the child node contain at least
-     *         one child container schema node or child list schema node,
-     *         otherwise will always returns <code>false</code>
-     */
-    private boolean containChildDataNodeContainer(
-            final DataNodeContainer container) {
-        if (container != null) {
-            final Set<DataSchemaNode> childs = container.getChildNodes();
-            if ((childs != null) && (childs.size() > 0)) {
-                for (final DataSchemaNode childNode : childs) {
-                    if (childNode instanceof DataNodeContainer) {
-                        return true;
-                    }
-                }
-            }
-        }
-        return false;
-    }
-}
+/*\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.generator.impl;\r
+\r
+import java.net.URI;\r
+import java.util.ArrayList;\r
+import java.util.Calendar;\r
+import java.util.GregorianCalendar;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.opendaylight.controller.binding.generator.util.CodeGeneratorHelper;\r
+import org.opendaylight.controller.binding.generator.util.Types;\r
+import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;\r
+import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;\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.Type;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;\r
+import org.opendaylight.controller.sal.binding.yang.types.TypeProviderImpl;\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.SchemaContext;\r
+import org.opendaylight.controller.yang.model.api.SchemaPath;\r
+import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
+\r
+public class BindingGeneratorImpl implements BindingGenerator {\r
+\r
+    private static Calendar calendar = new GregorianCalendar();\r
+    private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;\r
+    private List<ContainerSchemaNode> schemaContainers;\r
+    private List<ListSchemaNode> schemaLists;\r
+    private TypeProvider typeProvider;\r
+    private String basePackageName;\r
+\r
+    public BindingGeneratorImpl() {\r
+        super();\r
+    }\r
+\r
+    @Override\r
+    public List<Type> generateTypes(final SchemaContext context) {\r
+        final List<Type> genTypes = new ArrayList<Type>();\r
+        \r
+        typeProvider = new TypeProviderImpl(context);\r
+        if (context != null) {\r
+            final Set<Module> modules = context.getModules();\r
+            \r
+            if (modules != null) {\r
+                for (final Module module : modules) {\r
+                    genTypeBuilders = new HashMap<String, Map<String, GeneratedTypeBuilder>>();\r
+                    schemaContainers = new ArrayList<ContainerSchemaNode>();\r
+                    schemaLists = new ArrayList<ListSchemaNode>();\r
+                    \r
+                    basePackageName = resolveBasePackageName(module.getNamespace(),\r
+                            module.getYangVersion());\r
+\r
+                    traverseModule(module);\r
+                    if (schemaContainers.size() > 0) {\r
+                        for (final ContainerSchemaNode container : schemaContainers) {\r
+                            genTypes.add(containerToGenType(container));\r
+                        }\r
+                    }\r
+\r
+                    if (schemaLists.size() > 0) {\r
+                        for (final ListSchemaNode list : schemaLists) {\r
+                            genTypes.addAll(listToGenType(list));\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+        return genTypes;\r
+    }\r
+    \r
+    private String resolveGeneratedTypePackageName(final SchemaPath schemaPath) {\r
+        final StringBuilder builder = new StringBuilder();\r
+        builder.append(basePackageName);\r
+        if ((schemaPath != null) && (schemaPath.getPath() != null)) {\r
+            final List<QName> pathToNode = schemaPath.getPath();\r
+            final int traversalSteps = (pathToNode.size() - 1); \r
+            for (int i = 0; i < traversalSteps; ++i) {\r
+                builder.append(".");\r
+                String nodeLocalName = pathToNode.get(i).getLocalName();\r
+\r
+                // TODO: create method\r
+                nodeLocalName = nodeLocalName.replace(":", ".");\r
+                nodeLocalName = nodeLocalName.replace("-", ".");\r
+                builder.append(nodeLocalName);\r
+            }\r
+            return builder.toString();\r
+        }\r
+        return null;\r
+    }\r
+\r
+    private GeneratedType containerToGenType(ContainerSchemaNode container) {\r
+        if (container == null) {\r
+            return null;\r
+        }\r
+        final Set<DataSchemaNode> schemaNodes = container.getChildNodes();\r
+        final GeneratedTypeBuilder typeBuilder = addRawInterfaceDefinition(container);\r
+\r
+        for (final DataSchemaNode node : schemaNodes) {\r
+            if (node instanceof LeafSchemaNode) {\r
+                resolveLeafSchemaNodeAsMethod(typeBuilder,\r
+                        (LeafSchemaNode) node);\r
+            } else if (node instanceof LeafListSchemaNode) {\r
+                resolveLeafListSchemaNode(typeBuilder,\r
+                        (LeafListSchemaNode) node);\r
+\r
+            } else if (node instanceof ContainerSchemaNode) {\r
+                resolveContainerSchemaNode(typeBuilder,\r
+                        (ContainerSchemaNode) node);\r
+            } else if (node instanceof ListSchemaNode) {\r
+                resolveListSchemaNode(typeBuilder, (ListSchemaNode) node);\r
+            }\r
+        }\r
+        return typeBuilder.toInstance();\r
+    }\r
+\r
+    private boolean resolveLeafSchemaNodeAsMethod(\r
+            final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) {\r
+        if ((leaf != null) && (typeBuilder != null)) {\r
+            final String leafName = leaf.getQName().getLocalName();\r
+            String leafDesc = leaf.getDescription();\r
+            if (leafDesc == null) {\r
+                leafDesc = "";\r
+            }\r
+\r
+            if (leafName != null) {\r
+                final TypeDefinition<?> typeDef = leaf.getType();\r
+                final Type javaType = typeProvider\r
+                        .javaTypeForSchemaDefinitionType(typeDef);\r
+\r
+                constructGetter(typeBuilder, leafName, leafDesc, javaType);\r
+                if (!leaf.isConfiguration()) {\r
+                    constructSetter(typeBuilder, leafName, leafDesc, javaType);\r
+                }\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private boolean resolveLeafSchemaNodeAsProperty(\r
+            final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,\r
+            boolean isReadOnly) {\r
+        if ((leaf != null) && (toBuilder != null)) {\r
+            final String leafName = leaf.getQName().getLocalName();\r
+            String leafDesc = leaf.getDescription();\r
+            if (leafDesc == null) {\r
+                leafDesc = "";\r
+            }\r
+\r
+            if (leafName != null) {\r
+                final TypeDefinition<?> typeDef = leaf.getType();\r
+                \r
+                //TODO: properly resolve enum types\r
+                final Type javaType = typeProvider\r
+                        .javaTypeForSchemaDefinitionType(typeDef);\r
+\r
+                final GeneratedPropertyBuilder propBuilder = toBuilder\r
+                        .addProperty(CodeGeneratorHelper\r
+                                .parseToClassName(leafName));\r
+\r
+                propBuilder.setReadOnly(isReadOnly);\r
+                propBuilder.addReturnType(javaType);\r
+                propBuilder.addComment(leafDesc);\r
+\r
+                toBuilder.addEqualsIdentity(propBuilder);\r
+                toBuilder.addHashIdentity(propBuilder);\r
+                toBuilder.addToStringProperty(propBuilder);\r
+\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private boolean resolveLeafListSchemaNode(\r
+            final GeneratedTypeBuilder typeBuilder,\r
+            final LeafListSchemaNode node) {\r
+        if ((node != null) && (typeBuilder != null)) {\r
+            final String nodeName = node.getQName().getLocalName();\r
+            String nodeDesc = node.getDescription();\r
+            if (nodeDesc == null) {\r
+                nodeDesc = "";\r
+            }\r
+\r
+            if (nodeName != null) {\r
+                final TypeDefinition<?> type = node.getType();\r
+                final Type listType = Types.listTypeFor(typeProvider\r
+                        .javaTypeForSchemaDefinitionType(type));\r
+\r
+                constructGetter(typeBuilder, nodeName, nodeDesc, listType);\r
+                if (!node.isConfiguration()) {\r
+                    constructSetter(typeBuilder, nodeName, nodeDesc, listType);\r
+                }\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private boolean resolveContainerSchemaNode(\r
+            final GeneratedTypeBuilder typeBuilder,\r
+            final ContainerSchemaNode node) {\r
+        if ((node != null) && (typeBuilder != null)) {\r
+            final String nodeName = node.getQName().getLocalName();\r
+\r
+            if (nodeName != null) {\r
+                final GeneratedTypeBuilder rawGenType = addRawInterfaceDefinition(node);\r
+                constructGetter(typeBuilder, nodeName, "", rawGenType);\r
+\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private boolean resolveListSchemaNode(\r
+            final GeneratedTypeBuilder typeBuilder, final ListSchemaNode node) {\r
+        if ((node != null) && (typeBuilder != null)) {\r
+            final String nodeName = node.getQName().getLocalName();\r
+\r
+            if (nodeName != null) {\r
+                final GeneratedTypeBuilder rawGenType = addRawInterfaceDefinition(node);\r
+                constructGetter(typeBuilder, nodeName, "",\r
+                        Types.listTypeFor(rawGenType));\r
+                if (!node.isConfiguration()) {\r
+                    constructSetter(typeBuilder, nodeName, "",\r
+                            Types.listTypeFor(rawGenType));\r
+                }\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private GeneratedTypeBuilder addRawInterfaceDefinition(\r
+            final DataSchemaNode schemaNode) {\r
+        if (schemaNode == null) {\r
+            return null;\r
+        }\r
+\r
+        final String packageName = resolveGeneratedTypePackageName(schemaNode\r
+                .getPath());\r
+        final String schemaNodeName = schemaNode.getQName().getLocalName();\r
+\r
+        if ((packageName != null) && (schemaNode != null)\r
+                && (schemaNodeName != null)) {\r
+            final String genTypeName = CodeGeneratorHelper\r
+                    .parseToClassName(schemaNodeName);\r
+            final GeneratedTypeBuilder newType = new GeneratedTypeBuilderImpl(\r
+                    packageName, genTypeName);\r
+\r
+            if (!genTypeBuilders.containsKey(packageName)) {\r
+                final Map<String, GeneratedTypeBuilder> builders = new HashMap<String, GeneratedTypeBuilder>();\r
+                builders.put(genTypeName, newType);\r
+                genTypeBuilders.put(packageName, builders);\r
+            } else {\r
+                final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders\r
+                        .get(packageName);\r
+                if (!builders.containsKey(genTypeName)) {\r
+                    builders.put(genTypeName, newType);\r
+                }\r
+            }\r
+            return newType;\r
+        }\r
+        return null;\r
+    }\r
+\r
+    private String getterMethodName(final String methodName) {\r
+        final StringBuilder method = new StringBuilder();\r
+        method.append("get");\r
+        method.append(CodeGeneratorHelper.parseToClassName(methodName));\r
+        return method.toString();\r
+    }\r
+\r
+    private String setterMethodName(final String methodName) {\r
+        final StringBuilder method = new StringBuilder();\r
+        method.append("set");\r
+        method.append(CodeGeneratorHelper.parseToClassName(methodName));\r
+        return method.toString();\r
+    }\r
+\r
+    private MethodSignatureBuilder constructGetter(\r
+            final GeneratedTypeBuilder interfaceBuilder,\r
+            final String schemaNodeName, final String comment,\r
+            final Type returnType) {\r
+        final MethodSignatureBuilder getMethod = interfaceBuilder\r
+                .addMethod(getterMethodName(schemaNodeName));\r
+\r
+        getMethod.addComment(comment);\r
+        getMethod.addReturnType(returnType);\r
+\r
+        return getMethod;\r
+    }\r
+\r
+    private MethodSignatureBuilder constructSetter(\r
+            final GeneratedTypeBuilder interfaceBuilder,\r
+            final String schemaNodeName, final String comment,\r
+            final Type parameterType) {\r
+        final MethodSignatureBuilder setMethod = interfaceBuilder\r
+                .addMethod(setterMethodName(schemaNodeName));\r
+\r
+        setMethod.addComment(comment);\r
+        setMethod.addParameter(parameterType,\r
+                CodeGeneratorHelper.parseToParamName(schemaNodeName));\r
+        setMethod.addReturnType(Types.voidType());\r
+\r
+        return setMethod;\r
+    }\r
+\r
+    private String resolveBasePackageName(final URI moduleNamespace,\r
+            final String yangVersion) {\r
+        final StringBuilder packageNameBuilder = new StringBuilder();\r
+\r
+        packageNameBuilder.append("org.opendaylight.yang.gen.v");\r
+        packageNameBuilder.append(yangVersion);\r
+        packageNameBuilder.append(".rev");\r
+        packageNameBuilder.append(calendar.get(Calendar.YEAR));\r
+        packageNameBuilder.append((calendar.get(Calendar.MONTH) + 1));\r
+        packageNameBuilder.append(calendar.get(Calendar.DAY_OF_MONTH));\r
+        packageNameBuilder.append(".");\r
+\r
+        String namespace = moduleNamespace.toString();\r
+        namespace = namespace.replace(":", ".");\r
+        namespace = namespace.replace("-", ".");\r
+\r
+        packageNameBuilder.append(namespace);\r
+\r
+        return packageNameBuilder.toString();\r
+    }\r
+\r
+    private List<Type> listToGenType(final ListSchemaNode list) {\r
+        if (list == null) {\r
+            return null;\r
+        }\r
+        final GeneratedTypeBuilder typeBuilder = resolveListTypeBuilder(list);\r
+        final List<String> listKeys = listKeys(list);\r
+        GeneratedTOBuilder genTOBuilder = null;\r
+        if (listKeys.size() > 0) {\r
+            genTOBuilder = resolveListKey(list);\r
+        }\r
+\r
+        final Set<DataSchemaNode> schemaNodes = list.getChildNodes();\r
+        for (final DataSchemaNode node : schemaNodes) {\r
+\r
+            if (node instanceof LeafSchemaNode) {\r
+                final LeafSchemaNode leaf = (LeafSchemaNode) node;\r
+                if (!isPartOfListKey(leaf, listKeys)) {\r
+                    resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);\r
+                } else {\r
+                    resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true);\r
+                }\r
+            } else if (node instanceof LeafListSchemaNode) {\r
+                resolveLeafListSchemaNode(typeBuilder,\r
+                        (LeafListSchemaNode) node);\r
+            } else if (node instanceof ContainerSchemaNode) {\r
+                resolveContainerSchemaNode(typeBuilder,\r
+                        (ContainerSchemaNode) node);\r
+            } else if (node instanceof ListSchemaNode) {\r
+                resolveListSchemaNode(typeBuilder, (ListSchemaNode) node);\r
+            }\r
+        }\r
+\r
+        final List<Type> genTypes = new ArrayList<Type>();\r
+        if (genTOBuilder != null) {\r
+            final GeneratedTransferObject genTO = genTOBuilder.toInstance();\r
+            constructGetter(typeBuilder, genTO.getName(), "Returns Primary Key of Yang List Type", genTO);\r
+            genTypes.add(genTO);\r
+        }\r
+        genTypes.add(typeBuilder.toInstance());\r
+        return genTypes;\r
+    }\r
+\r
+    /**\r
+     * @param list\r
+     * @return\r
+     */\r
+    private GeneratedTOBuilder resolveListKey(final ListSchemaNode list) {\r
+        final String packageName = resolveGeneratedTypePackageName(list\r
+                .getPath());\r
+        final String listName = list.getQName().getLocalName() + "Key";\r
+\r
+        if ((packageName != null) && (list != null) && (listName != null)) {\r
+            final String genTOName = CodeGeneratorHelper\r
+                    .parseToClassName(listName);\r
+            final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(\r
+                    packageName, genTOName);\r
+\r
+            return newType;\r
+        }\r
+        return null;\r
+    }\r
+\r
+    private boolean isPartOfListKey(final LeafSchemaNode leaf,\r
+            final List<String> keys) {\r
+        if ((leaf != null) && (keys != null) && (leaf.getQName() != null)) {\r
+            final String leafName = leaf.getQName().getLocalName();\r
+            if (keys.contains(leafName)) {\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private List<String> listKeys(final ListSchemaNode list) {\r
+        final List<String> listKeys = new ArrayList<String>();\r
+\r
+        if (list.getKeyDefinition() != null) {\r
+            final List<QName> keyDefinitions = list.getKeyDefinition();\r
+            \r
+            for (final QName keyDefinition : keyDefinitions) {\r
+                listKeys.add(keyDefinition.getLocalName());\r
+            }\r
+        }\r
+        return listKeys;\r
+    }\r
+\r
+    private GeneratedTypeBuilder resolveListTypeBuilder(\r
+            final ListSchemaNode list) {\r
+        final String packageName = resolveGeneratedTypePackageName(list\r
+                .getPath());\r
+        final String schemaNodeName = list.getQName().getLocalName();\r
+        final String genTypeName = CodeGeneratorHelper\r
+                .parseToClassName(schemaNodeName);\r
+\r
+        GeneratedTypeBuilder typeBuilder = null;\r
+        if (genTypeBuilders.containsKey(packageName)) {\r
+            final Map<String, GeneratedTypeBuilder> builders = new HashMap<String, GeneratedTypeBuilder>();\r
+            typeBuilder = builders.get(genTypeName);\r
+\r
+            if (null == typeBuilder) {\r
+                typeBuilder = addRawInterfaceDefinition(list);\r
+            }\r
+        }\r
+        return typeBuilder;\r
+    }\r
+\r
+    private void traverseModule(final Module module) {\r
+        final Set<DataSchemaNode> schemaNodes = module.getChildNodes();\r
+\r
+        for (DataSchemaNode node : schemaNodes) {\r
+            if (node instanceof ContainerSchemaNode) {\r
+                schemaContainers.add((ContainerSchemaNode) node);\r
+                traverse((ContainerSchemaNode) node);\r
+            }\r
+        }\r
+    }\r
+\r
+    private void traverse(final DataNodeContainer dataNode) {\r
+        if (!containChildDataNodeContainer(dataNode)) {\r
+            return;\r
+        }\r
+\r
+        final Set<DataSchemaNode> childs = dataNode.getChildNodes();\r
+        if (childs != null) {\r
+            for (DataSchemaNode childNode : childs) {\r
+                if (childNode instanceof ContainerSchemaNode) {\r
+                    final ContainerSchemaNode container = (ContainerSchemaNode) childNode;\r
+                    schemaContainers.add(container);\r
+                    traverse(container);\r
+                }\r
+\r
+                if (childNode instanceof ListSchemaNode) {\r
+                    final ListSchemaNode list = (ListSchemaNode) childNode;\r
+                    schemaLists.add(list);\r
+                    traverse(list);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Returns <code>true</code> if and only if the child node contain at least\r
+     * one child container schema node or child list schema node, otherwise will\r
+     * always returns <code>false</code>\r
+     * \r
+     * @param container\r
+     * @return <code>true</code> if and only if the child node contain at least\r
+     *         one child container schema node or child list schema node,\r
+     *         otherwise will always returns <code>false</code>\r
+     */\r
+    private boolean containChildDataNodeContainer(\r
+            final DataNodeContainer container) {\r
+        if (container != null) {\r
+            final Set<DataSchemaNode> childs = container.getChildNodes();\r
+            if ((childs != null) && (childs.size() > 0)) {\r
+                for (final DataSchemaNode childNode : childs) {\r
+                    if (childNode instanceof DataNodeContainer) {\r
+                        return true;\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+}\r
index 6eadb76..31ced79 100644 (file)
@@ -22,7 +22,7 @@ final class EnumerationBuilderImpl implements EnumBuilder {
 \r
     public EnumerationBuilderImpl(final String packageName, final String name) {\r
         super();\r
-        this.packageName = packageName;\r
+        this.packageName = GeneratedTypeBuilderImpl.validatePackage(packageName);\r
         this.name = name;\r
         values = new ArrayList<Enumeration.Pair>();\r
     }\r
index 90b2ebf..e39889b 100644 (file)
@@ -8,8 +8,11 @@
 package org.opendaylight.controller.sal.binding.generator.impl;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.Collections;\r
+import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Set;\r
 \r
 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;\r
 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
@@ -24,8 +27,21 @@ import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedP
 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;\r
 \r
 final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {\r
-\r
-    private final String packageName;\r
+    \r
+    private static final String[] SET_VALUES = new String[] { "abstract",\r
+        "assert", "boolean", "break", "byte", "case", "catch", "char",\r
+        "class", "const", "continue", "default", "double", "do", "else",\r
+        "enum", "extends", "false", "final", "finally", "float", "for",\r
+        "goto", "if", "implements", "import", "instanceof", "int",\r
+        "interface", "long", "native", "new", "null", "package", "private",\r
+        "protected", "public", "return", "short", "static", "strictfp",\r
+        "super", "switch", "synchronized", "this", "throw", "throws",\r
+        "transient", "true", "try", "void", "volatile", "while" };\r
+\r
+    public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
+            Arrays.asList(SET_VALUES));\r
+    \r
+    private String packageName;\r
     private final String name;\r
 \r
     private final List<EnumBuilder> enumerations = new ArrayList<EnumBuilder>();\r
@@ -36,10 +52,10 @@ final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {
 \r
     public GeneratedTOBuilderImpl(String packageName, String name) {\r
         super();\r
-        this.packageName = packageName;\r
+        this.packageName = GeneratedTypeBuilderImpl.validatePackage(packageName);\r
         this.name = name;\r
     }\r
-\r
+    \r
     @Override\r
     public String getPackageName() {\r
         return packageName;\r
@@ -49,7 +65,7 @@ final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {
     public String getName() {\r
         return name;\r
     }\r
-\r
+    \r
     @Override\r
     public EnumBuilder addEnumeration(String name) {\r
         final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
@@ -83,6 +99,7 @@ final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {
 \r
     @Override\r
     public GeneratedTransferObject toInstance() {\r
+       \r
         return new GeneratedTransferObjectImpl(packageName, name, enumerations,\r
                 properties, equalsProperties, hashProperties,\r
                 toStringProperties);\r
index c861b54..f6ed8f2 100644 (file)
@@ -8,8 +8,11 @@
 package org.opendaylight.controller.sal.binding.generator.impl;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.Collections;\r
+import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Set;\r
 \r
 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;\r
 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
@@ -24,480 +27,517 @@ import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSign
 \r
 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {\r
 \r
-    private final String packageName;\r
-    private String comment;\r
-    private final String name;\r
-    private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();\r
-    private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();\r
-    private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();\r
-\r
-    public GeneratedTypeBuilderImpl(final String packageName, final String name) {\r
-        this.packageName = packageName;\r
-        this.name = name;\r
-    }\r
-\r
-    @Override\r
-    public Type getParentType() {\r
-        return this;\r
-    }\r
-\r
-    @Override\r
-    public String getPackageName() {\r
-        return packageName;\r
-    }\r
-\r
-    @Override\r
-    public String getName() {\r
-        return name;\r
-    }\r
-\r
-    @Override\r
-    public void addComment(String comment) {\r
-        this.comment = comment;\r
-    }\r
-\r
-    @Override\r
-    public ConstantBuilder addConstant(Type type, String name, Object value) {\r
-        final ConstantBuilder builder = new ConstantBuilderImpl(type, name,\r
-                value);\r
-        constantDefintions.add(builder);\r
-\r
-        return builder;\r
-    }\r
-\r
-    @Override\r
-    public EnumBuilder addEnumeration(final String name) {\r
-        final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
-                name);\r
-        enumDefinitions.add(builder);\r
-        return builder;\r
-    }\r
-\r
-    @Override\r
-    public MethodSignatureBuilder addMethod(final String name) {\r
-        final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(\r
-                this, name);\r
-        methodDefinitions.add(builder);\r
-        return builder;\r
-    }\r
-\r
-    @Override\r
-    public GeneratedType toInstance() {\r
-        return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,\r
-                constantDefintions, methodDefinitions);\r
-    }\r
-\r
-    private static final class MethodSignatureBuilderImpl implements\r
-            MethodSignatureBuilder {\r
-\r
-        private final String name;\r
-        private Type returnType;\r
-        private final List<MethodSignature.Parameter> parameters;\r
-        private String comment = "";\r
-        private final Type parent;\r
-\r
-        public MethodSignatureBuilderImpl(final Type parent, final String name) {\r
-            super();\r
-            this.name = name;\r
-            this.parent = parent;\r
-            parameters = new ArrayList<MethodSignature.Parameter>();\r
-        }\r
-\r
-        @Override\r
-        public void addReturnType(Type returnType) {\r
-            if (returnType != null) {\r
-                this.returnType = returnType;\r
-            }\r
-        }\r
-\r
-        @Override\r
-        public void addParameter(Type type, String name) {\r
-            parameters.add(new MethodParameterImpl(name, type));\r
-        }\r
-\r
-        @Override\r
-        public void addComment(String comment) {\r
-            this.comment = comment;\r
-        }\r
-\r
-        @Override\r
-        public MethodSignature toInstance(Type definingType) {\r
-            return new MethodSignatureImpl(definingType, name, comment,\r
-                    returnType, parameters);\r
-        }\r
-\r
-        @Override\r
-        public int hashCode() {\r
-            final int prime = 31;\r
-            int result = 1;\r
-            result = prime * result + ((name == null) ? 0 : name.hashCode());\r
-            return result;\r
-        }\r
-\r
-        @Override\r
-        public boolean equals(Object obj) {\r
-            if (this == obj) {\r
-                return true;\r
-            }\r
-            if (obj == null) {\r
-                return false;\r
-            }\r
-            if (getClass() != obj.getClass()) {\r
-                return false;\r
-            }\r
-            MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;\r
-            if (name == null) {\r
-                if (other.name != null) {\r
-                    return false;\r
-                }\r
-            } else if (!name.equals(other.name)) {\r
-                return false;\r
-            }\r
-            return true;\r
-        }\r
-\r
-        @Override\r
-        public String toString() {\r
-            StringBuilder builder = new StringBuilder();\r
-            builder.append("MethodBuilderImpl [name=");\r
-            builder.append(name);\r
-            builder.append(", returnType=");\r
-            builder.append(returnType);\r
-            builder.append(", parameters=");\r
-            builder.append(parameters);\r
-            builder.append(", comment=");\r
-            builder.append(comment);\r
-            builder.append(", parent=");\r
-            builder.append(parent.getName());\r
-            builder.append("]");\r
-            return builder.toString();\r
-        }\r
-\r
-    }\r
-\r
-    private static final class MethodSignatureImpl implements MethodSignature {\r
-\r
-        private final String name;\r
-        private final String comment;\r
-        private final Type definingType;\r
-        private final Type returnType;\r
-        private final List<Parameter> params;\r
-\r
-        public MethodSignatureImpl(final Type definingType, final String name,\r
-                final String comment, final Type returnType,\r
-                final List<Parameter> params) {\r
-            super();\r
-            this.name = name;\r
-            this.comment = comment;\r
-            this.definingType = definingType;\r
-            this.returnType = returnType;\r
-            this.params = Collections.unmodifiableList(params);\r
-        }\r
-\r
-        @Override\r
-        public String getName() {\r
-            return name;\r
-        }\r
-\r
-        @Override\r
-        public String getComment() {\r
-            return comment;\r
-        }\r
-\r
-        @Override\r
-        public Type getDefiningType() {\r
-            return definingType;\r
-        }\r
-\r
-        @Override\r
-        public Type getReturnType() {\r
-            return returnType;\r
-        }\r
-\r
-        @Override\r
-        public List<Parameter> getParameters() {\r
-            return params;\r
-        }\r
-\r
-        @Override\r
-        public AccessModifier getAccessModifier() {\r
-            return AccessModifier.PUBLIC;\r
-        }\r
-\r
-        @Override\r
-        public int hashCode() {\r
-            final int prime = 31;\r
-            int result = 1;\r
-            result = prime * result\r
-                    + ((comment == null) ? 0 : comment.hashCode());\r
-            result = prime * result + ((name == null) ? 0 : name.hashCode());\r
-            result = prime * result\r
-                    + ((params == null) ? 0 : params.hashCode());\r
-            result = prime * result\r
-                    + ((returnType == null) ? 0 : returnType.hashCode());\r
-\r
-            if (definingType != null) {\r
-                result = prime\r
-                        * result\r
-                        + ((definingType.getPackageName() == null) ? 0\r
-                                : definingType.getPackageName().hashCode());\r
-                result = prime\r
-                        * result\r
-                        + ((definingType.getName() == null) ? 0 : definingType\r
-                                .getName().hashCode());\r
-            }\r
-\r
-            return result;\r
-        }\r
-\r
-        @Override\r
-        public boolean equals(Object obj) {\r
-            if (this == obj) {\r
-                return true;\r
-            }\r
-            if (obj == null) {\r
-                return false;\r
-            }\r
-            if (getClass() != obj.getClass()) {\r
-                return false;\r
-            }\r
-            MethodSignatureImpl other = (MethodSignatureImpl) obj;\r
-            if (comment == null) {\r
-                if (other.comment != null) {\r
-                    return false;\r
-                }\r
-            } else if (!comment.equals(other.comment)) {\r
-                return false;\r
-            }\r
-            if (name == null) {\r
-                if (other.name != null) {\r
-                    return false;\r
-                }\r
-            } else if (!name.equals(other.name)) {\r
-                return false;\r
-            }\r
-            if (params == null) {\r
-                if (other.params != null) {\r
-                    return false;\r
-                }\r
-            } else if (!params.equals(other.params)) {\r
-                return false;\r
-            }\r
-            if (definingType == null) {\r
-                if (other.definingType != null) {\r
-                    return false;\r
-                }\r
-            } else if ((definingType != null) && (other.definingType != null)) {\r
-                if (!definingType.getPackageName().equals(\r
-                        other.definingType.getPackageName())\r
-                        && !definingType.getName().equals(\r
-                                other.definingType.getName())) {\r
-                    return false;\r
-                }\r
-            }\r
-            if (returnType == null) {\r
-                if (other.returnType != null) {\r
-                    return false;\r
-                }\r
-            } else if (!returnType.equals(other.returnType)) {\r
-                return false;\r
-            }\r
-            return true;\r
-        }\r
-\r
-        @Override\r
-        public String toString() {\r
-            StringBuilder builder = new StringBuilder();\r
-            builder.append("MethodImpl [name=");\r
-            builder.append(name);\r
-            builder.append(", comment=");\r
-            builder.append(comment);\r
-            if (definingType != null) {\r
-                builder.append(", definingType=");\r
-                builder.append(definingType.getPackageName());\r
-                builder.append(".");\r
-                builder.append(definingType.getName());\r
-            } else {\r
-                builder.append(", definingType= null");\r
-            }\r
-            builder.append(", returnType=");\r
-            builder.append(returnType);\r
-            builder.append(", params=");\r
-            builder.append(params);\r
-            builder.append("]");\r
-            return builder.toString();\r
-        }\r
-    }\r
-\r
-    private static final class GeneratedTypeImpl implements GeneratedType {\r
-\r
-        private final Type parent;\r
-        private final String packageName;\r
-        private final String name;\r
-        private final List<Enumeration> enumDefinitions;\r
-        private final List<Constant> constantDefintions;\r
-        private final List<MethodSignature> methodDefinitions;\r
-\r
-        public GeneratedTypeImpl(final Type parent, final String packageName,\r
-                final String name, final List<EnumBuilder> enumBuilders,\r
-                final List<ConstantBuilder> constantBuilders,\r
-                final List<MethodSignatureBuilder> methodBuilders) {\r
-            super();\r
-            this.parent = parent;\r
-            this.packageName = packageName;\r
-            this.name = name;\r
-\r
-            this.constantDefintions = toUnmodifiableConstants(constantBuilders);\r
-            this.enumDefinitions = toUnmodifiableEnums(enumBuilders);\r
-            this.methodDefinitions = toUnmodifiableMethods(methodBuilders);\r
-        }\r
-\r
-        private List<MethodSignature> toUnmodifiableMethods(\r
-                List<MethodSignatureBuilder> methodBuilders) {\r
-            final List<MethodSignature> methods = new ArrayList<MethodSignature>();\r
-            for (final MethodSignatureBuilder methodBuilder : methodBuilders) {\r
-                methods.add(methodBuilder.toInstance(this));\r
-            }\r
-            return Collections.unmodifiableList(methods);\r
-        }\r
-\r
-        private List<Enumeration> toUnmodifiableEnums(\r
-                List<EnumBuilder> enumBuilders) {\r
-            final List<Enumeration> enums = new ArrayList<Enumeration>();\r
-            for (final EnumBuilder enumBuilder : enumBuilders) {\r
-                enums.add(enumBuilder.toInstance(this));\r
-            }\r
-            return Collections.unmodifiableList(enums);\r
-        }\r
-\r
-        private List<Constant> toUnmodifiableConstants(\r
-                List<ConstantBuilder> constantBuilders) {\r
-            final List<Constant> constants = new ArrayList<Constant>();\r
-            for (final ConstantBuilder enumBuilder : constantBuilders) {\r
-                constants.add(enumBuilder.toInstance(this));\r
-            }\r
-            return Collections.unmodifiableList(constants);\r
-        }\r
-\r
-        @Override\r
-        public String getPackageName() {\r
-            return packageName;\r
-        }\r
-\r
-        @Override\r
-        public String getName() {\r
-            return name;\r
-        }\r
-\r
-        @Override\r
-        public Type getParentType() {\r
-            return parent;\r
-        }\r
-\r
-        @Override\r
-        public List<Enumeration> getEnumDefintions() {\r
-            return enumDefinitions;\r
-        }\r
-\r
-        @Override\r
-        public List<Constant> getConstantDefinitions() {\r
-            return constantDefintions;\r
-        }\r
-\r
-        @Override\r
-        public List<MethodSignature> getMethodDefinitions() {\r
-            return methodDefinitions;\r
-        }\r
-\r
-        @Override\r
-        public int hashCode() {\r
-            final int prime = 31;\r
-            int result = 1;\r
-            result = prime\r
-                    * result\r
-                    + ((constantDefintions == null) ? 0 : constantDefintions\r
-                            .hashCode());\r
-            result = prime\r
-                    * result\r
-                    + ((enumDefinitions == null) ? 0 : enumDefinitions\r
-                            .hashCode());\r
-            result = prime\r
-                    * result\r
-                    + ((methodDefinitions == null) ? 0 : methodDefinitions\r
-                            .hashCode());\r
-            result = prime * result + ((name == null) ? 0 : name.hashCode());\r
-            result = prime * result\r
-                    + ((packageName == null) ? 0 : packageName.hashCode());\r
-            return result;\r
-        }\r
-\r
-        @Override\r
-        public boolean equals(Object obj) {\r
-            if (this == obj) {\r
-                return true;\r
-            }\r
-            if (obj == null) {\r
-                return false;\r
-            }\r
-            if (getClass() != obj.getClass()) {\r
-                return false;\r
-            }\r
-            GeneratedTypeImpl other = (GeneratedTypeImpl) obj;\r
-            if (constantDefintions == null) {\r
-                if (other.constantDefintions != null) {\r
-                    return false;\r
-                }\r
-            } else if (!constantDefintions.equals(other.constantDefintions)) {\r
-                return false;\r
-            }\r
-            if (enumDefinitions == null) {\r
-                if (other.enumDefinitions != null) {\r
-                    return false;\r
-                }\r
-            } else if (!enumDefinitions.equals(other.enumDefinitions)) {\r
-                return false;\r
-            }\r
-            if (methodDefinitions == null) {\r
-                if (other.methodDefinitions != null) {\r
-                    return false;\r
-                }\r
-            } else if (!methodDefinitions.equals(other.methodDefinitions)) {\r
-                return false;\r
-            }\r
-            if (name == null) {\r
-                if (other.name != null) {\r
-                    return false;\r
-                }\r
-            } else if (!name.equals(other.name)) {\r
-                return false;\r
-            }\r
-            if (packageName == null) {\r
-                if (other.packageName != null) {\r
-                    return false;\r
-                }\r
-            } else if (!packageName.equals(other.packageName)) {\r
-                return false;\r
-            }\r
-            return true;\r
-        }\r
-\r
-        @Override\r
-        public String toString() {\r
-            StringBuilder builder = new StringBuilder();\r
-            builder.append("GeneratedTypeImpl [parent=");\r
-            builder.append(parent.getName());\r
-            builder.append(", packageName=");\r
-            builder.append(packageName);\r
-            builder.append(", name=");\r
-            builder.append(name);\r
-            builder.append(", enumDefinitions=");\r
-            builder.append(enumDefinitions);\r
-            builder.append(", constantDefintions=");\r
-            builder.append(constantDefintions);\r
-            builder.append(", methodDefinitions=");\r
-            builder.append(methodDefinitions);\r
-            builder.append("]");\r
-            return builder.toString();\r
-        }\r
-    }\r
+       private static final String[] SET_VALUES = new String[] { "abstract",\r
+                       "assert", "boolean", "break", "byte", "case", "catch", "char",\r
+                       "class", "const", "continue", "default", "double", "do", "else",\r
+                       "enum", "extends", "false", "final", "finally", "float", "for",\r
+                       "goto", "if", "implements", "import", "instanceof", "int",\r
+                       "interface", "long", "native", "new", "null", "package", "private",\r
+                       "protected", "public", "return", "short", "static", "strictfp",\r
+                       "super", "switch", "synchronized", "this", "throw", "throws",\r
+                       "transient", "true", "try", "void", "volatile", "while" };\r
+\r
+       public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
+                       Arrays.asList(SET_VALUES));\r
+\r
+       private String packageName;\r
+       private String comment;\r
+       private final String name;\r
+       private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();\r
+       private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();\r
+       private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();\r
+\r
+       public GeneratedTypeBuilderImpl(final String packageName, final String name) {\r
+               this.packageName = validatePackage(packageName);\r
+               this.name = name;\r
+       }\r
+\r
+       public static String validatePackage(final String packageName) {\r
+               if (packageName != null) {\r
+                       final String[] packNameParts = packageName.split("\\.");\r
+                       if (packNameParts != null) {\r
+                               final StringBuilder builder = new StringBuilder();\r
+                               for (int i = 0; i < packNameParts.length; ++i) {\r
+                                       if (JAVA_RESERVED_WORDS.contains(packNameParts[i])) {\r
+                                               packNameParts[i] = "_" + packNameParts[i];\r
+                                       }\r
+                                       if (i > 0) {\r
+                                               builder.append(".");\r
+                                       }\r
+\r
+                                       builder.append(packNameParts[i]);\r
+                               }\r
+                               return builder.toString();\r
+                       }\r
+               }\r
+               return packageName;\r
+       }\r
+\r
+       @Override\r
+       public Type getParentType() {\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public String getPackageName() {\r
+               return packageName;\r
+       }\r
+\r
+       @Override\r
+       public String getName() {\r
+               return name;\r
+       }\r
+\r
+       @Override\r
+       public void addComment(String comment) {\r
+               this.comment = comment;\r
+       }\r
+\r
+       @Override\r
+       public ConstantBuilder addConstant(Type type, String name, Object value) {\r
+               final ConstantBuilder builder = new ConstantBuilderImpl(type, name,\r
+                               value);\r
+               constantDefintions.add(builder);\r
+\r
+               return builder;\r
+       }\r
+\r
+       @Override\r
+       public EnumBuilder addEnumeration(final String name) {\r
+               final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
+                               name);\r
+               enumDefinitions.add(builder);\r
+               return builder;\r
+       }\r
+\r
+       @Override\r
+       public MethodSignatureBuilder addMethod(final String name) {\r
+               final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(\r
+                               this, name);\r
+               methodDefinitions.add(builder);\r
+               return builder;\r
+       }\r
+\r
+       @Override\r
+       public GeneratedType toInstance() {\r
+               packageName = (packageName);\r
+\r
+               return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,\r
+                               constantDefintions, methodDefinitions);\r
+       }\r
+\r
+       private static final class MethodSignatureBuilderImpl implements\r
+                       MethodSignatureBuilder {\r
+               private final String name;\r
+               private Type returnType;\r
+               private final List<MethodSignature.Parameter> parameters;\r
+               private String comment = "";\r
+               private final Type parent;\r
+\r
+               public MethodSignatureBuilderImpl(final Type parent, final String name) {\r
+                       super();\r
+                       this.name = name;\r
+                       this.parent = parent;\r
+                       parameters = new ArrayList<MethodSignature.Parameter>();\r
+                       // TODO: move implementation elsewhere!\r
+\r
+               }\r
+\r
+               @Override\r
+               public void addReturnType(Type returnType) {\r
+                       if (returnType != null) {\r
+                               this.returnType = returnType;\r
+                       }\r
+               }\r
+\r
+               @Override\r
+               public void addParameter(Type type, String name) {\r
+                       parameters.add(new MethodParameterImpl(name, type));\r
+               }\r
+\r
+               @Override\r
+               public void addComment(String comment) {\r
+                       this.comment = comment;\r
+               }\r
+\r
+               @Override\r
+               public MethodSignature toInstance(Type definingType) {\r
+                       return new MethodSignatureImpl(definingType, name, comment,\r
+                                       returnType, parameters);\r
+               }\r
+\r
+               @Override\r
+               public int hashCode() {\r
+                       final int prime = 31;\r
+                       int result = 1;\r
+                       result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+                       return result;\r
+               }\r
+\r
+               @Override\r
+               public boolean equals(Object obj) {\r
+                       if (this == obj) {\r
+                               return true;\r
+                       }\r
+                       if (obj == null) {\r
+                               return false;\r
+                       }\r
+                       if (getClass() != obj.getClass()) {\r
+                               return false;\r
+                       }\r
+                       MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;\r
+                       if (name == null) {\r
+                               if (other.name != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!name.equals(other.name)) {\r
+                               return false;\r
+                       }\r
+                       return true;\r
+               }\r
+\r
+               @Override\r
+               public String toString() {\r
+                       StringBuilder builder = new StringBuilder();\r
+                       builder.append("MethodBuilderImpl [name=");\r
+                       builder.append(name);\r
+                       builder.append(", returnType=");\r
+                       builder.append(returnType);\r
+                       builder.append(", parameters=");\r
+                       builder.append(parameters);\r
+                       builder.append(", comment=");\r
+                       builder.append(comment);\r
+                       builder.append(", parent=");\r
+                       builder.append(parent.getName());\r
+                       builder.append("]");\r
+                       return builder.toString();\r
+               }\r
+\r
+       }\r
+\r
+       private static final class MethodSignatureImpl implements MethodSignature {\r
+\r
+               private final String name;\r
+               private final String comment;\r
+               private final Type definingType;\r
+               private final Type returnType;\r
+               private final List<Parameter> params;\r
+\r
+               public MethodSignatureImpl(final Type definingType, final String name,\r
+                               final String comment, final Type returnType,\r
+                               final List<Parameter> params) {\r
+                       super();\r
+                       this.name = name;\r
+                       this.comment = comment;\r
+                       this.definingType = definingType;\r
+                       this.returnType = returnType;\r
+                       this.params = Collections.unmodifiableList(params);\r
+               }\r
+\r
+               @Override\r
+               public String getName() {\r
+                       return name;\r
+               }\r
+\r
+               @Override\r
+               public String getComment() {\r
+                       return comment;\r
+               }\r
+\r
+               @Override\r
+               public Type getDefiningType() {\r
+                       return definingType;\r
+               }\r
+\r
+               @Override\r
+               public Type getReturnType() {\r
+                       return returnType;\r
+               }\r
+\r
+               @Override\r
+               public List<Parameter> getParameters() {\r
+                       return params;\r
+               }\r
+\r
+               @Override\r
+               public AccessModifier getAccessModifier() {\r
+                       return AccessModifier.PUBLIC;\r
+               }\r
+\r
+               @Override\r
+               public int hashCode() {\r
+                       final int prime = 31;\r
+                       int result = 1;\r
+                       result = prime * result\r
+                                       + ((comment == null) ? 0 : comment.hashCode());\r
+                       result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+                       result = prime * result\r
+                                       + ((params == null) ? 0 : params.hashCode());\r
+                       result = prime * result\r
+                                       + ((returnType == null) ? 0 : returnType.hashCode());\r
+\r
+                       if (definingType != null) {\r
+                               result = prime\r
+                                               * result\r
+                                               + ((definingType.getPackageName() == null) ? 0\r
+                                                               : definingType.getPackageName().hashCode());\r
+                               result = prime\r
+                                               * result\r
+                                               + ((definingType.getName() == null) ? 0 : definingType\r
+                                                               .getName().hashCode());\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+\r
+               @Override\r
+               public boolean equals(Object obj) {\r
+                       if (this == obj) {\r
+                               return true;\r
+                       }\r
+                       if (obj == null) {\r
+                               return false;\r
+                       }\r
+                       if (getClass() != obj.getClass()) {\r
+                               return false;\r
+                       }\r
+                       MethodSignatureImpl other = (MethodSignatureImpl) obj;\r
+                       if (comment == null) {\r
+                               if (other.comment != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!comment.equals(other.comment)) {\r
+                               return false;\r
+                       }\r
+                       if (name == null) {\r
+                               if (other.name != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!name.equals(other.name)) {\r
+                               return false;\r
+                       }\r
+                       if (params == null) {\r
+                               if (other.params != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!params.equals(other.params)) {\r
+                               return false;\r
+                       }\r
+                       if (definingType == null) {\r
+                               if (other.definingType != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if ((definingType != null) && (other.definingType != null)) {\r
+                               if (!definingType.getPackageName().equals(\r
+                                               other.definingType.getPackageName())\r
+                                               && !definingType.getName().equals(\r
+                                                               other.definingType.getName())) {\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       if (returnType == null) {\r
+                               if (other.returnType != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!returnType.equals(other.returnType)) {\r
+                               return false;\r
+                       }\r
+                       return true;\r
+               }\r
+\r
+               @Override\r
+               public String toString() {\r
+                       StringBuilder builder = new StringBuilder();\r
+                       builder.append("MethodImpl [name=");\r
+                       builder.append(name);\r
+                       builder.append(", comment=");\r
+                       builder.append(comment);\r
+                       if (definingType != null) {\r
+                               builder.append(", definingType=");\r
+                               builder.append(definingType.getPackageName());\r
+                               builder.append(".");\r
+                               builder.append(definingType.getName());\r
+                       } else {\r
+                               builder.append(", definingType= null");\r
+                       }\r
+                       builder.append(", returnType=");\r
+                       builder.append(returnType);\r
+                       builder.append(", params=");\r
+                       builder.append(params);\r
+                       builder.append("]");\r
+                       return builder.toString();\r
+               }\r
+       }\r
+\r
+       private static final class GeneratedTypeImpl implements GeneratedType {\r
+\r
+               private final Type parent;\r
+               private final String packageName;\r
+               private final String name;\r
+               private final List<Enumeration> enumDefinitions;\r
+               private final List<Constant> constantDefintions;\r
+               private final List<MethodSignature> methodDefinitions;\r
+\r
+               public GeneratedTypeImpl(final Type parent, final String packageName,\r
+                               final String name, final List<EnumBuilder> enumBuilders,\r
+                               final List<ConstantBuilder> constantBuilders,\r
+                               final List<MethodSignatureBuilder> methodBuilders) {\r
+                       super();\r
+                       this.parent = parent;\r
+                       this.packageName = packageName;\r
+                       this.name = name;\r
+\r
+                       this.constantDefintions = toUnmodifiableConstants(constantBuilders);\r
+                       this.enumDefinitions = toUnmodifiableEnums(enumBuilders);\r
+                       this.methodDefinitions = toUnmodifiableMethods(methodBuilders);\r
+               }\r
+\r
+               private List<MethodSignature> toUnmodifiableMethods(\r
+                               List<MethodSignatureBuilder> methodBuilders) {\r
+                       final List<MethodSignature> methods = new ArrayList<MethodSignature>();\r
+                       for (final MethodSignatureBuilder methodBuilder : methodBuilders) {\r
+                               methods.add(methodBuilder.toInstance(this));\r
+                       }\r
+                       return Collections.unmodifiableList(methods);\r
+               }\r
+\r
+               private List<Enumeration> toUnmodifiableEnums(\r
+                               List<EnumBuilder> enumBuilders) {\r
+                       final List<Enumeration> enums = new ArrayList<Enumeration>();\r
+                       for (final EnumBuilder enumBuilder : enumBuilders) {\r
+                               enums.add(enumBuilder.toInstance(this));\r
+                       }\r
+                       return Collections.unmodifiableList(enums);\r
+               }\r
+\r
+               private List<Constant> toUnmodifiableConstants(\r
+                               List<ConstantBuilder> constantBuilders) {\r
+                       final List<Constant> constants = new ArrayList<Constant>();\r
+                       for (final ConstantBuilder enumBuilder : constantBuilders) {\r
+                               constants.add(enumBuilder.toInstance(this));\r
+                       }\r
+                       return Collections.unmodifiableList(constants);\r
+               }\r
+\r
+               @Override\r
+               public String getPackageName() {\r
+                       return packageName;\r
+               }\r
+\r
+               @Override\r
+               public String getName() {\r
+                       return name;\r
+               }\r
+\r
+               @Override\r
+               public Type getParentType() {\r
+                       return parent;\r
+               }\r
+\r
+               @Override\r
+               public List<Enumeration> getEnumDefintions() {\r
+                       return enumDefinitions;\r
+               }\r
+\r
+               @Override\r
+               public List<Constant> getConstantDefinitions() {\r
+                       return constantDefintions;\r
+               }\r
+\r
+               @Override\r
+               public List<MethodSignature> getMethodDefinitions() {\r
+                       return methodDefinitions;\r
+               }\r
+\r
+               @Override\r
+               public int hashCode() {\r
+                       final int prime = 31;\r
+                       int result = 1;\r
+                       result = prime\r
+                                       * result\r
+                                       + ((constantDefintions == null) ? 0 : constantDefintions\r
+                                                       .hashCode());\r
+                       result = prime\r
+                                       * result\r
+                                       + ((enumDefinitions == null) ? 0 : enumDefinitions\r
+                                                       .hashCode());\r
+                       result = prime\r
+                                       * result\r
+                                       + ((methodDefinitions == null) ? 0 : methodDefinitions\r
+                                                       .hashCode());\r
+                       result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+                       result = prime * result\r
+                                       + ((packageName == null) ? 0 : packageName.hashCode());\r
+                       return result;\r
+               }\r
+\r
+               @Override\r
+               public boolean equals(Object obj) {\r
+                       if (this == obj) {\r
+                               return true;\r
+                       }\r
+                       if (obj == null) {\r
+                               return false;\r
+                       }\r
+                       if (getClass() != obj.getClass()) {\r
+                               return false;\r
+                       }\r
+                       GeneratedTypeImpl other = (GeneratedTypeImpl) obj;\r
+                       if (constantDefintions == null) {\r
+                               if (other.constantDefintions != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!constantDefintions.equals(other.constantDefintions)) {\r
+                               return false;\r
+                       }\r
+                       if (enumDefinitions == null) {\r
+                               if (other.enumDefinitions != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!enumDefinitions.equals(other.enumDefinitions)) {\r
+                               return false;\r
+                       }\r
+                       if (methodDefinitions == null) {\r
+                               if (other.methodDefinitions != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!methodDefinitions.equals(other.methodDefinitions)) {\r
+                               return false;\r
+                       }\r
+                       if (name == null) {\r
+                               if (other.name != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!name.equals(other.name)) {\r
+                               return false;\r
+                       }\r
+                       if (packageName == null) {\r
+                               if (other.packageName != null) {\r
+                                       return false;\r
+                               }\r
+                       } else if (!packageName.equals(other.packageName)) {\r
+                               return false;\r
+                       }\r
+                       return true;\r
+               }\r
+\r
+               @Override\r
+               public String toString() {\r
+                       StringBuilder builder = new StringBuilder();\r
+                       builder.append("GeneratedTypeImpl [parent=");\r
+                       builder.append(parent.getName());\r
+                       builder.append(", packageName=");\r
+                       builder.append(packageName);\r
+                       builder.append(", name=");\r
+                       builder.append(name);\r
+                       builder.append(", enumDefinitions=");\r
+                       builder.append(enumDefinitions);\r
+                       builder.append(", constantDefintions=");\r
+                       builder.append(constantDefintions);\r
+                       builder.append(", methodDefinitions=");\r
+                       builder.append(methodDefinitions);\r
+                       builder.append("]");\r
+                       return builder.toString();\r
+               }\r
+       }\r
 }\r
index d00b73d..b050ce6 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.controller.sal.binding.yang.types;\r
 \r
 import java.math.BigDecimal;\r
+import java.math.BigInteger;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
 \r
@@ -30,7 +31,7 @@ public class BaseYangTypes {
     public static final Type UINT8_TYPE = Types.typeForClass(Short.class);\r
     public static final Type UINT16_TYPE = Types.typeForClass(Integer.class);\r
     public static final Type UINT32_TYPE = Types.typeForClass(Long.class);\r
-    public static final Type UINT64_TYPE = Types.typeForClass(BigDecimal.class);\r
+    public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class);\r
 \r
     static {\r
         typeMap.put("boolean", BOOLEAN_TYPE);\r
@@ -44,7 +45,6 @@ public class BaseYangTypes {
         typeMap.put("uint16", UINT16_TYPE);\r
         typeMap.put("uint32", UINT32_TYPE);\r
         typeMap.put("uint64", UINT64_TYPE);\r
-\r
     }\r
 \r
     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {\r
index 2cc774a..54abd3d 100644 (file)
@@ -7,12 +7,39 @@
  */\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
@@ -23,23 +50,234 @@ public class TypeProviderImpl implements TypeProvider {
     public Type javaTypeForYangType(String type) {\r
         Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
                 .javaTypeForYangType(type);\r
-        // TODO: this needs to be implemented in better way\r
-        // if(t == null) {\r
-        // t = BaseYangTypes.IETF_INET_TYPES_PROVIDER.javaTypeForYangType(type);\r
-        // }\r
         return t;\r
     }\r
 \r
     @Override\r
     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type) {\r
+        Type returnType = null;\r
         if (type != null) {\r
-            Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
-                    .javaTypeForSchemaDefinitionType(type);\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
-            if (t != null) {\r
-                return t;\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
index d771946..0ac2a20 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.sal.binding.yang.types.test;
 import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertTrue;\r
 \r
+import org.junit.Ignore;\r
 import org.junit.Test;\r
 import org.opendaylight.controller.binding.generator.util.Types;\r
 import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;\r
@@ -31,7 +32,7 @@ public class BaseTypeProvider {
         ParameterizedType stringBooleanMap = Types.mapTypeFor(\r
                 provider.javaTypeForYangType("string"),\r
                 provider.javaTypeForYangType("boolean"));\r
-        assertTrue(stringBooleanMap instanceof ConcreteType);\r
+        assertTrue(!(stringBooleanMap instanceof ConcreteType));\r
         assertEquals("java.util", stringBooleanMap.getPackageName());\r
         assertEquals("Map", stringBooleanMap.getName());\r
         assertEquals(2, stringBooleanMap.getActualTypeArguments().length);\r
index a6817a7..077db92 100644 (file)
-/*
- * 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.assertTrue;
-
-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<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());
-        
-        
-    }
-    
-    @Ignore
-    @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);
-    }
-
-    @Ignore
-    @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);
-    }
-
-    @Ignore
-    @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);
-    }
-
-    @Ignore
-    @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);
-    }
-
-    @Ignore
-    @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);
-    }
-}
+/*\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.assertEquals;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.util.List;\r
+import java.util.Set;\r
+\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
+    @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
index 46aac78..88bfb81 100644 (file)
@@ -1,92 +1,96 @@
-module abstract-topology {
-    yang-version 1;
-    namespace "";
-    prefix "tp";
-
-    import ietf-inet-types { 
-        prefix "inet"; 
-        revision-date 2010-09-24;
-    }
-
-    organization "Cisco";
-    contact "WILL-BE-DEFINED-LATER";
-
-    description
-        "This module contains the definitions of elements that creates network 
-    topology i.e. definition of network nodes and links. This module is not designed to be used solely for network representation. This module SHOULD be used as base module in defining the network topology.";
-
-    revision "2013-02-08" {
-        reference "~~~ WILL BE DEFINED LATER";
-    }
-    
-    typedef node-id-ref {
-        type leafref {
-            path "/tp:topology/tp:network-nodes/tp:network-node/tp:node-id";
-        }
-        description "This type is used for leafs that reference network node instance.";
-    }
-
-    typedef link-id-ref {
-        type leafref {
-            path "/tp:topology/tp:network-links/tp:network-link/tp:link-id";
-        }
-        description "This type is used for leafs that reference network link instance.";
-    }
-
-    container topology {
-        description "This is the model of abstract topology which contains only Network Nodes and Network Links. Each topology MUST be identified by unique topology-id for reason that the store could contain many topologies.";
-
-        leaf topology-id {
-            type inet:uri;
-            description "It is presumed that datastore will contain many topologies. To distinguish between topologies it is vital to have
-            UNIQUE topology identifier.";
-        }
-
-        container network-nodes {
-            list network-node {
-                key "node-id";
-
-                leaf node-id {
-                    type inet:uri;
-                    description "The Topology identifier of network-node.";
-                }
-
-                container attributes {
-                    description "Aditional attributes that can Network Node contains.";
-                }
-                description "The list of network nodes defined for topology.";
-            }
-        }
-        
-        container network-links {
-            list network-link {
-                key "link-id";
-
-                leaf link-id {
-                    type inet:uri;
-                    description "";
-                }
-
-                container source-node {
-                    leaf id {
-                        type node-id-ref;
-                        description "Source node identifier.";
-                    }
-                }
-
-                container destination-node {
-                    leaf id {
-                        type node-id-ref;
-                        description "Destination node identifier.";
-                    }
-                }
-
-                container attributes {
-                    description "Aditional attributes that can Network Link contains.";
-                }
-                description "The Network Link which is defined by Local (Source) and Remote (Destination) Network Nodes. Every link MUST be defined either by identifier and
-                his local and remote Network Nodes (In real applications it is common that many links are originated from one node and end up in same remote node). To ensure that we would always know to distinguish between links, every link SHOULD have identifier.";
-            }
-        }
-    }
+module abstract-topology {\r
+    yang-version 1;\r
+    namespace "urn:model:abstract:topology";\r
+    prefix "tp";\r
+\r
+    import ietf-inet-types { \r
+        prefix "inet"; \r
+        revision-date 2010-09-24;\r
+    }\r
+\r
+    organization "Cisco";\r
+    contact "WILL-BE-DEFINED-LATER";\r
+\r
+    description\r
+        "This module contains the definitions of elements that creates network \r
+    topology i.e. definition of network nodes and links. This module is not designed to be used solely for network representation. This module SHOULD be used as base module in defining the network topology.";\r
+\r
+    revision "2013-02-08" {\r
+        reference "~~~ WILL BE DEFINED LATER";\r
+    }\r
+    \r
+    revision "2013-01-01" {\r
+        reference "~~~ WILL BE DEFINED LATER";\r
+    }\r
+    \r
+    typedef node-id-ref {\r
+        type leafref {\r
+            path "/tp:topology/tp:network-nodes/tp:network-node/tp:node-id";\r
+        }\r
+        description "This type is used for leafs that reference network node instance.";\r
+    }\r
+\r
+    typedef link-id-ref {\r
+        type leafref {\r
+            path "/tp:topology/tp:network-links/tp:network-link/tp:link-id";\r
+        }\r
+        description "This type is used for leafs that reference network link instance.";\r
+    }\r
+\r
+    container topology {\r
+        description "This is the model of abstract topology which contains only Network Nodes and Network Links. Each topology MUST be identified by unique topology-id for reason that the store could contain many topologies.";\r
+\r
+        leaf topology-id {\r
+            type inet:uri;\r
+            description "It is presumed that datastore will contain many topologies. To distinguish between topologies it is vital to have\r
+            UNIQUE topology identifier.";\r
+        }\r
+\r
+        container network-nodes {\r
+            list network-node {\r
+                key "node-id";\r
+\r
+                leaf node-id {\r
+                    type inet:uri;\r
+                    description "The Topology identifier of network-node.";\r
+                }\r
+\r
+                container attributes {\r
+                    description "Aditional attributes that can Network Node contains.";\r
+                }\r
+                description "The list of network nodes defined for topology.";\r
+            }\r
+        }\r
+        \r
+        container network-links {\r
+            list network-link {\r
+                key "link-id";\r
+\r
+                leaf link-id {\r
+                    type inet:uri;\r
+                    description "";\r
+                }\r
+\r
+                container source-node {\r
+                    leaf id {\r
+                        type node-id-ref;\r
+                        description "Source node identifier.";\r
+                    }\r
+                }\r
+\r
+                container destination-node {\r
+                    leaf id {\r
+                        type node-id-ref;\r
+                        description "Destination node identifier.";\r
+                    }\r
+                }\r
+\r
+                container attributes {\r
+                    description "Aditional attributes that can Network Link contains.";\r
+                }\r
+                description "The Network Link which is defined by Local (Source) and Remote (Destination) Network Nodes. Every link MUST be defined either by identifier and\r
+                his local and remote Network Nodes (In real applications it is common that many links are originated from one node and end up in same remote node). To ensure that we would always know to distinguish between links, every link SHOULD have identifier.";\r
+            }\r
+        }\r
+    }\r
 }
\ No newline at end of file
index 4e50f60..cc7bffa 100644 (file)
@@ -7,7 +7,9 @@
  */\r
 package org.opendaylight.controller.sal.java.api.generator;\r
 \r
-import static org.opendaylight.controller.sal.java.api.generator.Constants.*;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;\r
 \r
 import java.io.IOException;\r
 import java.io.StringWriter;\r
@@ -15,39 +17,43 @@ import java.io.Writer;
 import java.util.List;\r
 \r
 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;\r
-import org.opendaylight.controller.sal.binding.model.api.Constant;\r
-import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\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.Type;\r
 \r
 public class CompositeKeyGenerator implements CodeGenerator {\r
 \r
     @Override\r
-    public Writer generate(GeneratedType type) throws IOException {\r
+    public Writer generate(Type type) throws IOException {\r
         final Writer writer = new StringWriter();\r
-        final List<Constant> fields = type.getConstantDefinitions();\r
+        if (type instanceof GeneratedTransferObject) {\r
+            GeneratedTransferObject genTO = (GeneratedTransferObject)type;\r
+            final List<GeneratedProperty> fields = genTO.getProperties();\r
 \r
-        writer.write(GeneratorUtil.createClassDeclarationWithPkgName(\r
-                type.getPackageName(), type.getName(), ""));\r
-        writer.write(NL);\r
-        writer.write(NL);\r
-\r
-        if (fields != null) {\r
-            for (Constant field : fields) {\r
-                writer.write(GeneratorUtil.createField(field, TAB) + NL);\r
-            }\r
+            writer.write(GeneratorUtil.createClassDeclarationWithPkgName(\r
+                    type.getPackageName(), type.getName(), ""));\r
             writer.write(NL);\r
-\r
-            for (Constant field : fields) {\r
-                writer.write(GeneratorUtil.createGetter(field, TAB) + NL);\r
-            }\r
             writer.write(NL);\r
-\r
-            writer.write(GeneratorUtil.createHashCode(fields, TAB) + NL);\r
-            writer.write(GeneratorUtil.createEquals(type, fields, TAB) + NL);\r
-            writer.write(GeneratorUtil.createToString(type, fields, TAB) + NL);\r
-\r
-            writer.write(RCB);\r
+            \r
+            if (fields != null) {\r
+                for (GeneratedProperty field : fields) {\r
+                    writer.write(GeneratorUtil.createField(field, TAB) + NL);\r
+                }\r
+                writer.write(NL);\r
+                writer.write(GeneratorUtil.createConstructor(genTO, TAB) + NL);\r
+                writer.write(NL);\r
+                for (GeneratedProperty field : fields) {\r
+                    writer.write(GeneratorUtil.createGetter(field, TAB) + NL);\r
+                }\r
+                writer.write(NL);\r
+\r
+                writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL);\r
+                writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL);\r
+                writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL);\r
+\r
+                writer.write(RCB);\r
+            }\r
         }\r
-\r
         return writer;\r
     }\r
 \r
index 7944875..f2388fe 100644 (file)
@@ -13,21 +13,35 @@ import java.io.FileWriter;
 import java.io.IOException;\r
 import java.io.Writer;\r
 import java.util.ArrayList;\r
+import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Set;\r
 \r
 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;\r
+import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
 \r
 public class GeneratorJavaFile {\r
 \r
-    private final CodeGenerator codeGenerator;\r
+    private final CodeGenerator interfaceGenerator;\r
+    private final CompositeKeyGenerator classGenerator;\r
     private final Set<GeneratedType> types;\r
+    private final Set<GeneratedTransferObject> genTransferObjects;\r
 \r
-    public GeneratorJavaFile(CodeGenerator codeGenerator,\r
-            Set<GeneratedType> types) {\r
-        this.codeGenerator = codeGenerator;\r
+    public GeneratorJavaFile(final CodeGenerator codeGenerator,\r
+            final Set<GeneratedType> types) {\r
+        this.interfaceGenerator = codeGenerator;\r
         this.types = types;\r
+        this.genTransferObjects = new HashSet<GeneratedTransferObject>();\r
+        classGenerator = new CompositeKeyGenerator();\r
+    }\r
+\r
+    public GeneratorJavaFile(final Set<GeneratedType> types,\r
+            final Set<GeneratedTransferObject> genTransferObjects) {\r
+        this.interfaceGenerator = new InterfaceGenerator();\r
+        this.classGenerator = new CompositeKeyGenerator();\r
+        this.types = types;\r
+        this.genTransferObjects = genTransferObjects;\r
     }\r
 \r
     public boolean generateToFile() {\r
@@ -53,7 +67,36 @@ public class GeneratorJavaFile {
                     file.createNewFile();\r
                     fw = new FileWriter(file);\r
                     bw = new BufferedWriter(fw);\r
-                    Writer writer = codeGenerator.generate(type);\r
+                    Writer writer = interfaceGenerator.generate(type);\r
+                    bw.write(writer.toString());\r
+\r
+                    if (bw != null) {\r
+                        try {\r
+                            bw.close();\r
+                        } catch (IOException e) {\r
+                            // TODO: log?\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+            for (GeneratedTransferObject transferObject : genTransferObjects) {\r
+                String parentPath = generateParentPath(path,\r
+                        transferObject.getPackageName());\r
+\r
+                File file = new File(parentPath, transferObject.getName() + ".java");\r
+                File parent = file.getParentFile();\r
+                if (!parent.exists()) {\r
+                    parent.mkdirs();\r
+                }\r
+\r
+                if (!file.exists()) {\r
+                    FileWriter fw = null;\r
+                    BufferedWriter bw = null;\r
+\r
+                    file.createNewFile();\r
+                    fw = new FileWriter(file);\r
+                    bw = new BufferedWriter(fw);\r
+                    Writer writer = classGenerator.generate(transferObject);\r
                     bw.write(writer.toString());\r
 \r
                     if (bw != null) {\r
index 673d37e..a4018a5 100644 (file)
  */\r
 package org.opendaylight.controller.sal.java.api.generator;\r
 \r
-import static org.opendaylight.controller.sal.java.api.generator.Constants.*;\r
-\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.CLASS;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.COMMA;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.ENUM;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.FINAL;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.GAP;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.IFC;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.LB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.LCB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.PKG;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.PRIVATE;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.PUBLIC;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.RB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.SC;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.STATIC;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;\r
+\r
+import java.util.Arrays;\r
+import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Set;\r
 \r
 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
 import org.opendaylight.controller.sal.binding.model.api.Enumeration;\r
+import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;\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.MethodSignature;\r
+import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;\r
 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;\r
 import org.opendaylight.controller.sal.binding.model.api.Type;\r
-import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;\r
-import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;\r
 \r
 public class GeneratorUtil {\r
 \r
+    private static final String[] SET_VALUES = new String[] { "abstract",\r
+            "assert", "boolean", "break", "byte", "case", "catch", "char",\r
+            "class", "const", "continue", "default", "double", "do", "else",\r
+            "enum", "extends", "false", "final", "finally", "float", "for",\r
+            "goto", "if", "implements", "import", "instanceof", "int",\r
+            "interface", "long", "native", "new", "null", "package", "private",\r
+            "protected", "public", "return", "short", "static", "strictfp",\r
+            "super", "switch", "synchronized", "this", "throw", "throws",\r
+            "transient", "true", "try", "void", "volatile", "while" };\r
+\r
+    public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
+            Arrays.asList(SET_VALUES));\r
+\r
     private GeneratorUtil() {\r
     }\r
 \r
-    public static String createIfcDeclarationWithPkgName(String packageName,\r
-            String name, String indent) {\r
-        return createFileDeclarationWithPkgName(IFC, packageName, name, indent);\r
+    private static String validateParamName(final String paramName) {\r
+        if (paramName != null) {\r
+            if (JAVA_RESERVED_WORDS.contains(paramName)) {\r
+                return "_" + paramName;\r
+            }\r
+        }\r
+        return paramName;\r
+    }\r
+\r
+    public static String createIfcDeclarationWithPkgName(\r
+            final String packageName, final String name, final String indent) {\r
+        return createFileDeclarationWithPkgName(IFC,\r
+                packageName, validateParamName(name), indent);\r
     }\r
 \r
-    public static String createClassDeclarationWithPkgName(String packageName,\r
-            String name, String indent) {\r
-        return createFileDeclarationWithPkgName(CLASS, packageName, name,\r
-                indent);\r
+    public static String createClassDeclarationWithPkgName(\r
+            final String packageName, final String name, final String indent) {\r
+        return createFileDeclarationWithPkgName(CLASS,\r
+                packageName, validateParamName(name), indent);\r
     }\r
 \r
-    private static String createFileDeclarationWithPkgName(String type,\r
-            String packageName, String name, String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
-        sb.append(PKG + GAP + packageName + SC);\r
-        sb.append(NL);\r
-        sb.append(NL);\r
-        sb.append(PUBLIC + GAP + type + GAP + name + GAP + LCB);\r
-        return sb.toString();\r
+    private static String createFileDeclarationWithPkgName(final String type,\r
+            final String packageName, final String name, final String indent) {\r
+        final StringBuilder builder = new StringBuilder();\r
+        builder.append(PKG + GAP + packageName + SC);\r
+        builder.append(NL);\r
+        builder.append(NL);\r
+        builder.append(PUBLIC + GAP + type + GAP + validateParamName(name) + GAP + LCB);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createConstant(Constant constant, String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
-        sb.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);\r
-        sb.append(getExplicitType(constant.getType()) + GAP\r
+    public static String createConstant(final Constant constant,\r
+            final String indent) {\r
+        final StringBuilder builder = new StringBuilder();\r
+        builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);\r
+        builder.append(getExplicitType(constant.getType()) + GAP\r
                 + constant.getName());\r
-        sb.append(GAP + "=" + GAP);\r
-        sb.append(constant.getValue() + SC);\r
-        return sb.toString();\r
+        builder.append(GAP + "=" + GAP);\r
+        builder.append(constant.getValue() + SC);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createField(Constant field, String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
-        sb.append(indent + PRIVATE + GAP);\r
-        sb.append(getExplicitType(field.getType()) + GAP + field.getName());\r
-        sb.append(GAP + "=" + GAP);\r
-        sb.append(field.getValue() + SC);\r
-        return sb.toString();\r
+    public static String createField(final GeneratedProperty property,\r
+            final String indent) {\r
+        final StringBuilder builder = new StringBuilder();\r
+        builder.append(indent + PRIVATE + GAP);\r
+        builder.append(getExplicitType(property.getReturnType()) + GAP\r
+                + property.getName());\r
+        builder.append(SC);\r
+        return builder.toString();\r
     }\r
 \r
     /**\r
@@ -71,197 +117,271 @@ public class GeneratorUtil {
      * @param indent\r
      * @return\r
      */\r
-    public static String createMethodDeclaration(MethodSignature method,\r
-            String indent) {\r
-        String comment = method.getComment();\r
-        Type type = method.getReturnType();\r
-        String name = method.getName();\r
-        List<Parameter> parameters = method.getParameters();\r
-\r
-        StringBuilder sb = new StringBuilder();\r
-        createComment(sb, comment, indent);\r
-\r
-        sb.append(indent + getExplicitType(type) + GAP + name);\r
-        sb.append(LB);\r
+    public static String createMethodDeclaration(final MethodSignature method,\r
+            final String indent) {\r
+        final String comment = method.getComment();\r
+        final Type type = method.getReturnType();\r
+        final String name = method.getName();\r
+        final List<Parameter> parameters = method.getParameters();\r
+\r
+        final StringBuilder builder = new StringBuilder();\r
+        createComment(builder, comment, indent);\r
+\r
+        builder.append(indent + getExplicitType(type) + GAP + name);\r
+        builder.append(LB);\r
         for (int i = 0; i < parameters.size(); i++) {\r
             Parameter p = parameters.get(i);\r
             String separator = COMMA;\r
             if (i + 1 == parameters.size()) {\r
                 separator = "";\r
             }\r
-            sb.append(getExplicitType(p.getType()) + GAP + p.getName()\r
+            builder.append(getExplicitType(p.getType()) + GAP + validateParamName(p.getName())\r
                     + separator);\r
         }\r
-        sb.append(RB);\r
-        sb.append(SC);\r
+        builder.append(RB);\r
+        builder.append(SC);\r
+\r
+        return builder.toString();\r
+    }\r
+\r
+    public static String createConstructor(\r
+            GeneratedTransferObject genTransferObject, final String indent) {\r
+        final StringBuilder builder = new StringBuilder();\r
+\r
+        final List<GeneratedProperty> properties = genTransferObject\r
+                .getProperties();\r
+        builder.append(indent);\r
+        builder.append(PUBLIC);\r
+        builder.append(GAP);\r
+        builder.append(genTransferObject.getName());\r
+        builder.append(LB);\r
+\r
+        boolean first = true;\r
+        if (properties != null) {\r
+            for (final GeneratedProperty property : properties) {\r
+                if (first) {\r
+                    builder.append(getExplicitType(property.getReturnType()));\r
+                    builder.append(" ");\r
+                    builder.append(property.getName());\r
+                    first = false;\r
+                } else {\r
+                    builder.append(", ");\r
+                    builder.append(getExplicitType(property.getReturnType()));\r
+                    builder.append(builder.append(" "));\r
+                    builder.append(property.getName());\r
+                }\r
+            }\r
+        }\r
+\r
+        builder.append(RB);\r
+        builder.append(GAP);\r
+        builder.append(LCB);\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(TAB);\r
+        builder.append("super();");\r
+        builder.append(NL);\r
+\r
+        if (properties != null) {\r
+            for (final GeneratedProperty property : properties) {\r
+                builder.append(indent);\r
+                builder.append(TAB);\r
+                builder.append("this.");\r
+                builder.append(property.getName());\r
+                builder.append(" = ");\r
+                builder.append(property.getName());\r
+                builder.append(SC);\r
+                builder.append(NL);\r
+            }\r
+        }\r
+\r
+        builder.append(indent);\r
+        builder.append(RCB);\r
 \r
-        return sb.toString();\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createGetter(Constant field, String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
+    public static String createGetter(final GeneratedProperty property,\r
+            final String indent) {\r
+        final StringBuilder builder = new StringBuilder();\r
 \r
-        Type type = field.getType();\r
-        String varName = field.getName();\r
-        char first = Character.toUpperCase(varName.charAt(0));\r
-        String methodName = "get" + first + varName.substring(1);\r
+        final Type type = property.getReturnType();\r
+        final String varName = property.getName();\r
+        final char first = Character.toUpperCase(varName.charAt(0));\r
+        final String methodName = "get" + first + varName.substring(1);\r
 \r
-        sb.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP\r
+        builder.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP\r
                 + methodName);\r
-        sb.append(LB + RB + LCB + NL);\r
+        builder.append(LB + RB + LCB + NL);\r
 \r
         String currentIndent = indent + TAB;\r
 \r
-        sb.append(currentIndent + "return " + varName + SC + NL);\r
+        builder.append(currentIndent + "return " + varName + SC + NL);\r
 \r
-        sb.append(indent + RCB);\r
-        return sb.toString();\r
+        builder.append(indent + RCB);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createHashCode(List<Constant> fields, String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
-        sb.append(indent + "public int hashCode() {" + NL);\r
-        sb.append(indent + TAB + "final int prime = 31;" + NL);\r
-        sb.append(indent + TAB + "int result = 1;" + NL);\r
-\r
-        for (Constant field : fields) {\r
-            String fieldName = field.getName();\r
-            sb.append(indent + TAB + "result = prime * result + ((" + fieldName\r
-                    + " == null) ? 0 : " + fieldName + ".hashCode());" + NL);\r
+    public static String createHashCode(\r
+            final List<GeneratedProperty> properties, final String indent) {\r
+        StringBuilder builder = new StringBuilder();\r
+        builder.append(indent + "public int hashCode() {" + NL);\r
+        builder.append(indent + TAB + "final int prime = 31;" + NL);\r
+        builder.append(indent + TAB + "int result = 1;" + NL);\r
+\r
+        for (GeneratedProperty property : properties) {\r
+            String fieldName = property.getName();\r
+            builder.append(indent + TAB + "result = prime * result + (("\r
+                    + fieldName + " == null) ? 0 : " + fieldName\r
+                    + ".hashCode());" + NL);\r
         }\r
 \r
-        sb.append(indent + TAB + "return result;" + NL);\r
-        sb.append(indent + RCB + NL);\r
-        return sb.toString();\r
+        builder.append(indent + TAB + "return result;" + NL);\r
+        builder.append(indent + RCB + NL);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createEquals(Type type, List<Constant> fields,\r
-            String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
+    public static String createEquals(final GeneratedTransferObject type,\r
+            final List<GeneratedProperty> properties, final String indent) {\r
+        StringBuilder builder = new StringBuilder();\r
         final String indent1 = indent + TAB;\r
         final String indent2 = indent + TAB + TAB;\r
         final String indent3 = indent + TAB + TAB + TAB;\r
 \r
-        sb.append(indent + "public boolean equals(Object obj) {" + NL);\r
-        sb.append(indent1 + "if (this == obj) {" + NL);\r
-        sb.append(indent2 + "return true;" + NL);\r
-        sb.append(indent1 + "}" + NL);\r
-        sb.append(indent1 + "if (obj == null) {" + NL);\r
-        sb.append(indent2 + "return false;" + NL);\r
-        sb.append(indent1 + "}" + NL);\r
-        sb.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);\r
-        sb.append(indent2 + "return false;" + NL);\r
-        sb.append(indent1 + "}" + NL);\r
+        builder.append(indent + "public boolean equals(Object obj) {" + NL);\r
+        builder.append(indent1 + "if (this == obj) {" + NL);\r
+        builder.append(indent2 + "return true;" + NL);\r
+        builder.append(indent1 + "}" + NL);\r
+        builder.append(indent1 + "if (obj == null) {" + NL);\r
+        builder.append(indent2 + "return false;" + NL);\r
+        builder.append(indent1 + "}" + NL);\r
+        builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);\r
+        builder.append(indent2 + "return false;" + NL);\r
+        builder.append(indent1 + "}" + NL);\r
 \r
         String typeStr = type.getPackageName() + "." + type.getName();\r
-        sb.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL);\r
-\r
-        for (Constant field : fields) {\r
-            String fieldName = field.getName();\r
-            sb.append(indent1 + "if (" + fieldName + " == null) {" + NL);\r
-            sb.append(indent2 + "if (other." + fieldName + " != null) {" + NL);\r
-            sb.append(indent3 + "return false;" + NL);\r
-            sb.append(indent2 + "}" + NL);\r
-            sb.append(indent1 + "} else if (!" + fieldName + ".equals(other."\r
-                    + fieldName + ")) {" + NL);\r
-            sb.append(indent2 + "return false;" + NL);\r
-            sb.append(indent1 + "}" + NL);\r
+        builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"\r
+                + NL);\r
+\r
+        for (GeneratedProperty property : properties) {\r
+            String fieldName = property.getName();\r
+            builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);\r
+            builder.append(indent2 + "if (other." + fieldName + " != null) {"\r
+                    + NL);\r
+            builder.append(indent3 + "return false;" + NL);\r
+            builder.append(indent2 + "}" + NL);\r
+            builder.append(indent1 + "} else if (!" + fieldName\r
+                    + ".equals(other." + fieldName + ")) {" + NL);\r
+            builder.append(indent2 + "return false;" + NL);\r
+            builder.append(indent1 + "}" + NL);\r
         }\r
 \r
-        sb.append(indent1 + "return true;" + NL);\r
+        builder.append(indent1 + "return true;" + NL);\r
 \r
-        sb.append(indent + RCB + NL);\r
-        return sb.toString();\r
+        builder.append(indent + RCB + NL);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createToString(Type type, List<Constant> fields,\r
-            String indent) {\r
-        StringBuilder sb = new StringBuilder();\r
-        String typeStr = type.getPackageName() + "." + type.getName();\r
-\r
-        sb.append(indent + "public String toString() {" + NL);\r
-        sb.append(indent + TAB + "return \"" + typeStr + "[");\r
+    public static String createToString(final GeneratedTransferObject type,\r
+            final List<GeneratedProperty> properties, final String indent) {\r
+        StringBuilder builder = new StringBuilder();\r
+        builder.append(indent);\r
+        builder.append("public String toString() {");\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(TAB);\r
+        builder.append("StringBuilder builder = new StringBuilder();");\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(TAB);\r
+        builder.append("builder.append(\"");\r
+        builder.append(type.getName());\r
+        builder.append(" [");\r
 \r
         boolean first = true;\r
-        for (Constant field : fields) {\r
-            String fieldName = field.getName();\r
-            String fieldType = field.getType().getPackageName() + "."\r
-                    + field.getType().getName();\r
+        for (GeneratedProperty property : properties) {\r
             if (first) {\r
-                if (fieldType.equals("java.lang.String")) {\r
-                    sb.append(fieldName + "=\\\""\r
-                            + parseStringValue((String) field.getValue())\r
-                            + "\\\"");\r
-                } else {\r
-                    sb.append(fieldName + "=" + field.getValue() + "");\r
-                }\r
+                builder.append(property.getName());\r
+                builder.append("=\");");\r
+                builder.append(NL);\r
+                builder.append(indent);\r
+                builder.append(TAB);\r
+                builder.append("builder.append(");\r
+                builder.append(property.getName());\r
+                builder.append(");");\r
+                first = false;\r
             } else {\r
-                if (fieldType.equals("java.lang.String")) {\r
-                    sb.append(", " + fieldName + "=\\\""\r
-                            + parseStringValue((String) field.getValue())\r
-                            + "\\\"");\r
-                } else {\r
-                    sb.append(", " + fieldName + "=" + field.getValue() + "");\r
-                }\r
-\r
+                builder.append(NL);\r
+                builder.append(indent);\r
+                builder.append(TAB);\r
+                builder.append("builder.append(\", ");\r
+                builder.append(property.getName());\r
+                builder.append("=\");");\r
+                builder.append(NL);\r
+                builder.append(indent);\r
+                builder.append(TAB);\r
+                builder.append("builder.append(\", ");\r
+                builder.append(property.getName());\r
+                builder.append(");");\r
             }\r
-            first = false;\r
         }\r
-        sb.append("]\"" + SC + NL);\r
-\r
-        sb.append(indent + RCB + NL);\r
-        return sb.toString();\r
-    }\r
-\r
-    /**\r
-     * Remove starting and ending quote sign\r
-     * \r
-     * @param o\r
-     * @return\r
-     */\r
-    private static String parseStringValue(String str) {\r
-        return str.substring(1, str.length() - 1);\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(TAB);\r
+        builder.append("builder.append(\"]\");");\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(TAB);\r
+        builder.append("return builder.toString();");\r
+\r
+        builder.append(NL);\r
+        builder.append(indent);\r
+        builder.append(RCB);\r
+        builder.append(NL);\r
+        return builder.toString();\r
     }\r
 \r
-    public static String createEnum(Enumeration e, String indent) {\r
-        StringBuilder sb = new StringBuilder(indent + ENUM + GAP + e.getName()\r
-                + GAP + LCB + NL);\r
+    public static String createEnum(final Enumeration enumeration,\r
+            final String indent) {\r
+        final StringBuilder builder = new StringBuilder(indent + ENUM + GAP\r
+                + enumeration.getName() + GAP + LCB + NL);\r
 \r
         String separator = COMMA;\r
-        List<Pair> values = e.getValues();\r
-        sb.append(indent + TAB);\r
+        final List<Pair> values = enumeration.getValues();\r
+        builder.append(indent + TAB);\r
         for (int i = 0; i < values.size(); i++) {\r
             if (i + 1 == values.size()) {\r
                 separator = SC;\r
             }\r
-            sb.append(values.get(i).getName() + separator);\r
+            builder.append(values.get(i).getName() + separator);\r
         }\r
-        sb.append(NL);\r
-        sb.append(indent + RCB);\r
-        return sb.toString();\r
+        builder.append(NL);\r
+        builder.append(indent + RCB);\r
+        return builder.toString();\r
     }\r
 \r
-    private static String getExplicitType(Type type) {\r
+    private static String getExplicitType(final Type type) {\r
         String packageName = type.getPackageName();\r
         if (packageName.endsWith(".")) {\r
             packageName = packageName.substring(0, packageName.length() - 1);\r
         }\r
-        StringBuilder sb = new StringBuilder(packageName + "." + type.getName());\r
+        final StringBuilder builder = new StringBuilder(packageName + "."\r
+                + type.getName());\r
         if (type instanceof ParameterizedType) {\r
             ParameterizedType pType = (ParameterizedType) type;\r
             Type[] pTypes = pType.getActualTypeArguments();\r
-            sb.append("<");\r
-            sb.append(getParameters(pTypes));\r
-            sb.append(">");\r
+            builder.append("<");\r
+            builder.append(getParameters(pTypes));\r
+            builder.append(">");\r
         }\r
-        if (sb.toString().equals("java.lang.Void")) {\r
+        if (builder.toString().equals("java.lang.Void")) {\r
             return "void";\r
         }\r
-        return sb.toString();\r
+        return builder.toString();\r
     }\r
 \r
-    private static String getParameters(Type[] pTypes) {\r
-        StringBuilder sb = new StringBuilder();\r
+    private static String getParameters(final Type[] pTypes) {\r
+        final StringBuilder builder = new StringBuilder();\r
         for (int i = 0; i < pTypes.length; i++) {\r
             Type t = pTypes[i];\r
 \r
@@ -269,17 +389,17 @@ public class GeneratorUtil {
             if (i + 1 == pTypes.length) {\r
                 separator = "";\r
             }\r
-            sb.append(getExplicitType(t) + separator);\r
+            builder.append(getExplicitType(t) + separator);\r
         }\r
-        return sb.toString();\r
+        return builder.toString();\r
     }\r
 \r
-    private static void createComment(StringBuilder sb, String comment,\r
-            String indent) {\r
+    private static void createComment(final StringBuilder builder,\r
+            final String comment, final String indent) {\r
         if (comment != null && comment.length() > 0) {\r
-            sb.append(indent + "/*" + NL);\r
-            sb.append(indent + comment + NL);\r
-            sb.append(indent + "*/" + NL);\r
+            builder.append(indent + "/*" + NL);\r
+            builder.append(indent + comment + NL);\r
+            builder.append(indent + "*/" + NL);\r
         }\r
     }\r
 \r
index 8a0054b..a947866 100644 (file)
@@ -7,7 +7,9 @@
  */\r
 package org.opendaylight.controller.sal.java.api.generator;\r
 \r
-import static org.opendaylight.controller.sal.java.api.generator.Constants.*;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;\r
+import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;\r
 \r
 import java.io.IOException;\r
 import java.io.StringWriter;\r
@@ -19,42 +21,46 @@ import org.opendaylight.controller.sal.binding.model.api.Constant;
 import org.opendaylight.controller.sal.binding.model.api.Enumeration;\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
 \r
 public class InterfaceGenerator implements CodeGenerator {\r
 \r
-    public Writer generate(GeneratedType type) throws IOException {\r
+    public Writer generate(Type type) throws IOException {\r
         Writer writer = new StringWriter();\r
-        final List<Constant> constants = type.getConstantDefinitions();\r
-        final List<MethodSignature> methods = type.getMethodDefinitions();\r
-        final List<Enumeration> enums = type.getEnumDefintions();\r
+        if (type instanceof GeneratedType) {\r
+            GeneratedType genType = (GeneratedType) type;\r
+            \r
+            final List<Constant> constants = genType.getConstantDefinitions();\r
+            final List<MethodSignature> methods = genType.getMethodDefinitions();\r
+            final List<Enumeration> enums = genType.getEnumDefintions();\r
 \r
-        writer.write(GeneratorUtil.createIfcDeclarationWithPkgName(\r
-                type.getPackageName(), type.getName(), ""));\r
-        writer.write(NL);\r
-\r
-        if (constants != null) {\r
-            for (Constant c : constants) {\r
-                writer.write(GeneratorUtil.createConstant(c, TAB) + NL);\r
-            }\r
+            writer.write(GeneratorUtil.createIfcDeclarationWithPkgName(\r
+                    type.getPackageName(), type.getName(), ""));\r
             writer.write(NL);\r
-        }\r
 \r
-        if (methods != null) {\r
-            for (MethodSignature m : methods) {\r
-                writer.write(GeneratorUtil.createMethodDeclaration(m, TAB) + NL);\r
+            if (constants != null) {\r
+                for (Constant c : constants) {\r
+                    writer.write(GeneratorUtil.createConstant(c, TAB) + NL);\r
+                }\r
+                writer.write(NL);\r
             }\r
-            writer.write(NL);\r
-        }\r
 \r
-        if (enums != null) {\r
-            for (Enumeration e : enums) {\r
-                writer.write(GeneratorUtil.createEnum(e, TAB) + NL);\r
+            if (methods != null) {\r
+                for (MethodSignature m : methods) {\r
+                    writer.write(GeneratorUtil.createMethodDeclaration(m, TAB) + NL);\r
+                }\r
+                writer.write(NL);\r
             }\r
-            writer.write(NL);\r
-        }\r
 \r
-        writer.write(RCB);\r
+            if (enums != null) {\r
+                for (Enumeration e : enums) {\r
+                    writer.write(GeneratorUtil.createEnum(e, TAB) + NL);\r
+                }\r
+                writer.write(NL);\r
+            }\r
 \r
+            writer.write(RCB);\r
+        }\r
         return writer;\r
     }\r
 \r
index c202f76..ac84b77 100644 (file)
@@ -1,8 +1,8 @@
-/*
- * 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
- */
+/*\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.java.api.generator;
\ No newline at end of file
index 2096c3b..9490e7f 100644 (file)
-/*
- * 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.yang.model.parser.builder.impl;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.opendaylight.controller.yang.common.QName;
-import org.opendaylight.controller.yang.model.api.AugmentationSchema;
-import org.opendaylight.controller.yang.model.api.SchemaPath;
-import org.opendaylight.controller.yang.model.api.UsesNode;
-import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
-import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
-
-public class UsesNodeBuilderImpl implements UsesNodeBuilder, Builder {
-
-    private final UsesNodeImpl instance;
-    private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
-
-    UsesNodeBuilderImpl(String groupingPathStr) {
-        SchemaPath groupingPath = parseUsesPath(groupingPathStr);
-        instance = new UsesNodeImpl(groupingPath);
-    }
-
-    @Override
-    public UsesNode build() {
-        // AUGMENTATIONS
-        final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
-        for (AugmentationSchemaBuilder builder : addedAugments) {
-            augments.add(builder.build());
-        }
-        instance.setAugmentations(augments);
-
-        return instance;
-    }
-
-    @Override
-    public void addAugment(AugmentationSchemaBuilder augmentBuilder) {
-        addedAugments.add(augmentBuilder);
-    }
-
-    @Override
-    public void setAugmenting(boolean augmenting) {
-        instance.setAugmenting(augmenting);
-    }
-
-    private SchemaPath parseUsesPath(String augmentPath) {
-        String[] splittedPath = augmentPath.split("/");
-        List<QName> path = new ArrayList<QName>();
-        QName name;
-        for (String pathElement : splittedPath) {
-            String[] splittedElement = pathElement.split(":");
-            if (splittedElement.length == 1) {
-                name = new QName(null, null, null, splittedElement[0]);
-            } else {
-                name = new QName(null, null, splittedElement[0],
-                        splittedElement[1]);
-            }
-            path.add(name);
-        }
-        final boolean absolute = augmentPath.startsWith("/");
-        return new SchemaPath(path, absolute);
-    }
-
-    private static class UsesNodeImpl implements UsesNode {
-
-        private final SchemaPath groupingPath;
-        private Set<AugmentationSchema> augmentations = Collections.emptySet();
-        private boolean augmenting;
-
-        private UsesNodeImpl(SchemaPath groupingPath) {
-            this.groupingPath = groupingPath;
-        }
-
-        @Override
-        public SchemaPath getGroupingPath() {
-            return groupingPath;
-        }
-
-        @Override
-        public Set<AugmentationSchema> getAugmentations() {
-            return augmentations;
-        }
-
-        private void setAugmentations(Set<AugmentationSchema> augmentations) {
-            if (augmentations != null) {
-                this.augmentations = augmentations;
-            }
-        }
-
-        @Override
-        public boolean isAugmenting() {
-            return augmenting;
-        }
-
-        private void setAugmenting(boolean augmenting) {
-            this.augmenting = augmenting;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
-            result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
-            result = prime * result + (augmenting ? 1231 : 1237);
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            UsesNodeImpl other = (UsesNodeImpl) obj;
-            if (groupingPath == null) {
-                if (other.groupingPath != null) {
-                    return false;
-                }
-            } else if (!groupingPath.equals(other.groupingPath)) {
-                return false;
-            }
-            if (augmentations == null) {
-                if (other.augmentations != null) {
-                    return false;
-                }
-            } else if (!augmentations.equals(other.augmentations)) {
-                return false;
-            }
-            if (augmenting != other.augmenting) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder(
-                    UsesNodeImpl.class.getSimpleName());
-            sb.append("[groupingPath=" + groupingPath +"]");
-            return sb.toString();
-        }
-
-    }
-
-}
+/*\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.yang.model.parser.builder.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.opendaylight.controller.yang.common.QName;\r
+import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
+import org.opendaylight.controller.yang.model.api.SchemaNode;\r
+import org.opendaylight.controller.yang.model.api.SchemaPath;\r
+import org.opendaylight.controller.yang.model.api.UsesNode;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.Builder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;\r
+\r
+public class UsesNodeBuilderImpl implements UsesNodeBuilder, Builder {\r
+\r
+    private final UsesNodeImpl instance;\r
+    private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();\r
+\r
+    UsesNodeBuilderImpl(String groupingPathStr) {\r
+        SchemaPath groupingPath = parseUsesPath(groupingPathStr);\r
+        instance = new UsesNodeImpl(groupingPath);\r
+    }\r
+\r
+    @Override\r
+    public UsesNode build() {\r
+        // AUGMENTATIONS\r
+        final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();\r
+        for (AugmentationSchemaBuilder builder : addedAugments) {\r
+            augments.add(builder.build());\r
+        }\r
+        instance.setAugmentations(augments);\r
+\r
+        return instance;\r
+    }\r
+\r
+    @Override\r
+    public void addAugment(AugmentationSchemaBuilder augmentBuilder) {\r
+        addedAugments.add(augmentBuilder);\r
+    }\r
+\r
+    @Override\r
+    public void setAugmenting(boolean augmenting) {\r
+        instance.setAugmenting(augmenting);\r
+    }\r
+\r
+    private SchemaPath parseUsesPath(String augmentPath) {\r
+        String[] splittedPath = augmentPath.split("/");\r
+        List<QName> path = new ArrayList<QName>();\r
+        QName name;\r
+        for (String pathElement : splittedPath) {\r
+            String[] splittedElement = pathElement.split(":");\r
+            if (splittedElement.length == 1) {\r
+                name = new QName(null, null, null, splittedElement[0]);\r
+            } else {\r
+                name = new QName(null, null, splittedElement[0],\r
+                        splittedElement[1]);\r
+            }\r
+            path.add(name);\r
+        }\r
+        final boolean absolute = augmentPath.startsWith("/");\r
+        return new SchemaPath(path, absolute);\r
+    }\r
+\r
+    private static class UsesNodeImpl implements UsesNode {\r
+\r
+        private final SchemaPath groupingPath;\r
+        private Set<AugmentationSchema> augmentations = Collections.emptySet();\r
+        private boolean augmenting;\r
+\r
+        private UsesNodeImpl(SchemaPath groupingPath) {\r
+            this.groupingPath = groupingPath;\r
+        }\r
+\r
+        @Override\r
+        public SchemaPath getGroupingPath() {\r
+            return groupingPath;\r
+        }\r
+\r
+        @Override\r
+        public Set<AugmentationSchema> getAugmentations() {\r
+            return augmentations;\r
+        }\r
+\r
+        private void setAugmentations(Set<AugmentationSchema> augmentations) {\r
+            if (augmentations != null) {\r
+                this.augmentations = augmentations;\r
+            }\r
+        }\r
+\r
+        @Override\r
+        public boolean isAugmenting() {\r
+            return augmenting;\r
+        }\r
+        \r
+        private void setAugmenting(boolean augmenting) {\r
+            this.augmenting = augmenting;\r
+        }\r
+        \r
+\r
+        @Override\r
+        public Map<SchemaPath, SchemaNode> getRefines() {\r
+            // TODO Auto-generated method stub\r
+            return null;\r
+        }\r
+        \r
+        @Override\r
+        public int hashCode() {\r
+            final int prime = 31;\r
+            int result = 1;\r
+            result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());\r
+            result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());\r
+            result = prime * result + (augmenting ? 1231 : 1237);\r
+            return result;\r
+        }\r
+\r
+        @Override\r
+        public boolean equals(Object obj) {\r
+            if (this == obj) {\r
+                return true;\r
+            }\r
+            if (obj == null) {\r
+                return false;\r
+            }\r
+            if (getClass() != obj.getClass()) {\r
+                return false;\r
+            }\r
+            UsesNodeImpl other = (UsesNodeImpl) obj;\r
+            if (groupingPath == null) {\r
+                if (other.groupingPath != null) {\r
+                    return false;\r
+                }\r
+            } else if (!groupingPath.equals(other.groupingPath)) {\r
+                return false;\r
+            }\r
+            if (augmentations == null) {\r
+                if (other.augmentations != null) {\r
+                    return false;\r
+                }\r
+            } else if (!augmentations.equals(other.augmentations)) {\r
+                return false;\r
+            }\r
+            if (augmenting != other.augmenting) {\r
+                return false;\r
+            }\r
+            return true;\r
+        }\r
+\r
+        @Override\r
+        public String toString() {\r
+            StringBuilder sb = new StringBuilder(\r
+                    UsesNodeImpl.class.getSimpleName());\r
+            sb.append("[groupingPath=" + groupingPath +"]");\r
+            return sb.toString();\r
+        }\r
+    }\r
+}\r
index 81a5c97..5cc9f8e 100644 (file)
-/*
- * 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.yang.model.parser.impl;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-
-import org.antlr.v4.runtime.ANTLRInputStream;
-import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.tree.ParseTree;
-import org.antlr.v4.runtime.tree.ParseTreeWalker;
-import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
-import org.opendaylight.controller.antlrv4.code.gen.YangParser;
-import org.opendaylight.controller.yang.common.QName;
-import org.opendaylight.controller.yang.model.api.AugmentationSchema;
-import org.opendaylight.controller.yang.model.api.DataSchemaNode;
-import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
-import org.opendaylight.controller.yang.model.api.Module;
-import org.opendaylight.controller.yang.model.api.ModuleImport;
-import org.opendaylight.controller.yang.model.api.NotificationDefinition;
-import org.opendaylight.controller.yang.model.api.RpcDefinition;
-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.BinaryTypeDefinition;
-import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
-import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition;
-import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition;
-import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
-import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
-import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
-import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;
-import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
-import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.impl.IdentitySchemaNodeBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.impl.UnionTypeBuilder;
-import org.opendaylight.controller.yang.model.parser.util.TypeConstraints;
-import org.opendaylight.controller.yang.model.parser.util.YangParseException;
-import org.opendaylight.controller.yang.model.util.BinaryType;
-import org.opendaylight.controller.yang.model.util.BitsType;
-import org.opendaylight.controller.yang.model.util.StringType;
-import org.opendaylight.controller.yang.model.util.UnknownType;
-import org.opendaylight.controller.yang.model.util.YangTypesConverter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class YangModelParserImpl implements YangModelParser {
-
-    private static final Logger logger = LoggerFactory
-            .getLogger(YangModelParserImpl.class);
-
-    @Override
-    public Module parseYangModel(String yangFile) {
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFile);
-        Set<Module> result = build(modules);
-        return result.iterator().next();
-    }
-
-    @Override
-    public Set<Module> parseYangModels(String... yangFiles) {
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFiles);
-        Set<Module> result = build(modules);
-        return result;
-    }
-
-    @Override
-    public Set<Module> parseYangModelsFromStreams(
-            InputStream... yangModelStreams) {
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangModelStreams);
-        Set<Module> result = build(modules);
-        return result;
-    }
-
-    @Override
-    public SchemaContext resolveSchemaContext(Set<Module> modules) {
-        return new SchemaContextImpl(modules);
-    }
-
-    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(
-            String... yangFiles) {
-        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);
-            FileInputStream inStream = null;
-            try {
-                inStream = new FileInputStream(yangFile);
-            } catch (FileNotFoundException e) {
-                logger.warn("Exception while reading yang stream: " + inStream,
-                        e);
-            }
-            streams[i] = inStream;
-        }
-        return resolveModuleBuildersFromStreams(streams);
-    }
-
-    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(
-            InputStream... yangFiles) {
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = new HashMap<String, TreeMap<Date, ModuleBuilder>>();
-        final ParseTreeWalker walker = new ParseTreeWalker();
-        final List<ParseTree> trees = parseStreams(yangFiles);
-        final ModuleBuilder[] builders = new ModuleBuilder[trees.size()];
-
-        for (int i = 0; i < trees.size(); i++) {
-            final YangModelParserListenerImpl yangModelParser = new YangModelParserListenerImpl();
-            walker.walk(yangModelParser, trees.get(i));
-            builders[i] = yangModelParser.getModuleBuilder();
-        }
-
-        for (ModuleBuilder builder : builders) {
-            final String builderName = builder.getName();
-            Date builderRevision = builder.getRevision();
-            if (builderRevision == null) {
-                builderRevision = createEpochTime();
-            }
-            TreeMap<Date, ModuleBuilder> builderByRevision = modules
-                    .get(builderName);
-            if (builderByRevision == null) {
-                builderByRevision = new TreeMap<Date, ModuleBuilder>();
-            }
-            builderByRevision.put(builderRevision, builder);
-            modules.put(builderName, builderByRevision);
-        }
-        return modules;
-    }
-
-    private List<ParseTree> parseStreams(InputStream... yangStreams) {
-        List<ParseTree> trees = new ArrayList<ParseTree>();
-        for (InputStream yangStream : yangStreams) {
-            trees.add(parseStream(yangStream));
-        }
-        return trees;
-    }
-
-    private ParseTree parseStream(InputStream yangStream) {
-        ParseTree result = null;
-        try {
-            final ANTLRInputStream input = new ANTLRInputStream(yangStream);
-            final YangLexer lexer = new YangLexer(input);
-            final CommonTokenStream tokens = new CommonTokenStream(lexer);
-            final YangParser parser = new YangParser(tokens);
-            result = parser.yang();
-        } catch (IOException e) {
-            logger.warn("Exception while reading yang file: " + yangStream, e);
-        }
-        return result;
-    }
-
-    private Set<Module> build(Map<String, TreeMap<Date, ModuleBuilder>> modules) {
-        // validate
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
-                .entrySet()) {
-            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()
-                    .entrySet()) {
-                ModuleBuilder moduleBuilder = childEntry.getValue();
-                validateBuilder(modules, moduleBuilder);
-            }
-        }
-        // build
-        final Set<Module> result = new HashSet<Module>();
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
-                .entrySet()) {
-            final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();
-            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()
-                    .entrySet()) {
-                ModuleBuilder moduleBuilder = childEntry.getValue();
-                modulesByRevision.put(childEntry.getKey(),
-                        moduleBuilder.build());
-                result.add(moduleBuilder.build());
-            }
-        }
-
-        return result;
-    }
-
-    private void validateBuilder(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder builder) {
-        resolveTypedefs(modules, builder);
-        resolveAugments(modules, builder);
-        resolveIdentities(modules, builder);
-    }
-
-    /**
-     * Search for dirty nodes (node which contains UnknownType) and resolve
-     * unknown types.
-     *
-     * @param modules
-     *            all available modules
-     * @param module
-     *            current module
-     */
-    private void resolveTypedefs(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder module) {
-        Map<List<String>, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
-        if (dirtyNodes.size() == 0) {
-            return;
-        } else {
-            for (Map.Entry<List<String>, TypeAwareBuilder> entry : dirtyNodes
-                    .entrySet()) {
-                TypeAwareBuilder typeToResolve = entry.getValue();
-
-                if (typeToResolve instanceof UnionTypeBuilder) {
-                    resolveUnionTypeBuilder(modules, module,
-                            (UnionTypeBuilder) typeToResolve);
-                } else {
-                    UnknownType ut = (UnknownType) typeToResolve.getType();
-                    TypeDefinition<?> resolvedType = findTargetType(ut,
-                            modules, module);
-                    typeToResolve.setType(resolvedType);
-                }
-            }
-        }
-    }
-
-    private UnionTypeBuilder resolveUnionTypeBuilder(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder builder, UnionTypeBuilder unionTypeBuilderToResolve) {
-        List<TypeDefinition<?>> resolvedTypes = new ArrayList<TypeDefinition<?>>();
-        List<TypeDefinition<?>> typesToRemove = new ArrayList<TypeDefinition<?>>();
-
-        for (TypeDefinition<?> td : unionTypeBuilderToResolve.getTypes()) {
-            if (td instanceof UnknownType) {
-                TypeDefinition<?> resolvedType = findTargetType(
-                        (UnknownType) td, modules, builder);
-                resolvedTypes.add(resolvedType);
-                typesToRemove.add(td);
-            }
-        }
-
-        List<TypeDefinition<?>> unionTypeBuilderTypes = unionTypeBuilderToResolve
-                .getTypes();
-        unionTypeBuilderTypes.addAll(resolvedTypes);
-        unionTypeBuilderTypes.removeAll(typesToRemove);
-
-        return unionTypeBuilderToResolve;
-    }
-
-    private TypeDefinition<?> findTargetType(UnknownType ut,
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder builder) {
-
-        TypeConstraints constraints = new TypeConstraints();
-        // RANGE
-        List<RangeConstraint> ranges = ut.getRangeStatements();
-        constraints.addRanges(ranges);
-        // LENGTH
-        List<LengthConstraint> lengths = ut.getLengthStatements();
-        constraints.addLengths(lengths);
-        // PATTERN
-        List<PatternConstraint> patterns = ut.getPatterns();
-        constraints.addPatterns(patterns);
-        // Fraction Digits
-        Integer fractionDigits = ut.getFractionDigits();
-
-        Map<TypeDefinitionBuilder, TypeConstraints> foundedTypeDefinitionBuilder = findTypeDefinitionBuilderWithConstraints(
-                constraints, modules, ut, builder);
-        TypeDefinitionBuilder targetType = foundedTypeDefinitionBuilder
-                .entrySet().iterator().next().getKey();
-
-        TypeDefinition<?> targetTypeBaseType = targetType.getBaseType();
-        targetTypeBaseType = mergeConstraints(targetTypeBaseType, constraints,
-                fractionDigits);
-
-        return targetTypeBaseType;
-    }
-
-    /**
-     * Traverse through all referenced types chain until base YANG type is
-     * founded.
-     *
-     * @param constraints
-     *            current type constraints
-     * @param modules
-     *            all available modules
-     * @param unknownType
-     *            unknown type
-     * @param builder
-     *            current module
-     * @return map, where key is type referenced and value is its constraints
-     */
-    private Map<TypeDefinitionBuilder, TypeConstraints> findTypeDefinitionBuilderWithConstraints(
-            TypeConstraints constraints,
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            UnknownType unknownType, ModuleBuilder builder) {
-        Map<TypeDefinitionBuilder, TypeConstraints> result = new HashMap<TypeDefinitionBuilder, TypeConstraints>();
-        QName unknownTypeQName = unknownType.getQName();
-        String unknownTypeName = unknownTypeQName.getLocalName();
-        String unknownTypePrefix = unknownTypeQName.getPrefix();
-
-        // search for module which contains referenced typedef
-        ModuleBuilder dependentModuleBuilder = findDependentModule(modules,
-                builder, unknownTypePrefix);
-
-        TypeDefinitionBuilder lookedUpBuilder = findTypedefBuilder(
-                dependentModuleBuilder.getModuleTypedefs(), unknownTypeName);
-
-        // if referenced type is UnknownType again, search recursively with
-        // current constraints
-        TypeDefinition<?> referencedType = lookedUpBuilder.getBaseType();
-        if (referencedType instanceof UnknownType) {
-            UnknownType unknown = (UnknownType) referencedType;
-
-            final List<RangeConstraint> ranges = unknown.getRangeStatements();
-            constraints.addRanges(ranges);
-            final List<LengthConstraint> lengths = unknown
-                    .getLengthStatements();
-            constraints.addLengths(lengths);
-            final List<PatternConstraint> patterns = unknown.getPatterns();
-            constraints.addPatterns(patterns);
-            return findTypeDefinitionBuilderWithConstraints(constraints,
-                    modules, unknown, dependentModuleBuilder);
-        } else {
-            mergeConstraints(referencedType, constraints);
-            result.put(lookedUpBuilder, constraints);
-            return result;
-        }
-    }
-
-    /**
-     * Go through all typedef statements from given module and search for one
-     * with given name
-     *
-     * @param typedefs
-     *            typedef statements to search
-     * @param name
-     *            name of searched typedef
-     * @return typedef with name equals to given name
-     */
-    private TypeDefinitionBuilder findTypedefBuilder(
-            Set<TypeDefinitionBuilder> typedefs, String name) {
-        TypeDefinitionBuilder result = null;
-        for (TypeDefinitionBuilder td : typedefs) {
-            if (td.getQName().getLocalName().equals(name)) {
-                result = td;
-                break;
-            }
-        }
-        if (result == null) {
-            throw new YangParseException(
-                    "Target module does not contain typedef '" + name + "'.");
-        }
-        return result;
-    }
-
-    /**
-     * Merge curent constraints with founded type constraints
-     *
-     * @param targetTypeBaseType
-     * @param constraints
-     * @param fractionDigits
-     * @return
-     */
-    private TypeDefinition<?> mergeConstraints(
-            TypeDefinition<?> targetTypeBaseType, TypeConstraints constraints,
-            Integer fractionDigits) {
-        String targetTypeBaseTypeName = targetTypeBaseType.getQName()
-                .getLocalName();
-        // enumeration, leafref and identityref omitted because they have no
-        // restrictions
-        if (targetTypeBaseType instanceof DecimalTypeDefinition) {
-            List<RangeConstraint> ranges = constraints.getRange();
-            Integer fd = fractionDigits == null ? constraints
-                    .getFractionDigits() : fractionDigits;
-            targetTypeBaseType = YangTypesConverter
-                    .javaTypeForBaseYangDecimal64Type(ranges, fd);
-        } else if (targetTypeBaseType instanceof IntegerTypeDefinition) {
-            List<RangeConstraint> ranges = constraints.getRange();
-            if (targetTypeBaseTypeName.startsWith("int")) {
-                targetTypeBaseType = YangTypesConverter
-                        .javaTypeForBaseYangSignedIntegerType(
-                                targetTypeBaseTypeName, ranges);
-            } else {
-                targetTypeBaseType = YangTypesConverter
-                        .javaTypeForBaseYangUnsignedIntegerType(
-                                targetTypeBaseTypeName, ranges);
-            }
-        } else if (targetTypeBaseType instanceof StringTypeDefinition) {
-            List<LengthConstraint> lengths = constraints.getLength();
-            List<PatternConstraint> patterns = constraints.getPatterns();
-            targetTypeBaseType = new StringType(lengths, patterns);
-        } else if (targetTypeBaseType instanceof BitsTypeDefinition) {
-            BitsTypeDefinition bitsType = (BitsTypeDefinition) targetTypeBaseType;
-            List<Bit> bits = bitsType.getBits();
-            targetTypeBaseType = new BitsType(bits);
-        } else if (targetTypeBaseType instanceof BinaryTypeDefinition) {
-            List<LengthConstraint> lengths = constraints.getLength();
-            List<Byte> bytes = Collections.emptyList();
-            targetTypeBaseType = new BinaryType(bytes, lengths, null);
-        }
-        return targetTypeBaseType;
-    }
-
-    /**
-     * Pull restriction from base type and add them to given constraints
-     *
-     * @param referencedType
-     * @param constraints
-     */
-    private void mergeConstraints(TypeDefinition<?> referencedType,
-            TypeConstraints constraints) {
-
-        if (referencedType instanceof DecimalTypeDefinition) {
-            constraints.addRanges(((DecimalTypeDefinition) referencedType)
-                    .getRangeStatements());
-            constraints
-                    .setFractionDigits(((DecimalTypeDefinition) referencedType)
-                            .getFractionDigits());
-        } else if (referencedType instanceof IntegerTypeDefinition) {
-            constraints.addRanges(((IntegerTypeDefinition) referencedType)
-                    .getRangeStatements());
-        } else if (referencedType instanceof StringTypeDefinition) {
-            constraints.addPatterns(((StringTypeDefinition) referencedType)
-                    .getPatterns());
-            constraints.addLengths(((StringTypeDefinition) referencedType)
-                    .getLengthStatements());
-        } else if (referencedType instanceof BinaryTypeDefinition) {
-            constraints.addLengths(((BinaryTypeDefinition) referencedType)
-                    .getLengthConstraints());
-        }
-    }
-
-    /**
-     * 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
-     *            current module
-     */
-    private void resolveAugments(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder module) {
-        Set<AugmentationSchemaBuilder> augmentBuilders = module
-                .getAddedAugments();
-
-        Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
-        for (AugmentationSchemaBuilder augmentBuilder : augmentBuilders) {
-            SchemaPath augmentTargetSchemaPath = augmentBuilder.getTargetPath();
-            String prefix = null;
-            List<String> augmentTargetPath = new ArrayList<String>();
-
-            for (QName pathPart : augmentTargetSchemaPath.getPath()) {
-                prefix = pathPart.getPrefix();
-                augmentTargetPath.add(pathPart.getLocalName());
-            }
-            ModuleBuilder dependentModule = findDependentModule(modules,
-                    module, prefix);
-            augmentTargetPath.add(0, dependentModule.getName());
-
-            AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule
-                    .getNode(augmentTargetPath);
-            AugmentationSchema result = augmentBuilder.build();
-            augmentTarget.addAugmentation(result);
-            fillAugmentTarget(augmentBuilder, (ChildNodeBuilder) augmentTarget);
-            augments.add(result);
-        }
-        module.setAugmentations(augments);
-    }
-
-    /**
-     * Add all augment's child nodes to given target.
-     *
-     * @param augment
-     * @param target
-     */
-    private void fillAugmentTarget(AugmentationSchemaBuilder augment,
-            ChildNodeBuilder target) {
-        for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {
-            builder.setAugmenting(true);
-            target.addChildNode(builder);
-        }
-    }
-
-    /**
-     * Go through identity statements defined in current module and resolve
-     * their 'base' statement if present.
-     *
-     * @param modules
-     *            all modules
-     * @param module
-     *            module being resolved
-     */
-    private void resolveIdentities(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder module) {
-        Set<IdentitySchemaNodeBuilder> identities = module.getAddedIdentities();
-        for (IdentitySchemaNodeBuilder identity : identities) {
-            String baseIdentityName = identity.getBaseIdentityName();
-            if (baseIdentityName != null) {
-                String baseIdentityPrefix = null;
-                String baseIdentityLocalName = null;
-                if (baseIdentityName.contains(":")) {
-                    String[] splitted = baseIdentityName.split(":");
-                    baseIdentityPrefix = splitted[0];
-                    baseIdentityLocalName = splitted[1];
-                } else {
-                    baseIdentityPrefix = module.getPrefix();
-                    baseIdentityLocalName = baseIdentityName;
-                }
-                ModuleBuilder dependentModule = findDependentModule(modules,
-                        module, baseIdentityPrefix);
-
-                Set<IdentitySchemaNodeBuilder> dependentModuleIdentities = dependentModule
-                        .getAddedIdentities();
-                for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) {
-                    if (idBuilder.getQName().getLocalName()
-                            .equals(baseIdentityLocalName)) {
-                        identity.setBaseIdentity(idBuilder);
-                    }
-                }
-            }
-        }
-    }
-
-    /**
-     * Find dependent module based on given prefix
-     *
-     * @param modules
-     *            all available modules
-     * @param module
-     *            current module
-     * @param prefix
-     *            target module prefix
-     * @return
-     */
-    private ModuleBuilder findDependentModule(
-            Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            ModuleBuilder module, String prefix) {
-        ModuleBuilder dependentModule = null;
-        Date dependentModuleRevision = null;
-
-        if (prefix.equals(module.getPrefix())) {
-            dependentModule = module;
-        } else {
-            ModuleImport dependentModuleImport = getModuleImport(module, prefix);
-            if (dependentModuleImport == null) {
-                throw new YangParseException("No import found with prefix '"
-                        + prefix + "' in module " + module.getName() + "'.");
-            }
-            String dependentModuleName = dependentModuleImport.getModuleName();
-            dependentModuleRevision = dependentModuleImport.getRevision();
-
-            TreeMap<Date, ModuleBuilder> moduleBuildersByRevision = modules
-                    .get(dependentModuleName);
-            if (dependentModuleRevision == null) {
-                dependentModule = moduleBuildersByRevision.lastEntry()
-                        .getValue();
-            } else {
-                dependentModule = moduleBuildersByRevision
-                        .get(dependentModuleRevision);
-            }
-        }
-
-        if (dependentModule == null) {
-            throw new YangParseException(
-                    "Failed to find dependent module with prefix '" + prefix
-                            + "' and revision '" + dependentModuleRevision
-                            + "'.");
-        }
-        return dependentModule;
-    }
-
-    /**
-     * Get module import referenced by given prefix.
-     *
-     * @param builder
-     *            module to search
-     * @param prefix
-     *            prefix associated with import
-     * @return ModuleImport based on given prefix
-     */
-    private ModuleImport getModuleImport(ModuleBuilder builder, String prefix) {
-        ModuleImport moduleImport = null;
-        for (ModuleImport mi : builder.getModuleImports()) {
-            if (mi.getPrefix().equals(prefix)) {
-                moduleImport = mi;
-                break;
-            }
-        }
-        return moduleImport;
-    }
-
-    private Date createEpochTime() {
-        Calendar c = Calendar.getInstance();
-        c.setTimeInMillis(0);
-        return c.getTime();
-    }
-
-    private static class SchemaContextImpl implements SchemaContext {
-        private final Set<Module> modules;
-
-        private SchemaContextImpl(Set<Module> modules) {
-            this.modules = modules;
-        }
-
-        @Override
-        public Set<DataSchemaNode> getDataDefinitions() {
-            final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();
-            for (Module m : modules) {
-                dataDefs.addAll(m.getChildNodes());
-            }
-            return dataDefs;
-        }
-
-        @Override
-        public Set<Module> getModules() {
-            return modules;
-        }
-
-        @Override
-        public Set<NotificationDefinition> getNotifications() {
-            final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
-            for (Module m : modules) {
-                notifications.addAll(m.getNotifications());
-            }
-            return notifications;
-        }
-
-        @Override
-        public Set<RpcDefinition> getOperations() {
-            final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
-            for (Module m : modules) {
-                rpcs.addAll(m.getRpcs());
-            }
-            return rpcs;
-        }
-
-        @Override
-        public Set<ExtensionDefinition> getExtensions() {
-            final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();
-            for (Module m : modules) {
-                extensions.addAll(m.getExtensionSchemaNodes());
-            }
-            return extensions;
-        }
-    }
-
-}
+/*\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.yang.model.parser.impl;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.ArrayList;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+\r
+import org.antlr.v4.runtime.ANTLRInputStream;\r
+import org.antlr.v4.runtime.CommonTokenStream;\r
+import org.antlr.v4.runtime.tree.ParseTree;\r
+import org.antlr.v4.runtime.tree.ParseTreeWalker;\r
+import org.opendaylight.controller.antlrv4.code.gen.YangLexer;\r
+import org.opendaylight.controller.antlrv4.code.gen.YangParser;\r
+import org.opendaylight.controller.yang.common.QName;\r
+import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
+import org.opendaylight.controller.yang.model.api.ExtensionDefinition;\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.NotificationDefinition;\r
+import org.opendaylight.controller.yang.model.api.RpcDefinition;\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.BinaryTypeDefinition;\r
+import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition;\r
+import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;\r
+import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition;\r
+import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition;\r
+import org.opendaylight.controller.yang.model.api.type.LengthConstraint;\r
+import org.opendaylight.controller.yang.model.api.type.PatternConstraint;\r
+import org.opendaylight.controller.yang.model.api.type.RangeConstraint;\r
+import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;\r
+import org.opendaylight.controller.yang.model.parser.api.YangModelParser;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.impl.IdentitySchemaNodeBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.impl.UnionTypeBuilder;\r
+import org.opendaylight.controller.yang.model.util.BaseConstraints;\r
+import org.opendaylight.controller.yang.model.util.BinaryType;\r
+import org.opendaylight.controller.yang.model.util.BitsType;\r
+import org.opendaylight.controller.yang.model.util.StringType;\r
+import org.opendaylight.controller.yang.model.util.UnknownType;\r
+import org.opendaylight.controller.yang.model.util.YangTypesConverter;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class YangModelParserImpl implements YangModelParser {\r
+\r
+    private static final Logger logger = LoggerFactory\r
+            .getLogger(YangModelParserImpl.class);\r
+\r
+    @Override\r
+    public Module parseYangModel(String yangFile) {\r
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFile);\r
+        Set<Module> result = build(modules);\r
+        return result.iterator().next();\r
+    }\r
+\r
+    @Override\r
+    public Set<Module> parseYangModels(String... yangFiles) {\r
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFiles);\r
+        Set<Module> result = build(modules);\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public Set<Module> parseYangModelsFromStreams(\r
+            InputStream... yangModelStreams) {\r
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangModelStreams);\r
+        Set<Module> result = build(modules);\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public SchemaContext resolveSchemaContext(Set<Module> modules) {\r
+        return new SchemaContextImpl(modules);\r
+    }\r
+\r
+    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(\r
+            String... yangFiles) {\r
+        InputStream[] streams = new InputStream[yangFiles.length];\r
+        for (int i = 0; i < yangFiles.length; i++) {\r
+            final String yangFileName = yangFiles[i];\r
+            final File yangFile = new File(yangFileName);\r
+            FileInputStream inStream = null;\r
+            try {\r
+                inStream = new FileInputStream(yangFile);\r
+            } catch (FileNotFoundException e) {\r
+                logger.warn("Exception while reading yang stream: " + inStream,\r
+                        e);\r
+            }\r
+            streams[i] = inStream;\r
+        }\r
+        return resolveModuleBuildersFromStreams(streams);\r
+    }\r
+\r
+    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(\r
+            InputStream... yangFiles) {\r
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = new HashMap<String, TreeMap<Date, ModuleBuilder>>();\r
+        final ParseTreeWalker walker = new ParseTreeWalker();\r
+        final List<ParseTree> trees = parseStreams(yangFiles);\r
+        final ModuleBuilder[] builders = new ModuleBuilder[trees.size()];\r
+\r
+        for (int i = 0; i < trees.size(); i++) {\r
+            final YangModelParserListenerImpl yangModelParser = new YangModelParserListenerImpl();\r
+            walker.walk(yangModelParser, trees.get(i));\r
+            builders[i] = yangModelParser.getModuleBuilder();\r
+        }\r
+\r
+        for (ModuleBuilder builder : builders) {\r
+            final String builderName = builder.getName();\r
+            Date builderRevision = builder.getRevision();\r
+            if (builderRevision == null) {\r
+                builderRevision = createEpochTime();\r
+            }\r
+            TreeMap<Date, ModuleBuilder> builderByRevision = modules\r
+                    .get(builderName);\r
+            if (builderByRevision == null) {\r
+                builderByRevision = new TreeMap<Date, ModuleBuilder>();\r
+            }\r
+            builderByRevision.put(builderRevision, builder);\r
+            modules.put(builderName, builderByRevision);\r
+        }\r
+        return modules;\r
+    }\r
+\r
+    private List<ParseTree> parseStreams(InputStream... yangStreams) {\r
+        List<ParseTree> trees = new ArrayList<ParseTree>();\r
+        for (InputStream yangStream : yangStreams) {\r
+            trees.add(parseStream(yangStream));\r
+        }\r
+        return trees;\r
+    }\r
+\r
+    private ParseTree parseStream(InputStream yangStream) {\r
+        ParseTree result = null;\r
+        try {\r
+            final ANTLRInputStream input = new ANTLRInputStream(yangStream);\r
+            final YangLexer lexer = new YangLexer(input);\r
+            final CommonTokenStream tokens = new CommonTokenStream(lexer);\r
+            final YangParser parser = new YangParser(tokens);\r
+            result = parser.yang();\r
+        } catch (IOException e) {\r
+            logger.warn("Exception while reading yang file: " + yangStream, e);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    private Set<Module> build(Map<String, TreeMap<Date, ModuleBuilder>> modules) {\r
+        // first validate\r
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules\r
+                .entrySet()) {\r
+            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()\r
+                    .entrySet()) {\r
+                ModuleBuilder moduleBuilder = childEntry.getValue();\r
+                validateBuilder(modules, moduleBuilder);\r
+            }\r
+        }\r
+        // then build\r
+        final Set<Module> result = new HashSet<Module>();\r
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules\r
+                .entrySet()) {\r
+            final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();\r
+            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()\r
+                    .entrySet()) {\r
+                ModuleBuilder moduleBuilder = childEntry.getValue();\r
+                modulesByRevision.put(childEntry.getKey(),\r
+                        moduleBuilder.build());\r
+                result.add(moduleBuilder.build());\r
+            }\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    private void validateBuilder(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder) {\r
+        resolveTypedefs(modules, builder);\r
+        resolveAugments(modules, builder);\r
+        resolveIdentities(modules, builder);\r
+    }\r
+\r
+    /**\r
+     * Search for dirty nodes (node which contains UnknownType) and resolve\r
+     * unknown types.\r
+     *\r
+     * @param modules\r
+     *            all available modules\r
+     * @param module\r
+     *            current module\r
+     */\r
+    private void resolveTypedefs(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder module) {\r
+        Map<List<String>, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();\r
+        if (dirtyNodes.size() == 0) {\r
+            return;\r
+        } else {\r
+            for (Map.Entry<List<String>, TypeAwareBuilder> entry : dirtyNodes\r
+                    .entrySet()) {\r
+                TypeAwareBuilder typeToResolve = entry.getValue();\r
+\r
+                if (typeToResolve instanceof UnionTypeBuilder) {\r
+                    resolveUnionTypeBuilder(modules, module,\r
+                            (UnionTypeBuilder) typeToResolve);\r
+                } else {\r
+                    UnknownType ut = (UnknownType) typeToResolve.getType();\r
+                    TypeDefinition<?> resolvedType = findTargetType(ut,\r
+                            modules, module);\r
+                    typeToResolve.setType(resolvedType);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private UnionTypeBuilder resolveUnionTypeBuilder(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder, UnionTypeBuilder unionTypeBuilderToResolve) {\r
+        List<TypeDefinition<?>> resolvedTypes = new ArrayList<TypeDefinition<?>>();\r
+        List<TypeDefinition<?>> typesToRemove = new ArrayList<TypeDefinition<?>>();\r
+\r
+        for (TypeDefinition<?> td : unionTypeBuilderToResolve.getTypes()) {\r
+            if (td instanceof UnknownType) {\r
+                TypeDefinition<?> resolvedType = findTargetType(\r
+                        (UnknownType) td, modules, builder);\r
+                resolvedTypes.add(resolvedType);\r
+                typesToRemove.add(td);\r
+            }\r
+        }\r
+\r
+        List<TypeDefinition<?>> unionTypeBuilderTypes = unionTypeBuilderToResolve\r
+                .getTypes();\r
+        unionTypeBuilderTypes.addAll(resolvedTypes);\r
+        unionTypeBuilderTypes.removeAll(typesToRemove);\r
+\r
+        return unionTypeBuilderToResolve;\r
+    }\r
+\r
+    private TypeDefinition<?> findTargetType(UnknownType ut,\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder) {\r
+\r
+        Map<TypeDefinitionBuilder, TypeConstraints> foundedTypeDefinitionBuilder = findTypeDefinitionBuilderWithConstraints(\r
+                modules, ut, builder);\r
+        TypeDefinitionBuilder targetType = foundedTypeDefinitionBuilder\r
+                .entrySet().iterator().next().getKey();\r
+        TypeConstraints constraints = foundedTypeDefinitionBuilder.entrySet()\r
+                .iterator().next().getValue();\r
+\r
+        TypeDefinition<?> targetTypeBaseType = targetType.getBaseType();\r
+\r
+        // RANGE\r
+        List<RangeConstraint> ranges = ut.getRangeStatements();\r
+        resolveRanges(ranges, targetType, modules, builder);\r
+        // LENGTH\r
+        List<LengthConstraint> lengths = ut.getLengthStatements();\r
+        resolveLengths(lengths, targetType, modules, builder);\r
+        // PATTERN\r
+        List<PatternConstraint> patterns = ut.getPatterns();\r
+        // Fraction Digits\r
+        Integer fractionDigits = ut.getFractionDigits();\r
+\r
+        targetTypeBaseType = mergeConstraints(targetTypeBaseType, constraints, ranges, lengths,\r
+                patterns, fractionDigits);\r
+\r
+        return targetTypeBaseType;\r
+    }\r
+\r
+    /**\r
+     * Merge curent constraints with founded type constraints\r
+     *\r
+     * @param targetTypeBaseType\r
+     * @param constraints\r
+     * @param ranges\r
+     * @param lengths\r
+     * @param patterns\r
+     * @param fractionDigits\r
+     */\r
+    private TypeDefinition<?> mergeConstraints(TypeDefinition<?> targetTypeBaseType,\r
+            TypeConstraints constraints, List<RangeConstraint> ranges,\r
+            List<LengthConstraint> lengths, List<PatternConstraint> patterns,\r
+            Integer fractionDigits) {\r
+        String targetTypeBaseTypeName = targetTypeBaseType.getQName()\r
+                .getLocalName();\r
+        // enumeration, leafref and identityref omitted because they have no\r
+        // restrictions\r
+        if (targetTypeBaseType instanceof DecimalTypeDefinition) {\r
+            List<RangeConstraint> fullRanges = new ArrayList<RangeConstraint>();\r
+            fullRanges.addAll(constraints.getRanges());\r
+            fullRanges.addAll(ranges);\r
+            Integer fd = fractionDigits == null ? constraints\r
+                    .getFractionDigits() : fractionDigits;\r
+            targetTypeBaseType = YangTypesConverter\r
+                    .javaTypeForBaseYangDecimal64Type(fullRanges, fd);\r
+        } else if (targetTypeBaseType instanceof IntegerTypeDefinition) {\r
+            List<RangeConstraint> fullRanges = new ArrayList<RangeConstraint>();\r
+            fullRanges.addAll(constraints.getRanges());\r
+            fullRanges.addAll(ranges);\r
+            if (targetTypeBaseTypeName.startsWith("int")) {\r
+                targetTypeBaseType = YangTypesConverter\r
+                        .javaTypeForBaseYangSignedIntegerType(\r
+                                targetTypeBaseTypeName, fullRanges);\r
+            } else {\r
+                targetTypeBaseType = YangTypesConverter\r
+                        .javaTypeForBaseYangUnsignedIntegerType(\r
+                                targetTypeBaseTypeName, fullRanges);\r
+            }\r
+        } else if (targetTypeBaseType instanceof StringTypeDefinition) {\r
+            List<LengthConstraint> fullLengths = new ArrayList<LengthConstraint>();\r
+            fullLengths.addAll(constraints.getLengths());\r
+            fullLengths.addAll(lengths);\r
+            List<PatternConstraint> fullPatterns = new ArrayList<PatternConstraint>();\r
+            fullPatterns.addAll(constraints.getPatterns());\r
+            fullPatterns.addAll(patterns);\r
+            targetTypeBaseType = new StringType(fullLengths, fullPatterns);\r
+        } else if (targetTypeBaseType instanceof BitsTypeDefinition) {\r
+            BitsTypeDefinition bitsType = (BitsTypeDefinition) targetTypeBaseType;\r
+            List<Bit> bits = bitsType.getBits();\r
+            targetTypeBaseType = new BitsType(bits);\r
+        } else if (targetTypeBaseType instanceof BinaryTypeDefinition) {\r
+            targetTypeBaseType = new BinaryType(null, lengths, null);\r
+        }\r
+        return targetTypeBaseType;\r
+    }\r
+\r
+    private TypeDefinitionBuilder findTypeDefinitionBuilder(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            UnknownType unknownType, ModuleBuilder builder) {\r
+        Map<TypeDefinitionBuilder, TypeConstraints> result = findTypeDefinitionBuilderWithConstraints(\r
+                modules, unknownType, builder);\r
+        return result.entrySet().iterator().next().getKey();\r
+    }\r
+\r
+    private Map<TypeDefinitionBuilder, TypeConstraints> findTypeDefinitionBuilderWithConstraints(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            UnknownType unknownType, ModuleBuilder builder) {\r
+        return findTypeDefinitionBuilderWithConstraints(new TypeConstraints(),\r
+                modules, unknownType, builder);\r
+    }\r
+\r
+    /**\r
+     * Traverse through all referenced types chain until base YANG type is\r
+     * founded.\r
+     *\r
+     * @param constraints\r
+     *            current type constraints\r
+     * @param modules\r
+     *            all available modules\r
+     * @param unknownType\r
+     *            unknown type\r
+     * @param builder\r
+     *            current module\r
+     * @return map, where key is type referenced and value is its constraints\r
+     */\r
+    private Map<TypeDefinitionBuilder, TypeConstraints> findTypeDefinitionBuilderWithConstraints(\r
+            TypeConstraints constraints,\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            UnknownType unknownType, ModuleBuilder builder) {\r
+        Map<TypeDefinitionBuilder, TypeConstraints> result = new HashMap<TypeDefinitionBuilder, TypeConstraints>();\r
+        QName unknownTypeQName = unknownType.getQName();\r
+        String unknownTypeName = unknownTypeQName.getLocalName();\r
+        String unknownTypePrefix = unknownTypeQName.getPrefix();\r
+\r
+        // search for module which contains referenced typedef\r
+        ModuleBuilder dependentModuleBuilder;\r
+        if (unknownTypePrefix.equals(builder.getPrefix())) {\r
+            dependentModuleBuilder = builder;\r
+        } else {\r
+            dependentModuleBuilder = findDependentModule(modules, builder,\r
+                    unknownTypePrefix);\r
+        }\r
+\r
+        // pull all typedef statements from dependent module...\r
+        final Set<TypeDefinitionBuilder> typedefs = dependentModuleBuilder\r
+                .getModuleTypedefs();\r
+        // and search for referenced typedef\r
+        TypeDefinitionBuilder lookedUpBuilder = null;\r
+        for (TypeDefinitionBuilder tdb : typedefs) {\r
+            QName qname = tdb.getQName();\r
+            if (qname.getLocalName().equals(unknownTypeName)) {\r
+                lookedUpBuilder = tdb;\r
+                break;\r
+            }\r
+        }\r
+\r
+        // if referenced type is UnknownType again, search recursively with\r
+        // current constraints\r
+        TypeDefinition<?> referencedType = lookedUpBuilder.getBaseType();\r
+        if (referencedType instanceof UnknownType) {\r
+            UnknownType unknown = (UnknownType) lookedUpBuilder.getBaseType();\r
+\r
+            final List<RangeConstraint> ranges = unknown.getRangeStatements();\r
+            constraints.addRanges(ranges);\r
+            final List<LengthConstraint> lengths = unknown\r
+                    .getLengthStatements();\r
+            constraints.addLengths(lengths);\r
+            final List<PatternConstraint> patterns = unknown.getPatterns();\r
+            constraints.addPatterns(patterns);\r
+            return findTypeDefinitionBuilderWithConstraints(constraints,\r
+                    modules, unknown, dependentModuleBuilder);\r
+        } else {\r
+            // pull restriction from this base type and add them to\r
+            // 'constraints'\r
+            if (referencedType instanceof DecimalTypeDefinition) {\r
+                constraints.addRanges(((DecimalTypeDefinition) referencedType)\r
+                        .getRangeStatements());\r
+                constraints\r
+                        .setFractionDigits(((DecimalTypeDefinition) referencedType)\r
+                                .getFractionDigits());\r
+            } else if (referencedType instanceof IntegerTypeDefinition) {\r
+                constraints.addRanges(((IntegerTypeDefinition) referencedType)\r
+                        .getRangeStatements());\r
+            } else if (referencedType instanceof StringTypeDefinition) {\r
+                constraints.addPatterns(((StringTypeDefinition) referencedType)\r
+                        .getPatterns());\r
+            } else if (referencedType instanceof BinaryTypeDefinition) {\r
+                constraints.addLengths(((BinaryTypeDefinition) referencedType)\r
+                        .getLengthConstraints());\r
+            }\r
+            result.put(lookedUpBuilder, constraints);\r
+            return result;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Go through all augmentation definitions and resolve them. This means find\r
+     * referenced node and add child nodes to it.\r
+     *\r
+     * @param modules\r
+     *            all available modules\r
+     * @param module\r
+     *            current module\r
+     */\r
+    private void resolveAugments(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder module) {\r
+        Set<AugmentationSchemaBuilder> augmentBuilders = module\r
+                .getAddedAugments();\r
+\r
+        Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();\r
+        for (AugmentationSchemaBuilder augmentBuilder : augmentBuilders) {\r
+            SchemaPath augmentTargetSchemaPath = augmentBuilder.getTargetPath();\r
+            String prefix = null;\r
+            List<String> augmentTargetPath = new ArrayList<String>();\r
+\r
+            for (QName pathPart : augmentTargetSchemaPath.getPath()) {\r
+                prefix = pathPart.getPrefix();\r
+                augmentTargetPath.add(pathPart.getLocalName());\r
+            }\r
+            ModuleBuilder dependentModule = findDependentModule(modules,\r
+                    module, prefix);\r
+            //\r
+            augmentTargetPath.add(0, dependentModule.getName());\r
+            //\r
+\r
+\r
+            AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule\r
+                    .getNode(augmentTargetPath);\r
+            AugmentationSchema result = augmentBuilder.build();\r
+            augmentTarget.addAugmentation(result);\r
+            fillAugmentTarget(augmentBuilder, (ChildNodeBuilder) augmentTarget);\r
+            augments.add(result);\r
+        }\r
+        module.setAugmentations(augments);\r
+    }\r
+\r
+    /**\r
+     * Add all augment's child nodes to given target.\r
+     *\r
+     * @param augment\r
+     * @param target\r
+     */\r
+    private void fillAugmentTarget(AugmentationSchemaBuilder augment,\r
+            ChildNodeBuilder target) {\r
+        for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {\r
+            builder.setAugmenting(true);\r
+            target.addChildNode(builder);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Go through identity statements defined in current module and resolve\r
+     * their 'base' statement if present.\r
+     *\r
+     * @param modules\r
+     *            all modules\r
+     * @param module\r
+     *            module being resolved\r
+     */\r
+    private void resolveIdentities(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder module) {\r
+        Set<IdentitySchemaNodeBuilder> identities = module.getAddedIdentities();\r
+        for (IdentitySchemaNodeBuilder identity : identities) {\r
+            String baseIdentityName = identity.getBaseIdentityName();\r
+            if (baseIdentityName != null) {\r
+                String baseIdentityPrefix = null;\r
+                String baseIdentityLocalName = null;\r
+                if (baseIdentityName.contains(":")) {\r
+                    String[] splitted = baseIdentityName.split(":");\r
+                    baseIdentityPrefix = splitted[0];\r
+                    baseIdentityLocalName = splitted[1];\r
+                } else {\r
+                    baseIdentityPrefix = module.getPrefix();\r
+                    baseIdentityLocalName = baseIdentityName;\r
+                }\r
+                ModuleBuilder dependentModule;\r
+                if (baseIdentityPrefix.equals(module.getPrefix())) {\r
+                    dependentModule = module;\r
+                } else {\r
+                    dependentModule = findDependentModule(modules, module,\r
+                            baseIdentityPrefix);\r
+                }\r
+\r
+                Set<IdentitySchemaNodeBuilder> dependentModuleIdentities = dependentModule\r
+                        .getAddedIdentities();\r
+                for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) {\r
+                    if (idBuilder.getQName().getLocalName()\r
+                            .equals(baseIdentityLocalName)) {\r
+                        identity.setBaseIdentity(idBuilder);\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Find dependent module based on given prefix\r
+     *\r
+     * @param modules\r
+     *            all available modules\r
+     * @param module\r
+     *            current module\r
+     * @param prefix\r
+     *            target module prefix\r
+     * @return dependent module builder\r
+     */\r
+    private ModuleBuilder findDependentModule(\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder module, String prefix) {\r
+        ModuleImport dependentModuleImport = getModuleImport(module, prefix);\r
+        String dependentModuleName = dependentModuleImport.getModuleName();\r
+        Date dependentModuleRevision = dependentModuleImport.getRevision();\r
+\r
+        TreeMap<Date, ModuleBuilder> moduleBuildersByRevision = modules\r
+                .get(dependentModuleName);\r
+        ModuleBuilder dependentModule;\r
+        if (dependentModuleRevision == null) {\r
+            dependentModule = moduleBuildersByRevision.lastEntry().getValue();\r
+        } else {\r
+            dependentModule = moduleBuildersByRevision\r
+                    .get(dependentModuleRevision);\r
+        }\r
+        return dependentModule;\r
+    }\r
+\r
+    /**\r
+     * Get module import referenced by given prefix.\r
+     *\r
+     * @param builder\r
+     *            module to search\r
+     * @param prefix\r
+     *            prefix associated with import\r
+     * @return ModuleImport based on given prefix\r
+     */\r
+    private ModuleImport getModuleImport(ModuleBuilder builder, String prefix) {\r
+        ModuleImport moduleImport = null;\r
+        for (ModuleImport mi : builder.getModuleImports()) {\r
+            if (mi.getPrefix().equals(prefix)) {\r
+                moduleImport = mi;\r
+                break;\r
+            }\r
+        }\r
+        return moduleImport;\r
+    }\r
+\r
+    /**\r
+     * Helper method for resolving special 'min' or 'max' values in range\r
+     * constraint\r
+     *\r
+     * @param ranges\r
+     *            ranges to resolve\r
+     * @param targetType\r
+     *            target type\r
+     * @param modules\r
+     *            all available modules\r
+     * @param builder\r
+     *            current module\r
+     */\r
+    private void resolveRanges(List<RangeConstraint> ranges,\r
+            TypeDefinitionBuilder targetType,\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder) {\r
+        if (ranges != null && ranges.size() > 0) {\r
+            Long min = (Long) ranges.get(0).getMin();\r
+            Long max = (Long) ranges.get(ranges.size() - 1).getMax();\r
+            // if range contains one of the special values 'min' or 'max'\r
+            if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) {\r
+                Long[] values = parseRangeConstraint(targetType, modules,\r
+                        builder);\r
+                if (min.equals(Long.MIN_VALUE)) {\r
+                    min = values[0];\r
+                    RangeConstraint oldFirst = ranges.get(0);\r
+                    RangeConstraint newFirst = BaseConstraints.rangeConstraint(\r
+                            min, oldFirst.getMax(), oldFirst.getDescription(),\r
+                            oldFirst.getReference());\r
+                    ranges.set(0, newFirst);\r
+                }\r
+                if (max.equals(Long.MAX_VALUE)) {\r
+                    max = values[1];\r
+                    RangeConstraint oldLast = ranges.get(ranges.size() - 1);\r
+                    RangeConstraint newLast = BaseConstraints.rangeConstraint(\r
+                            oldLast.getMin(), max, oldLast.getDescription(),\r
+                            oldLast.getReference());\r
+                    ranges.set(ranges.size() - 1, newLast);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Helper method for resolving special 'min' or 'max' values in length\r
+     * constraint\r
+     *\r
+     * @param lengths\r
+     *            lengths to resolve\r
+     * @param targetType\r
+     *            target type\r
+     * @param modules\r
+     *            all available modules\r
+     * @param builder\r
+     *            current module\r
+     */\r
+    private void resolveLengths(List<LengthConstraint> lengths,\r
+            TypeDefinitionBuilder targetType,\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder) {\r
+        if (lengths != null && lengths.size() > 0) {\r
+            Long min = lengths.get(0).getMin().longValue();\r
+            Long max = lengths.get(lengths.size() - 1).getMax().longValue();\r
+            // if length contains one of the special values 'min' or 'max'\r
+            if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) {\r
+                Long[] values = parseRangeConstraint(targetType, modules,\r
+                        builder);\r
+                if (min.equals(Long.MIN_VALUE)) {\r
+                    min = values[0];\r
+                    LengthConstraint oldFirst = lengths.get(0);\r
+                    LengthConstraint newFirst = BaseConstraints\r
+                            .lengthConstraint(min, oldFirst.getMax(),\r
+                                    oldFirst.getDescription(),\r
+                                    oldFirst.getReference());\r
+                    lengths.set(0, newFirst);\r
+                }\r
+                if (max.equals(Long.MAX_VALUE)) {\r
+                    max = values[1];\r
+                    LengthConstraint oldLast = lengths.get(lengths.size() - 1);\r
+                    LengthConstraint newLast = BaseConstraints\r
+                            .lengthConstraint(oldLast.getMin(), max,\r
+                                    oldLast.getDescription(),\r
+                                    oldLast.getReference());\r
+                    lengths.set(lengths.size() - 1, newLast);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private Long[] parseRangeConstraint(TypeDefinitionBuilder targetType,\r
+            Map<String, TreeMap<Date, ModuleBuilder>> modules,\r
+            ModuleBuilder builder) {\r
+        TypeDefinition<?> targetBaseType = targetType.getBaseType();\r
+\r
+        if (targetBaseType instanceof IntegerTypeDefinition) {\r
+            IntegerTypeDefinition itd = (IntegerTypeDefinition) targetBaseType;\r
+            List<RangeConstraint> ranges = itd.getRangeStatements();\r
+            Long min = (Long) ranges.get(0).getMin();\r
+            Long max = (Long) ranges.get(ranges.size() - 1).getMax();\r
+            return new Long[] { min, max };\r
+        } else if (targetBaseType instanceof DecimalTypeDefinition) {\r
+            DecimalTypeDefinition dtd = (DecimalTypeDefinition) targetBaseType;\r
+            List<RangeConstraint> ranges = dtd.getRangeStatements();\r
+            Long min = (Long) ranges.get(0).getMin();\r
+            Long max = (Long) ranges.get(ranges.size() - 1).getMax();\r
+            return new Long[] { min, max };\r
+        } else {\r
+            return parseRangeConstraint(\r
+                    findTypeDefinitionBuilder(modules,\r
+                            (UnknownType) targetBaseType, builder), modules,\r
+                    builder);\r
+        }\r
+    }\r
+\r
+    private Date createEpochTime() {\r
+        Calendar c = Calendar.getInstance();\r
+        c.setTimeInMillis(0);\r
+        return c.getTime();\r
+    }\r
+\r
+    private static class SchemaContextImpl implements SchemaContext {\r
+        private final Set<Module> modules;\r
+\r
+        private SchemaContextImpl(Set<Module> modules) {\r
+            this.modules = modules;\r
+        }\r
+\r
+        @Override\r
+        public Set<DataSchemaNode> getDataDefinitions() {\r
+            final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();\r
+            for (Module m : modules) {\r
+                dataDefs.addAll(m.getChildNodes());\r
+            }\r
+            return dataDefs;\r
+        }\r
+\r
+        @Override\r
+        public Set<Module> getModules() {\r
+            return modules;\r
+        }\r
+\r
+        @Override\r
+        public Set<NotificationDefinition> getNotifications() {\r
+            final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();\r
+            for (Module m : modules) {\r
+                notifications.addAll(m.getNotifications());\r
+            }\r
+            return notifications;\r
+        }\r
+\r
+        @Override\r
+        public Set<RpcDefinition> getOperations() {\r
+            final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();\r
+            for (Module m : modules) {\r
+                rpcs.addAll(m.getRpcs());\r
+            }\r
+            return rpcs;\r
+        }\r
+\r
+        @Override\r
+        public Set<ExtensionDefinition> getExtensions() {\r
+            final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();\r
+            for (Module m : modules) {\r
+                extensions.addAll(m.getExtensionSchemaNodes());\r
+            }\r
+            return extensions;\r
+        }\r
+    }\r
+\r
+    private static class TypeConstraints {\r
+        private final List<RangeConstraint> ranges = new ArrayList<RangeConstraint>();\r
+        private final List<LengthConstraint> lengths = new ArrayList<LengthConstraint>();\r
+        private final List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();\r
+        private Integer fractionDigits;\r
+\r
+        public List<RangeConstraint> getRanges() {\r
+            return ranges;\r
+        }\r
+\r
+        public void addRanges(List<RangeConstraint> ranges) {\r
+            this.ranges.addAll(0, ranges);\r
+        }\r
+\r
+        public List<LengthConstraint> getLengths() {\r
+            return lengths;\r
+        }\r
+\r
+        public void addLengths(List<LengthConstraint> lengths) {\r
+            this.lengths.addAll(0, lengths);\r
+        }\r
+\r
+        public List<PatternConstraint> getPatterns() {\r
+            return patterns;\r
+        }\r
+\r
+        public void addPatterns(List<PatternConstraint> patterns) {\r
+            this.patterns.addAll(0, patterns);\r
+        }\r
+\r
+        public Integer getFractionDigits() {\r
+            return fractionDigits;\r
+        }\r
+\r
+        public void setFractionDigits(Integer fractionDigits) {\r
+            if (fractionDigits != null) {\r
+                this.fractionDigits = fractionDigits;\r
+            }\r
+        }\r
+    }\r
+\r
+}\r
index 6e62065..6ff69ee 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
@@ -21,7 +22,6 @@ import java.util.TreeMap;
 \r
 import org.antlr.v4.runtime.tree.ParseTree;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser;\r
-import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Contact_stmtContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Container_stmtContext;\r
@@ -47,6 +47,7 @@ import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtCont
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext;\r
+import org.opendaylight.controller.antlrv4.code.gen.YangParser.Union_specificationContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext;\r
 import org.opendaylight.controller.antlrv4.code.gen.YangParserBaseListener;\r
 import org.opendaylight.controller.yang.common.QName;\r
@@ -81,7 +82,7 @@ final class YangModelParserListenerImpl extends YangParserBaseListener {
     private String moduleName;\r
     private URI namespace;\r
     private String yangModelPrefix;\r
-    private Date revision;\r
+    private Date revision = new Date(0L);\r
 \r
     private final DateFormat simpleDateFormat = new SimpleDateFormat(\r
             "yyyy-mm-dd");\r
@@ -122,7 +123,8 @@ final class YangModelParserListenerImpl extends YangParserBaseListener {
     @Override\r
     public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {\r
         super.enterModule_header_stmts(ctx);\r
-\r
+        \r
+        String yangVersion = null;\r
         for (int i = 0; i < ctx.getChildCount(); ++i) {\r
             final ParseTree treeNode = ctx.getChild(i);\r
             if (treeNode instanceof Namespace_stmtContext) {\r
@@ -133,10 +135,14 @@ final class YangModelParserListenerImpl extends YangParserBaseListener {
                 yangModelPrefix = stringFromNode(treeNode);\r
                 moduleBuilder.setPrefix(yangModelPrefix);\r
             } else if (treeNode instanceof Yang_version_stmtContext) {\r
-                final String yangVersion = stringFromNode(treeNode);\r
-                moduleBuilder.setYangVersion(yangVersion);\r
+                yangVersion = stringFromNode(treeNode);\r
             }\r
         }\r
+        \r
+        if (yangVersion == null) {\r
+            yangVersion = "1";\r
+        }\r
+        moduleBuilder.setYangVersion(yangVersion);\r
     }\r
 \r
     @Override\r
@@ -168,36 +174,37 @@ final class YangModelParserListenerImpl extends YangParserBaseListener {
 \r
     @Override\r
     public void enterRevision_stmts(Revision_stmtsContext ctx) {\r
-        TreeMap<Date, Revision_stmtContext> revisions = new TreeMap<Date, Revision_stmtContext>();\r
-\r
-        for (int i = 0; i < ctx.getChildCount(); ++i) {\r
-            final ParseTree treeNode = ctx.getChild(i);\r
-            if (treeNode instanceof Revision_stmtContext) {\r
-                final String revisionDateStr = stringFromNode(treeNode);\r
-                try {\r
-                    Date revision = simpleDateFormat.parse(revisionDateStr);\r
-                    revisions.put(revision, (Revision_stmtContext)treeNode);\r
-\r
-                } catch (ParseException e) {\r
-                    final String message = "Failed to parse revision string: "+ revisionDateStr;\r
-                    logger.warn(message);\r
+        if (ctx != null) {\r
+            for (int i = 0; i < ctx.getChildCount(); ++i) {\r
+                final ParseTree treeNode = ctx.getChild(i);\r
+                if (treeNode instanceof Revision_stmtContext) {\r
+                    updateRevisionForRevisionStatement(treeNode);\r
                 }\r
             }\r
         }\r
-        if(revisions.size() > 0) {\r