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.ArrayList;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
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.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
34 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements
35 AugmentationSchemaBuilder {
36 private final int order;
37 private AugmentationSchemaImpl instance;
38 private String whenCondition;
40 private final String augmentTargetStr;
41 private final SchemaPath targetPath;
42 private SchemaPath targetNodeSchemaPath;
44 private boolean resolved;
45 private AugmentationSchemaBuilder copyOf;
47 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
48 super(moduleName, line, null);
50 this.augmentTargetStr = augmentTargetStr;
51 targetPath = BuilderUtils.parseXPathString(augmentTargetStr);
55 protected String getStatementName() {
60 public SchemaPath getPath() {
61 return targetNodeSchemaPath;
65 public AugmentationSchema build() {
66 if (instance != null) {
71 instance = new AugmentationSchemaImpl(targetPath, order,this);
73 Builder parent = getParent();
74 if (parent instanceof ModuleBuilder) {
75 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
76 instance.namespace = moduleBuilder.getNamespace();
77 instance.revision = moduleBuilder.getRevision();
80 if (parent instanceof UsesNodeBuilder) {
81 final ModuleBuilder mb = BuilderUtils.getParentModule(this);
82 final QNameModule qm = QNameModule.create(mb.getNamespace(), mb.getRevision());
84 List<QName> newPath = new ArrayList<>();
85 for (QName name : targetPath.getPathFromRoot()) {
86 newPath.add(QName.create(qm, name.getPrefix(), name.getLocalName()));
88 instance.targetPath = SchemaPath.create(newPath, false);
90 instance.targetPath = targetNodeSchemaPath;
94 instance.setCopyOf(copyOf.build());
97 RevisionAwareXPath whenStmt;
98 if (whenCondition == null) {
101 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
103 instance.whenCondition = whenStmt;
106 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
107 unknownNodes.add(b.build());
109 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
115 public boolean isResolved() {
120 public void setResolved(final boolean resolved) {
121 this.resolved = resolved;
125 public String getWhenCondition() {
126 return whenCondition;
130 public void addWhenCondition(final String whenCondition) {
131 this.whenCondition = whenCondition;
135 public String getTargetPathAsString() {
136 return augmentTargetStr;
140 public SchemaPath getTargetPath() {
145 public SchemaPath getTargetNodeSchemaPath() {
146 return targetNodeSchemaPath;
150 public void setTargetNodeSchemaPath(final SchemaPath path) {
151 this.targetNodeSchemaPath = path;
155 public int getOrder() {
160 public int hashCode() {
161 final int prime = 17;
163 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
164 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
165 result = prime * result + getChildNodeBuilders().hashCode();
170 public boolean equals(final Object obj) {
177 if (getClass() != obj.getClass()) {
180 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
181 if (augmentTargetStr == null) {
182 if (other.augmentTargetStr != null) {
185 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
188 if (whenCondition == null) {
189 if (other.whenCondition != null) {
192 } else if (!whenCondition.equals(other.whenCondition)) {
195 if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
202 public String toString() {
203 return "augment " + augmentTargetStr;
206 public void setCopyOf(final AugmentationSchemaBuilder old) {
210 private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware,
211 Comparable<AugmentationSchemaImpl> {
212 private final int order;
213 private SchemaPath targetPath;
214 private RevisionAwareXPath whenCondition;
216 private URI namespace;
217 private Date revision;
218 private ImmutableList<UnknownSchemaNode> unknownNodes;
219 private AugmentationSchema copyOf;
221 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
223 this.targetPath = targetPath;
227 public void setCopyOf(final AugmentationSchema build) {
232 public Optional<AugmentationSchema> getOriginalDefinition() {
233 return Optional.fromNullable(this.copyOf);
237 public SchemaPath getTargetPath() {
242 public RevisionAwareXPath getWhenCondition() {
243 return whenCondition;
247 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
252 public URI getNamespace() {
257 public Date getRevision() {
262 public int hashCode() {
263 final int prime = 17;
265 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
266 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
267 result = prime * result + getChildNodes().hashCode();
272 public boolean equals(final Object obj) {
279 if (getClass() != obj.getClass()) {
282 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
283 if (targetPath == null) {
284 if (other.targetPath != null) {
287 } else if (!targetPath.equals(other.targetPath)) {
290 if (whenCondition == null) {
291 if (other.whenCondition != null) {
294 } else if (!whenCondition.equals(other.whenCondition)) {
297 if (!getChildNodes().equals(other.getChildNodes())) {
304 public String toString() {
305 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
307 sb.append("targetPath=").append(targetPath);
308 sb.append(", when=").append(whenCondition);
310 return sb.toString();
314 public int compareTo(final AugmentationSchemaImpl o) {
316 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
317 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
318 while (thisIt.hasNext()) {
319 if (otherIt.hasNext()) {
320 int comp = thisIt.next().compareTo(otherIt.next());
328 if (otherIt.hasNext()) {
331 return this.order - o.order;