import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.TreeSet;
import org.opendaylight.controller.yang.common.QName;
import org.opendaylight.controller.yang.model.api.AugmentationSchema;
import org.opendaylight.controller.yang.model.api.SchemaPath;
import org.opendaylight.controller.yang.model.api.Status;
import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
-import org.opendaylight.controller.yang.parser.builder.api.ChildNodeBuilder;
import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
-import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
+import org.opendaylight.controller.yang.parser.util.Comparators;
+import org.opendaylight.controller.yang.parser.util.ParserUtils;
-public class ChoiceBuilder implements DataSchemaNodeBuilder, ChildNodeBuilder,
- AugmentationTargetBuilder {
+public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
+ AugmentationTargetBuilder, GroupingMember {
+ private boolean isBuilt;
private final ChoiceNodeImpl instance;
- private final int line;
- // SchemaNode args
- private final QName qname;
- private SchemaPath schemaPath;
- private String description;
- private String reference;
- private Status status = Status.CURRENT;
- private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
// DataSchemaNode args
private boolean augmenting;
- private boolean configuration;
+ private boolean addedByUses;
+ private Boolean configuration;
private final ConstraintsBuilder constraints;
- // DataNodeContainer args
- private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
- private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
// AugmentationTarget args
private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
// ChoiceNode args
- private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
+ private Set<ChoiceCaseNode> cases;
+ private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
private String defaultCase;
- public ChoiceBuilder(final QName qname, final int line) {
- this.qname = qname;
- this.line = line;
+ public ChoiceBuilder(final int line, final QName qname) {
+ super(line, qname);
instance = new ChoiceNodeImpl(qname);
constraints = new ConstraintsBuilder(line);
}
+ public ChoiceBuilder(ChoiceBuilder b) {
+ super(b.getLine(), b.getQName());
+ parent = b.getParent();
+ instance = new ChoiceNodeImpl(qname);
+ constraints = b.getConstraints();
+ schemaPath = b.getPath();
+ description = b.getDescription();
+ reference = b.getReference();
+ status = b.getStatus();
+ unknownNodes = b.unknownNodes;
+ addedUnknownNodes.addAll(b.getUnknownNodes());
+ augmenting = b.isAugmenting();
+ addedByUses = b.isAddedByUses();
+ configuration = b.isConfiguration();
+ addedAugmentations.addAll(b.getAugmentations());
+ cases = b.cases;
+ addedCases.addAll(b.getCases());
+ defaultCase = b.getDefaultCase();
+ }
+
@Override
public ChoiceNode build() {
- instance.setPath(schemaPath);
- instance.setDescription(description);
- instance.setReference(reference);
- instance.setStatus(status);
- instance.setAugmenting(augmenting);
- instance.setConfiguration(configuration);
- instance.setConstraints(constraints.build());
- instance.setDefaultCase(defaultCase);
+ if (!isBuilt) {
+ instance.setPath(schemaPath);
+ instance.setDescription(description);
+ instance.setReference(reference);
+ instance.setStatus(status);
+ instance.setAugmenting(augmenting);
+ instance.setAddedByUses(addedByUses);
+ instance.setConfiguration(configuration);
+ instance.setConstraints(constraints.build());
+ instance.setDefaultCase(defaultCase);
+
+ // CASES
+ if (cases == null) {
+ cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
+ for (ChoiceCaseBuilder caseBuilder : addedCases) {
+ cases.add(caseBuilder.build());
+ }
+ }
+ instance.setCases(cases);
- // CASES
- final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
- for (ChoiceCaseBuilder caseBuilder : cases) {
- choiceCases.add(caseBuilder.build());
- }
- instance.setCases(choiceCases);
+ // AUGMENTATIONS
+ final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
+ for (AugmentationSchemaBuilder builder : addedAugmentations) {
+ augmentations.add(builder.build());
+ }
+ instance.setAvailableAugmentations(augmentations);
- // AUGMENTATIONS
- final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
- for (AugmentationSchemaBuilder builder : addedAugmentations) {
- augmentations.add(builder.build());
- }
- instance.setAvailableAugmentations(augmentations);
+ // UNKNOWN NODES
+ if (unknownNodes == null) {
+ unknownNodes = new ArrayList<UnknownSchemaNode>();
+ for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+ unknownNodes.add(b.build());
+ }
+ Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
+ }
+ instance.setUnknownSchemaNodes(unknownNodes);
- // UNKNOWN NODES
- final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
- for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
- unknownNodes.add(b.build());
+ isBuilt = true;
}
- instance.setUnknownSchemaNodes(unknownNodes);
-
return instance;
}
@Override
- public int getLine() {
- return line;
+ public void rebuild() {
+ isBuilt = false;
+ build();
}
public Set<ChoiceCaseBuilder> getCases() {
- return cases;
+ return addedCases;
}
- @Override
public void addChildNode(DataSchemaNodeBuilder childNode) {
if (!(childNode instanceof ChoiceCaseBuilder)) {
- ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(
- childNode.getQName(), childNode.getLine());
+ ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getLine(), childNode.getQName());
+ if (childNode.isAugmenting()) {
+ caseBuilder.setAugmenting(true);
+ childNode.setAugmenting(false);
+ }
+ caseBuilder.setPath(childNode.getPath());
+ SchemaPath newPath = ParserUtils.createSchemaPath(childNode.getPath(), childNode.getQName().getLocalName());
+ childNode.setPath(newPath);
caseBuilder.addChildNode(childNode);
- cases.add(caseBuilder);
+ addedCases.add(caseBuilder);
} else {
- cases.add((ChoiceCaseBuilder) childNode);
+ addedCases.add((ChoiceCaseBuilder) childNode);
}
}
- @Override
- public QName getQName() {
- return qname;
- }
-
- /**
- * Choice can not contains grouping statements, so this method always
- * returns an empty set.
- *
- * @return
- */
- public Set<GroupingBuilder> getGroupings() {
- return Collections.emptySet();
- }
-
- @Override
- public void addGrouping(GroupingBuilder groupingBuilder) {
- throw new IllegalStateException(
- "Can not add grouping to 'choice' node.");
- }
-
- public Set<TypeDefinitionBuilder> getTypedefs() {
- return addedTypedefs;
+ public void setCases(Set<ChoiceCaseNode> cases) {
+ this.cases = cases;
}
@Override
- public void addTypedef(final TypeDefinitionBuilder type) {
- addedTypedefs.add(type);
- }
-
- public SchemaPath getPath() {
- return schemaPath;
- }
-
- @Override
- public void setPath(final SchemaPath schemaPath) {
- this.schemaPath = schemaPath;
- }
-
- public String getDescription() {
- return description;
- }
-
- @Override
- public void setDescription(final String description) {
- this.description = description;
- }
-
- public String getReference() {
- return reference;
+ public boolean isAugmenting() {
+ return augmenting;
}
@Override
- public void setReference(String reference) {
- this.reference = reference;
- }
-
- public Status getStatus() {
- return status;
+ public void setAugmenting(boolean augmenting) {
+ this.augmenting = augmenting;
}
@Override
- public void setStatus(Status status) {
- if (status != null) {
- this.status = status;
- }
- }
-
- public boolean isAugmenting() {
- return augmenting;
+ public boolean isAddedByUses() {
+ return addedByUses;
}
@Override
- public void setAugmenting(boolean augmenting) {
- this.augmenting = augmenting;
+ public void setAddedByUses(final boolean addedByUses) {
+ this.addedByUses = addedByUses;
}
- public boolean isConfiguration() {
+ public Boolean isConfiguration() {
return configuration;
}
@Override
- public void setConfiguration(boolean configuration) {
+ public void setConfiguration(Boolean configuration) {
this.configuration = configuration;
}
return constraints;
}
- public Set<UsesNodeBuilder> getUsesNodes() {
- return addedUsesNodes;
- }
-
- @Override
- public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
- addedUsesNodes.add(usesNodeBuilder);
- }
-
- public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
- return addedUnknownNodes;
- }
-
public Set<AugmentationSchemaBuilder> getAugmentations() {
return addedAugmentations;
}
addedAugmentations.add(augment);
}
- @Override
- public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
- addedUnknownNodes.add(unknownNode);
+ public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
+ return addedUnknownNodes;
}
public String getDefaultCase() {
}
@Override
- public Set<DataSchemaNodeBuilder> getChildNodes() {
- return new HashSet<DataSchemaNodeBuilder>(cases);
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((schemaPath == null) ? 0 : schemaPath.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;
+ }
+ ChoiceBuilder other = (ChoiceBuilder) obj;
+ if (schemaPath == null) {
+ if (other.schemaPath != null) {
+ return false;
+ }
+ } else if (!schemaPath.equals(other.schemaPath)) {
+ return false;
+ }
+ if (parent == null) {
+ if (other.parent != null) {
+ return false;
+ }
+ } else if (!parent.equals(other.parent)) {
+ return false;
+ }
+ return true;
}
- private static class ChoiceNodeImpl implements ChoiceNode {
+ @Override
+ public String toString() {
+ return "choice " + qname.getLocalName();
+ }
+
+ public final class ChoiceNodeImpl implements ChoiceNode {
private final QName qname;
private SchemaPath path;
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<ChoiceCaseNode> cases = Collections.emptySet();
this.augmenting = augmenting;
}
+ @Override
+ public boolean isAddedByUses() {
+ return addedByUses;
+ }
+
+ private void setAddedByUses(boolean addedByUses) {
+ this.addedByUses = addedByUses;
+ }
+
@Override
public boolean isConfiguration() {
return configuration;
return augmentations;
}
- private void setAvailableAugmentations(
- Set<AugmentationSchema> availableAugmentations) {
+ private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
if (availableAugmentations != null) {
this.augmentations = availableAugmentations;
}
return unknownNodes;
}
- private void setUnknownSchemaNodes(
- List<UnknownSchemaNode> unknownSchemaNodes) {
+ private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
if (unknownSchemaNodes != null) {
this.unknownNodes = unknownSchemaNodes;
}
return cases;
}
+ @Override
+ public ChoiceCaseNode getCaseNodeByName(final QName name) {
+ if (name == null) {
+ throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
+ }
+ for (final ChoiceCaseNode caseNode : cases) {
+ if (caseNode != null) {
+ if (name.equals(caseNode.getQName())) {
+ return caseNode;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public ChoiceCaseNode getCaseNodeByName(final String name) {
+ if (name == null) {
+ throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
+ }
+ for (final ChoiceCaseNode caseNode : cases) {
+ if (caseNode != null && (caseNode.getQName() != null)) {
+ if (name.equals(caseNode.getQName().getLocalName())) {
+ return caseNode;
+ }
+ }
+ }
+ return null;
+ }
+
private void setCases(Set<ChoiceCaseNode> cases) {
if (cases != null) {
this.cases = cases;
}
}
+ @Override
public String getDefaultCase() {
return defaultCase;
}
this.defaultCase = defaultCase;
}
+ public ChoiceBuilder toBuilder() {
+ return ChoiceBuilder.this;
+ }
+
@Override
public int hashCode() {
final int prime = 31;
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(
- ChoiceNodeImpl.class.getSimpleName());
+ StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
sb.append("[");
sb.append("qname=" + qname);
sb.append("]");