*/
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.Set;
-import java.util.TreeSet;
+import static com.google.common.base.Preconditions.checkNotNull;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import java.net.URI;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
import org.opendaylight.yangtools.yang.common.QName;
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.NamespaceRevisionAware;
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.AbstractDocumentedDataNodeContainer;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
+
+public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
+ private final int order;
+ private AugmentationSchemaImpl instance;
private String whenCondition;
private final String augmentTargetStr;
- private SchemaPath targetPath;
- private SchemaPath targetNodeSchemaPath;
+ private final SchemaPath targetPath;
private boolean resolved;
+ 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);
+ this.targetPath = targetPath;
}
@Override
- public Set<GroupingDefinition> getGroupings() {
- return Collections.emptySet();
+ protected String getStatementName() {
+ return "augment";
}
@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");
+ public SchemaPath getPath() {
+ return targetPath;
}
@Override
- public SchemaPath getPath() {
- return targetNodeSchemaPath;
+ public SchemaPath getTargetPath() {
+ return targetPath;
}
@Override
public AugmentationSchema build() {
- if (!built) {
- 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
- for (DataSchemaNodeBuilder node : addedChildNodes) {
- DataSchemaNode child = node.build();
- childNodes.add(child);
- }
- instance.addChildNodes(childNodes);
+ instance = new AugmentationSchemaImpl(targetPath, order,this);
- // USES
- for (UsesNodeBuilder builder : addedUsesNodes) {
- usesNodes.add(builder.build());
- }
- instance.addUses(usesNodes);
+ Builder parent = getParent();
+ if (parent instanceof ModuleBuilder) {
+ ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
+ instance.namespace = moduleBuilder.getNamespace();
+ instance.revision = moduleBuilder.getRevision();
+ }
- // UNKNOWN NODES
- for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
- unknownNodes.add(b.build());
- }
- Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
- instance.addUnknownSchemaNodes(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;
}
}
@Override
- public void setResolved(boolean resolved) {
+ public void setResolved(final boolean resolved) {
this.resolved = resolved;
}
+ @Override
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 instance.description;
- }
-
- @Override
- public void setDescription(final String description) {
- instance.description = description;
- }
-
- @Override
- public String getReference() {
- return instance.reference;
- }
-
- @Override
- public void setReference(final String reference) {
- instance.reference = reference;
- }
-
- @Override
- public Status getStatus() {
- return instance.status;
- }
-
- @Override
- public void setStatus(Status status) {
- if (status != null) {
- instance.status = status;
- }
+ public void addWhenCondition(final String whenCondition) {
+ this.whenCondition = whenCondition;
}
@Override
}
@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
int result = 1;
result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
- result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
+ result = prime * result + getChildNodeBuilders().hashCode();
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
} else if (!whenCondition.equals(other.whenCondition)) {
return false;
}
- if (addedChildNodes == null) {
- if (other.addedChildNodes != null) {
- return false;
- }
- } else if (!addedChildNodes.equals(other.addedChildNodes)) {
+ if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
return false;
}
return true;
}
+ @Override
public String toString() {
return "augment " + augmentTargetStr;
}
- private static final class AugmentationSchemaImpl implements AugmentationSchema {
- private SchemaPath targetPath;
- private RevisionAwareXPath whenCondition;
- private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
- private final Set<UsesNode> uses = new HashSet<>();
- private String description;
- private String reference;
- private Status status;
- private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
-
- 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() {
- return Collections.unmodifiableSet(childNodes);
- }
-
- private void addChildNodes(Set<DataSchemaNode> childNodes) {
- if (childNodes != null) {
- this.childNodes.addAll(childNodes);
- }
- }
+ public void setCopyOf(final AugmentationSchemaBuilder old) {
+ copyOf = old;
+ }
- /**
- * Always returns an empty set, because augment can not contains
- * grouping statement.
- */
- @Override
- public Set<GroupingDefinition> getGroupings() {
- return Collections.emptySet();
- }
+ private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
+ private final int order;
+ private final SchemaPath targetPath;
+ private RevisionAwareXPath whenCondition;
- @Override
- public Set<UsesNode> getUses() {
- return Collections.unmodifiableSet(uses);
- }
+ private URI namespace;
+ private Date revision;
+ private ImmutableList<UnknownSchemaNode> unknownNodes;
+ private AugmentationSchema copyOf;
- private void addUses(Set<UsesNode> uses) {
- if (uses != null) {
- this.uses.addAll(uses);
- }
+ public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
+ super(builder);
+ this.targetPath = targetPath;
+ this.order = order;
}
- /**
- * Always returns an empty set, because augment can not contains type
- * definitions.
- */
- @Override
- public Set<TypeDefinition<?>> getTypeDefinitions() {
- return Collections.emptySet();
+ public void setCopyOf(final AugmentationSchema build) {
+ this.copyOf = build;
}
@Override
- public String getDescription() {
- return description;
+ public Optional<AugmentationSchema> getOriginalDefinition() {
+ return Optional.fromNullable(this.copyOf);
}
@Override
- public String getReference() {
- return reference;
+ public SchemaPath getTargetPath() {
+ return targetPath;
}
@Override
- public Status getStatus() {
- return status;
+ public RevisionAwareXPath getWhenCondition() {
+ return whenCondition;
}
@Override
public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return Collections.unmodifiableList(unknownNodes);
- }
-
- private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
- if (unknownSchemaNodes != null) {
- this.unknownNodes.addAll(unknownSchemaNodes);
- }
+ return unknownNodes;
}
@Override
- public DataSchemaNode getDataChildByName(QName name) {
- return getChildNode(childNodes, name);
+ public URI getNamespace() {
+ return namespace;
}
@Override
- public DataSchemaNode getDataChildByName(String name) {
- return getChildNode(childNodes, name);
+ public Date getRevision() {
+ return revision;
}
@Override
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());
+ result = prime * result + getChildNodes().hashCode();
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
} else if (!whenCondition.equals(other.whenCondition)) {
return false;
}
- if (childNodes == null) {
- if (other.childNodes != null) {
- return false;
- }
- } else if (!childNodes.equals(other.childNodes)) {
+ if (!getChildNodes().equals(other.getChildNodes())) {
return false;
}
return true;
public String toString() {
StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
sb.append("[");
- sb.append("targetPath=" + targetPath);
- sb.append(", when=" + whenCondition);
+ sb.append("targetPath=").append(targetPath);
+ sb.append(", when=").append(whenCondition);
sb.append("]");
return sb.toString();
}
- }
+ @Override
+ public int compareTo(final AugmentationSchemaImpl o) {
+ checkNotNull(o);
+ Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
+ Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
+ while (thisIt.hasNext()) {
+ if (otherIt.hasNext()) {
+ int comp = thisIt.next().compareTo(otherIt.next());
+ if (comp != 0) {
+ return comp;
+ }
+ } else {
+ return 1;
+ }
+ }
+ if (otherIt.hasNext()) {
+ return -1;
+ }
+ return this.order - o.order;
+ }
+ }
}