BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ListSchemaNodeBuilder.java
index 79cdf048e9a8e9222da5f7a6cb98907c52a1cd07..dbd0951cde02edbe761f0e420261daad2b1d3788 100644 (file)
@@ -7,51 +7,45 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
-import java.net.URI;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.Status;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
+import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-
-public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
-        AugmentationTargetBuilder {
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
+public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
+        DataSchemaNodeBuilder, AugmentationTargetBuilder {
     private ListSchemaNodeImpl instance;
     private boolean userOrdered;
-    private List<String> keys;
+    private Set<String> keys;
     private List<QName> keyDefinition;
     // SchemaNode args
     private SchemaPath schemaPath;
-    private String description;
-    private String reference;
-    private Status status = Status.CURRENT;
     // DataSchemaNode args
     private boolean augmenting;
     private boolean addedByUses;
+    private ListSchemaNodeBuilder originalBuilder;
+    private ListSchemaNode originalNode;
     private boolean configuration;
     private final ConstraintsBuilder constraints;
     // AugmentationTarget args
@@ -60,37 +54,27 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
 
     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
         super(moduleName, line, qname);
-        this.schemaPath = path;
-        constraints = new ConstraintsBuilder(moduleName, line);
+        this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
+        constraints = new ConstraintsBuilderImpl(moduleName, line);
     }
 
     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
             final ListSchemaNode base) {
-        super(moduleName, line, qname);
-        schemaPath = path;
-        constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
+        super(moduleName, line, qname, path, base);
+        this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
+        constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
 
         keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
         userOrdered = base.isUserOrdered();
 
-        description = base.getDescription();
-        reference = base.getReference();
-        status = base.getStatus();
         augmenting = base.isAugmenting();
         addedByUses = base.isAddedByUses();
+        originalNode = base;
         configuration = base.isConfiguration();
 
-        URI ns = qname.getNamespace();
-        Date rev = qname.getRevision();
-        String pref = qname.getPrefix();
-        addedChildNodes.addAll(ParserUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
-        addedGroupings.addAll(ParserUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
-        addedTypedefs.addAll(ParserUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
-        addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
-                rev, pref));
-
+        addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
+                qname));
         augmentations.addAll(base.getAvailableAugmentations());
-        usesNodes.addAll(base.getUses());
     }
 
     @Override
@@ -98,52 +82,45 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
         if (instance != null) {
             return instance;
         }
+        buildChildren();
+        instance = new ListSchemaNodeImpl(qname, schemaPath, this);
 
-        instance = new ListSchemaNodeImpl(qname, schemaPath);
-
-        instance.description = description;
-        instance.reference = reference;
-        instance.status = status;
         instance.augmenting = augmenting;
         instance.addedByUses = addedByUses;
         instance.configuration = configuration;
         instance.constraints = constraints.build();
         instance.userOrdered = userOrdered;
 
-        // CHILD NODES
-        for (DataSchemaNodeBuilder node : addedChildNodes) {
-            childNodes.add(node.build());
-        }
-        instance.childNodes = ImmutableSet.copyOf(childNodes);
-
         // KEY
-        if (keys == null) {
-            instance.keyDefinition = ImmutableList.of();
-        } else {
+        if (keys != null) {
             keyDefinition = new ArrayList<>();
             for (String key : keys) {
-                keyDefinition.add(instance.getDataChildByName(key).getQName());
-            }
-            instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
-        }
+                DataSchemaNode keyPart = instance.getDataChildByName(key);
+                if (keyPart == null) {
+                    throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
+                            + key);
+                }
 
-        // GROUPINGS
-        for (GroupingBuilder builder : addedGroupings) {
-            groupings.add(builder.build());
-        }
-        instance.groupings = ImmutableSet.copyOf(groupings);
+                if (!(keyPart instanceof LeafSchemaNode)) {
+                    throw new YangParseException(getModuleName(), getLine(), "List key : \"" + key
+                            + "\" does not reference any Leaf of the List");
+                }
 
-        // TYPEDEFS
-        for (TypeDefinitionBuilder entry : addedTypedefs) {
-            typedefs.add(entry.build());
+                final QName qname = keyPart.getQName();
+                if (!keyDefinition.contains(qname)) {
+                    keyDefinition.add(qname);
+                }
+            }
+            instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
+        } else {
+            instance.keyDefinition = ImmutableList.of();
         }
-        instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
 
-        // USES
-        for (UsesNodeBuilder builder : addedUsesNodes) {
-            usesNodes.add(builder.build());
+        // ORIGINAL NODE
+        if (originalNode == null && originalBuilder != null) {
+            originalNode = originalBuilder.build();
         }
-        instance.uses = ImmutableSet.copyOf(usesNodes);
+        instance.original = originalNode;
 
         // AUGMENTATIONS
         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
@@ -161,20 +138,8 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
     }
 
     @Override
-    public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
-        return addedTypedefs;
-    }
-
-    @Override
-    public void addTypedef(final TypeDefinitionBuilder type) {
-        String typeName = type.getQName().getLocalName();
-        for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
-            if (addedTypedef.getQName().getLocalName().equals(typeName)) {
-                throw new YangParseException(getModuleName(), type.getLine(), "Can not add typedef '" + typeName
-                        + "': typedef with same name already declared at line " + addedTypedef.getLine());
-            }
-        }
-        addedTypedefs.add(type);
+    protected String getStatementName() {
+        return "list";
     }
 
     @Override
@@ -183,40 +148,10 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
     }
 
     @Override
-    public void setPath(SchemaPath path) {
+    public void setPath(final SchemaPath path) {
         this.schemaPath = path;
     }
 
-    @Override
-    public String getDescription() {
-        return description;
-    }
-
-    @Override
-    public void setDescription(final String description) {
-        this.description = description;
-    }
-
-    @Override
-    public String getReference() {
-        return reference;
-    }
-
-    @Override
-    public void setReference(final String reference) {
-        this.reference = reference;
-    }
-
-    @Override
-    public Status getStatus() {
-        return status;
-    }
-
-    @Override
-    public void setStatus(Status status) {
-        this.status = Preconditions.checkNotNull(status, "status cannot be null");
-    }
-
     @Override
     public void addAugmentation(final AugmentationSchemaBuilder augment) {
         augmentationBuilders.add(augment);
@@ -226,11 +161,11 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
         return augmentationBuilders;
     }
 
-    public List<String> getKeys() {
+    public Set<String> getKeys() {
         return keys;
     }
 
-    public void setKeys(final List<String> keys) {
+    public void setKeys(final Set<String> keys) {
         this.keys = keys;
     }
 
@@ -240,7 +175,7 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
     }
 
     @Override
-    public void setAugmenting(boolean augmenting) {
+    public void setAugmenting(final boolean augmenting) {
         this.augmenting = augmenting;
     }
 
@@ -254,13 +189,24 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
         this.addedByUses = addedByUses;
     }
 
+    @Override
+    public ListSchemaNodeBuilder getOriginal() {
+        return originalBuilder;
+    }
+
+    @Override
+    public void setOriginal(final SchemaNodeBuilder builder) {
+        Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
+        this.originalBuilder = (ListSchemaNodeBuilder) builder;
+    }
+
     @Override
     public boolean isConfiguration() {
         return configuration;
     }
 
     @Override
-    public void setConfiguration(boolean configuration) {
+    public void setConfiguration(final boolean configuration) {
         this.configuration = configuration;
     }
 
@@ -281,7 +227,7 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
+        result = prime * result + Objects.hashCode(schemaPath);
         return result;
     }
 
@@ -319,167 +265,4 @@ public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilde
         return "list " + qname.getLocalName();
     }
 
-    private static final class ListSchemaNodeImpl implements ListSchemaNode {
-        private final QName qname;
-        private final SchemaPath path;
-        private String description;
-        private String reference;
-        private Status status;
-        private ImmutableList<QName> keyDefinition;
-        private boolean augmenting;
-        private boolean addedByUses;
-        private boolean configuration;
-        private ConstraintDefinition constraints;
-        private ImmutableSet<AugmentationSchema> augmentations;
-        private ImmutableSet<DataSchemaNode> childNodes;
-        private ImmutableSet<GroupingDefinition> groupings;
-        private ImmutableSet<TypeDefinition<?>> typeDefinitions;
-        private ImmutableSet<UsesNode> uses;
-        private ImmutableList<UnknownSchemaNode> unknownNodes;
-        private boolean userOrdered;
-
-        private ListSchemaNodeImpl(final QName qname, final SchemaPath path) {
-            this.qname = qname;
-            this.path = path;
-        }
-
-        @Override
-        public QName getQName() {
-            return qname;
-        }
-
-        @Override
-        public SchemaPath getPath() {
-            return path;
-        }
-
-        @Override
-        public String getDescription() {
-            return description;
-        }
-
-        @Override
-        public String getReference() {
-            return reference;
-        }
-
-        @Override
-        public Status getStatus() {
-            return status;
-        }
-
-        @Override
-        public List<QName> getKeyDefinition() {
-            return keyDefinition;
-        }
-
-        @Override
-        public boolean isAugmenting() {
-            return augmenting;
-        }
-
-        @Override
-        public boolean isAddedByUses() {
-            return addedByUses;
-        }
-
-        @Override
-        public boolean isConfiguration() {
-            return configuration;
-        }
-
-        @Override
-        public ConstraintDefinition getConstraints() {
-            return constraints;
-        }
-
-        @Override
-        public Set<AugmentationSchema> getAvailableAugmentations() {
-            return augmentations;
-        }
-
-        @Override
-        public Set<DataSchemaNode> getChildNodes() {
-            return childNodes;
-        }
-
-        @Override
-        public Set<GroupingDefinition> getGroupings() {
-            return groupings;
-        }
-
-        @Override
-        public Set<TypeDefinition<?>> getTypeDefinitions() {
-            return typeDefinitions;
-        }
-
-        @Override
-        public Set<UsesNode> getUses() {
-            return uses;
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(final QName name) {
-            return getChildNode(childNodes, name);
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(final String name) {
-            return getChildNode(childNodes, name);
-        }
-
-        @Override
-        public boolean isUserOrdered() {
-            return userOrdered;
-        }
-
-        @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());
-            return result;
-        }
-
-        @Override
-        public boolean equals(final Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            final ListSchemaNodeImpl other = (ListSchemaNodeImpl) 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;
-        }
-
-        @Override
-        public String toString() {
-            return "list " + qname.getLocalName();
-        }
-    }
-
 }