BUG-1371: improved parser handling of invalid yang list key.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
index 3b75d1190cab0ccd863bf60c9883e9483e0f099d..18497d7f5d86f2431029957b0316358942cf544b 100644 (file)
@@ -7,49 +7,36 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
+
 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.Set;
+
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.Status;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
-import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
-import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
-import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
 
-public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
-        AugmentationSchemaBuilder {
+public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
     private final int order;
     private AugmentationSchemaImpl instance;
     private String whenCondition;
 
-    private String description;
-    private String reference;
-    private Status status = Status.CURRENT;
-
     private final String augmentTargetStr;
     private final SchemaPath targetPath;
     private SchemaPath targetNodeSchemaPath;
@@ -57,26 +44,16 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     private boolean resolved;
     private AugmentationSchemaBuilder copyOf;
 
-    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, int order) {
+    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
         super(moduleName, line, null);
         this.order = order;
         this.augmentTargetStr = augmentTargetStr;
-        targetPath = ParserUtils.parseXPathString(augmentTargetStr);
-    }
-
-    @Override
-    public Set<GroupingDefinition> getGroupings() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public Set<GroupingBuilder> getGroupingBuilders() {
-        return Collections.emptySet();
+        targetPath = BuilderUtils.parseXPathString(augmentTargetStr);
     }
 
     @Override
-    public void addGrouping(final GroupingBuilder grouping) {
-        throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
+    protected String getStatementName() {
+        return "augment";
     }
 
     @Override
@@ -90,11 +67,8 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             return instance;
         }
 
-        instance = new AugmentationSchemaImpl(targetPath, order);
-
-        instance.description = description;
-        instance.reference = reference;
-        instance.status = status;
+        buildChildren();
+        instance = new AugmentationSchemaImpl(targetPath, order,this);
 
         Builder parent = getParent();
         if (parent instanceof ModuleBuilder) {
@@ -104,11 +78,11 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         }
 
         if (parent instanceof UsesNodeBuilder) {
-            ModuleBuilder mb = ParserUtils.getParentModule(this);
+            final ModuleBuilder mb = BuilderUtils.getParentModule(this);
+
             List<QName> newPath = new ArrayList<>();
-            List<QName> parsedPath = targetPath.getPath();
-            for (QName name : parsedPath) {
-                newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
+            for (QName name : targetPath.getPathFromRoot()) {
+                newPath.add(QName.create(mb.getQNameModule(), name.getPrefix(), name.getLocalName()));
             }
             instance.targetPath = SchemaPath.create(newPath, false);
         } else {
@@ -127,18 +101,6 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         }
         instance.whenCondition = whenStmt;
 
-        // CHILD NODES
-        for (DataSchemaNodeBuilder node : addedChildNodes) {
-            childNodes.add(node.build());
-        }
-        instance.childNodes = ImmutableSet.copyOf(childNodes);
-
-        // USES
-        for (UsesNodeBuilder builder : addedUsesNodes) {
-            usesNodes.add(builder.build());
-        }
-        instance.uses = ImmutableSet.copyOf(usesNodes);
-
         // UNKNOWN NODES
         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
             unknownNodes.add(b.build());
@@ -168,46 +130,6 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         this.whenCondition = whenCondition;
     }
 
-    @Override
-    public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public void addTypedef(final TypeDefinitionBuilder type) {
-        throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
-    }
-
-    @Override
-    public String getDescription() {
-        return description;
-    }
-
-    @Override
-    public void setDescription(final String description) {
-        this.description = description;
-    }
-
-    @Override
-    public String getReference() {
-        return reference;
-    }
-
-    @Override
-    public void setReference(final String reference) {
-        this.reference = reference;
-    }
-
-    @Override
-    public Status getStatus() {
-        return status;
-    }
-
-    @Override
-    public void setStatus(final Status status) {
-        this.status = Preconditions.checkNotNull(status, "status cannot be null");
-    }
-
     @Override
     public String getTargetPathAsString() {
         return augmentTargetStr;
@@ -239,7 +161,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         int result = 1;
         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
-        result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
+        result = prime * result + getChildNodeBuilders().hashCode();
         return result;
     }
 
@@ -269,11 +191,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         } else if (!whenCondition.equals(other.whenCondition)) {
             return false;
         }
-        if (addedChildNodes == null) {
-            if (other.addedChildNodes != null) {
-                return false;
-            }
-        } else if (!addedChildNodes.equals(other.addedChildNodes)) {
+        if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
             return false;
         }
         return true;
@@ -288,23 +206,18 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         copyOf = old;
     }
 
-    private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
-            Comparable<AugmentationSchemaImpl> {
+    private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
         private final int order;
         private SchemaPath targetPath;
         private RevisionAwareXPath whenCondition;
-        private ImmutableSet<DataSchemaNode> childNodes;
-        private ImmutableSet<UsesNode> uses;
-        private String description;
-        private String reference;
-        private Status status;
 
         private URI namespace;
         private Date revision;
         private ImmutableList<UnknownSchemaNode> unknownNodes;
         private AugmentationSchema copyOf;
 
-        public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
+        public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
+            super(builder);
             this.targetPath = targetPath;
             this.order = order;
         }
@@ -328,64 +241,11 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             return whenCondition;
         }
 
-        @Override
-        public Set<DataSchemaNode> getChildNodes() {
-            return childNodes;
-        }
-
-        /**
-         * Always returns an empty set, because augment can not contains
-         * grouping statement.
-         */
-        @Override
-        public Set<GroupingDefinition> getGroupings() {
-            return Collections.emptySet();
-        }
-
-        @Override
-        public Set<UsesNode> getUses() {
-            return uses;
-        }
-
-        /**
-         * Always returns an empty set, because augment can not contains type
-         * definitions.
-         */
-        @Override
-        public Set<TypeDefinition<?>> getTypeDefinitions() {
-            return Collections.emptySet();
-        }
-
-        @Override
-        public String getDescription() {
-            return description;
-        }
-
-        @Override
-        public String getReference() {
-            return reference;
-        }
-
-        @Override
-        public Status getStatus() {
-            return status;
-        }
-
         @Override
         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
             return unknownNodes;
         }
 
-        @Override
-        public DataSchemaNode getDataChildByName(final QName name) {
-            return getChildNode(childNodes, name);
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(final String name) {
-            return getChildNode(childNodes, name);
-        }
-
         @Override
         public URI getNamespace() {
             return namespace;
@@ -402,7 +262,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             int result = 1;
             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
-            result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
+            result = prime * result + getChildNodes().hashCode();
             return result;
         }
 
@@ -432,11 +292,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             } else if (!whenCondition.equals(other.whenCondition)) {
                 return false;
             }
-            if (childNodes == null) {
-                if (other.childNodes != null) {
-                    return false;
-                }
-            } else if (!childNodes.equals(other.childNodes)) {
+            if (!getChildNodes().equals(other.getChildNodes())) {
                 return false;
             }
             return true;
@@ -446,16 +302,17 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         public String toString() {
             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
             sb.append("[");
-            sb.append("targetPath=" + targetPath);
-            sb.append(", when=" + whenCondition);
+            sb.append("targetPath=").append(targetPath);
+            sb.append(", when=").append(whenCondition);
             sb.append("]");
             return sb.toString();
         }
 
         @Override
-        public int compareTo(AugmentationSchemaImpl o) {
-            Iterator<QName> thisIt = this.targetPath.getPath().iterator();
-            Iterator<QName> otherIt = o.getTargetPath().getPath().iterator();
+        public int compareTo(final AugmentationSchemaImpl o) {
+            checkNotNull(o);
+            Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
+            Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
             while (thisIt.hasNext()) {
                 if (otherIt.hasNext()) {
                     int comp = thisIt.next().compareTo(otherIt.next());
@@ -472,5 +329,4 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             return this.order - o.order;
         }
     }
-
 }