X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Frfc6020%2Feffective%2Ftype%2FBitsSpecificationEffectiveStatementImpl.java;h=b688f18f46d00c8fb0d6933940b1eba63d358ecf;hb=04fa25a4fe8957f6492618aa9a1e9a4f9af39df4;hp=8ce3d864c20e56e8c115fd5ed65a9cb6b8c0cbe2;hpb=30c67c4d780e897d972d60bb79d546b512516f90;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java index 8ce3d864c2..b688f18f46 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java @@ -7,173 +7,67 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import org.opendaylight.yangtools.yang.model.util.BitsType; - -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit; +import org.opendaylight.yangtools.yang.model.util.BitImpl; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; - -public class BitsSpecificationEffectiveStatementImpl extends - EffectiveStatementBase implements BitsTypeDefinition, TypeDefinitionEffectiveBuilder { +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; - private static final QName QNAME = BaseTypes.BITS_QNAME; - private static final String DESCRIPTION = "The bits built-in type represents a bit set. " - + "That is, a bits value is a set of flags identified by small integer position " - + "numbers starting at 0. Each bit number has an assigned name."; +public final class BitsSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.7"; - private static final String UNITS = ""; - private final SchemaPath path; - private final List bits; + private final BitsTypeDefinition typeDefinition; - public BitsSpecificationEffectiveStatementImpl(StmtContext> ctx) { + public BitsSpecificationEffectiveStatementImpl(final StmtContext> ctx) { super(ctx); - List bitsInit = new ArrayList<>(); - - path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME); - - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof Bit) { - bitsInit.add(((Bit) effectiveStatement)); + final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get()); + Long highestPosition = null; + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof Bit) { + Bit b = (Bit) stmt; + + if (b.getPosition() == null) { + final Long newPos; + if (highestPosition == null) { + newPos = 0L; + } else if (highestPosition != 4294967295L) { + newPos = highestPosition + 1; + } else { + throw new SourceException(ctx.getStatementSourceReference(), + "Bit %s must have a position statement", b); + } + + b = new BitImpl(newPos, b.getQName(), b.getPath(), b.getDescription(), b.getReference(), + b.getStatus(), b.getUnknownSchemaNodes()); + } + + SourceException.throwIf(b.getPosition() < 0L && b.getPosition() > 4294967295L, + ctx.getStatementSourceReference(), "Bit %s has illegal position", b); + + if (highestPosition == null || highestPosition < b.getPosition()) { + highestPosition = b.getPosition(); + } + + builder.addBit(b); } - } - - bits = ImmutableList.copyOf(bitsInit); - } - - @Override - public List getBits() { - return bits; - } - - @Override - public BitsTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return bits; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((bits == null) ? 0 : bits.hashCode()); - result = prime * result + QNAME.hashCode(); - result = prime * result + path.hashCode(); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - BitsSpecificationEffectiveStatementImpl other = (BitsSpecificationEffectiveStatementImpl) obj; - if (bits == null) { - if (other.bits != null) { - return false; - } - } else if (!bits.equals(other.bits)) { - return false; - } - if (path == null) { - if (other.path != null) { - return false; + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } - } else if (!path.equals(other.path)) { - return false; } - return true; - } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(BitsSpecificationEffectiveStatementImpl.class.getSimpleName()); - builder.append(" [name="); - builder.append(QNAME); - builder.append(", path="); - builder.append(path); - builder.append(", description="); - builder.append(DESCRIPTION); - builder.append(", reference="); - builder.append(REFERENCE); - builder.append(", bits="); - builder.append(bits); - builder.append(", units="); - builder.append(UNITS); - builder.append("]"); - return builder.toString(); + typeDefinition = builder.build(); } - private BitsType bitsTypeInstance = null; @Override - public TypeDefinition buildType() { - - if(bitsTypeInstance != null) { - return bitsTypeInstance; - } - - bitsTypeInstance = BitsType.create(path, bits); - - return bitsTypeInstance; + public BitsTypeDefinition getTypeDefinition() { + return typeDefinition; } }