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.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
37 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
38 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
40 import com.google.common.base.Optional;
41 import com.google.common.base.Preconditions;
42 import com.google.common.collect.ImmutableList;
43 import com.google.common.collect.ImmutableSet;
45 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
46 AugmentationSchemaBuilder {
47 private final int order;
48 private AugmentationSchemaImpl instance;
49 private String whenCondition;
51 private String description;
52 private String reference;
53 private Status status = Status.CURRENT;
55 private final String augmentTargetStr;
56 private final SchemaPath targetPath;
57 private SchemaPath targetNodeSchemaPath;
59 private boolean resolved;
60 private AugmentationSchemaBuilder copyOf;
62 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
63 super(moduleName, line, null);
65 this.augmentTargetStr = augmentTargetStr;
66 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
70 public Set<GroupingDefinition> getGroupings() {
71 return Collections.emptySet();
75 public Set<GroupingBuilder> getGroupingBuilders() {
76 return Collections.emptySet();
80 public void addGrouping(final GroupingBuilder grouping) {
81 throw new YangParseException(getModuleName(), grouping.getLine(), "augment can not contains grouping statement");
85 public SchemaPath getPath() {
86 return targetNodeSchemaPath;
90 public AugmentationSchema build() {
91 if (instance != null) {
95 instance = new AugmentationSchemaImpl(targetPath, order);
97 instance.description = description;
98 instance.reference = reference;
99 instance.status = status;
101 Builder parent = getParent();
102 if (parent instanceof ModuleBuilder) {
103 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
104 instance.namespace = moduleBuilder.getNamespace();
105 instance.revision = moduleBuilder.getRevision();
108 if (parent instanceof UsesNodeBuilder) {
109 ModuleBuilder mb = ParserUtils.getParentModule(this);
110 List<QName> newPath = new ArrayList<>();
111 List<QName> parsedPath = targetPath.getPath();
112 for (QName name : parsedPath) {
113 newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
115 instance.targetPath = SchemaPath.create(newPath, false);
117 instance.targetPath = targetNodeSchemaPath;
120 if (copyOf != null) {
121 instance.setCopyOf(copyOf.build());
124 RevisionAwareXPath whenStmt;
125 if (whenCondition == null) {
128 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
130 instance.whenCondition = whenStmt;
133 for (DataSchemaNodeBuilder node : addedChildNodes) {
134 childNodes.put(node.getQName(), node.build());
136 instance.childNodes = ImmutableSet.copyOf(childNodes.values());
139 for (UsesNodeBuilder builder : addedUsesNodes) {
140 usesNodes.add(builder.build());
142 instance.uses = ImmutableSet.copyOf(usesNodes);
145 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
146 unknownNodes.add(b.build());
148 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
154 public boolean isResolved() {
159 public void setResolved(final boolean resolved) {
160 this.resolved = resolved;
164 public String getWhenCondition() {
165 return whenCondition;
169 public void addWhenCondition(final String whenCondition) {
170 this.whenCondition = whenCondition;
174 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
175 return Collections.emptySet();
179 public void addTypedef(final TypeDefinitionBuilder type) {
180 throw new YangParseException(getModuleName(), type.getLine(), "Augmentation can not contains typedef statement.");
184 public String getDescription() {
189 public void setDescription(final String description) {
190 this.description = description;
194 public String getReference() {
199 public void setReference(final String reference) {
200 this.reference = reference;
204 public Status getStatus() {
209 public void setStatus(final Status status) {
210 this.status = Preconditions.checkNotNull(status, "status cannot be null");
214 public String getTargetPathAsString() {
215 return augmentTargetStr;
219 public SchemaPath getTargetPath() {
224 public SchemaPath getTargetNodeSchemaPath() {
225 return targetNodeSchemaPath;
229 public void setTargetNodeSchemaPath(final SchemaPath path) {
230 this.targetNodeSchemaPath = path;
234 public int getOrder() {
239 public int hashCode() {
240 final int prime = 17;
242 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
243 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
244 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
249 public boolean equals(final Object obj) {
256 if (getClass() != obj.getClass()) {
259 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
260 if (augmentTargetStr == null) {
261 if (other.augmentTargetStr != null) {
264 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
267 if (whenCondition == null) {
268 if (other.whenCondition != null) {
271 } else if (!whenCondition.equals(other.whenCondition)) {
274 if (addedChildNodes == null) {
275 if (other.addedChildNodes != null) {
278 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
285 public String toString() {
286 return "augment " + augmentTargetStr;
289 public void setCopyOf(final AugmentationSchemaBuilder old) {
293 private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
294 Comparable<AugmentationSchemaImpl> {
295 private final int order;
296 private SchemaPath targetPath;
297 private RevisionAwareXPath whenCondition;
298 private ImmutableSet<DataSchemaNode> childNodes;
299 private ImmutableSet<UsesNode> uses;
300 private String description;
301 private String reference;
302 private Status status;
304 private URI namespace;
305 private Date revision;
306 private ImmutableList<UnknownSchemaNode> unknownNodes;
307 private AugmentationSchema copyOf;
309 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
310 this.targetPath = targetPath;
314 public void setCopyOf(final AugmentationSchema build) {
319 public Optional<AugmentationSchema> getOriginalDefinition() {
320 return Optional.fromNullable(this.copyOf);
324 public SchemaPath getTargetPath() {
329 public RevisionAwareXPath getWhenCondition() {
330 return whenCondition;
334 public Set<DataSchemaNode> getChildNodes() {
339 * Always returns an empty set, because augment can not contains
340 * grouping statement.
343 public Set<GroupingDefinition> getGroupings() {
344 return Collections.emptySet();
348 public Set<UsesNode> getUses() {
353 * Always returns an empty set, because augment can not contains type
357 public Set<TypeDefinition<?>> getTypeDefinitions() {
358 return Collections.emptySet();
362 public String getDescription() {
367 public String getReference() {
372 public Status getStatus() {
377 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
382 public DataSchemaNode getDataChildByName(final QName name) {
383 return getChildNode(childNodes, name);
387 public DataSchemaNode getDataChildByName(final String name) {
388 return getChildNode(childNodes, name);
392 public URI getNamespace() {
397 public Date getRevision() {
402 public int hashCode() {
403 final int prime = 17;
405 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
406 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
407 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
412 public boolean equals(final Object obj) {
419 if (getClass() != obj.getClass()) {
422 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
423 if (targetPath == null) {
424 if (other.targetPath != null) {
427 } else if (!targetPath.equals(other.targetPath)) {
430 if (whenCondition == null) {
431 if (other.whenCondition != null) {
434 } else if (!whenCondition.equals(other.whenCondition)) {
437 if (childNodes == null) {
438 if (other.childNodes != null) {
441 } else if (!childNodes.equals(other.childNodes)) {
448 public String toString() {
449 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
451 sb.append("targetPath=" + targetPath);
452 sb.append(", when=" + whenCondition);
454 return sb.toString();
458 public int compareTo(final AugmentationSchemaImpl o) {
459 Iterator<QName> thisIt = this.targetPath.getPath().iterator();
460 Iterator<QName> otherIt = o.getTargetPath().getPath().iterator();
461 while (thisIt.hasNext()) {
462 if (otherIt.hasNext()) {
463 int comp = thisIt.next().compareTo(otherIt.next());
471 if (otherIt.hasNext()) {
474 return this.order - o.order;