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;
33 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
34 private final AugmentationSchemaImpl instance;
35 private final int line;
36 private final String augmentTargetStr;
37 private SchemaPath augmentTarget;
38 private SchemaPath finalAugmentTarget;
39 private String whenCondition;
40 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
41 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
42 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
43 private boolean resolved;
45 AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
46 this.augmentTargetStr = augmentTargetStr;
48 final SchemaPath targetPath = YangModelBuilderUtil
49 .parseAugmentPath(augmentTargetStr);
50 augmentTarget = targetPath;
51 instance = new AugmentationSchemaImpl(targetPath);
55 public int getLine() {
60 public void addChildNode(DataSchemaNodeBuilder childNode) {
61 childNodes.add(childNode);
65 public Set<DataSchemaNodeBuilder> getChildNodes() {
70 public void addGrouping(GroupingBuilder grouping) {
71 groupings.add(grouping);
75 public void addUsesNode(UsesNodeBuilder usesBuilder) {
76 usesNodes.add(usesBuilder);
80 * Always returns null.
83 public QName getQName() {
88 * Always returns null.
91 public SchemaPath getPath() {
96 public AugmentationSchema build() {
97 instance.setTargetPath(finalAugmentTarget);
99 RevisionAwareXPath whenStmt;
100 if (whenCondition == null) {
103 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
105 instance.setWhenCondition(whenStmt);
108 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
109 for (DataSchemaNodeBuilder node : childNodes) {
110 childs.put(node.getQName(), node.build());
112 instance.setChildNodes(childs);
115 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
116 for (GroupingBuilder builder : groupings) {
117 groupingDefinitions.add(builder.build());
119 instance.setGroupings(groupingDefinitions);
122 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
123 for (UsesNodeBuilder builder : usesNodes) {
124 usesNodeDefinitions.add(builder.build());
126 instance.setUses(usesNodeDefinitions);
132 public boolean isResolved() {
137 public void setResolved(boolean resolved) {
138 this.resolved = resolved;
141 public String getWhenCondition() {
142 return whenCondition;
145 public void addWhenCondition(String whenCondition) {
146 this.whenCondition = whenCondition;
150 public void addTypedef(TypeDefinitionBuilder type) {
151 throw new UnsupportedOperationException(
152 "Augmentation can not contains type definitions");
156 public void setDescription(String description) {
157 instance.setDescription(description);
161 public void setReference(String reference) {
162 instance.setReference(reference);
166 public void setStatus(Status status) {
167 instance.setStatus(status);
171 public SchemaPath getTargetPath() {
172 return augmentTarget;
176 public void setTargetPath(SchemaPath path) {
177 this.finalAugmentTarget = path;
181 public String getTargetPathAsString() {
182 return augmentTargetStr;
186 public int hashCode() {
187 final int prime = 17;
189 result = prime * result
190 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
191 result = prime * result
192 + ((childNodes == null) ? 0 : childNodes.hashCode());
193 result = prime * result
194 + ((groupings == null) ? 0 : groupings.hashCode());
195 result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
196 result = prime * result
197 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
202 public boolean equals(Object obj) {
209 if (getClass() != obj.getClass()) {
212 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
213 if (augmentTargetStr == null) {
214 if (other.augmentTargetStr != null) {
217 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
220 if (childNodes == null) {
221 if (other.childNodes != null) {
224 } else if (!childNodes.equals(other.childNodes)) {
227 if (groupings == null) {
228 if (other.groupings != null) {
231 } else if (!groupings.equals(other.groupings)) {
234 if (usesNodes == null) {
235 if (other.usesNodes != null) {
238 } else if (!usesNodes.equals(other.usesNodes)) {
241 if (whenCondition == null) {
242 if (other.whenCondition != null) {
245 } else if (!whenCondition.equals(other.whenCondition)) {
252 private static class AugmentationSchemaImpl implements AugmentationSchema {
253 private SchemaPath targetPath;
254 private RevisionAwareXPath whenCondition;
255 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
256 private Set<GroupingDefinition> groupings = Collections.emptySet();
257 private Set<UsesNode> uses = Collections.emptySet();
259 private String description;
260 private String reference;
261 private Status status;
263 private AugmentationSchemaImpl(SchemaPath targetPath) {
264 this.targetPath = targetPath;
268 public SchemaPath getTargetPath() {
272 private void setTargetPath(SchemaPath path) {
273 this.targetPath = path;
277 public RevisionAwareXPath getWhenCondition() {
278 return whenCondition;
281 private void setWhenCondition(RevisionAwareXPath whenCondition) {
282 this.whenCondition = whenCondition;
286 public Set<DataSchemaNode> getChildNodes() {
287 return new HashSet<DataSchemaNode>(childNodes.values());
290 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
291 if (childNodes != null) {
292 this.childNodes = childNodes;
297 public Set<GroupingDefinition> getGroupings() {
301 private void setGroupings(Set<GroupingDefinition> groupings) {
302 if (groupings != null) {
303 this.groupings = groupings;
308 public Set<UsesNode> getUses() {
312 private void setUses(Set<UsesNode> uses) {
319 * Always returns an empty set, because augmentation can not contains
323 public Set<TypeDefinition<?>> getTypeDefinitions() {
324 return Collections.emptySet();
328 public String getDescription() {
332 private void setDescription(String description) {
333 this.description = description;
337 public String getReference() {
341 private void setReference(String reference) {
342 this.reference = reference;
346 public Status getStatus() {
350 private void setStatus(Status status) {
351 this.status = status;
355 public DataSchemaNode getDataChildByName(QName name) {
356 return childNodes.get(name);
360 public DataSchemaNode getDataChildByName(String name) {
361 DataSchemaNode result = null;
362 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
363 if (entry.getKey().getLocalName().equals(name)) {
364 result = entry.getValue();
372 public int hashCode() {
373 final int prime = 17;
375 result = prime * result
376 + ((targetPath == null) ? 0 : targetPath.hashCode());
377 result = prime * result
378 + ((childNodes == null) ? 0 : childNodes.hashCode());
379 result = prime * result
380 + ((groupings == null) ? 0 : groupings.hashCode());
381 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
382 result = prime * result
383 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
388 public boolean equals(Object obj) {
395 if (getClass() != obj.getClass()) {
398 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
399 if (targetPath == null) {
400 if (other.targetPath != null) {
403 } else if (!targetPath.equals(other.targetPath)) {
406 if (childNodes == null) {
407 if (other.childNodes != null) {
410 } else if (!childNodes.equals(other.childNodes)) {
413 if (groupings == null) {
414 if (other.groupings != null) {
417 } else if (!groupings.equals(other.groupings)) {
421 if (other.uses != null) {
424 } else if (!uses.equals(other.uses)) {
427 if (whenCondition == null) {
428 if (other.whenCondition != null) {
431 } else if (!whenCondition.equals(other.whenCondition)) {
438 public String toString() {
439 StringBuilder sb = new StringBuilder(
440 AugmentationSchemaImpl.class.getSimpleName());
442 sb.append("targetPath=" + targetPath);
443 sb.append(", childNodes=" + childNodes.values());
444 sb.append(", groupings=" + groupings);
445 sb.append(", uses=" + uses);
447 return sb.toString();