Bug 6022: Deviation statement is not fully available in the YANG parser output
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / DeviationEffectiveStatementImpl.java
index 66a19f22ab3504e883ed27da28a451c5b9256e46..e4c0e85f61ab2ee00182a3d9acbdf1329f1c701c 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import java.util.LinkedList;
+import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
 import java.util.List;
-
+import java.util.Objects;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.model.api.DeviateDefinition;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
@@ -19,36 +21,32 @@ import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-import com.google.common.collect.ImmutableList;
-
-public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<SchemaNodeIdentifier, DeviationStatement>
+public class DeviationEffectiveStatementImpl extends DeclaredEffectiveStatementBase<SchemaNodeIdentifier, DeviationStatement>
         implements Deviation, Immutable {
+    private final SchemaPath targetPath;
+    private final String description;
+    private final String reference;
+    private final List<UnknownSchemaNode> unknownSchemaNodes;
+    private final List<DeviateDefinition> deviateDefinitions;
 
-    private SchemaPath targetPath;
-    private Deviate deviate;
-    private String reference;
-    private ImmutableList<UnknownSchemaNode> unknownSchemaNodes;
-
-    public DeviationEffectiveStatementImpl(StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
+    public DeviationEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
         super(ctx);
+        this.targetPath = ctx.getStatementArgument().asSchemaPath();
 
-        List<UnknownSchemaNode> unknownSchemaNodesInit = new LinkedList<>();
+        this.deviateDefinitions = ImmutableList.copyOf(allSubstatementsOfType(DeviateDefinition.class));
 
-        targetPath = SchemaPath.create(ctx.getStatementArgument().getPathFromRoot(), ctx.getStatementArgument()
-                .isAbsolute());
+        DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
+        this.description = (descriptionStmt == null) ? null : descriptionStmt.argument();
 
+        ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
+        this.reference = (referenceStmt == null) ? null : referenceStmt.argument();
+
+        List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
-            if (effectiveStatement instanceof DeviateEffectiveStatementImpl) {
-                deviate = ((DeviateEffectiveStatementImpl) effectiveStatement).argument();
-            }
-            if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
-                reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
-            }
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
             }
         }
-
         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
     }
 
@@ -58,8 +56,13 @@ public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<Sche
     }
 
     @Override
-    public Deviate getDeviate() {
-        return deviate;
+    public List<DeviateDefinition> getDeviates() {
+        return deviateDefinitions;
+    }
+
+    @Override
+    public String getDescription() {
+        return description;
     }
 
     @Override
@@ -76,14 +79,15 @@ public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<Sche
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
-        result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
-        result = prime * result + ((reference == null) ? 0 : reference.hashCode());
+        result = prime * result + Objects.hashCode(targetPath);
+        result = prime * result + Objects.hashCode(deviateDefinitions);
+        result = prime * result + Objects.hashCode(description);
+        result = prime * result + Objects.hashCode(reference);
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -94,25 +98,16 @@ public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<Sche
             return false;
         }
         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
-        if (targetPath == null) {
-            if (other.targetPath != null) {
-                return false;
-            }
-        } else if (!targetPath.equals(other.targetPath)) {
+        if (!Objects.equals(targetPath, other.targetPath)) {
             return false;
         }
-        if (deviate == null) {
-            if (other.deviate != null) {
-                return false;
-            }
-        } else if (!deviate.equals(other.deviate)) {
+        if (!Objects.equals(deviateDefinitions, other.deviateDefinitions)) {
             return false;
         }
-        if (reference == null) {
-            if (other.reference != null) {
-                return false;
-            }
-        } else if (!reference.equals(other.reference)) {
+        if (!Objects.equals(description, other.description)) {
+            return false;
+        }
+        if (!Objects.equals(reference, other.reference)) {
             return false;
         }
         return true;
@@ -120,12 +115,11 @@ public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<Sche
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(DeviationEffectiveStatementImpl.class.getSimpleName());
-        sb.append("[");
-        sb.append("targetPath=").append(targetPath);
-        sb.append(", deviate=").append(deviate);
-        sb.append(", reference=").append(reference);
-        sb.append("]");
-        return sb.toString();
+        return DeviationEffectiveStatementImpl.class.getSimpleName() + "[" +
+                "targetPath=" + targetPath +
+                ", deviates=" + deviateDefinitions +
+                ", description=" + description +
+                ", reference=" + reference +
+                "]";
     }
-}
\ No newline at end of file
+}