BUG-6497: Do not lose augmentation statement order
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ChoiceEffectiveStatementImpl.java
index 7c36706d07b3cb0aace0088468f6a7a40f085e51..74fdd97c242d39de19ed18762818326a7cfd2635 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,85 +7,62 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
-import java.util.List;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.LinkedHashSet;
+import java.util.Objects;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
-    private final QName qname;
-    private final SchemaPath path;
+public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
+        ChoiceSchemaNode, DerivableSchemaNode {
+    /**
+     * Comparator based on alphabetical order of local name of SchemaNode's
+     * qname.
+     */
+    private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> {
+        return o1.getQName().compareTo(o2.getQName());
+    };
 
-    boolean augmenting;
-    boolean addedByUses;
-    ChoiceSchemaNode original;
-    boolean configuration;
-    ConstraintDefinition constraints;
-    String defaultCase;
+    private final ChoiceSchemaNode original;
+    private final String defaultCase;
 
-    ImmutableSet<ChoiceCaseNode> cases;
-    ImmutableSet<AugmentationSchema> augmentations;
-    ImmutableList<UnknownSchemaNode> unknownNodes;
+    private final Set<ChoiceCaseNode> cases;
+    private final Set<AugmentationSchema> augmentations;
 
-    public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
+    public ChoiceEffectiveStatementImpl(
+            final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
         super(ctx);
+        this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
 
-        this.qname = ctx.getStatementArgument();
-        this.path = Utils.getSchemaPath(ctx);
-        //:TODO init other fields
-
-        initSubstatementCollections();
-        initCopyType(ctx);
-    }
+        DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
+        this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
 
-    private void initCopyType(
-            StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
-
-        TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
-        switch (typeOfCopy) {
-        case ADDED_BY_AUGMENTATION:
-            augmenting = true;
-            original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
-            break;
-        case ADDED_BY_USES:
-            addedByUses = true;
-            original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
-            break;
-        default:
-            break;
-        }
-    }
-
-    private void initSubstatementCollections() {
+        // initSubstatementCollectionsAndFields
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-        Set<AugmentationSchema> augmentationsInit = new HashSet<>();
-        Set<ChoiceCaseNode> casesInit = new HashSet<>();
+        Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
+        SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
 
         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodesInit.add(unknownNode);
-            }
             if (effectiveStatement instanceof AugmentationSchema) {
                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
                 augmentationsInit.add(augmentationSchema);
@@ -94,31 +71,41 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
                 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
                 casesInit.add(choiceCaseNode);
             }
+            if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode
+                    || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode
+                    || effectiveStatement instanceof LeafSchemaNode) {
+
+                DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+                ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
+                casesInit.add(shorthandCase);
+
+                if (dataSchemaNode.isAugmenting() && !this.augmenting) {
+                    resetAugmenting(dataSchemaNode);
+                }
+            }
         }
 
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
         this.cases = ImmutableSet.copyOf(casesInit);
     }
 
-    @Override
-    public QName getQName() {
-        return qname;
-    }
-
-    @Override
-    public SchemaPath getPath() {
-        return path;
-    }
-
-    @Override
-    public boolean isAugmenting() {
-        return augmenting;
-    }
-
-    @Override
-    public boolean isAddedByUses() {
-        return addedByUses;
+    private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
+        if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
+            LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
+            leaf.augmenting = false;
+        } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
+            ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
+            container.augmenting = false;
+        } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
+            LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
+            leafList.augmenting = false;
+        } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
+            ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
+            list.augmenting = false;
+        } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
+            AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
+            anyXml.augmenting = false;
+        }
     }
 
     @Override
@@ -126,26 +113,11 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
         return Optional.fromNullable(original);
     }
 
-    @Override
-    public boolean isConfiguration() {
-        return configuration;
-    }
-
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return constraints;
-    }
-
     @Override
     public Set<AugmentationSchema> getAvailableAugmentations() {
         return augmentations;
     }
 
-    @Override
-    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
-    }
-
     @Override
     public Set<ChoiceCaseNode> getCases() {
         return cases;
@@ -153,9 +125,8 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
 
     @Override
     public ChoiceCaseNode getCaseNodeByName(final QName name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
-        }
+        Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
+
         for (final ChoiceCaseNode caseNode : cases) {
             if (caseNode != null && name.equals(caseNode.getQName())) {
                 return caseNode;
@@ -166,12 +137,10 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
 
     @Override
     public ChoiceCaseNode getCaseNodeByName(final String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
-        }
+        Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
+
         for (final ChoiceCaseNode caseNode : cases) {
-            if (caseNode != null && (caseNode.getQName() != null)
-                    && name.equals(caseNode.getQName().getLocalName())) {
+            if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
                 return caseNode;
             }
         }
@@ -187,8 +156,8 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((qname == null) ? 0 : qname.hashCode());
-        result = prime * result + ((path == null) ? 0 : path.hashCode());
+        result = prime * result + Objects.hashCode(getQName());
+        result = prime * result + Objects.hashCode(getPath());
         return result;
     }
 
@@ -204,30 +173,13 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod
             return false;
         }
         ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
-        if (qname == null) {
-            if (other.qname != null) {
-                return false;
-            }
-        } else if (!qname.equals(other.qname)) {
-            return false;
-        }
-        if (path == null) {
-            if (other.path != null) {
-                return false;
-            }
-        } else if (!path.equals(other.path)) {
-            return false;
-        }
-        return true;
+        return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
     }
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
-        sb.append("[");
-        sb.append("qname=").append(qname);
-        sb.append("]");
-        return sb.toString();
+        return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
+                "qname=" + getQName() +
+                "]";
     }
-
-}
\ No newline at end of file
+}