BUG-1382: eliminate use of QName.getPrefix from yang parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ParserListenerUtils.java
index 4447ed196bbda8308243c0b26f46f6526800da60..b5f66925e4a0ba9cd83997273efdce4b7b936d16 100644 (file)
@@ -13,17 +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 java.util.Stack;
-
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.antlr.v4.runtime.tree.TerminalNode;
@@ -110,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;
@@ -123,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;
@@ -311,17 +308,6 @@ public final class ParserListenerUtils {
         return null;
     }
 
-    /**
-     * Create SchemaPath from actualPath and new node name.
-     *
-     * @param actualPath
-     *            current position in model
-     * @return SchemaPath object
-     */
-    public static SchemaPath createActualSchemaPath(final Stack<QName> actualPath) {
-        return SchemaPath.create(actualPath, true);
-    }
-
     /**
      * Create java.util.List of key node names.
      *
@@ -346,7 +332,7 @@ public final class ParserListenerUtils {
      * @return List of EnumPair object parsed from given context
      */
     private static List<EnumTypeDefinition.EnumPair> getEnumConstants(final Type_body_stmtsContext ctx,
-            final Stack<QName> path, final String moduleName) {
+            final SchemaPath path, final String moduleName) {
         List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<>();
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
@@ -382,7 +368,7 @@ public final class ParserListenerUtils {
      * @return EnumPair object parsed from given context
      */
     private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue,
-            final Stack<QName> actualPath, final String moduleName) {
+            final SchemaPath actualPath, final String moduleName) {
         final String name = stringFromNode(ctx);
         SchemaPath path = createTypePath(actualPath, name);
         Integer value = null;
@@ -758,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);
     }
 
     /**
@@ -854,7 +840,7 @@ public final class ParserListenerUtils {
      *            current module name
      * @return List of Bit objects created from this context
      */
-    private static List<BitsTypeDefinition.Bit> getBits(final Type_body_stmtsContext ctx, final Stack<QName> actualPath,
+    private static List<BitsTypeDefinition.Bit> getBits(final Type_body_stmtsContext ctx, final SchemaPath actualPath,
             final String moduleName) {
         final List<BitsTypeDefinition.Bit> bits = new ArrayList<>();
         for (int j = 0; j < ctx.getChildCount(); j++) {
@@ -890,7 +876,7 @@ public final class ParserListenerUtils {
      * @return Bit object parsed from this context
      */
     private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, final long highestPosition,
-            final Stack<QName> actualPath, final String moduleName) {
+            final SchemaPath actualPath, final String moduleName) {
         String name = stringFromNode(ctx);
         Long position = null;
 
@@ -1052,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 Stack<QName> 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();
     }
 
     /**
@@ -1129,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 Stack<QName> 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();
@@ -1148,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;
@@ -1157,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")) {
@@ -1226,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)) {
@@ -1238,12 +1213,10 @@ public final class ParserListenerUtils {
             return baseType;
         }
 
-        List<QName> path = new ArrayList<>(actualPath);
-        path.add(new QName(namespace, revision, prefix, typeName));
-        SchemaPath schemaPath = SchemaPath.create(path, true);
-
-        QName qname = schemaPath.getPath().get(schemaPath.getPath().size() - 1);
-        ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath);
+        QName qname = QName.create(moduleQName, typeName);
+        SchemaPath schemaPath = actualPath.createChild(qname);
+        final Optional<String> opt = Optional.of("");
+        ExtendedType.Builder typeBuilder = ExtendedType.builder(qname, baseType, opt, opt, schemaPath);
 
         typeBuilder.ranges(constraints.getRange());
         typeBuilder.lengths(constraints.getLength());
@@ -1253,28 +1226,19 @@ public final class ParserListenerUtils {
         return typeBuilder.build();
     }
 
-    private static SchemaPath createTypePath(final Stack<QName> actual, final String typeName) {
-        QName last = actual.peek();
-        QName typeQName = QName.create(last, typeName);
-        List<QName> path = new ArrayList<>(actual);
-        path.add(typeQName);
-        return SchemaPath.create(path, true);
+    private static SchemaPath createTypePath(final SchemaPath actual, final String typeName) {
+        QName last = actual.getLastComponent();
+        return actual.createChild(QName.create(last, typeName));
     }
 
-    private static SchemaPath createBaseTypePath(final Stack<QName> actual, final String typeName) {
-        List<QName> path = new ArrayList<>(actual);
-        path.add(BaseTypes.constructQName(typeName));
-        return SchemaPath.create(path, true);
+    private static SchemaPath createBaseTypePath(final SchemaPath actual, final String typeName) {
+        return actual.createChild(BaseTypes.constructQName(typeName));
     }
 
-    private static SchemaPath createExtendedBaseTypePath(final Stack<QName> actual, final URI namespace, final Date revision,
-            final String prefix, final String typeName) {
-        QName extTypeName = new QName(namespace, revision, prefix, typeName);
-        QName baseTypeName = BaseTypes.constructQName(typeName);
-        List<QName> path = new ArrayList<>(actual);
-        path.add(extTypeName);
-        path.add(baseTypeName);
-        return SchemaPath.create(path, true);
+    private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final QName moduleQName, final String typeName) {
+        return actual.createChild(
+                QName.create(moduleQName, typeName),
+                BaseTypes.constructQName(typeName));
     }
 
     /**
@@ -1359,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) {
@@ -1384,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);
     }
 
     /**
@@ -1455,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) {