Bug 6410: Fixed initialization of typedefs in rpc
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / RpcEffectiveStatementImpl.java
index 1fcecde23e0ab67808ff54699d8608183a6a82c1..478a18d8ef697b401df8b7498b1d7a17ead8e1b0 100644 (file)
@@ -7,86 +7,53 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
+import com.google.common.collect.ImmutableSet;
 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.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.model.api.stmt.RpcStatement;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import java.util.List;
+import java.util.LinkedHashSet;
+import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-
-public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, RpcStatement> implements RpcDefinition {
-    private final QName qname;
-    private final SchemaPath path;
-
-    private ContainerSchemaNode input;
-    private ContainerSchemaNode output;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RpcStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-    ImmutableSet<TypeDefinition<?>> typeDefinitions;
-    ImmutableSet<GroupingDefinition> groupings;
-    ImmutableList<UnknownSchemaNode> unknownNodes;
+public class RpcEffectiveStatementImpl extends AbstractEffectiveSchemaNode<RpcStatement> implements RpcDefinition {
+    private final ContainerSchemaNode input;
+    private final ContainerSchemaNode output;
+    private final Set<TypeDefinition<?>> typeDefinitions;
+    private final Set<GroupingDefinition> groupings;
 
-    public RpcEffectiveStatementImpl(StmtContext<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> ctx) {
+    public RpcEffectiveStatementImpl(final StmtContext<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> ctx) {
         super(ctx);
-        this.qname = ctx.getStatementArgument();
-        this.path = Utils.getSchemaPath(ctx);
+        this.input = firstEffective(InputEffectiveStatementImpl.class);
+        this.output = firstEffective(OutputEffectiveStatementImpl.class);
 
-        initSubstatements();
-
-    }
-
-    private void initSubstatements() {
+        // initSubstatements
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
         Set<GroupingDefinition> groupingsInit = new HashSet<>();
-        Set<TypeDefinition<?>> typeDefinitionsInit = new HashSet<>();
-
+        Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodesInit.add(unknownNode);
-            }
             if (effectiveStatement instanceof GroupingDefinition) {
                 GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
                 groupingsInit.add(groupingDefinition);
             }
-            if (effectiveStatement instanceof TypeDefinition) {
-                TypeDefinition<?> typeDefinition = (TypeDefinition<?>) effectiveStatement;
-                typeDefinitionsInit.add(typeDefinition);
-            }
-            if (this.input == null && effectiveStatement instanceof InputEffectiveStatementImpl) {
-                this.input = (InputEffectiveStatementImpl) effectiveStatement;
-            }
-            if (this.output == null && effectiveStatement instanceof OutputEffectiveStatementImpl) {
-                this.output = (OutputEffectiveStatementImpl) effectiveStatement;
+            if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
+                TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
+                TypeDefinition<?> type = typeDef.getTypeDefinition();
+                if (!mutableTypeDefinitions.contains(type)) {
+                    mutableTypeDefinitions.add(type);
+                } else {
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                }
             }
         }
-
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
         this.groupings = ImmutableSet.copyOf(groupingsInit);
-        this.typeDefinitions = ImmutableSet.copyOf(typeDefinitionsInit);
-    }
-
-    @Override
-    public QName getQName() {
-        return qname;
-    }
-
-    @Override
-    public SchemaPath getPath() {
-        return path;
+        this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
     }
 
     @Override
@@ -94,19 +61,11 @@ public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         return input;
     }
 
-    void setInput(final ContainerSchemaNode input) {
-        this.input = input;
-    }
-
     @Override
     public ContainerSchemaNode getOutput() {
         return output;
     }
 
-    void setOutput(final ContainerSchemaNode output) {
-        this.output = output;
-    }
-
     @Override
     public Set<TypeDefinition<?>> getTypeDefinitions() {
         return typeDefinitions;
@@ -117,17 +76,12 @@ public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         return groupings;
     }
 
-    @Override
-    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
-    }
-
     @Override
     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;
     }
 
@@ -143,21 +97,7 @@ public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
             return false;
         }
         final RpcEffectiveStatementImpl other = (RpcEffectiveStatementImpl) 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
@@ -165,9 +105,9 @@ public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         StringBuilder sb = new StringBuilder(RpcEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
         sb.append("qname=");
-        sb.append(qname);
+        sb.append(getQName());
         sb.append(", path=");
-        sb.append(path);
+        sb.append(getPath());
         sb.append(", input=");
         sb.append(input);
         sb.append(", output=");
@@ -175,4 +115,4 @@ public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         sb.append("]");
         return sb.toString();
     }
-}
\ No newline at end of file
+}