Migrate IdentityRefSpecificationSupport 00/90900/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 21:28:27 +0000 (23:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
IdentityRefSpecificationSupport can use memory-efficient representation
through BaseStatementSupport. Furthermore we can reuse
TypeEffectiveStatementImpl instead of brewing a separate effective
implementation.

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

index 91ea4029ee0c6bf6ec722a2cf1fef5b9a795a475..dcb380573b8c694384b33f80a4b30d6ac54a738e 100644 (file)
@@ -7,21 +7,30 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 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.BaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
-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.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractIdentityRefSpecificationSupport
-        extends AbstractStatementSupport<String, IdentityRefSpecification,
+        extends BaseStatementSupport<String, IdentityRefSpecification,
             EffectiveStatement<String, IdentityRefSpecification>> {
     AbstractIdentityRefSpecificationSupport() {
         super(YangStmtMapping.TYPE);
@@ -32,18 +41,6 @@ abstract class AbstractIdentityRefSpecificationSupport
         return value;
     }
 
-    @Override
-    public final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx) {
-        return new IdentityRefSpecificationImpl(ctx);
-    }
-
-    @Override
-    public final EffectiveStatement<String, IdentityRefSpecification> createEffective(
-            final StmtContext<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> ctx) {
-        return new IdentityRefSpecificationEffectiveStatement(ctx);
-    }
-
     @Override
     public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
             EffectiveStatement<String, IdentityRefSpecification>> stmt) {
@@ -59,4 +56,55 @@ abstract class AbstractIdentityRefSpecificationSupport
                     baseIdentity.getLocalName());
         }
     }
+
+    @Override
+    protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new IdentityRefSpecificationImpl(ctx, substatements);
+    }
+
+    @Override
+    protected final IdentityRefSpecification createEmptyDeclared(
+            final StmtContext<String, IdentityRefSpecification, ?> ctx) {
+        throw noBase(ctx);
+    }
+
+    @Override
+    protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
+            final StmtContext<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> ctx,
+            final IdentityRefSpecification declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(ctx.getSchemaPath().get());
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BaseEffectiveStatement) {
+                final QName identityQName = ((BaseEffectiveStatement) stmt).argument();
+                final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
+                        ctx.getFromNamespace(IdentityNamespace.class, identityQName);
+                builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected final EffectiveStatement<String, IdentityRefSpecification> createEmptyEffective(
+            final StmtContext<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> ctx,
+            final IdentityRefSpecification declared) {
+        throw noBase(ctx);
+    }
+
+    private static SourceException noBase(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.10.2
+         *
+         *     The "base" statement, which is a substatement to the "type"
+         *     statement, MUST be present at least once if the type is
+         *     "identityref".
+         */
+        return new SourceException("At least one base statement has to be present",
+            ctx.getStatementSourceReference());
+    }
 }
\ No newline at end of file
diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/IdentityRefSpecificationEffectiveStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/IdentityRefSpecificationEffectiveStatement.java
deleted file mode 100644 (file)
index 126fe38..0000000
+++ /dev/null
@@ -1,57 +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 org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
-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.BaseEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
-import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
-import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-
-final class IdentityRefSpecificationEffectiveStatement
-        extends DeclaredEffectiveStatementBase<String, IdentityRefSpecification>
-        implements TypeEffectiveStatement<IdentityRefSpecification> {
-
-    private final @NonNull IdentityrefTypeDefinition typeDefinition;
-
-    IdentityRefSpecificationEffectiveStatement(final StmtContext<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> ctx) {
-        super(ctx);
-
-        final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(ctx.getSchemaPath().get());
-        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof BaseEffectiveStatement) {
-                final QName identityQName = ((BaseEffectiveStatement) stmt).argument();
-                final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
-                        ctx.getFromNamespace(IdentityNamespace.class, identityQName);
-                builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
-            }
-            if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    @Override
-    public IdentityrefTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}
index 02ba0bb17dfe9437bec488ef0fcc671c9bd6737d..779993a086f90fcec2584372f2474459b47f21c6 100644 (file)
@@ -7,12 +7,15 @@
  */
 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.IdentityRefSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithRawStringArgument.WithSubstatements;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-final class IdentityRefSpecificationImpl extends AbstractDeclaredStatement<String> implements IdentityRefSpecification {
-    IdentityRefSpecificationImpl(final StmtContext<String, IdentityRefSpecification, ?> context) {
-        super(context);
+final class IdentityRefSpecificationImpl extends WithSubstatements implements IdentityRefSpecification {
+    IdentityRefSpecificationImpl(final StmtContext<String, ?, ?> context,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        super(context, substatements);
     }
 }