Migrate InstanceIdentifierSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / InstanceIdentifierSpecificationSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.InstanceIdentifierSpecification;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
22
23 final class InstanceIdentifierSpecificationSupport extends BaseStatementSupport<String,
24         InstanceIdentifierSpecification, EffectiveStatement<String, InstanceIdentifierSpecification>> {
25     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
26         YangStmtMapping.TYPE)
27         .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
28         .build();
29
30     InstanceIdentifierSpecificationSupport() {
31         super(YangStmtMapping.TYPE);
32     }
33
34     @Override
35     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
36         return value;
37     }
38
39     @Override
40     protected SubstatementValidator getSubstatementValidator() {
41         return SUBSTATEMENT_VALIDATOR;
42     }
43
44     @Override
45     protected InstanceIdentifierSpecification createDeclared(
46             final StmtContext<String, InstanceIdentifierSpecification, ?> ctx,
47             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
48         return new RegularInstanceIdentifierSpecification(ctx, substatements);
49     }
50
51     @Override
52     protected InstanceIdentifierSpecification createEmptyDeclared(
53             final StmtContext<String, InstanceIdentifierSpecification, ?> ctx) {
54         return new EmptyIdentifierSpecification(ctx);
55     }
56
57     @Override
58     protected EffectiveStatement<String, InstanceIdentifierSpecification> createEffective(
59             final StmtContext<String, InstanceIdentifierSpecification,
60                 EffectiveStatement<String, InstanceIdentifierSpecification>> ctx,
61             final InstanceIdentifierSpecification declared,
62             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
63         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(
64             BaseTypes.instanceIdentifierType(), ctx.getSchemaPath().get());
65
66         for (EffectiveStatement<?, ?> stmt : substatements) {
67             if (stmt instanceof RequireInstanceEffectiveStatement) {
68                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
69             }
70         }
71
72         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
73     }
74
75     @Override
76     protected EffectiveStatement<String, InstanceIdentifierSpecification> createEmptyEffective(
77             final StmtContext<String, InstanceIdentifierSpecification,
78                 EffectiveStatement<String, InstanceIdentifierSpecification>> ctx,
79             final InstanceIdentifierSpecification declared) {
80         // TODO: we could do better here, but its really splitting hairs
81         return createEffective(ctx, declared, ImmutableList.of());
82     }
83 }