Migrate BitsSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / BitsSpecificationSupport.java
index 6a215826894c872c18e3a03d2df9d16cf5eb9af0..8640170f866a51eb55bc90068172d30b3a7f6bb4 100644 (file)
@@ -7,15 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
+import java.util.Optional;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 final class BitsSpecificationSupport
-        extends AbstractStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
+        extends BaseStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
         .addMultiple(YangStmtMapping.BIT)
@@ -31,18 +40,71 @@ final class BitsSpecificationSupport
     }
 
     @Override
-    public BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
-        return new BitsSpecificationImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<String, BitsSpecification> createEffective(
-            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
-        return new BitsSpecificationEffectiveStatement(ctx);
+    protected BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new BitsSpecificationImpl(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected BitsSpecification createEmptyDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
+        throw noBits(ctx);
+    }
+
+    @Override
+    protected EffectiveStatement<String, BitsSpecification> createEffective(
+            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
+            final BitsSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
+        Uint32 highestPosition = null;
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BitEffectiveStatement) {
+                final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
+
+                final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
+                final Uint32 effectivePos;
+                if (declaredPosition.isEmpty()) {
+                    if (highestPosition != null) {
+                        SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
+                            ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
+                        effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
+                    } else {
+                        effectivePos = Uint32.ZERO;
+                    }
+                } else {
+                    effectivePos = declaredPosition.get();
+                }
+
+                final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
+                if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
+                    highestPosition = bit.getPosition();
+                }
+
+                builder.addBit(bit);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected EffectiveStatement<String, BitsSpecification> createEmptyEffective(
+            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
+            final BitsSpecification declared) {
+        throw noBits(ctx);
+    }
+
+    private static SourceException noBits(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.7.4:
+         *
+         *     The "bit" statement, which is a substatement to the "type" statement,
+         *     MUST be present if the type is "bits".
+         */
+        return new SourceException("At least one bit statement has to be present", ctx.getStatementSourceReference());
     }
-}
\ No newline at end of file
+}