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.controller.yang.parser.builder.impl;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
32 import org.opendaylight.controller.yang.parser.util.YangParseException;
34 public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
35 private boolean built;
36 private final AugmentationSchemaImpl instance;
37 private final int line;
38 private final String augmentTargetStr;
39 private SchemaPath augmentTarget;
40 private SchemaPath finalAugmentTarget;
41 private String whenCondition;
42 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
43 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
44 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
45 private boolean resolved;
47 AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
48 this.augmentTargetStr = augmentTargetStr;
50 final SchemaPath targetPath = YangModelBuilderUtil
51 .parseAugmentPath(augmentTargetStr);
52 augmentTarget = targetPath;
53 instance = new AugmentationSchemaImpl(targetPath);
57 public int getLine() {
62 public void addChildNode(DataSchemaNodeBuilder childNode) {
63 childNodes.add(childNode);
67 public Set<DataSchemaNodeBuilder> getChildNodes() {
72 public Set<GroupingBuilder> getGroupings() {
77 public void addGrouping(GroupingBuilder grouping) {
78 groupings.add(grouping);
82 public void addUsesNode(UsesNodeBuilder usesBuilder) {
83 usesNodes.add(usesBuilder);
87 * Always returns null.
90 public QName getQName() {
95 * Always returns null.
98 public SchemaPath getPath() {
103 public AugmentationSchema build() {
105 instance.setTargetPath(finalAugmentTarget);
107 RevisionAwareXPath whenStmt;
108 if (whenCondition == null) {
111 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
113 instance.setWhenCondition(whenStmt);
116 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
117 for (DataSchemaNodeBuilder node : childNodes) {
118 childs.put(node.getQName(), node.build());
120 instance.setChildNodes(childs);
123 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
124 for (GroupingBuilder builder : groupings) {
125 groupingDefinitions.add(builder.build());
127 instance.setGroupings(groupingDefinitions);
130 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
131 for (UsesNodeBuilder builder : usesNodes) {
132 usesNodeDefinitions.add(builder.build());
134 instance.setUses(usesNodeDefinitions);
142 public boolean isResolved() {
147 public void setResolved(boolean resolved) {
148 this.resolved = resolved;
151 public String getWhenCondition() {
152 return whenCondition;
155 public void addWhenCondition(String whenCondition) {
156 this.whenCondition = whenCondition;
160 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
161 return Collections.emptySet();
165 public void addTypedef(TypeDefinitionBuilder type) {
166 throw new YangParseException(line,
167 "Augmentation can not contains type definitions");
171 public void setDescription(String description) {
172 instance.setDescription(description);
176 public void setReference(String reference) {
177 instance.setReference(reference);
181 public void setStatus(Status status) {
182 instance.setStatus(status);
186 public SchemaPath getTargetPath() {
187 return augmentTarget;
191 public void setTargetPath(SchemaPath path) {
192 this.finalAugmentTarget = path;
196 public String getTargetPathAsString() {
197 return augmentTargetStr;
201 public int hashCode() {
202 final int prime = 17;
206 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
207 result = prime * result
208 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
209 result = prime * result
210 + ((childNodes == null) ? 0 : childNodes.hashCode());
215 public boolean equals(Object obj) {
222 if (getClass() != obj.getClass()) {
225 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
226 if (augmentTargetStr == null) {
227 if (other.augmentTargetStr != null) {
230 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
233 if (whenCondition == null) {
234 if (other.whenCondition != null) {
237 } else if (!whenCondition.equals(other.whenCondition)) {
240 if (childNodes == null) {
241 if (other.childNodes != null) {
244 } else if (!childNodes.equals(other.childNodes)) {
250 private final class AugmentationSchemaImpl implements AugmentationSchema {
251 private SchemaPath targetPath;
252 private RevisionAwareXPath whenCondition;
253 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
254 private Set<GroupingDefinition> groupings = Collections.emptySet();
255 private Set<UsesNode> uses = Collections.emptySet();
257 private String description;
258 private String reference;
259 private Status status;
261 private AugmentationSchemaImpl(SchemaPath targetPath) {
262 this.targetPath = targetPath;
266 public SchemaPath getTargetPath() {
270 private void setTargetPath(SchemaPath path) {
271 this.targetPath = path;
275 public RevisionAwareXPath getWhenCondition() {
276 return whenCondition;
279 private void setWhenCondition(RevisionAwareXPath whenCondition) {
280 this.whenCondition = whenCondition;
284 public Set<DataSchemaNode> getChildNodes() {
285 return new HashSet<DataSchemaNode>(childNodes.values());
288 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
289 if (childNodes != null) {
290 this.childNodes = childNodes;
295 public Set<GroupingDefinition> getGroupings() {
299 private void setGroupings(Set<GroupingDefinition> groupings) {
300 if (groupings != null) {
301 this.groupings = groupings;
306 public Set<UsesNode> getUses() {
310 private void setUses(Set<UsesNode> uses) {
317 * Always returns an empty set, because augmentation can not contains
321 public Set<TypeDefinition<?>> getTypeDefinitions() {
322 return Collections.emptySet();
326 public String getDescription() {
330 private void setDescription(String description) {
331 this.description = description;
335 public String getReference() {
339 private void setReference(String reference) {
340 this.reference = reference;
344 public Status getStatus() {
348 private void setStatus(Status status) {
349 this.status = status;
353 public DataSchemaNode getDataChildByName(QName name) {
354 return childNodes.get(name);
358 public DataSchemaNode getDataChildByName(String name) {
359 DataSchemaNode result = null;
360 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
361 if (entry.getKey().getLocalName().equals(name)) {
362 result = entry.getValue();
370 public int hashCode() {
371 final int prime = 17;
373 result = prime * result
374 + ((targetPath == null) ? 0 : targetPath.hashCode());
375 result = prime * result
376 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
377 result = prime * result
378 + ((childNodes == null) ? 0 : childNodes.hashCode());
383 public boolean equals(Object obj) {
390 if (getClass() != obj.getClass()) {
393 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
394 if (targetPath == null) {
395 if (other.targetPath != null) {
398 } else if (!targetPath.equals(other.targetPath)) {
401 if (whenCondition == null) {
402 if (other.whenCondition != null) {
405 } else if (!whenCondition.equals(other.whenCondition)) {
408 if (childNodes == null) {
409 if (other.childNodes != null) {
412 } else if (!childNodes.equals(other.childNodes)) {
419 public String toString() {
420 StringBuilder sb = new StringBuilder(
421 AugmentationSchemaImpl.class.getSimpleName());
423 sb.append("targetPath=" + targetPath);
424 sb.append(", childNodes=" + childNodes.values());
425 sb.append(", groupings=" + groupings);
426 sb.append(", uses=" + uses);
428 return sb.toString();