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