Migrate AbstractTypeStatementSupport 82/90882/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 16:11:15 +0000 (18:11 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
Support for 'type' statement is rather twisted, but at the end of
the day, externalizing substatement creation is rather easy.

Switching to BaseStatementSupport allows us to discern when we can
end up pointing to a BuiltinTypeStatement -- since there is not such
thing as an implicit type statement.

On the declared front, this is a straightforward migration to
AbstractDeclaredStatement.WithRawStringArgument subclasses, which
ends up reducing typical footprint from 32 bytes to 16/24 bytes.

On the effective front, this does not really do anything, as these
are handled on per-type basis.

JIRA: YANGTOOLS-1065
Change-Id: I421f72a765fd6a28f984d463d8b9991dd7c1d231
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/BuiltinTypeStatement.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/EmptyTypeStatement.java [moved from yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/TypeStatementImpl.java with 57% similarity]
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/RegularTypeStatement.java [new file with mode: 0644]

index 2330fedf670f9a5b13f051e1c24b15ae1d0c5ed7..517043be800d07555d1363300ba4f4fe8a56f125 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
 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 org.eclipse.jdt.annotation.NonNull;
@@ -16,6 +17,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 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.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
@@ -41,8 +43,8 @@ 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.RestrictedTypes;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
@@ -57,7 +59,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractTypeStatementSupport
-        extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
+        extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
         .addOptional(YangStmtMapping.BASE)
@@ -135,21 +137,78 @@ abstract class AbstractTypeStatementSupport
     }
 
     @Override
-    public final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
-        return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
+    public final void onFullDefinitionDeclared(
+            final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
+        super.onFullDefinitionDeclared(stmt);
+
+        // if it is yang built-in type, no prerequisite is needed, so simply return
+        if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
+            return;
+        }
+
+        final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
+        final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
+        final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
+                typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
+        typeAction.mutatesEffectiveCtx(stmt.getParentContext());
+
+        /*
+         * If the type does not exist, throw new InferenceException.
+         * Otherwise perform no operation.
+         */
+        typeAction.apply(new InferenceAction() {
+            @Override
+            public void apply(final InferenceContext ctx) {
+                // Intentional NOOP
+            }
+
+            @Override
+            public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
+                InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
+                    "Type [%s] was not found.", typeQName);
+            }
+        });
     }
 
     @Override
-    public final TypeEffectiveStatement<TypeStatement> createEffective(
-            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
+    public final String internArgument(final String rawArgument) {
+        final String found;
+        return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
+    }
 
-        // First look up the proper base type
-        final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
+    @Override
+    public boolean hasArgumentSpecificSupports() {
+        return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
+    }
 
-        if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
-            return typeStmt;
-        }
+    @Override
+    public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
+        return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
+    }
+
+    @Override
+    protected final SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
+    }
+
+    @Override
+    protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularTypeStatement(ctx, substatements);
+    }
+
+    @Override
+    protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
+        final TypeStatement builtin;
+        return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin : new EmptyTypeStatement(ctx);
+    }
 
+    @Override
+    protected final EffectiveStatement<String, TypeStatement> createEffective(
+            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
+            final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        // First look up the proper base type
+        final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
         // Now instantiate the proper effective statement for that type
         final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
         if (baseType instanceof BinaryTypeDefinition) {
@@ -205,58 +264,10 @@ abstract class AbstractTypeStatementSupport
     }
 
     @Override
-    public final void onFullDefinitionDeclared(
-            final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
-        super.onFullDefinitionDeclared(stmt);
-
-        // if it is yang built-in type, no prerequisite is needed, so simply return
-        if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
-            return;
-        }
-
-        final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
-        final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
-        final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
-                typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
-        typeAction.mutatesEffectiveCtx(stmt.getParentContext());
-
-        /*
-         * If the type does not exist, throw new InferenceException.
-         * Otherwise perform no operation.
-         */
-        typeAction.apply(new InferenceAction() {
-            @Override
-            public void apply(final InferenceContext ctx) {
-                // Intentional NOOP
-            }
-
-            @Override
-            public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
-                InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
-                    "Type [%s] was not found.", typeQName);
-            }
-        });
-    }
-
-    @Override
-    protected final SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
-    }
-
-    @Override
-    public final String internArgument(final String rawArgument) {
-        final String found;
-        return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
-    }
-
-    @Override
-    public boolean hasArgumentSpecificSupports() {
-        return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
-    }
-
-    @Override
-    public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
-        return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
+    protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
+            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
+            final TypeStatement declared) {
+        return resolveType(ctx);
     }
 
     static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
index e3130e06849017690c6ead66796cbb07b6114b16..b29fdf02cd45cbf01d4f770e80e7e3178e4c0262 100644 (file)
@@ -11,10 +11,10 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
-import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
-import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithRawStringArgument;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
 final class BuiltinTypeStatement extends WithRawStringArgument implements TypeStatement {
     private static final ImmutableMap<String, BuiltinTypeStatement> BUILTINS;
@@ -45,15 +45,7 @@ final class BuiltinTypeStatement extends WithRawStringArgument implements TypeSt
         super(requireNonNull(rawArgument));
     }
 
-    static TypeStatement maybeReplace(final TypeStatementImpl orig) {
-        if (orig.declaredSubstatements().isEmpty() && orig.getStatementSource() == StatementSource.DECLARATION
-                && orig.statementDefinition() == YangStmtMapping.TYPE) {
-            final BuiltinTypeStatement builtin = BUILTINS.get(orig.argument());
-            if (builtin != null) {
-                return builtin;
-            }
-        }
-
-        return orig;
+    static @Nullable TypeStatement lookup(final StmtContext<String, TypeStatement, ?> ctx) {
+        return BUILTINS.get(ctx.coerceStatementArgument());
     }
 }
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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,
@@ -8,11 +8,11 @@
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithRawStringArgument;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-final class TypeStatementImpl extends AbstractDeclaredStatement<String> implements TypeStatement {
-    TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
+final class EmptyTypeStatement extends WithRawStringArgument implements TypeStatement {
+    EmptyTypeStatement(final StmtContext<String, ?, ?> context) {
         super(context);
     }
 }
diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/RegularTypeStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/RegularTypeStatement.java
new file mode 100644 (file)
index 0000000..8947968
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 com.google.common.collect.ImmutableList;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithRawStringArgument.WithSubstatements;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+
+final class RegularTypeStatement extends WithSubstatements implements TypeStatement {
+    RegularTypeStatement(final StmtContext<String, ?, ?> context,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        super(context, substatements);
+    }
+}