*/
package org.opendaylight.yangtools.yang.parser.builder.impl;
+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.Collections;
-import java.util.HashSet;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
+import java.util.Objects;
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.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
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.api.YangNode;
-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.Comparators;
-import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+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;
-public final class ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
+public final class ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
AugmentationTargetBuilder, DataSchemaNodeBuilder {
- private boolean isBuilt;
- private final ContainerSchemaNodeImpl instance;
- private YangNode parent;
-
+ private ContainerSchemaNodeImpl instance;
+ private boolean presence;
// SchemaNode args
- private SchemaPath schemaPath;
- private String description;
- private String reference;
- private Status status = Status.CURRENT;
+ private SchemaPath path;
// DataSchemaNode args
private boolean augmenting;
private boolean addedByUses;
- private Boolean configuration;
+ private boolean configuration;
+ private ContainerSchemaNode originalNode;
+ private ContainerSchemaNodeBuilder originalBuilder;
private final ConstraintsBuilder constraints;
// AugmentationTarget args
private final List<AugmentationSchema> augmentations = new ArrayList<>();
private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
- // ContainerSchemaNode args
- private boolean presence;
- public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
- final SchemaPath schemaPath) {
+ public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
super(moduleName, line, qname);
- this.schemaPath = schemaPath;
- instance = new ContainerSchemaNodeImpl(qname);
- constraints = new ConstraintsBuilder(moduleName, line);
+ this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
+ this.constraints = new ConstraintsBuilderImpl(moduleName, line);
}
- @Override
- public ContainerSchemaNode build(YangNode parent) {
- if (!isBuilt) {
- this.parent = parent;
- instance.setParent(parent);
- instance.setPath(schemaPath);
- instance.setDescription(description);
- instance.setReference(reference);
- instance.setStatus(status);
- instance.setPresenceContainer(presence);
- instance.setAugmenting(augmenting);
- instance.setAddedByUses(addedByUses);
-
- // if this builder represents rpc input or output, it can has
- // configuration value set to null
- if (configuration == null) {
- configuration = false;
- }
- instance.setConfiguration(configuration);
-
- // USES
- for (UsesNodeBuilder builder : addedUsesNodes) {
- usesNodes.add(builder.build(instance));
- }
- instance.setUses(usesNodes);
-
- // CHILD NODES
- for (DataSchemaNodeBuilder node : addedChildNodes) {
- DataSchemaNode child = node.build(instance);
- childNodes.put(child.getQName(), node.build(instance));
- }
- instance.setChildNodes(childNodes);
-
- // GROUPINGS
- for (GroupingBuilder builder : addedGroupings) {
- groupings.add(builder.build(instance));
- }
- instance.setGroupings(groupings);
-
- // TYPEDEFS
- for (TypeDefinitionBuilder entry : addedTypedefs) {
- typedefs.add(entry.build(instance));
- }
- instance.setTypeDefinitions(typedefs);
+ // constructor for uses
+ public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
+ final SchemaPath path, final ContainerSchemaNode base) {
+ super(moduleName, line, qname, path, base);
+ this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
- // AUGMENTATIONS
- for (AugmentationSchemaBuilder builder : augmentationBuilders) {
- augmentations.add(builder.build(instance));
- }
- instance.setAvailableAugmentations(new HashSet<>(augmentations));
+ constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
- // UNKNOWN NODES
- for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
- unknownNodes.add(b.build(instance));
- }
- Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
- instance.setUnknownSchemaNodes(unknownNodes);
+ augmenting = base.isAugmenting();
+ addedByUses = base.isAddedByUses();
+ originalNode = base;
+ configuration = base.isConfiguration();
+ presence = base.isPresenceContainer();
- instance.setConstraints(constraints.build());
+ augmentations.addAll(base.getAvailableAugmentations());
- isBuilt = true;
- }
- return instance;
}
@Override
- public void rebuild() {
- isBuilt = false;
- build(parent);
+ protected String getStatementName() {
+ return "container";
}
@Override
- public void setQName(QName qname) {
- this.qname = qname;
- instance.setQName(qname);
- }
+ public ContainerSchemaNode build() {
+ if (instance != null) {
+ return instance;
+ }
- @Override
- public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
- return addedTypedefs;
- }
+ buildChildren();
+ instance = new ContainerSchemaNodeImpl(this);
- @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(moduleName, type.getLine(), "Can not add typedef '" + typeName
- + "': typedef with same name already declared at line " + addedTypedef.getLine());
- }
+ instance.augmenting = augmenting;
+ instance.addedByUses = addedByUses;
+ instance.configuration = configuration;
+ instance.constraints = constraints.build();
+ instance.presence = presence;
+
+ // ORIGINAL NODE
+ if (originalNode == null && originalBuilder != null) {
+ originalNode = originalBuilder.build();
}
- addedTypedefs.add(type);
- }
+ instance.original = originalNode;
- public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
- this.typedefs = typedefs;
- }
+ // AUGMENTATIONS
+ for (AugmentationSchemaBuilder builder : augmentationBuilders) {
+ augmentations.add(builder.build());
+ }
+ instance.augmentations = ImmutableSet.copyOf(augmentations);
- public List<AugmentationSchema> getAugmentations() {
- return augmentations;
+ // UNKNOWN NODES
+ for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+ unknownNodes.add(b.build());
+ }
+ instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
+
+ return instance;
}
public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
}
@Override
- public void addAugmentation(AugmentationSchemaBuilder augment) {
+ public void addAugmentation(final AugmentationSchemaBuilder augment) {
augmentationBuilders.add(augment);
}
- public SchemaPath getPath() {
- return schemaPath;
- }
-
@Override
- public void setPath(final SchemaPath schemaPath) {
- this.schemaPath = schemaPath;
- }
-
- @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(String reference) {
- this.reference = reference;
- }
-
- @Override
- public Status getStatus() {
- return status;
+ public SchemaPath getPath() {
+ return path;
}
@Override
- public void setStatus(Status status) {
- if (status != null) {
- this.status = status;
- }
+ public void setPath(final SchemaPath path) {
+ this.path = path;
}
@Override
}
@Override
- public void setAugmenting(boolean augmenting) {
+ public void setAugmenting(final boolean augmenting) {
this.augmenting = augmenting;
}
}
@Override
- public Boolean isConfiguration() {
+ public ContainerSchemaNodeBuilder getOriginal() {
+ return originalBuilder;
+ }
+
+ @Override
+ public void setOriginal(final SchemaNodeBuilder builder) {
+ Preconditions.checkArgument(builder instanceof ContainerSchemaNodeBuilder, "Original of container cannot be "
+ + builder);
+ this.originalBuilder = (ContainerSchemaNodeBuilder) builder;
+ }
+
+ @Override
+ public boolean isConfiguration() {
return configuration;
}
@Override
- public void setConfiguration(Boolean configuration) {
+ public void setConfiguration(final boolean configuration) {
this.configuration = configuration;
}
return presence;
}
- public void setPresence(boolean presence) {
+ public void setPresence(final boolean presence) {
this.presence = presence;
}
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
+ result = prime * result + Objects.hashCode(path);
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
return false;
}
ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
- if (schemaPath == null) {
- if (other.schemaPath != null) {
+ if (path == null) {
+ if (other.path != null) {
return false;
}
- } else if (!schemaPath.equals(other.schemaPath)) {
+ } else if (!path.equals(other.path)) {
return false;
}
- if (parent == null) {
- if (other.parent != null) {
+ if (getParent() == null) {
+ if (other.getParent() != null) {
return false;
}
- } else if (!parent.equals(other.parent)) {
+ } else if (!getParent().equals(other.getParent())) {
return false;
}
return true;
return "container " + qname.getLocalName();
}
- public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
- private QName qname;
- private SchemaPath path;
- private YangNode parent;
- private String description;
- private String reference;
- private Status status = Status.CURRENT;
- private boolean augmenting;
- private boolean addedByUses;
- private boolean configuration;
- private ConstraintDefinition constraints;
- private Set<AugmentationSchema> augmentations = Collections.emptySet();
- private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
- private Set<GroupingDefinition> groupings = Collections.emptySet();
- private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
- private Set<UsesNode> uses = Collections.emptySet();
- private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
- private boolean presence;
-
- private ContainerSchemaNodeImpl(QName qname) {
- this.qname = qname;
- }
-
- @Override
- public QName getQName() {
- return qname;
- }
-
- private void setQName(QName qname) {
- this.qname = qname;
- }
-
- @Override
- public SchemaPath getPath() {
- return path;
- }
-
- private void setPath(SchemaPath path) {
- this.path = path;
- }
-
- @Override
- public YangNode getParent() {
- return parent;
- }
-
- private void setParent(YangNode parent) {
- this.parent = parent;
- }
-
- @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) {
- if (status != null) {
- this.status = status;
- }
- }
-
- @Override
- public boolean isAugmenting() {
- return augmenting;
- }
-
- private void setAugmenting(boolean augmenting) {
- this.augmenting = augmenting;
- }
-
- @Override
- public boolean isAddedByUses() {
- return addedByUses;
- }
-
- private void setAddedByUses(boolean addedByUses) {
- this.addedByUses = addedByUses;
- }
-
- @Override
- public boolean isConfiguration() {
- return configuration;
- }
-
- private void setConfiguration(boolean configuration) {
- this.configuration = configuration;
- }
-
- @Override
- public ConstraintDefinition getConstraints() {
- return constraints;
- }
-
- private void setConstraints(ConstraintDefinition constraints) {
- this.constraints = constraints;
- }
-
- @Override
- public Set<AugmentationSchema> getAvailableAugmentations() {
- return augmentations;
- }
-
- private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
- if (augmentations != null) {
- this.augmentations = augmentations;
- }
- }
-
- @Override
- public Set<DataSchemaNode> getChildNodes() {
- return new HashSet<>(childNodes.values());
- }
-
- private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
- if (childNodes != null) {
- this.childNodes = childNodes;
- }
- }
-
- @Override
- public Set<GroupingDefinition> getGroupings() {
- return groupings;
- }
-
- private void setGroupings(Set<GroupingDefinition> groupings) {
- if (groupings != null) {
- this.groupings = groupings;
- }
- }
-
- @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 Set<UsesNode> getUses() {
- return uses;
- }
-
- private void setUses(Set<UsesNode> uses) {
- if (uses != null) {
- this.uses = uses;
- }
- }
-
- @Override
- public boolean isPresenceContainer() {
- return presence;
- }
-
- private void setPresenceContainer(boolean presence) {
- this.presence = presence;
- }
-
- @Override
- public Set<TypeDefinition<?>> getTypeDefinitions() {
- return typeDefinitions;
- }
-
- private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
- if (typeDefinitions != null) {
- this.typeDefinitions = typeDefinitions;
- }
- }
-
- @Override
- public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return unknownNodes;
- }
-
- private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
- if (unknownSchemaNodes != null) {
- this.unknownNodes = unknownSchemaNodes;
- }
- }
-
- public ContainerSchemaNodeBuilder toBuilder() {
- return ContainerSchemaNodeBuilder.this;
- }
-
- @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(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) 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() {
- StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
- sb.append("[");
- sb.append("qname=");
- sb.append(qname);
- sb.append("]");
- return sb.toString();
- }
- }
-
}