Split off DeclaredEffectiveStatementBase
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / UnknownEffectiveStatementImpl.java
index 7822169ab0aa942b5995cfafcfa9dba0fe5ff05b..6a30cd3acfe2e61313788f5e39f3ae9a09f242d9 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import java.util.ArrayList;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
 import java.util.List;
 import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
@@ -24,24 +24,19 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
-public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName, UnknownStatement<QName>> implements
-        UnknownSchemaNode {
+public final class UnknownEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, UnknownStatement<String>>
+        implements UnknownSchemaNode {
 
-    private boolean augmenting;
-    private boolean addedByUses;
-    private UnknownSchemaNode original;
-
-    private final QName qName;
+    private final boolean addedByUses;
+    private final boolean addedByAugmentation;
+    private final QName maybeQNameArgument;
     private final SchemaPath path;
-    private ExtensionDefinition extension;
-    private String description;
-    private String reference;
-    private Status status = Status.CURRENT;
-    private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
-    private QName nodeType;
-    private String nodeParameter;
-
-    public UnknownEffectiveStatementImpl(final StmtContext<QName, UnknownStatement<QName>, ?> ctx) {
+    private final ExtensionDefinition extension;
+    private final List<UnknownSchemaNode> unknownNodes;
+    private final QName nodeType;
+    private final String nodeParameter;
+
+    public UnknownEffectiveStatementImpl(final StmtContext<String, UnknownStatement<String>, ?> ctx) {
         super(ctx);
 
         final StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>> extensionInit = ctx
@@ -55,45 +50,45 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName,
             nodeType = extension.getQName();
         }
 
-        qName = ctx.getStatementArgument();
-        path = Utils.getSchemaPath(ctx);
+        // initCopyType
+        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
+        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
+            this.addedByUses = this.addedByAugmentation = true;
+        } else {
+            this.addedByAugmentation = copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION);
+            this.addedByUses = copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES);
+        }
 
+        // FIXME: Remove following section after fixing 4380
+        final UnknownSchemaNode original = ctx.getOriginalCtx() == null ? null : (UnknownSchemaNode) ctx
+                .getOriginalCtx().buildEffective();
+        if (original != null) {
+            this.maybeQNameArgument = original.getQName();
+        } else {
+            QName maybeQNameArgumentInit = null;
+            try {
+                maybeQNameArgumentInit = Utils.qNameFromArgument(ctx, argument());
+            } catch (IllegalArgumentException e) {
+                maybeQNameArgumentInit = nodeType;
+            }
+            this.maybeQNameArgument = maybeQNameArgumentInit;
+        }
+        path = ctx.getParentContext().getSchemaPath().get().createChild(maybeQNameArgument);
         nodeParameter = (ctx.rawStatementArgument() == null) ? "" : ctx.rawStatementArgument();
 
         // TODO init other fields (see Bug1412Test)
-
+        final Builder<UnknownSchemaNode> builder = ImmutableList.builder();
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
-            if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) {
-                description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument();
-            }
-            if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
-                reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
-            }
             if (effectiveStatement instanceof UnknownEffectiveStatementImpl) {
-                unknownNodes.add((UnknownEffectiveStatementImpl) effectiveStatement);
+                builder.add((UnknownEffectiveStatementImpl) effectiveStatement);
             }
         }
-
-        initCopyType(ctx);
+        unknownNodes = builder.build();
     }
 
-    private void initCopyType(final StmtContext<QName, UnknownStatement<QName>, ?> ctx) {
-
-        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
-
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
-            augmenting = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
-            addedByUses = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
-            addedByUses = augmenting = true;
-        }
-
-        if (ctx.getOriginalCtx() != null) {
-            original = (UnknownSchemaNode) ctx.getOriginalCtx().buildEffective();
-        }
+    @Override
+    public boolean isAddedByAugmentation() {
+        return addedByAugmentation;
     }
 
     @Override
@@ -118,7 +113,7 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName,
 
     @Override
     public QName getQName() {
-        return qName;
+        return maybeQNameArgument;
     }
 
     @Override
@@ -131,26 +126,11 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName,
         return unknownNodes;
     }
 
-    @Override
-    public String getDescription() {
-        return description;
-    }
-
-    @Override
-    public String getReference() {
-        return reference;
-    }
-
-    @Override
-    public Status getStatus() {
-        return status;
-    }
-
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + Objects.hashCode(qName);
+        result = prime * result + Objects.hashCode(maybeQNameArgument);
         result = prime * result + Objects.hashCode(path);
         result = prime * result + Objects.hashCode(nodeType);
         result = prime * result + Objects.hashCode(nodeParameter);
@@ -169,32 +149,16 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName,
             return false;
         }
         UnknownEffectiveStatementImpl other = (UnknownEffectiveStatementImpl) obj;
-        if (qName == null) {
-            if (other.qName != null) {
-                return false;
-            }
-        } else if (!qName.equals(other.qName)) {
+        if (!Objects.equals(maybeQNameArgument, other.maybeQNameArgument)) {
             return false;
         }
-        if (path == null) {
-            if (other.path != null) {
-                return false;
-            }
-        } else if (!path.equals(other.path)) {
+        if (!Objects.equals(path, other.path)) {
             return false;
         }
-        if (nodeType == null) {
-            if (other.nodeType != null) {
-                return false;
-            }
-        } else if (!nodeType.equals(other.nodeType)) {
+        if (!Objects.equals(nodeType, other.nodeType)) {
             return false;
         }
-        if (nodeParameter == null) {
-            if (other.nodeParameter != null) {
-                return false;
-            }
-        } else if (!nodeParameter.equals(other.nodeParameter)) {
+        if (!Objects.equals(nodeParameter, other.nodeParameter)) {
             return false;
         }
         return true;
@@ -210,4 +174,5 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<QName,
         sb.append(nodeParameter);
         return sb.toString();
     }
+
 }