All StatementSupports must support DeclarationReference
[yangtools.git] / yang / rfc8528-parser-support / src / main / java / org / opendaylight / yangtools / rfc8528 / parser / MountPointStatementSupport.java
index 157cac6eae13f2aca9ec3e744fa9c364f066a4e2..0a35a86ae75ca1f29c910c0dce1b8cd7eb4afc1f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
+ * Copyright (c) 2019 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,
@@ -7,19 +7,22 @@
  */
 package org.opendaylight.yangtools.rfc8528.parser;
 
+import com.google.common.collect.ImmutableList;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.rfc8528.model.api.MountPointEffectiveStatement;
-import org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode;
 import org.opendaylight.yangtools.rfc8528.model.api.MountPointStatement;
 import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountStatements;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
+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.meta.StatementDefinition;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
-import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
+import org.opendaylight.yangtools.yang.parser.spi.meta.SchemaPathSupport;
 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;
@@ -27,90 +30,69 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public final class MountPointStatementSupport
-        extends AbstractStatementSupport<QName, MountPointStatement, MountPointEffectiveStatement> {
-
-    private static final class Declared extends AbstractDeclaredStatement<QName> implements MountPointStatement {
-        Declared(final StmtContext<QName, ?, ?> context) {
-            super(context);
-        }
-
-        @Override
-        public QName getArgument() {
-            return argument();
-        }
+        extends AbstractQNameStatementSupport<MountPointStatement, MountPointEffectiveStatement> {
+    private static final SubstatementValidator VALIDATOR =
+        SubstatementValidator.builder(SchemaMountStatements.MOUNT_POINT)
+            .addOptional(YangStmtMapping.CONFIG)
+            .addOptional(YangStmtMapping.DESCRIPTION)
+            .addOptional(YangStmtMapping.REFERENCE)
+            .addOptional(YangStmtMapping.STATUS)
+            .build();
+
+    public MountPointStatementSupport(final YangParserConfiguration config) {
+        super(SchemaMountStatements.MOUNT_POINT, StatementPolicy.copyDeclared((copy, current, substatements) ->
+            copy.getArgument().equals(current.getArgument())
+            // Implied by UnknownSchemaNode
+            && copy.history().isAugmenting() == current.history().isAugmenting()
+            && copy.history().isAddedByUses() == current.history().isAddedByUses()
+            && copy.equalParentPath(current)), config);
     }
 
-    private static final class Effective extends UnknownEffectiveStatementBase<QName, MountPointStatement>
-            implements MountPointEffectiveStatement, MountPointSchemaNode {
-
-        private final SchemaPath path;
-
-        Effective(final StmtContext<QName, MountPointStatement, ?> ctx) {
-            super(ctx);
-            path = ctx.coerceParentContext().getSchemaPath().get().createChild(argument());
-        }
-
-        @Override
-        public QName getQName() {
-            return path.getLastComponent();
-        }
-
-        @Override
-        public SchemaPath getPath() {
-            return path;
-        }
-    }
-
-    private static final MountPointStatementSupport INSTANCE = new MountPointStatementSupport(
-        SchemaMountStatements.MOUNT_POINT);
-
-    private final SubstatementValidator validator;
-
-    MountPointStatementSupport(final StatementDefinition definition) {
-        super(definition);
-        this.validator = SubstatementValidator.builder(definition)
-                .addOptional(YangStmtMapping.CONFIG)
-                .addOptional(YangStmtMapping.DESCRIPTION)
-                .addOptional(YangStmtMapping.REFERENCE)
-                .addOptional(YangStmtMapping.STATUS)
-                .build();
-    }
-
-    public static MountPointStatementSupport getInstance() {
-        return INSTANCE;
+    // FIXME: these two methods are not quite right. RFC8528 states that:
+    //
+    //    If a mount point is defined within a grouping, its label is
+    //    bound to the module where the grouping is used.
+    //
+    // We are not doing exactly that, in that we can end up rebinding the argument through 'augment', I think.
+    @Override
+    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+        return StmtContextUtils.parseIdentifier(ctx, value);
     }
 
     @Override
-    public MountPointStatement createDeclared(final StmtContext<QName, MountPointStatement, ?> ctx) {
-        return new Declared(ctx);
+    public QName adaptArgumentValue(final StmtContext<QName, MountPointStatement, MountPointEffectiveStatement> ctx,
+            final QNameModule targetModule) {
+        return ctx.getArgument().bindTo(targetModule).intern();
     }
 
     @Override
-    public MountPointEffectiveStatement createEffective(
-            final StmtContext<QName, MountPointStatement, MountPointEffectiveStatement> ctx) {
-        return new Effective(ctx);
+    public void onStatementAdded(final Mutable<QName, MountPointStatement, MountPointEffectiveStatement> stmt) {
+        final StatementDefinition parentDef = stmt.coerceParentContext().publicDefinition();
+        SourceException.throwIf(YangStmtMapping.CONTAINER != parentDef && YangStmtMapping.LIST != parentDef, stmt,
+            "Mount points may only be defined at either a container or a list");
     }
 
     @Override
-    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return StmtContextUtils.parseIdentifier(ctx, value);
+    protected SubstatementValidator getSubstatementValidator() {
+        return VALIDATOR;
     }
 
     @Override
-    public QName adaptArgumentValue(final StmtContext<QName, MountPointStatement, MountPointEffectiveStatement> ctx,
-            final QNameModule targetModule) {
-        return ctx.getFromNamespace(QNameCacheNamespace.class, ctx.getStatementArgument().withModule(targetModule));
+    protected MountPointStatement createDeclared(@NonNull final StmtContext<QName, MountPointStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new MountPointStatementImpl(ctx.getArgument(), substatements);
     }
 
     @Override
-    public void onStatementAdded(final Mutable<QName, MountPointStatement, MountPointEffectiveStatement> stmt) {
-        final StatementDefinition parentDef = stmt.coerceParentContext().getPublicDefinition();
-        SourceException.throwIf(YangStmtMapping.CONTAINER != parentDef && YangStmtMapping.LIST != parentDef,
-                stmt.getStatementSourceReference(), "Mount points may only be defined at either a container or a list");
+    protected MountPointStatement attachDeclarationReference(final MountPointStatement stmt,
+            final DeclarationReference reference) {
+        return new RefMountPointStatement(stmt, reference);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return validator;
+    protected MountPointEffectiveStatement createEffective(final Current<QName, MountPointStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new MountPointEffectiveStatementImpl(stmt, substatements, SchemaPathSupport.toOptionalPath(
+            stmt.getEffectiveParent().getSchemaPath().createChild(stmt.getArgument())));
     }
 }