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