Merge "Bug 1372 - toString methods in generated classes"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ParserListenerUtils.java
index b242cb7eee777f98397daa9250e54d36247249c0..b5f66925e4a0ba9cd83997273efdce4b7b936d16 100644 (file)
@@ -13,16 +13,12 @@ import com.google.common.base.CharMatcher;
 import com.google.common.base.Optional;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Lists;
-
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
-
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.antlr.v4.runtime.tree.TerminalNode;
@@ -109,7 +105,7 @@ import org.opendaylight.yangtools.yang.model.util.BitsType;
 import org.opendaylight.yangtools.yang.model.util.Decimal64;
 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
 import org.opendaylight.yangtools.yang.model.util.Int16;
 import org.opendaylight.yangtools.yang.model.util.Int32;
 import org.opendaylight.yangtools.yang.model.util.Int64;
@@ -122,15 +118,17 @@ 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.ConstraintsBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.RefineBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
+import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.util.TypeConstraints;
 import org.opendaylight.yangtools.yang.parser.util.UnknownBoundaryNumber;
@@ -746,18 +744,18 @@ public final class ParserListenerUtils {
      * @return PatternConstraint object
      */
     private static PatternConstraint parsePatternConstraint(final Pattern_stmtContext ctx) {
-        String description = null;
-        String reference = null;
+        Optional<String> description = Optional.absent();
+        Optional<String> reference = Optional.absent();
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
             if (child instanceof Description_stmtContext) {
-                description = stringFromNode(child);
+                description = Optional.of(stringFromNode(child));
             } else if (child instanceof Reference_stmtContext) {
-                reference = stringFromNode(child);
+                reference = Optional.of(stringFromNode(child));
             }
         }
         String pattern = parsePatternString(ctx);
-        return BaseConstraints.patternConstraint(pattern, description, reference);
+        return BaseConstraints.newPatternConstraint(pattern, description, reference);
     }
 
     /**
@@ -1040,61 +1038,51 @@ public final class ParserListenerUtils {
     }
 
     /**
-     * Parse type body and create UnknownType definition.
+     * Parse unknown type with body.
      *
-     * @param typedefQName
-     *            qname of current type
-     * @param ctx
+     * @param typeBody
      *            type body
-     * @param actualPath
-     *            actual path in model
-     * @param namespace
-     *            module namespace
-     * @param revision
-     *            module revision
-     * @param prefix
-     *            module prefix
      * @param parent
      *            current node parent
-     * @return UnknownType object with constraints from parsed type body
+     * @param prefixedQName
+     *            type qname with prefix
+     * @param moduleBuilder
+     *            current module builder
+     * @param moduleQName
+     *            current module qname
+     * @param actualPath
+     *            actual path in model
      */
-    public static TypeDefinition<?> parseUnknownTypeWithBody(final QName typedefQName,
-            final Type_body_stmtsContext ctx, final SchemaPath actualPath, final URI namespace, final Date revision,
-            final String prefix, final Builder parent) {
-        String moduleName = parent.getModuleName();
-        String typeName = typedefQName.getLocalName();
-
-        UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName);
-
-        if (ctx != null) {
-            List<RangeConstraint> rangeStatements = getRangeConstraints(ctx, moduleName);
-            List<LengthConstraint> lengthStatements = getLengthConstraints(ctx, moduleName);
-            List<PatternConstraint> patternStatements = getPatternConstraint(ctx);
-            Integer fractionDigits = getFractionDigits(ctx, moduleName);
-
-            if (parent instanceof TypeDefinitionBuilder) {
-                TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
-                typedef.setRanges(rangeStatements);
-                typedef.setLengths(lengthStatements);
-                typedef.setPatterns(patternStatements);
-                typedef.setFractionDigits(fractionDigits);
-                return unknownType.build();
-            } else {
-                TypeDefinition<?> baseType = unknownType.build();
-                QName qname = new QName(namespace, revision, prefix, typeName);
-                SchemaPath schemaPath = createTypePath(actualPath, typeName);
+    public static void parseUnknownTypeWithBody(Type_body_stmtsContext typeBody, TypeAwareBuilder parent,
+            QName prefixedQName, ModuleBuilder moduleBuilder, QName moduleQName, SchemaPath actualPath) {
+        final int line = typeBody.getStart().getLine();
 
-                ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath);
-                typeBuilder.ranges(rangeStatements);
-                typeBuilder.lengths(lengthStatements);
-                typeBuilder.patterns(patternStatements);
-                typeBuilder.fractionDigits(fractionDigits);
+        List<RangeConstraint> rangeStatements = getRangeConstraints(typeBody, moduleBuilder.getName());
+        List<LengthConstraint> lengthStatements = getLengthConstraints(typeBody, moduleBuilder.getName());
+        List<PatternConstraint> patternStatements = getPatternConstraint(typeBody);
+        Integer fractionDigits = getFractionDigits(typeBody, moduleBuilder.getName());
 
-                return typeBuilder.build();
-            }
+        if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
+            TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
+            typedef.setRanges(rangeStatements);
+            typedef.setLengths(lengthStatements);
+            typedef.setPatterns(patternStatements);
+            typedef.setFractionDigits(fractionDigits);
+            typedef.setTypeQName(prefixedQName);
+            // add parent node of this type statement to dirty nodes
+            moduleBuilder.markActualNodeDirty();
+        } else {
+            QName qname = QName.create(moduleQName, prefixedQName.getLocalName());
+            SchemaPath schemaPath = createTypePath(actualPath, prefixedQName.getLocalName());
+            TypeDefinitionBuilder typeBuilder = new TypeDefinitionBuilderImpl(moduleBuilder.getName(), line, qname, schemaPath);
+            typeBuilder.setRanges(rangeStatements);
+            typeBuilder.setLengths(lengthStatements);
+            typeBuilder.setPatterns(patternStatements);
+            typeBuilder.setFractionDigits(fractionDigits);
+            typeBuilder.setTypeQName(prefixedQName);
+            parent.setTypedef(typeBuilder);
+            moduleBuilder.getDirtyNodes().add(typeBuilder);
         }
-
-        return unknownType.build();
     }
 
     /**
@@ -1117,8 +1105,7 @@ public final class ParserListenerUtils {
      * @return TypeDefinition object based on parsed values.
      */
     public static TypeDefinition<?> parseTypeWithBody(final String typeName, final Type_body_stmtsContext typeBody,
-            final SchemaPath actualPath, final URI namespace, final Date revision, final String prefix,
-            final Builder parent) {
+            final SchemaPath actualPath, final QName moduleQName, final Builder parent) {
 
         final String moduleName = parent.getModuleName();
         final int line = typeBody.getStart().getLine();
@@ -1136,7 +1123,7 @@ public final class ParserListenerUtils {
         constraints.addRanges(rangeStatements);
 
         SchemaPath baseTypePath = createBaseTypePath(actualPath, typeName);
-        SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, namespace, revision, prefix, typeName);
+        SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, moduleQName, typeName);
 
         if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
             extBaseTypePath = baseTypePath;
@@ -1145,12 +1132,12 @@ public final class ParserListenerUtils {
         if ("decimal64".equals(typeName)) {
             if (rangeStatements.isEmpty()) {
                 try {
-                    return new Decimal64(baseTypePath, fractionDigits);
+                    return Decimal64.create(baseTypePath, fractionDigits);
                 } catch(Exception e) {
                     throw new YangParseException(moduleName, line, e.getMessage());
                 }
             }
-            Decimal64 decimalType = new Decimal64(extBaseTypePath, fractionDigits);
+            Decimal64 decimalType = Decimal64.create(extBaseTypePath, fractionDigits);
             constraints.addRanges(decimalType.getRangeConstraints());
             baseType = decimalType;
         } else if (typeName.startsWith("int")) {
@@ -1214,7 +1201,7 @@ public final class ParserListenerUtils {
             constraints.addLengths(binaryType.getLengthConstraints());
             baseType = binaryType;
         } else if ("instance-identifier".equals(typeName)) {
-            return InstanceIdentifier.create(isRequireInstance(typeBody));
+            return InstanceIdentifierType.create(isRequireInstance(typeBody));
         }
 
         if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
@@ -1226,9 +1213,10 @@ public final class ParserListenerUtils {
             return baseType;
         }
 
-        QName qname = new QName(namespace, revision, prefix, typeName);
+        QName qname = QName.create(moduleQName, typeName);
         SchemaPath schemaPath = actualPath.createChild(qname);
-        ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath);
+        final Optional<String> opt = Optional.of("");
+        ExtendedType.Builder typeBuilder = ExtendedType.builder(qname, baseType, opt, opt, schemaPath);
 
         typeBuilder.ranges(constraints.getRange());
         typeBuilder.lengths(constraints.getLength());
@@ -1247,10 +1235,9 @@ public final class ParserListenerUtils {
         return actual.createChild(BaseTypes.constructQName(typeName));
     }
 
-    private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final URI namespace, final Date revision,
-            final String prefix, final String typeName) {
+    private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final QName moduleQName, final String typeName) {
         return actual.createChild(
-                new QName(namespace, revision, prefix, typeName),
+                QName.create(moduleQName, typeName),
                 BaseTypes.constructQName(typeName));
     }
 
@@ -1336,10 +1323,10 @@ public final class ParserListenerUtils {
      */
     private static MustDefinition parseMust(final YangParser.Must_stmtContext ctx) {
         StringBuilder mustText = new StringBuilder();
-        String description = null;
-        String reference = null;
-        String errorAppTag = null;
-        String errorMessage = null;
+        Optional<String> description = Optional.absent();
+        Optional<String> reference = Optional.absent();
+        Optional<String> errorAppTag = Optional.absent();
+        Optional<String> errorMessage = Optional.absent();
         for (int i = 0; i < ctx.getChildCount(); ++i) {
             ParseTree child = ctx.getChild(i);
             if (child instanceof StringContext) {
@@ -1361,17 +1348,17 @@ public final class ParserListenerUtils {
                     }
                 }
             } else if (child instanceof Description_stmtContext) {
-                description = stringFromNode(child);
+                description = Optional.of(stringFromNode(child));
             } else if (child instanceof Reference_stmtContext) {
-                reference = stringFromNode(child);
+                reference = Optional.of(stringFromNode(child));
             } else if (child instanceof Error_app_tag_stmtContext) {
-                errorAppTag = stringFromNode(child);
+                errorAppTag = Optional.of(stringFromNode(child));
             } else if (child instanceof Error_message_stmtContext) {
-                errorMessage = stringFromNode(child);
+                errorMessage = Optional.of(stringFromNode(child));
             }
         }
 
-        return new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, errorMessage);
+        return MustDefinitionImpl.create(mustText.toString(), description, reference, errorAppTag, errorMessage);
     }
 
     /**
@@ -1432,7 +1419,12 @@ public final class ParserListenerUtils {
             for (int i = 0; i < ctx.getChildCount(); i++) {
                 ParseTree maxArg = ctx.getChild(i);
                 if (maxArg instanceof Max_value_argContext) {
-                    result = Integer.valueOf(stringFromNode(maxArg));
+                    String maxValue = stringFromNode(maxArg);
+                    if ("unbounded".equals(maxValue)) {
+                        result = Integer.MAX_VALUE;
+                    } else {
+                        result = Integer.valueOf(maxValue);
+                    }
                 }
             }
             if (result == null) {