/* * 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.model.parser.builder; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.opendaylight.controller.model.parser.api.AugmentationSchemaBuilder; import org.opendaylight.controller.model.parser.api.DataSchemaNodeBuilder; import org.opendaylight.controller.model.parser.api.GroupingBuilder; import org.opendaylight.controller.model.parser.api.TypeDefinitionBuilder; import org.opendaylight.controller.model.parser.api.UsesNodeBuilder; import org.opendaylight.controller.model.parser.util.YangModelBuilderHelper; 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.SchemaPath; import org.opendaylight.controller.yang.model.api.Status; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UsesNode; public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder { private final AugmentationSchemaImpl instance; private final SchemaPath augmentTarget; final Set childNodes = new HashSet(); final Set groupings = new HashSet(); private final Set usesNodes = new HashSet(); AugmentationSchemaBuilderImpl(String augmentPath) { SchemaPath targetPath = YangModelBuilderHelper.parsePath(augmentPath); augmentTarget = targetPath; instance = new AugmentationSchemaImpl(targetPath); } @Override public void addChildNode(DataSchemaNodeBuilder childNode) { childNodes.add(childNode); } @Override public void addGrouping(GroupingBuilder grouping) { groupings.add(grouping); } /** * Always returns null. */ @Override public QName getQName() { return null; } @Override public AugmentationSchema build() { // CHILD NODES Map childs = new HashMap(); for(DataSchemaNodeBuilder node : childNodes) { childs.put(node.getQName(), node.build()); } instance.setChildNodes(childs); // GROUPINGS Set groupingDefinitions = new HashSet(); for(GroupingBuilder builder : groupings) { groupingDefinitions.add(builder.build()); } instance.setGroupings(groupingDefinitions); // USES Set usesNodeDefinitions = new HashSet(); for(UsesNodeBuilder builder : usesNodes) { usesNodeDefinitions.add(builder.build()); } instance.setUses(usesNodeDefinitions); return instance; } @Override public void addUsesNode(UsesNodeBuilder usesBuilder) { usesNodes.add(usesBuilder); } @Override public void addTypedef(TypeDefinitionBuilder type) { throw new UnsupportedOperationException("Augmentation can not contains type definitions"); } @Override public void setDescription(String description) { instance.setDescription(description); } @Override public void setReference(String reference) { instance.setReference(reference); } @Override public void setStatus(Status status) { instance.setStatus(status); } @Override public SchemaPath getTargetPath() { return augmentTarget; } private static class AugmentationSchemaImpl implements AugmentationSchema { private final SchemaPath targetPath; private Map childNodes; private Set groupings; private Set uses; private String description; private String reference; private Status status; private AugmentationSchemaImpl(SchemaPath targetPath) { this.targetPath = targetPath; } @Override public SchemaPath getTargetPath() { return targetPath; } @Override public Set getChildNodes() { return new HashSet(childNodes.values()); } private void setChildNodes(Map childNodes) { this.childNodes = childNodes; } @Override public Set getGroupings() { return groupings; } private void setGroupings(Set groupings) { this.groupings = groupings; } @Override public Set getUses() { return uses; } private void setUses(Set uses) { this.uses = uses; } /** * This method always returns null, because augmentation can not contains type definitions. */ @Override public Set> getTypeDefinitions() { return null; } @Override public String getDescription() { return description; } private void setDescription(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) { this.status = status; } @Override public DataSchemaNode getDataChildByName(QName name) { return childNodes.get(name); } @Override public DataSchemaNode getDataChildByName(String name) { DataSchemaNode result = null; for(Map.Entry entry : childNodes.entrySet()) { if(entry.getKey().getLocalName().equals(name)) { result = entry.getValue(); break; } } return result; } @Override public int hashCode() { final int prime = 17; int result = 1; result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode()); result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode()); result = prime * result + ((groupings == null) ? 0 : groupings.hashCode()); result = prime * result + ((uses == null) ? 0 : uses.hashCode()); result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + ((reference == null) ? 0 : reference.hashCode()); result = prime * result + ((status == null) ? 0 : status.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; } AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj; if (targetPath == null) { if (other.targetPath != null) { return false; } } else if (!targetPath.equals(other.targetPath)) { return false; } if (childNodes == null) { if (other.childNodes != null) { return false; } } else if (!childNodes.equals(other.childNodes)) { return false; } if (groupings == null) { if (other.groupings != null) { return false; } } else if (!groupings.equals(other.groupings)) { return false; } if (uses == null) { if (other.uses != null) { return false; } } else if (!uses.equals(other.uses)) { return false; } if (description == null) { if (other.description != null) { return false; } } else if (!description.equals(other.description)) { return false; } if (reference == null) { if (other.reference != null) { return false; } } else if (!reference.equals(other.reference)) { return false; } if (status == null) { if (other.status != null) { return false; } } else if (!status.equals(other.status)) { return false; } return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName()); sb.append("["); sb.append("targetPath="+ targetPath); sb.append(", childNodes="+ childNodes.values()); sb.append(", groupings="+ groupings); sb.append(", uses="+ uses); sb.append("]"); return sb.toString(); } } }