--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.yang.parser.builder.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.opendaylight.controller.yang.common.QName;
+import org.opendaylight.controller.yang.model.api.AugmentationSchema;
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;
+import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.model.api.NotificationDefinition;
+import org.opendaylight.controller.yang.model.api.SchemaPath;
+import org.opendaylight.controller.yang.model.api.Status;
+import org.opendaylight.controller.yang.model.api.TypeDefinition;
+import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.controller.yang.model.api.UsesNode;
+import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.controller.yang.parser.util.Comparators;
+
+public final class NotificationBuilder extends AbstractDataNodeContainerBuilder implements SchemaNodeBuilder,
+ AugmentationTargetBuilder {
+ private boolean isBuilt;
+ private final NotificationDefinitionImpl instance;
+ private SchemaPath schemaPath;
+ private String description;
+ private String reference;
+ private Status status = Status.CURRENT;
+ private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
+ private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
+ private Set<AugmentationSchema> augmentations;
+ private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
+
+ NotificationBuilder(final String moduleName, final int line, final QName qname) {
+ super(moduleName, line, qname);
+ instance = new NotificationDefinitionImpl(qname);
+ }
+
+ @Override
+ public NotificationDefinition build() {
+ if (!isBuilt) {
+ instance.setPath(schemaPath);
+ instance.setDescription(description);
+ instance.setReference(reference);
+ instance.setStatus(status);
+
+ // CHILD NODES
+ final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
+ for (DataSchemaNodeBuilder node : addedChildNodes) {
+ childs.put(node.getQName(), node.build());
+ }
+ instance.setChildNodes(childs);
+
+ // GROUPINGS
+ final Set<GroupingDefinition> groupingDefs = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
+ for (GroupingBuilder builder : addedGroupings) {
+ groupingDefs.add(builder.build());
+ }
+ instance.setGroupings(groupingDefs);
+
+ // TYPEDEFS
+ final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
+ for (TypeDefinitionBuilder entry : addedTypedefs) {
+ typedefs.add(entry.build());
+ }
+ instance.setTypeDefinitions(typedefs);
+
+ // USES
+ final Set<UsesNode> uses = new HashSet<UsesNode>();
+ for (UsesNodeBuilder builder : addedUsesNodes) {
+ uses.add(builder.build());
+ }
+ instance.setUses(uses);
+
+ // AUGMENTATIONS
+ if (augmentations == null) {
+ augmentations = new HashSet<AugmentationSchema>();
+ for (AugmentationSchemaBuilder builder : addedAugmentations) {
+ augmentations.add(builder.build());
+ }
+ }
+ instance.setAvailableAugmentations(augmentations);
+
+ // UNKNOWN NODES
+ final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
+ for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+ unknownNodes.add(b.build());
+ }
+ Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
+ instance.setUnknownSchemaNodes(unknownNodes);
+
+ isBuilt = true;
+ }
+
+ return instance;
+ }
+
+ @Override
+ public void rebuild() {
+ isBuilt = false;
+ build();
+ }
+
+ @Override
+ public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
+ return addedTypedefs;
+ }
+
+ @Override
+ public void addTypedef(final TypeDefinitionBuilder type) {
+ addedTypedefs.add(type);
+ }
+
+ @Override
+ public void addUsesNode(final UsesNodeBuilder usesNodeBuilder) {
+ addedUsesNodes.add(usesNodeBuilder);
+ }
+
+ @Override
+ public SchemaPath getPath() {
+ return schemaPath;
+ }
+
+ @Override
+ public void setPath(SchemaPath schemaPath) {
+ this.schemaPath = schemaPath;
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ @Override
+ public void setDescription(final String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String getReference() {
+ return reference;
+ }
+
+ @Override
+ public void setReference(final String reference) {
+ this.reference = reference;
+ }
+
+ @Override
+ public Status getStatus() {
+ return status;
+ }
+
+ @Override
+ public void setStatus(final Status status) {
+ if (status != null) {
+ this.status = status;
+ }
+ }
+
+ public Set<AugmentationSchemaBuilder> getAugmentations() {
+ return addedAugmentations;
+ }
+
+ @Override
+ public void addAugmentation(AugmentationSchemaBuilder augment) {
+ addedAugmentations.add(augment);
+ }
+
+ public void setAugmentations(final Set<AugmentationSchema> augmentations) {
+ this.augmentations = augmentations;
+ }
+
+ @Override
+ public String toString() {
+ return "notification " + getQName().getLocalName();
+ }
+
+ public final class NotificationDefinitionImpl implements NotificationDefinition {
+ private final QName qname;
+ private SchemaPath path;
+ private String description;
+ private String reference;
+ private Status status = Status.CURRENT;
+ private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
+ private Set<GroupingDefinition> groupings = Collections.emptySet();
+ private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
+ private Set<UsesNode> uses = Collections.emptySet();
+ private Set<AugmentationSchema> augmentations = Collections.emptySet();
+ private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
+
+ private NotificationDefinitionImpl(final QName qname) {
+ this.qname = qname;
+ }
+
+ @Override
+ public QName getQName() {
+ return qname;
+ }
+
+ @Override
+ public SchemaPath getPath() {
+ return path;
+ }
+
+ private void setPath(final SchemaPath path) {
+ this.path = path;
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ private void setDescription(final String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String getReference() {
+ return reference;
+ }
+
+ private void setReference(String reference) {
+ this.reference = reference;
+ }
+
+ @Override
+ public Status getStatus() {
+ return status;
+ }
+
+ private void setStatus(Status status) {
+ if (status != null) {
+ this.status = status;
+ }
+ }
+
+ @Override
+ public Set<DataSchemaNode> getChildNodes() {
+ return new HashSet<DataSchemaNode>(childNodes.values());
+ }
+
+ private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
+ if (childNodes != null) {
+ this.childNodes = childNodes;
+ }
+ }
+
+ @Override
+ public Set<GroupingDefinition> getGroupings() {
+ return groupings;
+ }
+
+ private void setGroupings(Set<GroupingDefinition> groupings) {
+ if (groupings != null) {
+ this.groupings = groupings;
+ }
+ }
+
+ @Override
+ public Set<UsesNode> getUses() {
+ return uses;
+ }
+
+ private void setUses(Set<UsesNode> uses) {
+ if (uses != null) {
+ this.uses = uses;
+ }
+ }
+
+ @Override
+ public Set<TypeDefinition<?>> getTypeDefinitions() {
+ return typeDefinitions;
+ }
+
+ private void setTypeDefinitions(final Set<TypeDefinition<?>> typeDefinitions) {
+ if (typeDefinitions != null) {
+ this.typeDefinitions = typeDefinitions;
+ }
+ }
+
+ @Override
+ public Set<AugmentationSchema> getAvailableAugmentations() {
+ return augmentations;
+ }
+
+ private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
+ if (augmentations != null) {
+ this.augmentations = augmentations;
+ }
+ }
+
+ @Override
+ public List<UnknownSchemaNode> getUnknownSchemaNodes() {
+ return unknownNodes;
+ }
+
+ private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
+ if (unknownNodes != null) {
+ this.unknownNodes = unknownNodes;
+ }
+ }
+
+ @Override
+ public DataSchemaNode getDataChildByName(QName name) {
+ return childNodes.get(name);
+ }
+
+ @Override
+ public DataSchemaNode getDataChildByName(String name) {
+ DataSchemaNode result = null;
+ for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
+ if (entry.getKey().getLocalName().equals(name)) {
+ result = entry.getValue();
+ break;
+ }
+ }
+ return result;
+ }
+
+ public NotificationBuilder toBuilder() {
+ return NotificationBuilder.this;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((qname == null) ? 0 : qname.hashCode());
+ result = prime * result + ((path == null) ? 0 : path.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
+ if (qname == null) {
+ if (other.qname != null) {
+ return false;
+ }
+ } else if (!qname.equals(other.qname)) {
+ return false;
+ }
+ if (path == null) {
+ if (other.path != null) {
+ return false;
+ }
+ } else if (!path.equals(other.path)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder(NotificationDefinitionImpl.class.getSimpleName());
+ sb.append("[qname=" + qname + ", path=" + path + "]");
+ return sb.toString();
+ }
+ }
+
+}