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 static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
15 import java.util.Date;
16 import java.util.Iterator;
17 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.NamespaceRevisionAware;
21 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
31 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
32 private final int order;
33 private AugmentationSchemaImpl instance;
34 private String whenCondition;
36 private final String augmentTargetStr;
37 private final SchemaPath targetPath;
39 private boolean resolved;
40 private boolean unsupportedTarget = false;
42 private AugmentationSchemaBuilder copyOf;
44 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
45 final SchemaPath targetPath, final int order) {
46 super(moduleName, line, null);
48 this.augmentTargetStr = augmentTargetStr;
49 this.targetPath = targetPath;
53 protected String getStatementName() {
58 public SchemaPath getPath() {
63 public SchemaPath getTargetPath() {
68 public AugmentationSchema build() {
69 if (instance != null) {
75 instance = new AugmentationSchemaImpl(targetPath, order,this);
77 Builder parent = getParent();
78 if (parent instanceof ModuleBuilder) {
79 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
80 instance.namespace = moduleBuilder.getNamespace();
81 instance.revision = moduleBuilder.getRevision();
85 instance.setCopyOf(copyOf.build());
88 RevisionAwareXPath whenStmt;
89 if (whenCondition == null) {
92 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
94 instance.whenCondition = whenStmt;
97 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
98 unknownNodes.add(b.build());
100 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
106 public boolean isResolved() {
111 public void setResolved(final boolean resolved) {
112 this.resolved = resolved;
116 * Set true if target of augment is unsupported (e.g. node in body of extension).
117 * In such case, augmentation is skipped and AugmentationSchema is not built.
120 public void setUnsupportedTarget(boolean unsupportedTarget) {
121 this.unsupportedTarget = unsupportedTarget;
125 * Return true if target of augment is unsupported (e.g. node in body of extension).
126 * In such case, augmentation is skipped and AugmentationSchema is not built.
129 public boolean isUnsupportedTarget() {
130 return unsupportedTarget;
134 public String getWhenCondition() {
135 return whenCondition;
139 public void addWhenCondition(final String whenCondition) {
140 this.whenCondition = whenCondition;
144 public String getTargetPathAsString() {
145 return augmentTargetStr;
149 public int getOrder() {
154 public int hashCode() {
155 final int prime = 17;
157 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
158 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
159 result = prime * result + getChildNodeBuilders().hashCode();
164 public boolean equals(final Object obj) {
171 if (getClass() != obj.getClass()) {
174 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
175 if (augmentTargetStr == null) {
176 if (other.augmentTargetStr != null) {
179 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
182 if (whenCondition == null) {
183 if (other.whenCondition != null) {
186 } else if (!whenCondition.equals(other.whenCondition)) {
189 if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
196 public String toString() {
197 return "augment " + augmentTargetStr;
200 public void setCopyOf(final AugmentationSchemaBuilder old) {
204 private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
205 private final int order;
206 private final SchemaPath targetPath;
207 private RevisionAwareXPath whenCondition;
209 private URI namespace;
210 private Date revision;
211 private ImmutableList<UnknownSchemaNode> unknownNodes;
212 private AugmentationSchema copyOf;
214 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
216 this.targetPath = targetPath;
220 public void setCopyOf(final AugmentationSchema build) {
225 public Optional<AugmentationSchema> getOriginalDefinition() {
226 return Optional.fromNullable(this.copyOf);
230 public SchemaPath getTargetPath() {
235 public RevisionAwareXPath getWhenCondition() {
236 return whenCondition;
240 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
245 public URI getNamespace() {
250 public Date getRevision() {
255 public int hashCode() {
256 final int prime = 17;
258 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
259 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
260 result = prime * result + getChildNodes().hashCode();
265 public boolean equals(final Object obj) {
272 if (getClass() != obj.getClass()) {
275 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
276 if (targetPath == null) {
277 if (other.targetPath != null) {
280 } else if (!targetPath.equals(other.targetPath)) {
283 if (whenCondition == null) {
284 if (other.whenCondition != null) {
287 } else if (!whenCondition.equals(other.whenCondition)) {
290 if (!getChildNodes().equals(other.getChildNodes())) {
297 public String toString() {
298 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
300 sb.append("targetPath=").append(targetPath);
301 sb.append(", when=").append(whenCondition);
303 return sb.toString();
307 public int compareTo(final AugmentationSchemaImpl o) {
309 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
310 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
311 while (thisIt.hasNext()) {
312 if (otherIt.hasNext()) {
313 int comp = thisIt.next().compareTo(otherIt.next());
321 if (otherIt.hasNext()) {
324 return this.order - o.order;