Consolidate BitsTypeEffectiveStatementImpl 88/90888/4
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 17:59:18 +0000 (19:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
TypeEffectiveStatementImpl can easily support the case of an bits
type, migrate it.

JIRA: YANGTOOLS-1065
Change-Id: I80bb47dd6b5aa77aad619c271cea70fdab2547d1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/BitsTypeEffectiveStatementImpl.java [deleted file]

index dbad0d5e8191cd3f4b316f64c2894c868a44accb..6a3a9aeb780b905216f7d5e269a3dc6c3865043b 100644 (file)
@@ -12,13 +12,17 @@ import static com.google.common.base.Preconditions.checkArgument;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import java.util.Collection;
+import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 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.LengthEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
@@ -26,6 +30,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
@@ -43,6 +48,7 @@ import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
@@ -217,7 +223,7 @@ abstract class AbstractTypeStatementSupport
         if (baseType instanceof BinaryTypeDefinition) {
             return createBinary(ctx, (BinaryTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof BitsTypeDefinition) {
-            return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
+            return createBits(ctx, (BitsTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof BooleanTypeDefinition) {
             return createBoolean(ctx, (BooleanTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof DecimalTypeDefinition) {
@@ -353,6 +359,34 @@ abstract class AbstractTypeStatementSupport
         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
     }
 
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createBits(final StmtContext<?, ?, ?> ctx,
+            final BitsTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
+
+        final YangVersion yangVersion = ctx.getRootVersion();
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BitEffectiveStatement) {
+                SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
+                        "Restricted bits type is allowed only in YANG 1.1 version.");
+                final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
+
+                // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
+                final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
+                final Uint32 effectivePos;
+                if (declaredPosition.isEmpty()) {
+                    effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
+                } else {
+                    effectivePos = declaredPosition.get();
+                }
+
+                builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
     private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final StmtContext<?, ?, ?> ctx,
             final BooleanTypeDefinition baseType, final TypeStatement declared,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
@@ -380,4 +414,16 @@ abstract class AbstractTypeStatementSupport
         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
             typeEffectiveSchemaPath(ctx)));
     }
+
+    private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
+            final StmtContext<?, ?, ?> ctx) {
+        for (Bit baseTypeBit : baseType.getBits()) {
+            if (bitName.equals(baseTypeBit.getName())) {
+                return baseTypeBit.getPosition();
+            }
+        }
+
+        throw new SourceException(ctx.getStatementSourceReference(),
+                "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
+    }
 }
\ No newline at end of file
diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/BitsTypeEffectiveStatementImpl.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/BitsTypeEffectiveStatementImpl.java
deleted file mode 100644 (file)
index e0c83b3..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (c) 2015 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.rfc7950.stmt.type;
-
-import java.util.Optional;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.common.YangVersion;
-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.BitEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
-import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
-import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-
-final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
-        implements TypeEffectiveStatement<TypeStatement> {
-
-    private final @NonNull BitsTypeDefinition typeDefinition;
-
-    BitsTypeEffectiveStatementImpl(
-            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
-            final BitsTypeDefinition baseType) {
-        super(ctx);
-
-        final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
-
-        final YangVersion yangVersion = ctx.getRootVersion();
-        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof BitEffectiveStatement) {
-                SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
-                        "Restricted bits type is allowed only in YANG 1.1 version.");
-                final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
-
-                // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
-                final Optional<Uint32> declared = bitSubStmt.getDeclaredPosition();
-                final Uint32 effectivePos;
-                if (declared.isEmpty()) {
-                    effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
-                } else {
-                    effectivePos = declared.get();
-                }
-
-                builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
-            } else if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
-            final StmtContext<?, ?, ?> ctx) {
-        for (Bit baseTypeBit : baseType.getBits()) {
-            if (bitName.equals(baseTypeBit.getName())) {
-                return baseTypeBit.getPosition();
-            }
-        }
-
-        throw new SourceException(ctx.getStatementSourceReference(),
-                "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
-    }
-
-    @Override
-    public BitsTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}