Merge "Add IfNewHostNotify to DeviceManager"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / util / ParserUtils.java
index 2e3f0cc860ba80d22cef4d01b33b47d531626e10..d38bc5e7adf22f74f6678e9614d38e2872a8099d 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.controller.yang.parser.util;
 
 import java.lang.reflect.Method;
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import org.opendaylight.controller.yang.common.QName;
 import org.opendaylight.controller.yang.model.api.ModuleImport;
@@ -40,6 +40,7 @@ import org.opendaylight.controller.yang.model.util.BooleanType;
 import org.opendaylight.controller.yang.model.util.Decimal64;
 import org.opendaylight.controller.yang.model.util.EmptyType;
 import org.opendaylight.controller.yang.model.util.EnumerationType;
+import org.opendaylight.controller.yang.model.util.ExtendedType;
 import org.opendaylight.controller.yang.model.util.IdentityrefType;
 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
 import org.opendaylight.controller.yang.model.util.Int16;
@@ -79,7 +80,7 @@ public final class ParserUtils {
 
     /**
      * Get module import referenced by given prefix.
-     *
+     * 
      * @param builder
      *            module to search
      * @param prefix
@@ -100,7 +101,7 @@ public final class ParserUtils {
 
     /**
      * Parse uses path.
-     *
+     * 
      * @param usesPath
      *            as String
      * @return SchemaPath from given String
@@ -127,7 +128,7 @@ public final class ParserUtils {
 
     /**
      * Add all augment's child nodes to given target.
-     *
+     * 
      * @param augment
      * @param target
      */
@@ -136,54 +137,139 @@ public final class ParserUtils {
             final DataNodeContainerBuilder target) {
         for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {
             builder.setAugmenting(true);
-            correctAugmentChildPath(augment, target.getPath());
+            correctAugmentChildPath(builder, target.getPath());
             target.addChildNode(builder);
         }
     }
 
     public static void fillAugmentTarget(
-            final AugmentationSchemaBuilder augment,
-            final ChoiceBuilder target) {
+            final AugmentationSchemaBuilder augment, final ChoiceBuilder target) {
         for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {
             builder.setAugmenting(true);
-            correctAugmentChildPath(augment, target.getPath());
+            correctAugmentChildPath(builder, target.getPath());
             target.addChildNode(builder);
         }
     }
 
-    private static void correctAugmentChildPath(final DataNodeContainerBuilder node,
+    private static void correctAugmentChildPath(
+            final DataSchemaNodeBuilder childNode,
             final SchemaPath parentSchemaPath) {
-        for (DataSchemaNodeBuilder builder : node.getChildNodes()) {
-
-            // add correct path
-            List<QName> targetNodePath = new ArrayList<QName>(
-                    parentSchemaPath.getPath());
-            targetNodePath.add(builder.getQName());
-            builder.setPath(new SchemaPath(targetNodePath, true));
 
-            if (builder instanceof DataNodeContainerBuilder) {
-                DataNodeContainerBuilder cnb = (DataNodeContainerBuilder) builder;
-                correctAugmentChildPath(cnb, builder.getPath());
+        // set correct path
+        List<QName> targetNodePath = new ArrayList<QName>(
+                parentSchemaPath.getPath());
+        targetNodePath.add(childNode.getQName());
+        childNode.setPath(new SchemaPath(targetNodePath, true));
+
+        // set correct path for all child nodes
+        if (childNode instanceof DataNodeContainerBuilder) {
+            DataNodeContainerBuilder dataNodeContainer = (DataNodeContainerBuilder) childNode;
+            for (DataSchemaNodeBuilder child : dataNodeContainer
+                    .getChildNodes()) {
+                correctAugmentChildPath(child, childNode.getPath());
             }
+        }
 
-            // if child can contains type, correct path for this type too
-            if (builder instanceof TypeAwareBuilder) {
-                TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) builder;
-                QName nodeBuilderQName = nodeBuilder.getQName();
-                TypeDefinition<?> nodeBuilderType = nodeBuilder.getType();
-                if (nodeBuilderType != null) {
-                    TypeDefinition<?> newType = createCorrectTypeDefinition(
-                            parentSchemaPath, nodeBuilderQName, nodeBuilderType);
-                    nodeBuilder.setType(newType);
-                } else {
-                    TypeDefinitionBuilder nodeBuilderTypedef = nodeBuilder
-                            .getTypedef();
-                    SchemaPath newSchemaPath = createNewSchemaPath(
-                            nodeBuilderTypedef.getPath(), nodeBuilderQName,
-                            nodeBuilderTypedef.getQName());
-                    nodeBuilderTypedef.setPath(newSchemaPath);
+        // if node can contains type, correct path for this type too
+        if (childNode instanceof TypeAwareBuilder) {
+            TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) childNode;
+            correctTypeAwareNodePath(nodeBuilder, parentSchemaPath);
+        }
+    }
+
+    /**
+     * Repair schema path of node type.
+     * 
+     * @param node
+     *            node which contains type statement
+     * @param parentSchemaPath
+     *            schema path of parent node
+     */
+    private static void correctTypeAwareNodePath(
+            TypeAwareBuilder node, SchemaPath parentSchemaPath) {
+        final QName nodeBuilderQName = node.getQName();
+        final TypeDefinition<?> nodeType = node.getType();
+
+        Integer fd = null;
+        List<LengthConstraint> lengths = null;
+        List<PatternConstraint> patterns = null;
+        List<RangeConstraint> ranges = null;
+
+        if (nodeType != null) {
+            if (nodeType instanceof ExtendedType) {
+                ExtendedType et = (ExtendedType) nodeType;
+                if (nodeType
+                        .getQName()
+                        .getLocalName()
+                        .equals(nodeType.getBaseType().getQName()
+                                .getLocalName())) {
+                    fd = et.getFractionDigits();
+                    lengths = et.getLengths();
+                    patterns = et.getPatterns();
+                    ranges = et.getRanges();
+                    if (!hasConstraints(fd, lengths, patterns, ranges)) {
+                        return;
+                    }
                 }
             }
+            TypeDefinition<?> newType = createCorrectTypeDefinition(
+                    parentSchemaPath, nodeBuilderQName, nodeType);
+            node.setType(newType);
+        } else {
+            TypeDefinitionBuilder nodeBuilderTypedef = node.getTypedef();
+
+            fd = nodeBuilderTypedef.getFractionDigits();
+            lengths = nodeBuilderTypedef.getLengths();
+            patterns = nodeBuilderTypedef.getPatterns();
+            ranges = nodeBuilderTypedef.getRanges();
+
+            String tdbTypeName = nodeBuilderTypedef.getQName().getLocalName();
+            String baseTypeName = null;
+            if (nodeBuilderTypedef.getType() == null) {
+                baseTypeName = nodeBuilderTypedef.getTypedef().getQName()
+                        .getLocalName();
+            } else {
+                baseTypeName = nodeBuilderTypedef.getType().getQName()
+                        .getLocalName();
+            }
+            if (!(tdbTypeName.equals(baseTypeName))) {
+                return;
+            }
+
+            if (!hasConstraints(fd, lengths, patterns, ranges)) {
+                return;
+            }
+
+            SchemaPath newSchemaPath = createNewSchemaPath(
+                    nodeBuilderTypedef.getPath(), nodeBuilderQName,
+                    nodeBuilderTypedef.getQName());
+            nodeBuilderTypedef.setPath(newSchemaPath);
+        }
+    }
+
+    /**
+     * Check if there are some constraints.
+     * 
+     * @param fd
+     *            fraction digits
+     * @param lengths
+     *            length constraints
+     * @param patterns
+     *            pattern constraints
+     * @param ranges
+     *            range constraints
+     * @return true, if any of constraints are present, false otherwise
+     */
+    private static boolean hasConstraints(final Integer fd,
+            final List<LengthConstraint> lengths,
+            final List<PatternConstraint> patterns,
+            final List<RangeConstraint> ranges) {
+        if (fd == null && (lengths == null || lengths.isEmpty())
+                && (patterns == null || patterns.isEmpty())
+                && (ranges == null || ranges.isEmpty())) {
+            return false;
+        } else {
+            return true;
         }
     }
 
@@ -198,29 +284,22 @@ public final class ParserUtils {
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
                         nodeQName, binType.getQName());
                 List<Byte> bytes = (List<Byte>) binType.getDefaultValue();
-                result = new BinaryType(newSchemaPath, bytes,
-                        binType.getLengthConstraints(), binType.getUnits());
+                result = new BinaryType(newSchemaPath, bytes);
             } else if (nodeType instanceof BitsTypeDefinition) {
                 BitsTypeDefinition bitsType = (BitsTypeDefinition) nodeType;
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
                         nodeQName, nodeType.getQName());
-                result = new BitsType(newSchemaPath, bitsType.getBits(),
-                        bitsType.getUnits());
+                result = new BitsType(newSchemaPath, bitsType.getBits());
             } else if (nodeType instanceof BooleanTypeDefinition) {
                 BooleanTypeDefinition booleanType = (BooleanTypeDefinition) nodeType;
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
                         nodeQName, booleanType.getQName());
-                result = new BooleanType(newSchemaPath,
-                        (Boolean) booleanType.getDefaultValue(),
-                        booleanType.getUnits());
+                result = new BooleanType(newSchemaPath);
             } else if (nodeType instanceof DecimalTypeDefinition) {
                 DecimalTypeDefinition decimalType = (DecimalTypeDefinition) nodeType;
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
                         nodeQName, decimalType.getQName());
-                BigDecimal defaultValue = (BigDecimal) decimalType
-                        .getDefaultValue();
-                result = new Decimal64(newSchemaPath, decimalType.getUnits(),
-                        defaultValue, decimalType.getRangeStatements(),
+                result = new Decimal64(newSchemaPath,
                         decimalType.getFractionDigits());
             } else if (nodeType instanceof EmptyTypeDefinition) {
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
@@ -232,7 +311,7 @@ public final class ParserUtils {
                         nodeQName, enumType.getQName());
                 result = new EnumerationType(newSchemaPath,
                         (EnumPair) enumType.getDefaultValue(),
-                        enumType.getValues(), enumType.getUnits());
+                        enumType.getValues());
             } else if (nodeType instanceof IdentityrefTypeDefinition) {
                 IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition) nodeType;
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
@@ -247,13 +326,13 @@ public final class ParserUtils {
                         instIdType.getPathStatement(),
                         instIdType.requireInstance());
             } else if (nodeType instanceof StringTypeDefinition) {
-                result = copyStringType(parentSchemaPath, nodeQName,
+                result = createNewStringType(parentSchemaPath, nodeQName,
                         (StringTypeDefinition) nodeType);
             } else if (nodeType instanceof IntegerTypeDefinition) {
-                result = copyIntType(parentSchemaPath, nodeQName,
+                result = createNewIntType(parentSchemaPath, nodeQName,
                         (IntegerTypeDefinition) nodeType);
             } else if (nodeType instanceof UnsignedIntegerTypeDefinition) {
-                result = copyUIntType(parentSchemaPath, nodeQName,
+                result = createNewUintType(parentSchemaPath, nodeQName,
                         (UnsignedIntegerTypeDefinition) nodeType);
             } else if (nodeType instanceof LeafrefTypeDefinition) {
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
@@ -265,12 +344,34 @@ public final class ParserUtils {
                 newSchemaPath = createNewSchemaPath(parentSchemaPath,
                         nodeQName, unionType.getQName());
                 return new UnionType(newSchemaPath, unionType.getTypes());
+            } else if (nodeType instanceof ExtendedType) {
+                ExtendedType extType = (ExtendedType) nodeType;
+                newSchemaPath = createNewSchemaPath(parentSchemaPath,
+                        nodeQName, extType.getQName());
+                result = createNewExtendedType(newSchemaPath, extType);
             }
         }
         return result;
     }
 
-    private static TypeDefinition<?> copyStringType(SchemaPath schemaPath,
+    private static TypeDefinition<?> createNewExtendedType(
+            SchemaPath newSchemaPath, ExtendedType oldExtendedType) {
+        QName qname = oldExtendedType.getQName();
+        TypeDefinition<?> baseType = oldExtendedType.getBaseType();
+        String desc = oldExtendedType.getDescription();
+        String ref = oldExtendedType.getReference();
+        ExtendedType.Builder builder = new ExtendedType.Builder(qname,
+                baseType, desc, ref, newSchemaPath);
+        builder.status(oldExtendedType.getStatus());
+        builder.lengths(oldExtendedType.getLengths());
+        builder.patterns(oldExtendedType.getPatterns());
+        builder.ranges(oldExtendedType.getRanges());
+        builder.fractionDigits(oldExtendedType.getFractionDigits());
+        builder.unknownSchemaNodes(oldExtendedType.getUnknownSchemaNodes());
+        return builder.build();
+    }
+
+    private static TypeDefinition<?> createNewStringType(SchemaPath schemaPath,
             QName nodeQName, StringTypeDefinition nodeType) {
         List<QName> path = schemaPath.getPath();
         List<QName> newPath = new ArrayList<QName>(path);
@@ -279,64 +380,44 @@ public final class ParserUtils {
         SchemaPath newSchemaPath = new SchemaPath(newPath,
                 schemaPath.isAbsolute());
 
-        String newDefault = nodeType.getDefaultValue().toString();
-        String newUnits = nodeType.getUnits();
-        List<LengthConstraint> lengths = nodeType.getLengthStatements();
-        List<PatternConstraint> patterns = nodeType.getPatterns();
-
-        return new StringType(newSchemaPath, newDefault, lengths, patterns,
-                newUnits);
+        return new StringType(newSchemaPath);
     }
 
-    private static TypeDefinition<?> copyIntType(SchemaPath schemaPath,
+    private static TypeDefinition<?> createNewIntType(SchemaPath schemaPath,
             QName nodeQName, IntegerTypeDefinition type) {
         QName typeQName = type.getQName();
         SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName,
                 typeQName);
-
         String localName = typeQName.getLocalName();
-        List<RangeConstraint> ranges = type.getRangeStatements();
-        String units = type.getUnits();
 
         if ("int8".equals(localName)) {
-            Byte defaultValue = (Byte) type.getDefaultValue();
-            return new Int8(newSchemaPath, ranges, units, defaultValue);
+            return new Int8(newSchemaPath);
         } else if ("int16".equals(localName)) {
-            Short defaultValue = (Short) type.getDefaultValue();
-            return new Int16(newSchemaPath, ranges, units, defaultValue);
+            return new Int16(newSchemaPath);
         } else if ("int32".equals(localName)) {
-            Integer defaultValue = (Integer) type.getDefaultValue();
-            return new Int32(newSchemaPath, ranges, units, defaultValue);
+            return new Int32(newSchemaPath);
         } else if ("int64".equals(localName)) {
-            Long defaultValue = (Long) type.getDefaultValue();
-            return new Int64(newSchemaPath, ranges, units, defaultValue);
+            return new Int64(newSchemaPath);
         } else {
             return null;
         }
     }
 
-    private static TypeDefinition<?> copyUIntType(SchemaPath schemaPath,
+    private static TypeDefinition<?> createNewUintType(SchemaPath schemaPath,
             QName nodeQName, UnsignedIntegerTypeDefinition type) {
         QName typeQName = type.getQName();
         SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName,
                 typeQName);
-
         String localName = typeQName.getLocalName();
-        List<RangeConstraint> ranges = type.getRangeStatements();
-        String units = type.getUnits();
 
         if ("uint8".equals(localName)) {
-            Byte defaultValue = (Byte) type.getDefaultValue();
-            return new Int8(newSchemaPath, ranges, units, defaultValue);
+            return new Int8(newSchemaPath);
         } else if ("uint16".equals(localName)) {
-            Short defaultValue = (Short) type.getDefaultValue();
-            return new Int16(newSchemaPath, ranges, units, defaultValue);
+            return new Int16(newSchemaPath);
         } else if ("uint32".equals(localName)) {
-            Integer defaultValue = (Integer) type.getDefaultValue();
-            return new Int32(newSchemaPath, ranges, units, defaultValue);
+            return new Int32(newSchemaPath);
         } else if ("uint64".equals(localName)) {
-            Long defaultValue = (Long) type.getDefaultValue();
-            return new Int64(newSchemaPath, ranges, units, defaultValue);
+            return new Int64(newSchemaPath);
         } else {
             return null;
         }
@@ -569,9 +650,9 @@ public final class ParserUtils {
      * <li>reference</li>
      * <li>config</li>
      * </ul>
-     *
+     * 
      * These parameters may be refined for any node.
-     *
+     * 
      * @param node
      *            node to refine
      * @param refine
@@ -826,7 +907,7 @@ public final class ParserUtils {
 
     public static UsesNodeBuilder copyUsesNodeBuilder(UsesNodeBuilder old) {
         final UsesNodeBuilder copy = new UsesNodeBuilderImpl(
-                old.getGroupingPathString(), old.getLine());
+                old.getGroupingName(), old.getLine());
         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
             copy.addAugment(augment);
         }