Split out yang-model-ri
[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.ri.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.ri.type.LeafrefTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24
25 abstract class AbstractLeafrefSpecificationSupport extends AbstractStringStatementSupport<LeafrefSpecification,
26             EffectiveStatement<String, LeafrefSpecification>> {
27     AbstractLeafrefSpecificationSupport() {
28         super(YangStmtMapping.TYPE, StatementPolicy.exactReplica());
29     }
30
31     @Override
32     protected final LeafrefSpecification createDeclared(final StmtContext<String, LeafrefSpecification, ?> ctx,
33             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
34         return new LeafrefSpecificationImpl(ctx.getRawArgument(), substatements);
35     }
36
37     @Override
38     protected final LeafrefSpecification createEmptyDeclared(final StmtContext<String, LeafrefSpecification, ?> ctx) {
39         throw noPath(ctx);
40     }
41
42     @Override
43     protected EffectiveStatement<String, LeafrefSpecification> createEffective(
44             final Current<String, LeafrefSpecification> stmt,
45             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
46         if (substatements.isEmpty()) {
47             throw noPath(stmt);
48         }
49
50         final LeafrefTypeBuilder builder = BaseTypes.leafrefTypeBuilder(stmt.argumentAsTypeQName());
51
52         for (final EffectiveStatement<?, ?> subStmt : substatements) {
53             if (subStmt instanceof PathEffectiveStatement) {
54                 builder.setPathStatement(((PathEffectiveStatement) subStmt).argument());
55             } else if (subStmt instanceof RequireInstanceEffectiveStatement) {
56                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)subStmt).argument());
57             }
58         }
59
60         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
61     }
62
63     private static SourceException noPath(final CommonStmtCtx stmt) {
64         /*
65          *  https://tools.ietf.org/html/rfc7950#section-9.12
66          *
67          *     When the type is "union", the "type" statement (Section 7.4) MUST be
68          *     present.
69          */
70         return new SourceException("A path statement has to be present", stmt);
71     }
72 }