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.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
22 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.UsesNode;
28 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
29 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
36 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
37 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
39 import com.google.common.base.Optional;
40 import com.google.common.base.Preconditions;
41 import com.google.common.collect.ImmutableList;
42 import com.google.common.collect.ImmutableSet;
44 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
45 AugmentationSchemaBuilder {
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) {
61 super(moduleName, line, null);
62 this.augmentTargetStr = augmentTargetStr;
63 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
67 public Set<GroupingDefinition> getGroupings() {
68 return Collections.emptySet();
72 public Set<GroupingBuilder> getGroupingBuilders() {
73 return Collections.emptySet();
77 public void addGrouping(final GroupingBuilder grouping) {
78 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
82 public SchemaPath getPath() {
83 return targetNodeSchemaPath;
87 public AugmentationSchema build() {
88 if (instance != null) {
92 instance = new AugmentationSchemaImpl(targetPath);
94 instance.description = description;
95 instance.reference = reference;
96 instance.status = status;
98 Builder parent = getParent();
99 if (parent instanceof ModuleBuilder) {
100 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
101 instance.namespace = moduleBuilder.getNamespace();
102 instance.revision = moduleBuilder.getRevision();
105 if (parent instanceof UsesNodeBuilder) {
106 ModuleBuilder mb = ParserUtils.getParentModule(this);
107 List<QName> newPath = new ArrayList<>();
108 List<QName> parsedPath = targetPath.getPath();
109 for (QName name : parsedPath) {
110 newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
112 instance.targetPath = SchemaPath.create(newPath, false);
114 instance.targetPath = targetNodeSchemaPath;
117 if (copyOf != null) {
118 instance.setCopyOf(copyOf.build());
121 RevisionAwareXPath whenStmt;
122 if (whenCondition == null) {
125 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
127 instance.whenCondition = whenStmt;
130 for (DataSchemaNodeBuilder node : addedChildNodes) {
131 childNodes.add(node.build());
133 instance.childNodes = ImmutableSet.copyOf(childNodes);
136 for (UsesNodeBuilder builder : addedUsesNodes) {
137 usesNodes.add(builder.build());
139 instance.uses = ImmutableSet.copyOf(usesNodes);
142 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
143 unknownNodes.add(b.build());
145 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
151 public boolean isResolved() {
156 public void setResolved(final boolean resolved) {
157 this.resolved = resolved;
161 public String getWhenCondition() {
162 return whenCondition;
166 public void addWhenCondition(final String whenCondition) {
167 this.whenCondition = whenCondition;
171 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
172 return Collections.emptySet();
176 public void addTypedef(final TypeDefinitionBuilder type) {
177 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
181 public String getDescription() {
186 public void setDescription(final String description) {
187 this.description = description;
191 public String getReference() {
196 public void setReference(final String reference) {
197 this.reference = reference;
201 public Status getStatus() {
206 public void setStatus(final Status status) {
207 this.status = Preconditions.checkNotNull(status, "status cannot be null");
211 public String getTargetPathAsString() {
212 return augmentTargetStr;
216 public SchemaPath getTargetPath() {
221 public SchemaPath getTargetNodeSchemaPath() {
222 return targetNodeSchemaPath;
226 public void setTargetNodeSchemaPath(final SchemaPath path) {
227 this.targetNodeSchemaPath = path;
231 public int hashCode() {
232 final int prime = 17;
234 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
235 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
236 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
241 public boolean equals(final Object obj) {
248 if (getClass() != obj.getClass()) {
251 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
252 if (augmentTargetStr == null) {
253 if (other.augmentTargetStr != null) {
256 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
259 if (whenCondition == null) {
260 if (other.whenCondition != null) {
263 } else if (!whenCondition.equals(other.whenCondition)) {
266 if (addedChildNodes == null) {
267 if (other.addedChildNodes != null) {
270 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
277 public String toString() {
278 return "augment " + augmentTargetStr;
281 public void setCopyOf(final AugmentationSchemaBuilder old) {
285 private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware {
286 private SchemaPath targetPath;
287 private RevisionAwareXPath whenCondition;
288 private ImmutableSet<DataSchemaNode> childNodes;
289 private ImmutableSet<UsesNode> uses;
290 private String description;
291 private String reference;
292 private Status status;
294 private URI namespace;
295 private Date revision;
296 private ImmutableList<UnknownSchemaNode> unknownNodes;
297 private AugmentationSchema copyOf;
299 private AugmentationSchemaImpl(final SchemaPath targetPath) {
300 this.targetPath = targetPath;
303 public void setCopyOf(final AugmentationSchema build) {
308 public Optional<AugmentationSchema> getOriginalDefinition() {
309 return Optional.fromNullable(this.copyOf);
313 public SchemaPath getTargetPath() {
318 public RevisionAwareXPath getWhenCondition() {
319 return whenCondition;
323 public Set<DataSchemaNode> getChildNodes() {
328 * Always returns an empty set, because augment can not contains
329 * grouping statement.
332 public Set<GroupingDefinition> getGroupings() {
333 return Collections.emptySet();
337 public Set<UsesNode> getUses() {
342 * Always returns an empty set, because augment can not contains type
346 public Set<TypeDefinition<?>> getTypeDefinitions() {
347 return Collections.emptySet();
351 public String getDescription() {
356 public String getReference() {
361 public Status getStatus() {
366 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
371 public DataSchemaNode getDataChildByName(final QName name) {
372 return getChildNode(childNodes, name);
376 public DataSchemaNode getDataChildByName(final String name) {
377 return getChildNode(childNodes, name);
381 public URI getNamespace() {
386 public Date getRevision() {
391 public int hashCode() {
392 final int prime = 17;
394 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
395 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
396 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
401 public boolean equals(final Object obj) {
408 if (getClass() != obj.getClass()) {
411 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
412 if (targetPath == null) {
413 if (other.targetPath != null) {
416 } else if (!targetPath.equals(other.targetPath)) {
419 if (whenCondition == null) {
420 if (other.whenCondition != null) {
423 } else if (!whenCondition.equals(other.whenCondition)) {
426 if (childNodes == null) {
427 if (other.childNodes != null) {
430 } else if (!childNodes.equals(other.childNodes)) {
437 public String toString() {
438 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
440 sb.append("targetPath=" + targetPath);
441 sb.append(", when=" + whenCondition);
443 return sb.toString();