BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
index d8b07e64a058306faacd299505ce8c326671261c..c2e17c9b69837d079ba67955bcc9728b6efbdf24 100644 (file)
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.TreeSet;
-
-import org.opendaylight.yangtools.yang.common.QName;
+import com.google.common.collect.ImmutableList;
+import java.util.Objects;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 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.model.util.RevisionAwareXPathImpl;
-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.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.Comparators;
-import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
-import org.opendaylight.yangtools.yang.parser.util.YangParseException;
-
-public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
-        AugmentationSchemaBuilder {
-    private boolean built;
-    private final AugmentationSchemaImpl instance;
+import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
+public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
+    private final int order;
+    private AugmentationSchemaImpl instance;
     private String whenCondition;
-    private String description;
-    private String reference;
-    private Status status = Status.CURRENT;
 
     private final String augmentTargetStr;
-    private SchemaPath targetPath;
-    private SchemaPath targetNodeSchemaPath;
+    private final SchemaPath targetPath;
 
-    private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
     private boolean resolved;
+    private boolean unsupportedTarget = false;
+
+    private AugmentationSchemaBuilder copyOf;
 
-    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
+    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
+            final SchemaPath targetPath, final int order) {
         super(moduleName, line, null);
+        this.order = order;
         this.augmentTargetStr = augmentTargetStr;
-        targetPath = ParserUtils.parseXPathString(augmentTargetStr);
-        instance = new AugmentationSchemaImpl(targetPath);
-    }
-
-    @Override
-    public Set<GroupingDefinition> getGroupings() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public Set<GroupingBuilder> getGroupingBuilders() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public void addGrouping(GroupingBuilder grouping) {
-        throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
+        this.targetPath = targetPath;
     }
 
     @Override
-    public Set<UsesNodeBuilder> getUsesNodes() {
-        return usesNodes;
+    protected String getStatementName() {
+        return "augment";
     }
 
     @Override
-    public void addUsesNode(UsesNodeBuilder usesBuilder) {
-        usesNodes.add(usesBuilder);
+    public SchemaPath getPath() {
+        return targetPath;
     }
 
     @Override
-    public SchemaPath getPath() {
-        return targetNodeSchemaPath;
+    public SchemaPath getTargetPath() {
+        return targetPath;
     }
 
     @Override
     public AugmentationSchema build() {
-        if (!built) {
-            instance.setDescription(description);
-            instance.setReference(reference);
-            instance.setStatus(status);
-            instance.setTargetPath(targetNodeSchemaPath);
+        if (instance != null) {
+            return instance;
+        }
 
-            RevisionAwareXPath whenStmt;
-            if (whenCondition == null) {
-                whenStmt = null;
-            } else {
-                whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
-            }
-            instance.setWhenCondition(whenStmt);
+        buildChildren();
 
-            // CHILD NODES
-            final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
-            for (DataSchemaNodeBuilder node : addedChildNodes) {
-                childs.put(node.getQName(), node.build());
-            }
-            instance.setChildNodes(childs);
+        instance = new AugmentationSchemaImpl(targetPath, order,this);
 
-            // USES
-            final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
-            for (UsesNodeBuilder builder : usesNodes) {
-                usesNodeDefinitions.add(builder.build());
-            }
-            instance.setUses(usesNodeDefinitions);
+        Builder parent = getParent();
+        if (parent instanceof ModuleBuilder) {
+            ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
+            instance.namespace = moduleBuilder.getNamespace();
+            instance.revision = moduleBuilder.getRevision();
+        }
 
-            // UNKNOWN NODES
-            List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
-            for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
-                unknownNodes.add(b.build());
-            }
-            Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
-            instance.setUnknownSchemaNodes(unknownNodes);
+        if (copyOf != null) {
+            instance.setCopyOf(copyOf.build());
+        }
+
+        RevisionAwareXPath whenStmt;
+        if (whenCondition == null) {
+            whenStmt = null;
+        } else {
+            whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
+        }
+        instance.whenCondition = whenStmt;
 
-            built = true;
+        // UNKNOWN NODES
+        for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+            unknownNodes.add(b.build());
         }
+        instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
+
         return instance;
     }
 
@@ -140,58 +102,36 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     }
 
     @Override
-    public void setResolved(boolean resolved) {
+    public void setResolved(final boolean resolved) {
         this.resolved = resolved;
     }
 
-    public String getWhenCondition() {
-        return whenCondition;
-    }
-
-    public void addWhenCondition(String whenCondition) {
-        this.whenCondition = whenCondition;
-    }
-
-    @Override
-    public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public void addTypedef(TypeDefinitionBuilder type) {
-        throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
-    }
-
-    @Override
-    public String getDescription() {
-        return description;
-    }
-
-    @Override
-    public void setDescription(String description) {
-        this.description = description;
-    }
-
+    /**
+     *  Set true if target of augment is unsupported (e.g. node in body of extension).
+     *  In such case, augmentation is skipped and AugmentationSchema is not built.
+     */
     @Override
-    public String getReference() {
-        return reference;
+    public void setUnsupportedTarget(final boolean unsupportedTarget) {
+        this.unsupportedTarget = unsupportedTarget;
     }
 
+    /**
+     *  Return true if target of augment is unsupported (e.g. node in body of extension).
+     *  In such case, augmentation is skipped and AugmentationSchema is not built.
+     */
     @Override
-    public void setReference(String reference) {
-        this.reference = reference;
+    public boolean isUnsupportedTarget() {
+        return unsupportedTarget;
     }
 
     @Override
-    public Status getStatus() {
-        return status;
+    public String getWhenCondition() {
+        return whenCondition;
     }
 
     @Override
-    public void setStatus(Status status) {
-        if (status != null) {
-            this.status = status;
-        }
+    public void addWhenCondition(final String whenCondition) {
+        this.whenCondition = whenCondition;
     }
 
     @Override
@@ -200,32 +140,22 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     }
 
     @Override
-    public SchemaPath getTargetPath() {
-        return targetPath;
-    }
-
-    @Override
-    public SchemaPath getTargetNodeSchemaPath() {
-        return targetNodeSchemaPath;
-    }
-
-    @Override
-    public void setTargetNodeSchemaPath(SchemaPath path) {
-        this.targetNodeSchemaPath = path;
+    public int getOrder() {
+        return order;
     }
 
     @Override
     public int hashCode() {
         final int prime = 17;
         int result = 1;
-        result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
-        result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
-        result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
+        result = prime * result + Objects.hashCode(augmentTargetStr);
+        result = prime * result + Objects.hashCode(whenCondition);
+        result = prime * result + getChildNodeBuilders().hashCode();
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -250,204 +180,18 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         } else if (!whenCondition.equals(other.whenCondition)) {
             return false;
         }
-        if (childNodes == null) {
-            if (other.childNodes != null) {
-                return false;
-            }
-        } else if (!childNodes.equals(other.childNodes)) {
+        if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
             return false;
         }
         return true;
     }
 
+    @Override
     public String toString() {
         return "augment " + augmentTargetStr;
     }
 
-    private final class AugmentationSchemaImpl implements AugmentationSchema {
-        private SchemaPath targetPath;
-        private RevisionAwareXPath whenCondition;
-        private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
-        private Set<UsesNode> uses = Collections.emptySet();
-        private String description;
-        private String reference;
-        private Status status;
-        private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
-
-        private AugmentationSchemaImpl(SchemaPath targetPath) {
-            this.targetPath = targetPath;
-        }
-
-        @Override
-        public SchemaPath getTargetPath() {
-            return targetPath;
-        }
-
-        private void setTargetPath(SchemaPath path) {
-            this.targetPath = path;
-        }
-
-        @Override
-        public RevisionAwareXPath getWhenCondition() {
-            return whenCondition;
-        }
-
-        private void setWhenCondition(RevisionAwareXPath whenCondition) {
-            this.whenCondition = whenCondition;
-        }
-
-        @Override
-        public Set<DataSchemaNode> getChildNodes() {
-            final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
-            result.addAll(childNodes.values());
-            return result;
-        }
-
-        private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
-            if (childNodes != null) {
-                this.childNodes = childNodes;
-            }
-        }
-
-        /**
-         * Always returns an empty set, because augment can not contains
-         * grouping statement.
-         */
-        @Override
-        public Set<GroupingDefinition> getGroupings() {
-            return Collections.emptySet();
-        }
-
-        @Override
-        public Set<UsesNode> getUses() {
-            return uses;
-        }
-
-        private void setUses(Set<UsesNode> uses) {
-            if (uses != null) {
-                this.uses = uses;
-            }
-        }
-
-        /**
-         * Always returns an empty set, because augment can not contains type
-         * definitions.
-         */
-        @Override
-        public Set<TypeDefinition<?>> getTypeDefinitions() {
-            return Collections.emptySet();
-        }
-
-        @Override
-        public String getDescription() {
-            return description;
-        }
-
-        private void setDescription(String description) {
-            this.description = description;
-        }
-
-        @Override
-        public String getReference() {
-            return reference;
-        }
-
-        private void setReference(String reference) {
-            this.reference = reference;
-        }
-
-        @Override
-        public Status getStatus() {
-            return status;
-        }
-
-        private void setStatus(Status status) {
-            this.status = status;
-        }
-
-        @Override
-        public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-            return unknownNodes;
-        }
-
-        private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
-            if (unknownSchemaNodes != null) {
-                this.unknownNodes = unknownSchemaNodes;
-            }
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(QName name) {
-            return childNodes.get(name);
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(String name) {
-            DataSchemaNode result = null;
-            for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
-                if (entry.getKey().getLocalName().equals(name)) {
-                    result = entry.getValue();
-                    break;
-                }
-            }
-            return result;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 17;
-            int result = 1;
-            result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
-            result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
-            result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
-            if (targetPath == null) {
-                if (other.targetPath != null) {
-                    return false;
-                }
-            } else if (!targetPath.equals(other.targetPath)) {
-                return false;
-            }
-            if (whenCondition == null) {
-                if (other.whenCondition != null) {
-                    return false;
-                }
-            } else if (!whenCondition.equals(other.whenCondition)) {
-                return false;
-            }
-            if (childNodes == null) {
-                if (other.childNodes != null) {
-                    return false;
-                }
-            } else if (!childNodes.equals(other.childNodes)) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
-            sb.append("[");
-            sb.append("targetPath=" + targetPath);
-            sb.append(", when=" + whenCondition);
-            sb.append("]");
-            return sb.toString();
-        }
+    public void setCopyOf(final AugmentationSchemaBuilder old) {
+        copyOf = old;
     }
-
 }