Migrate InstanceIdentifierSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / InstanceIdentifierSpecificationSupport.java
index be40ac355a6e8e6cec466e91be9d2b99a9dff9e7..19dfe29ff90a61fab8068ab4b4bb4fb5ed8c3e8a 100644 (file)
@@ -7,14 +7,20 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
 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.RequireInstanceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.InstanceIdentifierSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
+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.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 
-final class InstanceIdentifierSpecificationSupport extends AbstractStatementSupport<String,
+final class InstanceIdentifierSpecificationSupport extends BaseStatementSupport<String,
         InstanceIdentifierSpecification, EffectiveStatement<String, InstanceIdentifierSpecification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
@@ -31,20 +37,47 @@ final class InstanceIdentifierSpecificationSupport extends AbstractStatementSupp
     }
 
     @Override
-    public InstanceIdentifierSpecification createDeclared(
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
+    }
+
+    @Override
+    protected InstanceIdentifierSpecification createDeclared(
+            final StmtContext<String, InstanceIdentifierSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularInstanceIdentifierSpecification(ctx, substatements);
+    }
+
+    @Override
+    protected InstanceIdentifierSpecification createEmptyDeclared(
             final StmtContext<String, InstanceIdentifierSpecification, ?> ctx) {
-        return new InstanceIdentifierSpecificationImpl(ctx);
+        return new EmptyIdentifierSpecification(ctx);
     }
 
     @Override
-    public EffectiveStatement<String, InstanceIdentifierSpecification> createEffective(
+    protected EffectiveStatement<String, InstanceIdentifierSpecification> createEffective(
             final StmtContext<String, InstanceIdentifierSpecification,
-            EffectiveStatement<String, InstanceIdentifierSpecification>> ctx) {
-        return new InstanceIdentifierSpecificationEffectiveStatement(ctx);
+                EffectiveStatement<String, InstanceIdentifierSpecification>> ctx,
+            final InstanceIdentifierSpecification declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(
+            BaseTypes.instanceIdentifierType(), ctx.getSchemaPath().get());
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RequireInstanceEffectiveStatement) {
+                builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected EffectiveStatement<String, InstanceIdentifierSpecification> createEmptyEffective(
+            final StmtContext<String, InstanceIdentifierSpecification,
+                EffectiveStatement<String, InstanceIdentifierSpecification>> ctx,
+            final InstanceIdentifierSpecification declared) {
+        // TODO: we could do better here, but its really splitting hairs
+        return createEffective(ctx, declared, ImmutableList.of());
     }
 }
\ No newline at end of file