b9ea4b28fd79afe9abb524b6b4e43c1e7e37b405
[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.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.UnionTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
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 BaseStatementSupport<String, 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);
35     }
36
37     @Override
38     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
39         return value;
40     }
41
42     @Override
43     protected SubstatementValidator getSubstatementValidator() {
44         return SUBSTATEMENT_VALIDATOR;
45     }
46
47     @Override
48     protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
49             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
50         return new UnionSpecificationImpl(ctx.getRawArgument(), substatements);
51     }
52
53     @Override
54     protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
55         throw noType(ctx);
56     }
57
58     @Override
59     protected EffectiveStatement<String, UnionSpecification> createEffective(
60             final Current<String, UnionSpecification> stmt,
61             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62         if (substatements.isEmpty()) {
63             throw noType(stmt);
64         }
65
66         final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.getSchemaPath());
67
68         for (final EffectiveStatement<?, ?> subStmt : substatements) {
69             if (subStmt instanceof TypeEffectiveStatement) {
70                 builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
71             }
72         }
73
74         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
75     }
76
77     private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
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", stmt);
85     }
86 }