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