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.Date;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.Status;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.CopyUtils;
31 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
32 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableSet;
38 public final class GroupingBuilderImpl extends AbstractDataNodeContainerBuilder implements GroupingBuilder {
39 private GroupingDefinitionImpl instance;
41 private SchemaPath schemaPath;
42 private String description;
43 private String reference;
44 private Status status = Status.CURRENT;
45 // DataSchemaNode args
46 private boolean addedByUses;
48 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
49 super(moduleName, line, qname);
53 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
54 final GroupingDefinition base) {
55 super(moduleName, line, base.getQName());
58 description = base.getDescription();
59 reference = base.getReference();
60 status = base.getStatus();
61 addedByUses = base.isAddedByUses();
63 URI ns = qname.getNamespace();
64 Date rev = qname.getRevision();
65 String pref = qname.getPrefix();
66 addedChildNodes.addAll(ParserUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
67 addedGroupings.addAll(ParserUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
68 addedTypedefs.addAll(ParserUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
69 addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
72 usesNodes.addAll(base.getUses());
76 public GroupingDefinition build() {
77 if (instance != null) {
81 instance = new GroupingDefinitionImpl(qname, schemaPath);
83 instance.description = description;
84 instance.reference = reference;
85 instance.status = status;
86 instance.addedByUses = addedByUses;
89 for (DataSchemaNodeBuilder node : addedChildNodes) {
90 childNodes.put(node.getQName(), node.build());
92 instance.childNodes = ImmutableSet.copyOf(childNodes.values());
95 for (GroupingBuilder builder : addedGroupings) {
96 groupings.add(builder.build());
98 instance.groupings = ImmutableSet.copyOf(groupings);
101 for (TypeDefinitionBuilder entry : addedTypedefs) {
102 typedefs.add(entry.build());
104 instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
107 for (UsesNodeBuilder builder : addedUsesNodes) {
108 usesNodes.add(builder.build());
110 instance.uses = ImmutableSet.copyOf(usesNodes);
113 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
114 unknownNodes.add(b.build());
116 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
122 public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
123 final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
124 for (DataSchemaNodeBuilder node : addedChildNodes) {
125 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
126 ParserUtils.setNodeAddedByUses(copy);
133 public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
134 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
135 for (TypeDefinitionBuilder node : addedTypedefs) {
136 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
143 public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
144 final Set<GroupingBuilder> nodes = new HashSet<>();
145 for (GroupingBuilder node : addedGroupings) {
146 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
147 copy.setAddedByUses(true);
148 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
149 ParserUtils.setNodeAddedByUses(childNode);
157 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
158 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
159 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
160 UnknownSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
161 copy.setAddedByUses(true);
168 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
169 return addedTypedefs;
173 public void addTypedef(final TypeDefinitionBuilder type) {
174 String typeName = type.getQName().getLocalName();
175 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
176 throw new YangParseException(getModuleName(), type.getLine(), "Can not add typedef '" + typeName
177 + "': typedef with same name already declared at line " + addedTypedef.getLine());
179 addedTypedefs.add(type);
183 public SchemaPath getPath() {
188 public void setPath(final SchemaPath path) {
189 this.schemaPath = path;
193 public String getDescription() {
198 public void setDescription(final String description) {
199 this.description = description;
203 public String getReference() {
208 public void setReference(final String reference) {
209 this.reference = reference;
213 public Status getStatus() {
218 public void setStatus(final Status status) {
219 this.status = Preconditions.checkNotNull(status, "status cannot be null");
223 public boolean isAddedByUses() {
228 public void setAddedByUses(final boolean addedByUses) {
229 this.addedByUses = addedByUses;
233 public String toString() {
234 return "grouping " + qname.getLocalName();
238 public int hashCode() {
239 final int prime = 31;
241 result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
242 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
247 public boolean equals(final Object obj) {
254 if (getClass() != obj.getClass()) {
257 if (!super.equals(obj)) {
260 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
261 if (getParent() == null) {
262 if (other.getParent() != null) {
265 } else if (!getParent().equals(other.getParent())) {
268 if (schemaPath == null) {
269 if (other.schemaPath != null) {
272 } else if (!schemaPath.equals(other.schemaPath)) {
278 private static final class GroupingDefinitionImpl implements GroupingDefinition {
279 private final QName qname;
280 private final SchemaPath path;
281 private String description;
282 private String reference;
283 private Status status;
284 private boolean addedByUses;
285 private ImmutableSet<DataSchemaNode> childNodes;
286 private ImmutableSet<GroupingDefinition> groupings;
287 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
288 private ImmutableSet<UsesNode> uses;
289 private ImmutableList<UnknownSchemaNode> unknownNodes;
291 private GroupingDefinitionImpl(final QName qname, final SchemaPath path) {
297 public QName getQName() {
302 public SchemaPath getPath() {
307 public String getDescription() {
312 public String getReference() {
317 public Status getStatus() {
322 public boolean isAddedByUses() {
327 public Set<DataSchemaNode> getChildNodes() {
332 public Set<GroupingDefinition> getGroupings() {
337 public Set<UsesNode> getUses() {
342 public Set<TypeDefinition<?>> getTypeDefinitions() {
343 return typeDefinitions;
347 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
352 public DataSchemaNode getDataChildByName(final QName name) {
353 return getChildNode(childNodes, name);
357 public DataSchemaNode getDataChildByName(final String name) {
358 return getChildNode(childNodes, name);
362 public int hashCode() {
363 final int prime = 31;
365 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
366 result = prime * result + ((path == null) ? 0 : path.hashCode());
371 public boolean equals(final Object obj) {
378 if (getClass() != obj.getClass()) {
381 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
383 if (other.qname != null) {
386 } else if (!qname.equals(other.qname)) {
390 if (other.path != null) {
393 } else if (!path.equals(other.path)) {
400 public String toString() {
401 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
403 sb.append("qname=" + qname);
405 return sb.toString();