Migrate LeafrefSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractLeafrefSpecificationSupport.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.PathEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.LeafrefSpecification;
17 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.LeafrefTypeBuilder;
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.source.SourceException;
22
23 abstract class AbstractLeafrefSpecificationSupport
24         extends BaseStatementSupport<String, LeafrefSpecification, EffectiveStatement<String, LeafrefSpecification>> {
25     AbstractLeafrefSpecificationSupport() {
26         super(YangStmtMapping.TYPE);
27     }
28
29     @Override
30     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
31         return value;
32     }
33
34     @Override
35     protected final LeafrefSpecification createDeclared(final StmtContext<String, LeafrefSpecification, ?> ctx,
36             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
37         return new LeafrefSpecificationImpl(ctx, substatements);
38     }
39
40     @Override
41     protected final LeafrefSpecification createEmptyDeclared(final StmtContext<String, LeafrefSpecification, ?> ctx) {
42         throw noPath(ctx);
43     }
44
45     @Override
46     protected final EffectiveStatement<String, LeafrefSpecification> createEffective(
47             final StmtContext<String, LeafrefSpecification, EffectiveStatement<String, LeafrefSpecification>> ctx,
48             final LeafrefSpecification declared,
49             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
50         final LeafrefTypeBuilder builder = BaseTypes.leafrefTypeBuilder(ctx.getSchemaPath().get());
51
52         for (final EffectiveStatement<?, ?> stmt : substatements) {
53             if (stmt instanceof PathEffectiveStatement) {
54                 builder.setPathStatement(((PathEffectiveStatement) stmt).argument());
55             } else if (stmt instanceof RequireInstanceEffectiveStatement) {
56                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
57             }
58         }
59
60         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
61     }
62
63     @Override
64     protected final EffectiveStatement<String, LeafrefSpecification> createEmptyEffective(
65             final StmtContext<String, LeafrefSpecification, EffectiveStatement<String, LeafrefSpecification>> ctx,
66             final LeafrefSpecification declared) {
67         throw noPath(ctx);
68     }
69
70     private static SourceException noPath(final StmtContext<?, ?, ?> ctx) {
71         /*
72          *  https://tools.ietf.org/html/rfc7950#section-9.12
73          *
74          *     When the type is "union", the "type" statement (Section 7.4) MUST be
75          *     present.
76          */
77         return new SourceException("A path statement has to be present", ctx.getStatementSourceReference());
78     }
79 }