Minor code refactoring.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TypeUtils.java
index 6d69bff53712210bf5249af1a31fc1d27e3453c6..d73807c9d577f513737150ce3573cf2b8a87ca44 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.*;
+import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromBuilders;
+import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromContext;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -19,7 +20,6 @@ import java.util.TreeMap;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
@@ -28,14 +28,6 @@ import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-import org.opendaylight.yangtools.yang.model.util.Int16;
-import org.opendaylight.yangtools.yang.model.util.Int32;
-import org.opendaylight.yangtools.yang.model.util.Int64;
-import org.opendaylight.yangtools.yang.model.util.Int8;
-import org.opendaylight.yangtools.yang.model.util.Uint16;
-import org.opendaylight.yangtools.yang.model.util.Uint32;
-import org.opendaylight.yangtools.yang.model.util.Uint64;
-import org.opendaylight.yangtools.yang.model.util.Uint8;
 import org.opendaylight.yangtools.yang.model.util.UnknownType;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
@@ -50,7 +42,7 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
 /**
  * Utility class which contains helper methods for dealing with type operations.
  */
-public class TypeUtils {
+public final class TypeUtils {
 
     private TypeUtils() {
     }
@@ -58,7 +50,7 @@ public class TypeUtils {
     /**
      * Resolve unknown type of node. It is assumed that type of node is either
      * UnknownType or ExtendedType with UnknownType as base type.
-     *
+     * 
      * @param nodeToResolve
      *            node with type to resolve
      * @param modules
@@ -68,37 +60,21 @@ public class TypeUtils {
      */
     public static void resolveType(final TypeAwareBuilder nodeToResolve,
             final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        TypeDefinitionBuilder resolvedType = null;
         final int line = nodeToResolve.getLine();
         final TypeDefinition<?> nodeToResolveType = nodeToResolve.getType();
         final QName unknownTypeQName = nodeToResolveType.getBaseType().getQName();
-        final ModuleBuilder dependentModule = findDependentModuleBuilder(modules, module, unknownTypeQName.getPrefix(),
-                line);
-
-        final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(nodeToResolve, dependentModule,
-                unknownTypeQName.getLocalName(), module.getName(), line);
-
-        if (nodeToResolveType instanceof ExtendedType) {
-            final ExtendedType extType = (ExtendedType) nodeToResolveType;
-            final TypeDefinitionBuilder newType = extendedTypeWithNewBaseTypeBuilder(targetTypeBuilder, extType,
-                    modules, module, nodeToResolve.getLine());
-            resolvedType = newType;
-        } else {
-            resolvedType = targetTypeBuilder;
-        }
-
-        // validate constraints
-        final TypeConstraints constraints = findConstraintsFromTypeBuilder(nodeToResolve,
-                new TypeConstraints(module.getName(), nodeToResolve.getLine()), modules, module, null);
-        constraints.validateConstraints();
+        final ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module,
+                unknownTypeQName.getPrefix(), line);
 
+        TypeDefinitionBuilder resolvedType = findUnknownTypeDefinition(nodeToResolve, dependentModuleBuilder, modules,
+                module);
         nodeToResolve.setTypedef(resolvedType);
     }
 
     /**
      * Resolve unknown type of node. It is assumed that type of node is either
      * UnknownType or ExtendedType with UnknownType as base type.
-     *
+     * 
      * @param nodeToResolve
      *            node with type to resolve
      * @param modules
@@ -111,11 +87,10 @@ public class TypeUtils {
     public static void resolveTypeWithContext(final TypeAwareBuilder nodeToResolve,
             final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module,
             final SchemaContext context) {
-        TypeDefinitionBuilder resolvedType = null;
         final int line = nodeToResolve.getLine();
         final TypeDefinition<?> nodeToResolveType = nodeToResolve.getType();
         final QName unknownTypeQName = nodeToResolveType.getBaseType().getQName();
-        final ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module,
+        final ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module,
                 unknownTypeQName.getPrefix(), line);
 
         if (dependentModuleBuilder == null) {
@@ -125,7 +100,7 @@ public class TypeUtils {
 
             if (nodeToResolveType instanceof ExtendedType) {
                 final ExtendedType extType = (ExtendedType) nodeToResolveType;
-                final TypeDefinitionBuilder newType = extendedTypeWithNewBaseType(type, extType, module,
+                final TypeDefinitionBuilder newType = extendedTypeWithNewBase(null, type, extType, modules, module,
                         nodeToResolve.getLine());
 
                 nodeToResolve.setTypedef(newType);
@@ -143,126 +118,153 @@ public class TypeUtils {
             }
 
         } else {
-            final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(nodeToResolve,
-                    dependentModuleBuilder, unknownTypeQName.getLocalName(), module.getName(), line);
-
-            if (nodeToResolveType instanceof ExtendedType) {
-                final ExtendedType extType = (ExtendedType) nodeToResolveType;
-                final TypeDefinitionBuilder newType = extendedTypeWithNewBaseTypeBuilder(targetTypeBuilder, extType,
-                        modules, module, nodeToResolve.getLine());
-                resolvedType = newType;
-            } else {
-                resolvedType = targetTypeBuilder;
-            }
-
-            // validate constraints
-            final TypeConstraints constraints = findConstraintsFromTypeBuilder(nodeToResolve, new TypeConstraints(
-                    module.getName(), nodeToResolve.getLine()), modules, module, context);
-            constraints.validateConstraints();
-
+            TypeDefinitionBuilder resolvedType = findUnknownTypeDefinition(nodeToResolve, dependentModuleBuilder,
+                    modules, module);
             nodeToResolve.setTypedef(resolvedType);
         }
     }
 
     public static void resolveTypeUnion(final UnionTypeBuilder union,
             final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder builder) {
-
         final List<TypeDefinition<?>> unionTypes = union.getTypes();
-        final List<TypeDefinition<?>> toRemove = new ArrayList<TypeDefinition<?>>();
+        final List<TypeDefinition<?>> toRemove = new ArrayList<>();
         for (TypeDefinition<?> unionType : unionTypes) {
             if (unionType instanceof UnknownType) {
-                final UnknownType ut = (UnknownType) unionType;
-                final ModuleBuilder dependentModule = findDependentModuleBuilder(modules, builder, ut.getQName()
+                final ModuleBuilder dependentModule = findModuleFromBuilders(modules, builder, unionType.getQName()
                         .getPrefix(), union.getLine());
-                final TypeDefinitionBuilder resolvedType = findTypeDefinitionBuilder(union, dependentModule, ut
+                final TypeDefinitionBuilder resolvedType = findTypeDefinitionBuilder(union, dependentModule, unionType
                         .getQName().getLocalName(), builder.getName(), union.getLine());
                 union.setTypedef(resolvedType);
-                toRemove.add(ut);
-            } else if (unionType instanceof ExtendedType) {
-                final ExtendedType extType = (ExtendedType) unionType;
-                final TypeDefinition<?> extTypeBase = extType.getBaseType();
-                if (extTypeBase instanceof UnknownType) {
-                    final UnknownType ut = (UnknownType) extTypeBase;
-                    final ModuleBuilder dependentModule = findDependentModuleBuilder(modules, builder, ut.getQName()
-                            .getPrefix(), union.getLine());
-                    final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(union, dependentModule,
-                            ut.getQName().getLocalName(), builder.getName(), union.getLine());
-
-                    final TypeDefinitionBuilder newType = extendedTypeWithNewBaseTypeBuilder(targetTypeBuilder,
-                            extType, modules, builder, union.getLine());
-
-                    union.setTypedef(newType);
-                    toRemove.add(extType);
-                }
+                toRemove.add(unionType);
+            } else if (unionType instanceof ExtendedType && unionType.getBaseType() instanceof UnknownType) {
+                final UnknownType ut = (UnknownType) unionType.getBaseType();
+                final ModuleBuilder dependentModule = findModuleFromBuilders(modules, builder, ut.getQName()
+                        .getPrefix(), union.getLine());
+                final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(union, dependentModule, ut
+                        .getQName().getLocalName(), builder.getName(), union.getLine());
+                final TypeDefinitionBuilder newType = extendedTypeWithNewBase(targetTypeBuilder, null,
+                        (ExtendedType) unionType, modules, builder, union.getLine());
+                union.setTypedef(newType);
+                toRemove.add(unionType);
             }
         }
         unionTypes.removeAll(toRemove);
     }
 
+    /**
+     * Resolve union type which contains one or more unresolved types.
+     * 
+     * @param union
+     *            union type builder to resolve
+     * @param modules
+     *            all loaded modules
+     * @param module
+     *            current module
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
     public static void resolveTypeUnionWithContext(final UnionTypeBuilder union,
-            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder builder,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module,
             final SchemaContext context) {
-
         final List<TypeDefinition<?>> unionTypes = union.getTypes();
-        final List<TypeDefinition<?>> toRemove = new ArrayList<TypeDefinition<?>>();
+        final List<TypeDefinition<?>> toRemove = new ArrayList<>();
         for (TypeDefinition<?> unionType : unionTypes) {
             if (unionType instanceof UnknownType) {
-                final UnknownType ut = (UnknownType) unionType;
-                final QName utQName = ut.getQName();
-                final ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, builder,
-                        utQName.getPrefix(), union.getLine());
-
-                if (dependentModuleBuilder == null) {
-                    Module dependentModule = findModuleFromContext(context, builder, utQName.getPrefix(),
-                            union.getLine());
-                    Set<TypeDefinition<?>> types = dependentModule.getTypeDefinitions();
-                    TypeDefinition<?> type = findTypeByName(types, utQName.getLocalName());
-                    union.setType(type);
-                    toRemove.add(ut);
-                } else {
-                    final TypeDefinitionBuilder resolvedType = findTypeDefinitionBuilder(union, dependentModuleBuilder,
-                            utQName.getLocalName(), builder.getName(), union.getLine());
-                    union.setTypedef(resolvedType);
-                    toRemove.add(ut);
-                }
-
-            } else if (unionType instanceof ExtendedType) {
-                final ExtendedType extType = (ExtendedType) unionType;
-                TypeDefinition<?> extTypeBase = extType.getBaseType();
-                if (extTypeBase instanceof UnknownType) {
-                    final UnknownType ut = (UnknownType) extTypeBase;
-                    final QName utQName = ut.getQName();
-                    final ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, builder,
-                            utQName.getPrefix(), union.getLine());
-
-                    if (dependentModuleBuilder == null) {
-                        final Module dependentModule = findModuleFromContext(context, builder, utQName.getPrefix(),
-                                union.getLine());
-                        Set<TypeDefinition<?>> types = dependentModule.getTypeDefinitions();
-                        TypeDefinition<?> type = findTypeByName(types, utQName.getLocalName());
-                        final TypeDefinitionBuilder newType = extendedTypeWithNewBaseType(type, extType, builder, 0);
-
-                        union.setTypedef(newType);
-                        toRemove.add(extType);
-                    } else {
-                        final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(union,
-                                dependentModuleBuilder, utQName.getLocalName(), builder.getName(), union.getLine());
-
-                        final TypeDefinitionBuilder newType = extendedTypeWithNewBaseTypeBuilder(targetTypeBuilder,
-                                extType, modules, builder, union.getLine());
-
-                        union.setTypedef(newType);
-                        toRemove.add(extType);
-                    }
-                }
+                resolveUnionUnknownType(union, (UnknownType) unionType, modules, module, context);
+                toRemove.add(unionType);
+            } else if (unionType instanceof ExtendedType && unionType.getBaseType() instanceof UnknownType) {
+                resolveUnionUnknownType(union, (ExtendedType) unionType, modules, module, context);
+                toRemove.add(unionType);
             }
         }
         unionTypes.removeAll(toRemove);
     }
 
+    private static void resolveUnionUnknownType(final UnionTypeBuilder union, final UnknownType ut,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module,
+            final SchemaContext context) {
+        final int line = union.getLine();
+        final QName utQName = ut.getQName();
+        final ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module, utQName.getPrefix(), line);
+
+        if (dependentModuleBuilder == null) {
+            Module dependentModule = findModuleFromContext(context, module, utQName.getPrefix(), line);
+            Set<TypeDefinition<?>> types = dependentModule.getTypeDefinitions();
+            TypeDefinition<?> type = findTypeByName(types, utQName.getLocalName());
+            union.setType(type);
+        } else {
+            final TypeDefinitionBuilder resolvedType = findTypeDefinitionBuilder(union, dependentModuleBuilder,
+                    utQName.getLocalName(), module.getName(), union.getLine());
+            union.setTypedef(resolvedType);
+        }
+    }
+
+    private static void resolveUnionUnknownType(final UnionTypeBuilder union, final ExtendedType extType,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module,
+            final SchemaContext context) {
+        final int line = union.getLine();
+        final TypeDefinition<?> extTypeBase = extType.getBaseType();
+        final UnknownType ut = (UnknownType) extTypeBase;
+        final QName utQName = ut.getQName();
+        final ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module, utQName.getPrefix(), line);
+
+        if (dependentModuleBuilder == null) {
+            final Module dependentModule = findModuleFromContext(context, module, utQName.getPrefix(), line);
+            Set<TypeDefinition<?>> types = dependentModule.getTypeDefinitions();
+            TypeDefinition<?> type = findTypeByName(types, utQName.getLocalName());
+            final TypeDefinitionBuilder newType = extendedTypeWithNewBase(null, type, extType, modules, module, 0);
+            union.setTypedef(newType);
+        } else {
+            final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(union, dependentModuleBuilder,
+                    utQName.getLocalName(), module.getName(), line);
+            final TypeDefinitionBuilder newType = extendedTypeWithNewBase(targetTypeBuilder, null, extType, modules,
+                    module, line);
+            union.setTypedef(newType);
+        }
+    }
+
+    /**
+     * Find type definition of type of unresolved node.
+     * 
+     * @param nodeToResolve
+     *            node with unresolved type
+     * @param dependentModuleBuilder
+     *            module in which type definition is present
+     * @param modules
+     *            all loaded modules
+     * @param module
+     *            current module
+     * @return TypeDefinitionBuilder of node type
+     */
+    private static TypeDefinitionBuilder findUnknownTypeDefinition(final TypeAwareBuilder nodeToResolve,
+            final ModuleBuilder dependentModuleBuilder, final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final ModuleBuilder module) {
+        final int line = nodeToResolve.getLine();
+        final TypeDefinition<?> nodeToResolveType = nodeToResolve.getType();
+        final QName unknownTypeQName = nodeToResolveType.getBaseType().getQName();
+        final TypeDefinitionBuilder targetTypeBuilder = findTypeDefinitionBuilder(nodeToResolve,
+                dependentModuleBuilder, unknownTypeQName.getLocalName(), module.getName(), line);
+
+        TypeDefinitionBuilder resolvedType;
+        if (nodeToResolveType instanceof ExtendedType) {
+            final ExtendedType extType = (ExtendedType) nodeToResolveType;
+            resolvedType = extendedTypeWithNewBase(targetTypeBuilder, null, extType, modules, module,
+                    nodeToResolve.getLine());
+        } else {
+            resolvedType = targetTypeBuilder;
+        }
+
+        // validate constraints
+        final TypeConstraints constraints = findConstraintsFromTypeBuilder(nodeToResolve,
+                new TypeConstraints(module.getName(), nodeToResolve.getLine()), modules, module, null);
+        constraints.validateConstraints();
+
+        return resolvedType;
+    }
+
     /**
      * Search types for type with given name.
-     *
+     * 
      * @param types
      *            types to search
      * @param name
@@ -280,7 +282,7 @@ public class TypeUtils {
 
     /**
      * Find type by name.
-     *
+     * 
      * @param types
      *            collection of types
      * @param typeName
@@ -299,85 +301,42 @@ public class TypeUtils {
 
     /**
      * Pull restriction from type and add them to constraints.
-     *
+     * 
      * @param type
+     *            type from which constraints will be read
      * @param constraints
+     *            constraints object to which constraints will be added
      */
-    private static void mergeConstraints(final TypeDefinition<?> type, final TypeConstraints constraints) {
+    private static TypeConstraints mergeConstraints(final TypeDefinition<?> type, final TypeConstraints constraints) {
         if (type instanceof DecimalTypeDefinition) {
-            constraints.addRanges(((DecimalTypeDefinition) type).getRangeStatements());
+            constraints.addRanges(((DecimalTypeDefinition) type).getRangeConstraints());
             constraints.addFractionDigits(((DecimalTypeDefinition) type).getFractionDigits());
         } else if (type instanceof IntegerTypeDefinition) {
-            constraints.addRanges(((IntegerTypeDefinition) type).getRangeStatements());
+            constraints.addRanges(((IntegerTypeDefinition) type).getRangeConstraints());
+        } else if (type instanceof UnsignedIntegerTypeDefinition) {
+            constraints.addRanges(((UnsignedIntegerTypeDefinition) type).getRangeConstraints());
         } else if (type instanceof StringTypeDefinition) {
-            constraints.addPatterns(((StringTypeDefinition) type).getPatterns());
-            constraints.addLengths(((StringTypeDefinition) type).getLengthStatements());
+            constraints.addPatterns(((StringTypeDefinition) type).getPatternConstraints());
+            constraints.addLengths(((StringTypeDefinition) type).getLengthConstraints());
         } else if (type instanceof BinaryTypeDefinition) {
             constraints.addLengths(((BinaryTypeDefinition) type).getLengthConstraints());
+        } else if (type instanceof ExtendedType) {
+            constraints.addFractionDigits(((ExtendedType) type).getFractionDigits());
+            constraints.addLengths(((ExtendedType) type).getLengthConstraints());
+            constraints.addPatterns(((ExtendedType) type).getPatternConstraints());
+            constraints.addRanges(((ExtendedType) type).getRangeConstraints());
         }
+        return constraints;
     }
 
     /**
-     * Create new ExtendedType based on given type and with schema path.
-     *
-     * @param newPath
-     *            schema path for new type
-     * @param oldType
-     *            type based
-     * @return
-     */
-    static ExtendedType createNewExtendedType(final ExtendedType oldType, final SchemaPath newPath) {
-        QName qname = oldType.getQName();
-        TypeDefinition<?> newBaseType = ParserUtils.createCorrectTypeDefinition(newPath, oldType.getBaseType());
-        String desc = oldType.getDescription();
-        String ref = oldType.getReference();
-        ExtendedType.Builder builder = new ExtendedType.Builder(qname, newBaseType, desc, ref, newPath);
-        builder.status(oldType.getStatus());
-        builder.lengths(oldType.getLengths());
-        builder.patterns(oldType.getPatterns());
-        builder.ranges(oldType.getRanges());
-        builder.fractionDigits(oldType.getFractionDigits());
-        builder.unknownSchemaNodes(oldType.getUnknownSchemaNodes());
-        return builder.build();
-    }
-
-    static IntegerTypeDefinition createNewIntType(final SchemaPath newSchemaPath,
-            final IntegerTypeDefinition type) {
-        final String localName = type.getQName().getLocalName();
-        if ("int8".equals(localName)) {
-            return new Int8(newSchemaPath);
-        } else if ("int16".equals(localName)) {
-            return new Int16(newSchemaPath);
-        } else if ("int32".equals(localName)) {
-            return new Int32(newSchemaPath);
-        } else if ("int64".equals(localName)) {
-            return new Int64(newSchemaPath);
-        } else {
-            return null;
-        }
-    }
-
-    static UnsignedIntegerTypeDefinition createNewUintType(final SchemaPath newSchemaPath,
-            final UnsignedIntegerTypeDefinition type) {
-        final String localName = type.getQName().getLocalName();
-        if ("uint8".equals(localName)) {
-            return new Uint8(newSchemaPath);
-        } else if ("uint16".equals(localName)) {
-            return new Uint16(newSchemaPath);
-        } else if ("uint32".equals(localName)) {
-            return new Uint32(newSchemaPath);
-        } else if ("uint64".equals(localName)) {
-            return new Uint64(newSchemaPath);
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Create new type builder based on old type with new base type.
-     *
+     * Create new type builder based on old type with new base type. Note: only
+     * one of newBaseTypeBuilder or newBaseType can be specified.
+     * 
+     * @param newBaseTypeBuilder
+     *            new base type builder or null
      * @param newBaseType
-     *            new base type builder
+     *            new base type or null
      * @param oldExtendedType
      *            old type
      * @param modules
@@ -388,56 +347,30 @@ public class TypeUtils {
      *            current line in module
      * @return new type builder based on old type with new base type
      */
-    private static TypeDefinitionBuilder extendedTypeWithNewBaseTypeBuilder(final TypeDefinitionBuilder newBaseType,
-            final ExtendedType oldExtendedType, final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, final int line) {
-        final TypeConstraints tc = new TypeConstraints(module.getName(), line);
-        tc.addFractionDigits(oldExtendedType.getFractionDigits());
-        tc.addLengths(oldExtendedType.getLengths());
-        tc.addPatterns(oldExtendedType.getPatterns());
-        tc.addRanges(oldExtendedType.getRanges());
+    private static TypeDefinitionBuilder extendedTypeWithNewBase(final TypeDefinitionBuilder newBaseTypeBuilder,
+            final TypeDefinition<?> newBaseType, final ExtendedType oldExtendedType,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module, final int line) {
+        if ((newBaseTypeBuilder == null && newBaseType == null) || (newBaseTypeBuilder != null && newBaseType != null)) {
+            throw new YangParseException(module.getName(), line,
+                    "only one of newBaseTypeBuilder or newBaseType can be specified");
+        }
 
-        final TypeConstraints constraints = findConstraintsFromTypeBuilder(newBaseType, tc, modules, module, null);
         final TypeDefinitionBuilderImpl newType = new TypeDefinitionBuilderImpl(module.getModuleName(), line,
                 oldExtendedType.getQName());
-        newType.setTypedef(newBaseType);
-        newType.setPath(oldExtendedType.getPath());
-        newType.setDescription(oldExtendedType.getDescription());
-        newType.setReference(oldExtendedType.getReference());
-        newType.setStatus(oldExtendedType.getStatus());
-        newType.setLengths(constraints.getLength());
-        newType.setPatterns(constraints.getPatterns());
-        newType.setRanges(constraints.getRange());
-        newType.setFractionDigits(constraints.getFractionDigits());
-        newType.setUnits(oldExtendedType.getUnits());
-        newType.setDefaultValue(oldExtendedType.getDefaultValue());
-        newType.setUnknownNodes(oldExtendedType.getUnknownSchemaNodes());
-        return newType;
-    }
-
-    /**
-     * Create new type builder based on old type with new base type.
-     *
-     * @param newBaseType
-     *            new base type
-     * @param oldExtendedType
-     *            old type
-     * @param modules
-     *            all loaded modules
-     * @param module
-     *            current module
-     * @param line
-     *            current line in module
-     * @return new type builder based on old type with new base type
-     */
-    private static TypeDefinitionBuilder extendedTypeWithNewBaseType(final TypeDefinition<?> newBaseType,
-            final ExtendedType oldExtendedType, final ModuleBuilder module, final int line) {
         final TypeConstraints tc = new TypeConstraints(module.getName(), line);
+        TypeConstraints constraints;
+        if (newBaseType == null) {
+            tc.addFractionDigits(oldExtendedType.getFractionDigits());
+            tc.addLengths(oldExtendedType.getLengthConstraints());
+            tc.addPatterns(oldExtendedType.getPatternConstraints());
+            tc.addRanges(oldExtendedType.getRangeConstraints());
+            constraints = findConstraintsFromTypeBuilder(newBaseTypeBuilder, tc, modules, module, null);
+            newType.setTypedef(newBaseTypeBuilder);
+        } else {
+            constraints = findConstraintsFromTypeDefinition(newBaseType, tc);
+            newType.setType(newBaseType);
+        }
 
-        final TypeConstraints constraints = findConstraintsFromTypeDefinition(newBaseType, tc);
-        final TypeDefinitionBuilderImpl newType = new TypeDefinitionBuilderImpl(module.getModuleName(), line,
-                oldExtendedType.getQName());
-        newType.setType(newBaseType);
         newType.setPath(oldExtendedType.getPath());
         newType.setDescription(oldExtendedType.getDescription());
         newType.setReference(oldExtendedType.getReference());
@@ -454,7 +387,7 @@ public class TypeUtils {
 
     /**
      * Pull restrictions from type and add them to constraints.
-     *
+     * 
      * @param typeToResolve
      *            type from which constraints will be read
      * @param constraints
@@ -471,9 +404,9 @@ public class TypeUtils {
         if (typeToResolve instanceof ExtendedType) {
             ExtendedType extType = (ExtendedType) typeToResolve;
             constraints.addFractionDigits(extType.getFractionDigits());
-            constraints.addLengths(extType.getLengths());
-            constraints.addPatterns(extType.getPatterns());
-            constraints.addRanges(extType.getRanges());
+            constraints.addLengths(extType.getLengthConstraints());
+            constraints.addPatterns(extType.getPatternConstraints());
+            constraints.addRanges(extType.getRangeConstraints());
             return findConstraintsFromTypeDefinition(extType.getBaseType(), constraints);
         } else {
             mergeConstraints(typeToResolve, constraints);
@@ -504,7 +437,7 @@ public class TypeUtils {
         } else {
             QName qname = type.getQName();
             if (type instanceof UnknownType) {
-                ModuleBuilder dependentModuleBuilder = ParserUtils.findDependentModuleBuilder(modules, builder,
+                ModuleBuilder dependentModuleBuilder = ParserUtils.findModuleFromBuilders(modules, builder,
                         qname.getPrefix(), nodeToResolve.getLine());
                 if (dependentModuleBuilder == null) {
                     if (context == null) {
@@ -514,54 +447,39 @@ public class TypeUtils {
                     Module dm = ParserUtils.findModuleFromContext(context, builder, qname.getPrefix(),
                             nodeToResolve.getLine());
                     TypeDefinition<?> t = findTypeByName(dm.getTypeDefinitions(), qname.getLocalName());
-                    if (t instanceof ExtendedType) {
-                        ExtendedType extType = (ExtendedType) t;
-                        constraints.addFractionDigits(extType.getFractionDigits());
-                        constraints.addLengths(extType.getLengths());
-                        constraints.addPatterns(extType.getPatterns());
-                        constraints.addRanges(extType.getRanges());
-                        return constraints;
-                    } else {
-                        mergeConstraints(t, constraints);
-                        return constraints;
-                    }
+                    return mergeConstraints(t, constraints);
+
                 } else {
                     TypeDefinitionBuilder tdb = findTypeDefinitionBuilder(nodeToResolve, dependentModuleBuilder,
                             qname.getLocalName(), builder.getName(), nodeToResolve.getLine());
                     return findConstraintsFromTypeBuilder(tdb, constraints, modules, dependentModuleBuilder, context);
                 }
             } else if (type instanceof ExtendedType) {
-                ExtendedType extType = (ExtendedType) type;
-                constraints.addFractionDigits(extType.getFractionDigits());
-                constraints.addLengths(extType.getLengths());
-                constraints.addPatterns(extType.getPatterns());
-                constraints.addRanges(extType.getRanges());
+                mergeConstraints(type, constraints);
 
-                TypeDefinition<?> base = extType.getBaseType();
+                TypeDefinition<?> base = ((ExtendedType) type).getBaseType();
                 if (base instanceof UnknownType) {
-                    ModuleBuilder dependentModule = ParserUtils.findDependentModuleBuilder(modules, builder, base
+                    ModuleBuilder dependentModule = ParserUtils.findModuleFromBuilders(modules, builder, base
                             .getQName().getPrefix(), nodeToResolve.getLine());
                     TypeDefinitionBuilder tdb = findTypeDefinitionBuilder(nodeToResolve, dependentModule, base
                             .getQName().getLocalName(), builder.getName(), nodeToResolve.getLine());
                     return findConstraintsFromTypeBuilder(tdb, constraints, modules, dependentModule, context);
                 } else {
                     // it has to be base yang type
-                    mergeConstraints(type, constraints);
-                    return constraints;
+                    return mergeConstraints(type, constraints);
                 }
             } else {
                 // it is base yang type
-                mergeConstraints(type, constraints);
-                return constraints;
+                return mergeConstraints(type, constraints);
             }
         }
     }
 
     /**
      * Search for type definition builder by name.
-     *
-     * @param dirtyNodeSchemaPath
-     *            schema path of node which contains unresolved type
+     * 
+     * @param nodeToResolve
+     *            node which contains unresolved type
      * @param dependentModule
      *            module which should contains referenced type
      * @param typeName
@@ -570,15 +488,12 @@ public class TypeUtils {
      *            name of current module
      * @param line
      *            current line in module
-     * @return
+     * @return typeDefinitionBuilder
      */
     private static TypeDefinitionBuilder findTypeDefinitionBuilder(final TypeAwareBuilder nodeToResolve,
             final ModuleBuilder dependentModule, final String typeName, final String currentModuleName, final int line) {
-
-        TypeDefinitionBuilder result = null;
-
         Set<TypeDefinitionBuilder> typedefs = dependentModule.getTypeDefinitionBuilders();
-        result = findTypedefBuilderByName(typedefs, typeName);
+        TypeDefinitionBuilder result = findTypedefBuilderByName(typedefs, typeName);
         if (result != null) {
             return result;
         }