5a792205c0cae1deabbec704c4d04e495bb11c54
[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, SUBSTATEMENT_VALIDATOR);
34     }
35
36     @Override
37     protected UnionSpecification createDeclared(final StmtContext<QName, UnionSpecification, ?> ctx,
38             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
39         if (substatements.isEmpty()) {
40             throw noType(ctx);
41         }
42         return new UnionSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
43     }
44
45     @Override
46     protected UnionSpecification attachDeclarationReference(final UnionSpecification stmt,
47             final DeclarationReference reference) {
48         return new RefUnionSpecification(stmt, reference);
49     }
50
51     @Override
52     protected EffectiveStatement<QName, UnionSpecification> createEffective(
53             final Current<QName, UnionSpecification> stmt,
54             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
55         if (substatements.isEmpty()) {
56             throw noType(stmt);
57         }
58
59         final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.argumentAsTypeQName());
60
61         for (final EffectiveStatement<?, ?> subStmt : substatements) {
62             if (subStmt instanceof TypeEffectiveStatement) {
63                 builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
64             }
65         }
66
67         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
68     }
69
70     private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
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("At least one type statement has to be present", stmt);
78     }
79 }