/* * 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.yangtools.yang.parser.builder.impl; import java.util.List; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.parser.builder.api.AbstractTypeAwareBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.util.YangParseException; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder { private LeafSchemaNodeImpl instance; private String defaultStr; private String unitsStr; // SchemaNode args private SchemaPath schemaPath; private String description; private String reference; private Status status = Status.CURRENT; // DataSchemaNode args private boolean augmenting; private boolean addedByUses; private boolean configuration; private final ConstraintsBuilder constraints; public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) { super(moduleName, line, qname); this.schemaPath = schemaPath; constraints = new ConstraintsBuilder(moduleName, line); } public LeafSchemaNodeBuilder(String moduleName, int line, QName qname, SchemaPath path, LeafSchemaNode base) { super(moduleName, line, qname); this.schemaPath = path; constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints()); description = base.getDescription(); reference = base.getReference(); status = base.getStatus(); augmenting = base.isAugmenting(); addedByUses = base.isAddedByUses(); configuration = base.isConfiguration(); this.type = base.getType(); unknownNodes.addAll(base.getUnknownSchemaNodes()); defaultStr = base.getDefault(); unitsStr = base.getUnits(); } @Override public LeafSchemaNode build() { if (instance != null) { return instance; } instance = new LeafSchemaNodeImpl(qname, schemaPath); instance.description = description; instance.reference = reference; instance.status = status; instance.augmenting = augmenting; instance.addedByUses = addedByUses; instance.configuration = configuration; instance.constraintsDef = constraints.build(); instance.defaultStr = defaultStr; instance.unitsStr = unitsStr; if (type == null && typedef == null) { throw new YangParseException(moduleName, line, "Failed to resolve leaf type."); } // TYPE if (type == null) { instance.type = typedef.build(); } else { instance.type = type; } // UNKNOWN NODES for (UnknownSchemaNodeBuilder b : addedUnknownNodes) { unknownNodes.add(b.build()); } instance.unknownNodes = ImmutableList.copyOf(unknownNodes); return instance; } @Override public SchemaPath getPath() { return schemaPath; } @Override public void setPath(SchemaPath path) { this.schemaPath = path; } @Override public ConstraintsBuilder getConstraints() { return constraints; } @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(Status status) { this.status = Preconditions.checkNotNull(status, "status cannot be null"); } @Override public boolean isAugmenting() { return augmenting; } @Override public void setAugmenting(final boolean augmenting) { this.augmenting = augmenting; } @Override public boolean isAddedByUses() { return addedByUses; } @Override public void setAddedByUses(final boolean addedByUses) { this.addedByUses = addedByUses; } @Override public boolean isConfiguration() { return configuration; } @Override public void setConfiguration(final boolean configuration) { this.configuration = configuration; } public String getDefaultStr() { return defaultStr; } public void setDefaultStr(String defaultStr) { this.defaultStr = defaultStr; } public String getUnits() { return unitsStr; } public void setUnits(String unitsStr) { this.unitsStr = unitsStr; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((schemaPath == null) ? 0 : schemaPath.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; } LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj; if (schemaPath == null) { if (other.schemaPath != null) { return false; } } else if (!schemaPath.equals(other.schemaPath)) { return false; } if (parentBuilder == null) { if (other.parentBuilder != null) { return false; } } else if (!parentBuilder.equals(other.parentBuilder)) { return false; } return true; } @Override public String toString() { return "leaf " + qname.getLocalName(); } private static final class LeafSchemaNodeImpl implements LeafSchemaNode { private final QName qname; private final SchemaPath path; private String description; private String reference; private Status status; private boolean augmenting; private boolean addedByUses; private boolean configuration; private ConstraintDefinition constraintsDef; private TypeDefinition type; private ImmutableList unknownNodes; private String defaultStr; private String unitsStr; private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) { this.qname = qname; this.path = path; } @Override public QName getQName() { return qname; } @Override public SchemaPath getPath() { return path; } @Override public String getDescription() { return description; } @Override public String getReference() { return reference; } @Override public Status getStatus() { return status; } @Override public boolean isAugmenting() { return augmenting; } @Override public boolean isAddedByUses() { return addedByUses; } @Override public boolean isConfiguration() { return configuration; } @Override public ConstraintDefinition getConstraints() { return constraintsDef; } @Override public TypeDefinition getType() { return type; } @Override public List getUnknownSchemaNodes() { return unknownNodes; } @Override public String getDefault() { return defaultStr; } @Override public String getUnits() { return unitsStr; } @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; } LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) 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(LeafSchemaNodeImpl.class.getSimpleName()); sb.append("["); sb.append("qname=" + qname); sb.append(", path=" + path); sb.append("]"); return sb.toString(); } } }