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;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Date;
18 import java.util.Iterator;
19 import java.util.List;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.Status;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.UsesNode;
32 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
33 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
40 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
41 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
43 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
44 AugmentationSchemaBuilder {
45 private final int order;
46 private AugmentationSchemaImpl instance;
47 private String whenCondition;
49 private String description;
50 private String reference;
51 private Status status = Status.CURRENT;
53 private final String augmentTargetStr;
54 private final SchemaPath targetPath;
55 private SchemaPath targetNodeSchemaPath;
57 private boolean resolved;
58 private AugmentationSchemaBuilder copyOf;
60 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, int order) {
61 super(moduleName, line, null);
63 this.augmentTargetStr = augmentTargetStr;
64 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
68 public Set<GroupingDefinition> getGroupings() {
69 return Collections.emptySet();
73 public Set<GroupingBuilder> getGroupingBuilders() {
74 return Collections.emptySet();
78 public void addGrouping(final GroupingBuilder grouping) {
79 throw new YangParseException(getModuleName(), grouping.getLine(), "augment can not contains grouping statement");
83 public SchemaPath getPath() {
84 return targetNodeSchemaPath;
88 public AugmentationSchema build() {
89 if (instance != null) {
93 instance = new AugmentationSchemaImpl(targetPath, order);
95 instance.description = description;
96 instance.reference = reference;
97 instance.status = status;
99 Builder parent = getParent();
100 if (parent instanceof ModuleBuilder) {
101 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
102 instance.namespace = moduleBuilder.getNamespace();
103 instance.revision = moduleBuilder.getRevision();
106 if (parent instanceof UsesNodeBuilder) {
107 ModuleBuilder mb = ParserUtils.getParentModule(this);
108 List<QName> newPath = new ArrayList<>();
109 List<QName> parsedPath = targetPath.getPath();
110 for (QName name : parsedPath) {
111 newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
113 instance.targetPath = SchemaPath.create(newPath, false);
115 instance.targetPath = targetNodeSchemaPath;
118 if (copyOf != null) {
119 instance.setCopyOf(copyOf.build());
122 RevisionAwareXPath whenStmt;
123 if (whenCondition == null) {
126 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
128 instance.whenCondition = whenStmt;
131 for (DataSchemaNodeBuilder node : addedChildNodes) {
132 childNodes.add(node.build());
134 instance.childNodes = ImmutableSet.copyOf(childNodes);
137 for (UsesNodeBuilder builder : addedUsesNodes) {
138 usesNodes.add(builder.build());
140 instance.uses = ImmutableSet.copyOf(usesNodes);
143 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
144 unknownNodes.add(b.build());
146 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
152 public boolean isResolved() {
157 public void setResolved(final boolean resolved) {
158 this.resolved = resolved;
162 public String getWhenCondition() {
163 return whenCondition;
167 public void addWhenCondition(final String whenCondition) {
168 this.whenCondition = whenCondition;
172 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
173 return Collections.emptySet();
177 public void addTypedef(final TypeDefinitionBuilder type) {
178 throw new YangParseException(getModuleName(), type.getLine(), "Augmentation can not contains typedef statement.");
182 public String getDescription() {
187 public void setDescription(final String description) {
188 this.description = description;
192 public String getReference() {
197 public void setReference(final String reference) {
198 this.reference = reference;
202 public Status getStatus() {
207 public void setStatus(final Status status) {
208 this.status = Preconditions.checkNotNull(status, "status cannot be null");
212 public String getTargetPathAsString() {
213 return augmentTargetStr;
217 public SchemaPath getTargetPath() {
222 public SchemaPath getTargetNodeSchemaPath() {
223 return targetNodeSchemaPath;
227 public void setTargetNodeSchemaPath(final SchemaPath path) {
228 this.targetNodeSchemaPath = path;
232 public int getOrder() {
237 public int hashCode() {
238 final int prime = 17;
240 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
241 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
242 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
247 public boolean equals(final Object obj) {
254 if (getClass() != obj.getClass()) {
257 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
258 if (augmentTargetStr == null) {
259 if (other.augmentTargetStr != null) {
262 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
265 if (whenCondition == null) {
266 if (other.whenCondition != null) {
269 } else if (!whenCondition.equals(other.whenCondition)) {
272 if (addedChildNodes == null) {
273 if (other.addedChildNodes != null) {
276 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
283 public String toString() {
284 return "augment " + augmentTargetStr;
287 public void setCopyOf(final AugmentationSchemaBuilder old) {
291 private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
292 Comparable<AugmentationSchemaImpl> {
293 private final int order;
294 private SchemaPath targetPath;
295 private RevisionAwareXPath whenCondition;
296 private ImmutableSet<DataSchemaNode> childNodes;
297 private ImmutableSet<UsesNode> uses;
298 private String description;
299 private String reference;
300 private Status status;
302 private URI namespace;
303 private Date revision;
304 private ImmutableList<UnknownSchemaNode> unknownNodes;
305 private AugmentationSchema copyOf;
307 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
308 this.targetPath = targetPath;
312 public void setCopyOf(final AugmentationSchema build) {
317 public Optional<AugmentationSchema> getOriginalDefinition() {
318 return Optional.fromNullable(this.copyOf);
322 public SchemaPath getTargetPath() {
327 public RevisionAwareXPath getWhenCondition() {
328 return whenCondition;
332 public Set<DataSchemaNode> getChildNodes() {
337 * Always returns an empty set, because augment can not contains
338 * grouping statement.
341 public Set<GroupingDefinition> getGroupings() {
342 return Collections.emptySet();
346 public Set<UsesNode> getUses() {
351 * Always returns an empty set, because augment can not contains type
355 public Set<TypeDefinition<?>> getTypeDefinitions() {
356 return Collections.emptySet();
360 public String getDescription() {
365 public String getReference() {
370 public Status getStatus() {
375 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
380 public DataSchemaNode getDataChildByName(final QName name) {
381 return getChildNode(childNodes, name);
385 public DataSchemaNode getDataChildByName(final String name) {
386 return getChildNode(childNodes, name);
390 public URI getNamespace() {
395 public Date getRevision() {
400 public int hashCode() {
401 final int prime = 17;
403 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
404 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
405 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
410 public boolean equals(final Object obj) {
417 if (getClass() != obj.getClass()) {
420 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
421 if (targetPath == null) {
422 if (other.targetPath != null) {
425 } else if (!targetPath.equals(other.targetPath)) {
428 if (whenCondition == null) {
429 if (other.whenCondition != null) {
432 } else if (!whenCondition.equals(other.whenCondition)) {
435 if (childNodes == null) {
436 if (other.childNodes != null) {
439 } else if (!childNodes.equals(other.childNodes)) {
446 public String toString() {
447 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
449 sb.append("targetPath=" + targetPath);
450 sb.append(", when=" + whenCondition);
452 return sb.toString();
456 public int compareTo(AugmentationSchemaImpl o) {
457 Iterator<QName> thisIt = this.targetPath.getPath().iterator();
458 Iterator<QName> otherIt = o.getTargetPath().getPath().iterator();
459 while (thisIt.hasNext()) {
460 if (otherIt.hasNext()) {
461 int comp = thisIt.next().compareTo(otherIt.next());
469 if (otherIt.hasNext()) {
472 return this.order - o.order;