2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.Iterator;
15 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
30 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
39 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
41 import com.google.common.base.Optional;
42 import com.google.common.base.Preconditions;
43 import com.google.common.collect.ImmutableList;
44 import com.google.common.collect.ImmutableSet;
46 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
47 AugmentationSchemaBuilder {
48 private final int order;
49 private AugmentationSchemaImpl instance;
50 private String whenCondition;
52 private String description;
53 private String reference;
54 private Status status = Status.CURRENT;
56 private final String augmentTargetStr;
57 private final SchemaPath targetPath;
58 private SchemaPath targetNodeSchemaPath;
60 private boolean resolved;
61 private AugmentationSchemaBuilder copyOf;
63 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
64 super(moduleName, line, null);
66 this.augmentTargetStr = augmentTargetStr;
67 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
71 public Set<GroupingDefinition> getGroupings() {
72 return Collections.emptySet();
76 public Set<GroupingBuilder> getGroupingBuilders() {
77 return Collections.emptySet();
81 public void addGrouping(final GroupingBuilder grouping) {
82 throw new YangParseException(getModuleName(), grouping.getLine(), "augment can not contains grouping statement");
86 public SchemaPath getPath() {
87 return targetNodeSchemaPath;
91 public AugmentationSchema build() {
92 if (instance != null) {
96 instance = new AugmentationSchemaImpl(targetPath, order);
98 instance.description = description;
99 instance.reference = reference;
100 instance.status = status;
102 Builder parent = getParent();
103 if (parent instanceof ModuleBuilder) {
104 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
105 instance.namespace = moduleBuilder.getNamespace();
106 instance.revision = moduleBuilder.getRevision();
109 if (parent instanceof UsesNodeBuilder) {
110 final ModuleBuilder mb = ParserUtils.getParentModule(this);
111 final QNameModule qm = QNameModule.create(mb.getNamespace(), mb.getRevision());
113 List<QName> newPath = new ArrayList<>();
114 for (QName name : targetPath.getPathFromRoot()) {
115 newPath.add(QName.create(qm, name.getPrefix(), name.getLocalName()));
117 instance.targetPath = SchemaPath.create(newPath, false);
119 instance.targetPath = targetNodeSchemaPath;
122 if (copyOf != null) {
123 instance.setCopyOf(copyOf.build());
126 RevisionAwareXPath whenStmt;
127 if (whenCondition == null) {
130 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
132 instance.whenCondition = whenStmt;
135 for (DataSchemaNodeBuilder node : addedChildNodes) {
136 childNodes.put(node.getQName(), node.build());
138 instance.childNodes = ImmutableSet.copyOf(childNodes.values());
141 for (UsesNodeBuilder builder : addedUsesNodes) {
142 usesNodes.add(builder.build());
144 instance.uses = ImmutableSet.copyOf(usesNodes);
147 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
148 unknownNodes.add(b.build());
150 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
156 public boolean isResolved() {
161 public void setResolved(final boolean resolved) {
162 this.resolved = resolved;
166 public String getWhenCondition() {
167 return whenCondition;
171 public void addWhenCondition(final String whenCondition) {
172 this.whenCondition = whenCondition;
176 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
177 return Collections.emptySet();
181 public void addTypedef(final TypeDefinitionBuilder type) {
182 throw new YangParseException(getModuleName(), type.getLine(), "Augmentation can not contains typedef statement.");
186 public String getDescription() {
191 public void setDescription(final String description) {
192 this.description = description;
196 public String getReference() {
201 public void setReference(final String reference) {
202 this.reference = reference;
206 public Status getStatus() {
211 public void setStatus(final Status status) {
212 this.status = Preconditions.checkNotNull(status, "status cannot be null");
216 public String getTargetPathAsString() {
217 return augmentTargetStr;
221 public SchemaPath getTargetPath() {
226 public SchemaPath getTargetNodeSchemaPath() {
227 return targetNodeSchemaPath;
231 public void setTargetNodeSchemaPath(final SchemaPath path) {
232 this.targetNodeSchemaPath = path;
236 public int getOrder() {
241 public int hashCode() {
242 final int prime = 17;
244 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
245 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
246 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
251 public boolean equals(final Object obj) {
258 if (getClass() != obj.getClass()) {
261 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
262 if (augmentTargetStr == null) {
263 if (other.augmentTargetStr != null) {
266 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
269 if (whenCondition == null) {
270 if (other.whenCondition != null) {
273 } else if (!whenCondition.equals(other.whenCondition)) {
276 if (addedChildNodes == null) {
277 if (other.addedChildNodes != null) {
280 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
287 public String toString() {
288 return "augment " + augmentTargetStr;
291 public void setCopyOf(final AugmentationSchemaBuilder old) {
295 private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
296 Comparable<AugmentationSchemaImpl> {
297 private final int order;
298 private SchemaPath targetPath;
299 private RevisionAwareXPath whenCondition;
300 private ImmutableSet<DataSchemaNode> childNodes;
301 private ImmutableSet<UsesNode> uses;
302 private String description;
303 private String reference;
304 private Status status;
306 private URI namespace;
307 private Date revision;
308 private ImmutableList<UnknownSchemaNode> unknownNodes;
309 private AugmentationSchema copyOf;
311 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
312 this.targetPath = targetPath;
316 public void setCopyOf(final AugmentationSchema build) {
321 public Optional<AugmentationSchema> getOriginalDefinition() {
322 return Optional.fromNullable(this.copyOf);
326 public SchemaPath getTargetPath() {
331 public RevisionAwareXPath getWhenCondition() {
332 return whenCondition;
336 public Set<DataSchemaNode> getChildNodes() {
341 * Always returns an empty set, because augment can not contains
342 * grouping statement.
345 public Set<GroupingDefinition> getGroupings() {
346 return Collections.emptySet();
350 public Set<UsesNode> getUses() {
355 * Always returns an empty set, because augment can not contains type
359 public Set<TypeDefinition<?>> getTypeDefinitions() {
360 return Collections.emptySet();
364 public String getDescription() {
369 public String getReference() {
374 public Status getStatus() {
379 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
384 public DataSchemaNode getDataChildByName(final QName name) {
385 return getChildNode(childNodes, name);
389 public DataSchemaNode getDataChildByName(final String name) {
390 return getChildNode(childNodes, name);
394 public URI getNamespace() {
399 public Date getRevision() {
404 public int hashCode() {
405 final int prime = 17;
407 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
408 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
409 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
414 public boolean equals(final Object obj) {
421 if (getClass() != obj.getClass()) {
424 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
425 if (targetPath == null) {
426 if (other.targetPath != null) {
429 } else if (!targetPath.equals(other.targetPath)) {
432 if (whenCondition == null) {
433 if (other.whenCondition != null) {
436 } else if (!whenCondition.equals(other.whenCondition)) {
439 if (childNodes == null) {
440 if (other.childNodes != null) {
443 } else if (!childNodes.equals(other.childNodes)) {
450 public String toString() {
451 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
453 sb.append("targetPath=" + targetPath);
454 sb.append(", when=" + whenCondition);
456 return sb.toString();
460 public int compareTo(final AugmentationSchemaImpl o) {
461 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
462 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
463 while (thisIt.hasNext()) {
464 if (otherIt.hasNext()) {
465 int comp = thisIt.next().compareTo(otherIt.next());
473 if (otherIt.hasNext()) {
476 return this.order - o.order;