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.HashSet;
15 import java.util.List;
17 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
30 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.util.Comparators;
39 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
40 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
42 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
43 AugmentationSchemaBuilder {
44 private boolean built;
45 private final AugmentationSchemaImpl instance;
47 private String whenCondition;
49 private final String augmentTargetStr;
50 private final SchemaPath targetPath;
51 private SchemaPath targetNodeSchemaPath;
53 private boolean resolved;
55 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
56 super(moduleName, line, null);
57 this.augmentTargetStr = augmentTargetStr;
58 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
59 instance = new AugmentationSchemaImpl(targetPath);
63 public Set<GroupingDefinition> getGroupings() {
64 return Collections.emptySet();
68 public Set<GroupingBuilder> getGroupingBuilders() {
69 return Collections.emptySet();
73 public void addGrouping(GroupingBuilder grouping) {
74 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
78 public SchemaPath getPath() {
79 return targetNodeSchemaPath;
83 public AugmentationSchema build() {
86 Builder parent = getParent();
87 if(parent instanceof ModuleBuilder) {
88 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
89 instance.setNamespace(moduleBuilder.getNamespace());
90 instance.setRevision(moduleBuilder.getRevision());
93 if (parent instanceof UsesNodeBuilder) {
94 ModuleBuilder mb = ParserUtils.getParentModule(this);
95 List<QName> newPath = new ArrayList<>();
96 List<QName> parsedPath = targetPath.getPath();
97 for (QName name : parsedPath) {
98 newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
100 instance.setTargetPath(new SchemaPath(newPath, false));
102 instance.setTargetPath(targetNodeSchemaPath);
105 RevisionAwareXPath whenStmt;
106 if (whenCondition == null) {
109 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
111 instance.setWhenCondition(whenStmt);
114 for (DataSchemaNodeBuilder node : addedChildNodes) {
115 DataSchemaNode child = node.build();
116 childNodes.add(child);
118 instance.addChildNodes(childNodes);
121 for (UsesNodeBuilder builder : addedUsesNodes) {
122 usesNodes.add(builder.build());
124 instance.addUses(usesNodes);
127 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
128 unknownNodes.add(b.build());
130 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
131 instance.addUnknownSchemaNodes(unknownNodes);
139 public boolean isResolved() {
144 public void setResolved(boolean resolved) {
145 this.resolved = resolved;
149 public String getWhenCondition() {
150 return whenCondition;
154 public void addWhenCondition(String whenCondition) {
155 this.whenCondition = whenCondition;
159 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
160 return Collections.emptySet();
164 public void addTypedef(TypeDefinitionBuilder type) {
165 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
169 public String getDescription() {
170 return instance.description;
174 public void setDescription(final String description) {
175 instance.description = description;
179 public String getReference() {
180 return instance.reference;
184 public void setReference(final String reference) {
185 instance.reference = reference;
189 public Status getStatus() {
190 return instance.status;
194 public void setStatus(Status status) {
195 if (status != null) {
196 instance.status = status;
201 public String getTargetPathAsString() {
202 return augmentTargetStr;
206 public SchemaPath getTargetPath() {
211 public SchemaPath getTargetNodeSchemaPath() {
212 return targetNodeSchemaPath;
216 public void setTargetNodeSchemaPath(SchemaPath path) {
217 this.targetNodeSchemaPath = path;
221 public int hashCode() {
222 final int prime = 17;
224 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
225 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
226 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
231 public boolean equals(Object obj) {
238 if (getClass() != obj.getClass()) {
241 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
242 if (augmentTargetStr == null) {
243 if (other.augmentTargetStr != null) {
246 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
249 if (whenCondition == null) {
250 if (other.whenCondition != null) {
253 } else if (!whenCondition.equals(other.whenCondition)) {
256 if (addedChildNodes == null) {
257 if (other.addedChildNodes != null) {
260 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
267 public String toString() {
268 return "augment " + augmentTargetStr;
271 private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware {
272 private SchemaPath targetPath;
273 private RevisionAwareXPath whenCondition;
274 private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
275 private final Set<UsesNode> uses = new HashSet<>();
276 private String description;
277 private String reference;
278 private Status status;
280 private URI namespace;
281 private Date revision;
282 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
284 private AugmentationSchemaImpl(SchemaPath targetPath) {
285 this.targetPath = targetPath;
289 public SchemaPath getTargetPath() {
293 private void setTargetPath(SchemaPath path) {
294 this.targetPath = path;
298 public RevisionAwareXPath getWhenCondition() {
299 return whenCondition;
302 private void setWhenCondition(RevisionAwareXPath whenCondition) {
303 this.whenCondition = whenCondition;
307 public Set<DataSchemaNode> getChildNodes() {
308 return Collections.unmodifiableSet(childNodes);
311 private void addChildNodes(Set<DataSchemaNode> childNodes) {
312 if (childNodes != null) {
313 this.childNodes.addAll(childNodes);
318 * Always returns an empty set, because augment can not contains
319 * grouping statement.
322 public Set<GroupingDefinition> getGroupings() {
323 return Collections.emptySet();
327 public Set<UsesNode> getUses() {
328 return Collections.unmodifiableSet(uses);
331 private void addUses(Set<UsesNode> uses) {
333 this.uses.addAll(uses);
338 * Always returns an empty set, because augment can not contains type
342 public Set<TypeDefinition<?>> getTypeDefinitions() {
343 return Collections.emptySet();
347 public String getDescription() {
352 public String getReference() {
357 public Status getStatus() {
362 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
363 return Collections.unmodifiableList(unknownNodes);
366 private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
367 if (unknownSchemaNodes != null) {
368 this.unknownNodes.addAll(unknownSchemaNodes);
373 public DataSchemaNode getDataChildByName(QName name) {
374 return getChildNode(childNodes, name);
378 public DataSchemaNode getDataChildByName(String name) {
379 return getChildNode(childNodes, name);
383 public URI getNamespace() {
387 protected void setNamespace(URI namespace) {
388 this.namespace = namespace;
392 public Date getRevision() {
396 protected void setRevision(Date revision) {
397 this.revision = revision;
401 public int hashCode() {
402 final int prime = 17;
404 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
405 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
406 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
411 public boolean equals(Object obj) {
418 if (getClass() != obj.getClass()) {
421 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
422 if (targetPath == null) {
423 if (other.targetPath != null) {
426 } else if (!targetPath.equals(other.targetPath)) {
429 if (whenCondition == null) {
430 if (other.whenCondition != null) {
433 } else if (!whenCondition.equals(other.whenCondition)) {
436 if (childNodes == null) {
437 if (other.childNodes != null) {
440 } else if (!childNodes.equals(other.childNodes)) {
447 public String toString() {
448 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
450 sb.append("targetPath=" + targetPath);
451 sb.append(", when=" + whenCondition);
453 return sb.toString();