Eliminate ConstraintDefition.isMandatory()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ChoiceEffectiveStatementImpl.java
index 74fdd97c242d39de19ed18762818326a7cfd2635..9eb0fa78773b1885088af8b9fbaaff7fedf4effb 100644 (file)
@@ -7,78 +7,60 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
-import java.util.Collection;
-import java.util.Comparator;
+import com.google.common.collect.ImmutableSortedMap;
 import java.util.LinkedHashSet;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.SortedMap;
+import java.util.TreeMap;
 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.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-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.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.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
 
 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());
-    };
 
+    private final Set<AugmentationSchemaNode> augmentations;
+    private final SortedMap<QName, ChoiceCaseNode> cases;
+    private final ChoiceCaseNode defaultCase;
     private final ChoiceSchemaNode original;
-    private final String defaultCase;
-
-    private final Set<ChoiceCaseNode> cases;
-    private final Set<AugmentationSchema> augmentations;
+    private final boolean mandatory;
 
     public ChoiceEffectiveStatementImpl(
             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
         super(ctx);
-        this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
-
-        DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
-        this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
+        this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
 
         // initSubstatementCollectionsAndFields
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-        Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
-        SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
+        final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
+        final SortedMap<QName, ChoiceCaseNode> casesInit = new TreeMap<>();
 
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof AugmentationSchema) {
-                AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
+        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
+            if (effectiveStatement instanceof AugmentationSchemaNode) {
+                final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
                 augmentationsInit.add(augmentationSchema);
             }
             if (effectiveStatement instanceof ChoiceCaseNode) {
-                ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
-                casesInit.add(choiceCaseNode);
+                final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
+                // FIXME: we may be overwriting a previous entry, is that really okay?
+                casesInit.put(choiceCaseNode.getQName(), 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 (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
+                final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+                final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
+                // FIXME: we may be overwriting a previous entry, is that really okay?
+                casesInit.put(shorthandCase.getQName(), shorthandCase);
                 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
                     resetAugmenting(dataSchemaNode);
                 }
@@ -86,70 +68,71 @@ public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSch
         }
 
         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
-        this.cases = ImmutableSet.copyOf(casesInit);
+        this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
+
+        final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
+        if (defaultStmt != null) {
+            final QName qname;
+            try {
+                qname = QName.create(getQName(), defaultStmt.argument());
+            } catch (IllegalArgumentException e) {
+                throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
+                    defaultStmt.argument(), e);
+            }
+
+            // FIXME: this does not work with submodules, as they are
+            defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
+                "Default statement refers to missing case %s", qname);
+        } else {
+            defaultCase = null;
+        }
+
+        final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
+        mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
     }
 
     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
-            LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
+            final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
             leaf.augmenting = false;
         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
-            ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
+            final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
             container.augmenting = false;
         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
-            LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
+            final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
             leafList.augmenting = false;
         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
-            ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
+            final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
             list.augmenting = false;
         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
-            AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
+            final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
             anyXml.augmenting = false;
         }
     }
 
     @Override
     public Optional<ChoiceSchemaNode> getOriginal() {
-        return Optional.fromNullable(original);
+        return Optional.ofNullable(original);
     }
 
     @Override
-    public Set<AugmentationSchema> getAvailableAugmentations() {
+    public Set<AugmentationSchemaNode> getAvailableAugmentations() {
         return augmentations;
     }
 
     @Override
-    public Set<ChoiceCaseNode> getCases() {
+    public SortedMap<QName, ChoiceCaseNode> getCases() {
         return cases;
     }
 
     @Override
-    public ChoiceCaseNode getCaseNodeByName(final QName name) {
-        Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
-
-        for (final ChoiceCaseNode caseNode : cases) {
-            if (caseNode != null && name.equals(caseNode.getQName())) {
-                return caseNode;
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public ChoiceCaseNode getCaseNodeByName(final String name) {
-        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())) {
-                return caseNode;
-            }
-        }
-        return null;
+    public Optional<ChoiceCaseNode> getDefaultCase() {
+        return Optional.ofNullable(defaultCase);
     }
 
     @Override
-    public String getDefaultCase() {
-        return defaultCase;
+    public boolean isMandatory() {
+        return mandatory;
     }
 
     @Override
@@ -172,14 +155,14 @@ public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSch
         if (getClass() != obj.getClass()) {
             return false;
         }
-        ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
+        final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
     }
 
     @Override
     public String toString() {
-        return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
-                "qname=" + getQName() +
-                "]";
+        return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
+                + "qname=" + getQName()
+                "]";
     }
 }