Split out yang-model-ri
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / UnionSpecificationSupport.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.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
17 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.ri.type.UnionTypeBuilder;
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.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 final class UnionSpecificationSupport
27         extends AbstractStringStatementSupport<UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
29         .TYPE)
30         .addMultiple(YangStmtMapping.TYPE)
31         .build();
32
33     UnionSpecificationSupport() {
34         super(YangStmtMapping.TYPE, StatementPolicy.exactReplica());
35     }
36
37     @Override
38     protected SubstatementValidator getSubstatementValidator() {
39         return SUBSTATEMENT_VALIDATOR;
40     }
41
42     @Override
43     protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
44             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
45         return new UnionSpecificationImpl(ctx.getRawArgument(), substatements);
46     }
47
48     @Override
49     protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
50         throw noType(ctx);
51     }
52
53     @Override
54     protected EffectiveStatement<String, UnionSpecification> createEffective(
55             final Current<String, UnionSpecification> stmt,
56             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
57         if (substatements.isEmpty()) {
58             throw noType(stmt);
59         }
60
61         final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.argumentAsTypeQName());
62
63         for (final EffectiveStatement<?, ?> subStmt : substatements) {
64             if (subStmt instanceof TypeEffectiveStatement) {
65                 builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
66             }
67         }
68
69         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
70     }
71
72     private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
73         /*
74          *  https://tools.ietf.org/html/rfc7950#section-9.12
75          *
76          *     When the type is "union", the "type" statement (Section 7.4) MUST be
77          *     present.
78          */
79         return new SourceException("At least one type statement has to be present", stmt);
80     }
81 }