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.DataSchemaNodeBuilder;
+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 final class ChoiceBuilder implements DataSchemaNodeBuilder, 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 List<UnknownSchemaNode> unknownNodes;
- 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;
// AugmentationTarget args
private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
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() {
if (!isBuilt) {
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 HashSet<ChoiceCaseNode>();
+ if (cases == null) {
+ cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
for (ChoiceCaseBuilder caseBuilder : addedCases) {
cases.add(caseBuilder.build());
}
instance.setAvailableAugmentations(augmentations);
// UNKNOWN NODES
- if(unknownNodes == null) {
+ 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);
build();
}
- @Override
- public int getLine() {
- return line;
- }
-
public Set<ChoiceCaseBuilder> getCases() {
return addedCases;
}
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);
addedCases.add(caseBuilder);
} else {
}
@Override
- public QName getQName() {
- return qname;
- }
-
- 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 addedUnknownNodes;
}
- @Override
- public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
- addedUnknownNodes.add(unknownNode);
- }
-
- public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
- this.unknownNodes = unknownNodes;
- }
-
public String getDefaultCase() {
return defaultCase;
}
this.defaultCase = defaultCase;
}
+ @Override
+ 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;
+ }
+
+ @Override
+ public String toString() {
+ return "choice " + qname.getLocalName();
+ }
+
public final class ChoiceNodeImpl implements ChoiceNode {
private final QName qname;
private SchemaPath path;
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 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;